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 | }; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 159 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 160 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 161 | FakeInputDispatcherPolicy() = default; |
| 162 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 163 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 164 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 165 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 166 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 167 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 168 | |
| 169 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 170 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 171 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 172 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 175 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 176 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 177 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 178 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 179 | |
| 180 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 181 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 182 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 183 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 184 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 185 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 186 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 187 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 190 | void assertFilterInputEventWasNotCalled() { |
| 191 | std::scoped_lock lock(mLock); |
| 192 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 193 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 194 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 195 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 196 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 197 | ASSERT_TRUE(mConfigurationChangedTime) |
| 198 | << "Timed out waiting for configuration changed call"; |
| 199 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 200 | mConfigurationChangedTime = std::nullopt; |
| 201 | } |
| 202 | |
| 203 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 204 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 205 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 206 | // 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] | 207 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 208 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 209 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 210 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 211 | mLastNotifySwitch = std::nullopt; |
| 212 | } |
| 213 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 214 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 215 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 216 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 217 | mOnPointerDownToken.clear(); |
| 218 | } |
| 219 | |
| 220 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 221 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 222 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 223 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 226 | // This function must be called soon after the expected ANR timer starts, |
| 227 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 228 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 229 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 230 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 231 | std::unique_lock lock(mLock); |
| 232 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 233 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 234 | ASSERT_NO_FATAL_FAILURE( |
| 235 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 236 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 239 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 240 | const sp<WindowInfoHandle>& window) { |
| 241 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 242 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 243 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 246 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 247 | const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 248 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 249 | std::unique_lock lock(mLock); |
| 250 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 251 | AnrResult result; |
| 252 | ASSERT_NO_FATAL_FAILURE(result = |
| 253 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 254 | ASSERT_EQ(expectedToken, result.token); |
| 255 | ASSERT_EQ(expectedPid, result.pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 258 | /** 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] | 259 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 260 | std::unique_lock lock(mLock); |
| 261 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 262 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 263 | const auto& [token, _] = result; |
| 264 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 267 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 268 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 269 | std::unique_lock lock(mLock); |
| 270 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 271 | AnrResult result; |
| 272 | ASSERT_NO_FATAL_FAILURE( |
| 273 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 274 | ASSERT_EQ(expectedToken, result.token); |
| 275 | ASSERT_EQ(expectedPid, result.pid); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** 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] | 279 | sp<IBinder> getResponsiveWindowToken() { |
| 280 | std::unique_lock lock(mLock); |
| 281 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 282 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 283 | const auto& [token, _] = result; |
| 284 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void assertNotifyAnrWasNotCalled() { |
| 288 | std::scoped_lock lock(mLock); |
| 289 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 290 | ASSERT_TRUE(mAnrWindows.empty()); |
| 291 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 292 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 293 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 296 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 297 | std::unique_lock lock(mLock); |
| 298 | base::ScopedLockAssertion assumeLocked(mLock); |
| 299 | |
| 300 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 301 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 302 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 303 | enabled; |
| 304 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 305 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 306 | << ") to be called."; |
| 307 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 308 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 309 | auto request = *mPointerCaptureRequest; |
| 310 | mPointerCaptureRequest.reset(); |
| 311 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void assertSetPointerCaptureNotCalled() { |
| 315 | std::unique_lock lock(mLock); |
| 316 | base::ScopedLockAssertion assumeLocked(mLock); |
| 317 | |
| 318 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 319 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 320 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 321 | << std::to_string(mPointerCaptureRequest->enable); |
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 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 326 | void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, |
| 327 | const sp<IBinder>& targetToken) { |
| 328 | dispatcher.waitForIdle(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 329 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 330 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 331 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 332 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 333 | } |
| 334 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 335 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 336 | std::unique_lock lock(mLock); |
| 337 | base::ScopedLockAssertion assumeLocked(mLock); |
| 338 | std::optional<sp<IBinder>> receivedToken = |
| 339 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 340 | mNotifyInputChannelBroken); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 341 | ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token"; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 342 | ASSERT_EQ(token, *receivedToken); |
| 343 | } |
| 344 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 345 | /** |
| 346 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 347 | */ |
| 348 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 349 | mInterceptKeyTimeout = timeout; |
| 350 | } |
| 351 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 352 | void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; } |
| 353 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 354 | void assertUserActivityPoked() { |
| 355 | std::scoped_lock lock(mLock); |
| 356 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; |
| 357 | } |
| 358 | |
| 359 | void assertUserActivityNotPoked() { |
| 360 | std::scoped_lock lock(mLock); |
| 361 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; |
| 362 | } |
| 363 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 364 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 365 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 366 | } |
| 367 | |
| 368 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 369 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 370 | } |
| 371 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 372 | void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) { |
| 373 | std::scoped_lock lock(mLock); |
| 374 | mUnhandledKeyHandler = handler; |
| 375 | } |
| 376 | |
| 377 | void assertUnhandledKeyReported(int32_t keycode) { |
| 378 | std::unique_lock lock(mLock); |
| 379 | base::ScopedLockAssertion assumeLocked(mLock); |
| 380 | std::optional<int32_t> unhandledKeycode = |
| 381 | getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock, |
| 382 | mNotifyUnhandledKey); |
| 383 | ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported"; |
| 384 | ASSERT_EQ(unhandledKeycode, keycode); |
| 385 | } |
| 386 | |
| 387 | void assertUnhandledKeyNotReported() { |
| 388 | std::unique_lock lock(mLock); |
| 389 | base::ScopedLockAssertion assumeLocked(mLock); |
| 390 | std::optional<int32_t> unhandledKeycode = |
| 391 | getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock, |
| 392 | mNotifyUnhandledKey); |
| 393 | ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported"; |
| 394 | } |
| 395 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 396 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 397 | std::mutex mLock; |
| 398 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 399 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 400 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 401 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 402 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 403 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 404 | |
| 405 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 406 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 407 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 408 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 409 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 410 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 411 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 412 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 413 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 414 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 415 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 416 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 417 | bool mPokedUserActivity GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 418 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 419 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 420 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 421 | std::chrono::nanoseconds mStaleEventTimeout = 1000ms; |
| 422 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 423 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 424 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 425 | std::condition_variable mNotifyUnhandledKey; |
| 426 | std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock); |
| 427 | std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock); |
| 428 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 429 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 430 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 431 | // first entry from the container and erase it. |
| 432 | template <class T> |
| 433 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 434 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 435 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 436 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 437 | // before checking if ANR was called. |
| 438 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 439 | // to provide it some time to act. 100ms seems reasonable. |
| 440 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 441 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 442 | std::optional<T> token = |
| 443 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 444 | if (!token.has_value()) { |
| 445 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 446 | return {}; |
| 447 | } |
| 448 | |
| 449 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 450 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 451 | // the dispatcher started counting before this function was called |
| 452 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 453 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 454 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 455 | << "ms, but waited " |
| 456 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 457 | << "ms instead"; |
| 458 | } |
| 459 | return *token; |
| 460 | } |
| 461 | |
| 462 | template <class T> |
| 463 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 464 | std::queue<T>& storage, |
| 465 | std::unique_lock<std::mutex>& lock, |
| 466 | std::condition_variable& condition) |
| 467 | REQUIRES(mLock) { |
| 468 | condition.wait_for(lock, timeout, |
| 469 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 470 | if (storage.empty()) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 471 | return std::nullopt; |
| 472 | } |
| 473 | T item = storage.front(); |
| 474 | storage.pop(); |
| 475 | return std::make_optional(item); |
| 476 | } |
| 477 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 478 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 479 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 480 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 483 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 484 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 485 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 486 | ASSERT_TRUE(pid.has_value()); |
| 487 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 488 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 489 | } |
| 490 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 491 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 492 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 493 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 494 | ASSERT_TRUE(pid.has_value()); |
| 495 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 496 | mNotifyAnr.notify_all(); |
| 497 | } |
| 498 | |
| 499 | void notifyNoFocusedWindowAnr( |
| 500 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 501 | std::scoped_lock lock(mLock); |
| 502 | mAnrApplications.push(applicationHandle); |
| 503 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 506 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 507 | std::scoped_lock lock(mLock); |
| 508 | mBrokenInputChannels.push(connectionToken); |
| 509 | mNotifyInputChannelBroken.notify_all(); |
| 510 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 511 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 512 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 513 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 514 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 515 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 516 | const std::vector<float>& values) override {} |
| 517 | |
| 518 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 519 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 520 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 521 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 522 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 523 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 524 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 525 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 526 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 527 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 528 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 532 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 533 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 534 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 535 | break; |
| 536 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 537 | default: { |
| 538 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 539 | break; |
| 540 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 545 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 546 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 547 | // Clear intercept state when we handled the event. |
| 548 | mInterceptKeyTimeout = 0ms; |
| 549 | } |
| 550 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 552 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 554 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 555 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 556 | // Clear intercept state so we could dispatch the event in next wake. |
| 557 | mInterceptKeyTimeout = 0ms; |
| 558 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 561 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event, |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 562 | uint32_t) override { |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 563 | std::scoped_lock lock(mLock); |
| 564 | mReportedUnhandledKeycodes.emplace(event.getKeyCode()); |
| 565 | mNotifyUnhandledKey.notify_all(); |
| 566 | return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 569 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 570 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 571 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 572 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 573 | * essentially a passthrough for notifySwitch. |
| 574 | */ |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 575 | mLastNotifySwitch = |
| 576 | NotifySwitchArgs(InputEvent::nextId(), when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 579 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { |
| 580 | std::scoped_lock lock(mLock); |
| 581 | mPokedUserActivity = true; |
| 582 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 583 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 584 | bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override { |
| 585 | return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout; |
| 586 | } |
| 587 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 588 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 589 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 590 | mOnPointerDownToken = newToken; |
| 591 | } |
| 592 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 593 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 594 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 595 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 596 | mPointerCaptureChangedCondition.notify_all(); |
| 597 | } |
| 598 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 599 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 600 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 601 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 602 | mDropTargetWindowToken = token; |
| 603 | } |
| 604 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 605 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 606 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 607 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 608 | } |
| 609 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 610 | void assertFilterInputEventWasCalledInternal( |
| 611 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 612 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 613 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 614 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 615 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 616 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | }; |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 618 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 619 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 620 | // --- InputDispatcherTest --- |
| 621 | |
| 622 | class InputDispatcherTest : public testing::Test { |
| 623 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 624 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 625 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 627 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 628 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 629 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
| 630 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 631 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 632 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 633 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 636 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 637 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 638 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 639 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 640 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 641 | |
| 642 | /** |
| 643 | * Used for debugging when writing the test |
| 644 | */ |
| 645 | void dumpDispatcherState() { |
| 646 | std::string dump; |
| 647 | mDispatcher->dump(dump); |
| 648 | std::stringstream ss(dump); |
| 649 | std::string to; |
| 650 | |
| 651 | while (std::getline(ss, to, '\n')) { |
| 652 | ALOGE("%s", to.c_str()); |
| 653 | } |
| 654 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 655 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 656 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 657 | FocusRequest request; |
| 658 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 659 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 660 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 661 | request.displayId = window->getInfo()->displayId; |
| 662 | mDispatcher->setFocusedWindow(request); |
| 663 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 664 | }; |
| 665 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 666 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 667 | KeyEvent event; |
| 668 | |
| 669 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 670 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 671 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 672 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 673 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 674 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 675 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 676 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 677 | << "Should reject key events with undefined action."; |
| 678 | |
| 679 | // 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] | 680 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 681 | 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] | 682 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 683 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 684 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 685 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | << "Should reject key events with ACTION_MULTIPLE."; |
| 687 | } |
| 688 | |
| 689 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 690 | MotionEvent event; |
| 691 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 692 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 693 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 694 | pointerProperties[i].clear(); |
| 695 | pointerProperties[i].id = i; |
| 696 | pointerCoords[i].clear(); |
| 697 | } |
| 698 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 699 | // Some constants commonly used below |
| 700 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 701 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 702 | constexpr int32_t metaState = AMETA_NONE; |
| 703 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 704 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 705 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 706 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 707 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 708 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 709 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 710 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 711 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 712 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
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 motion events with undefined action."; |
| 717 | |
| 718 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 719 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 720 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 721 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 722 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 723 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 724 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 725 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 726 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 727 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | << "Should reject motion events with pointer down index too large."; |
| 729 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 730 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 731 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 732 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 733 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 734 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 735 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 736 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 737 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 738 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 739 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 740 | << "Should reject motion events with pointer down index too small."; |
| 741 | |
| 742 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 743 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 744 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 745 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 746 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 747 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 748 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 749 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 750 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 751 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | << "Should reject motion events with pointer up index too large."; |
| 753 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 754 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 755 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 756 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 757 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 758 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 759 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 760 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 761 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 762 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 763 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 764 | << "Should reject motion events with pointer up index too small."; |
| 765 | |
| 766 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 767 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 768 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 769 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 770 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 771 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 772 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 773 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 774 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 775 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 | << "Should reject motion events with 0 pointers."; |
| 777 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 778 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 779 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 780 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 781 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 782 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 783 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 784 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 785 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 786 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 788 | |
| 789 | // Rejects motion events with invalid pointer ids. |
| 790 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 791 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 792 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 793 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 794 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 795 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 796 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 797 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 798 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 799 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 800 | << "Should reject motion events with pointer ids less than 0."; |
| 801 | |
| 802 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 803 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 804 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 805 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 806 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 807 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 808 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 809 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 810 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 811 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 813 | |
| 814 | // Rejects motion events with duplicate pointer ids. |
| 815 | pointerProperties[0].id = 1; |
| 816 | pointerProperties[1].id = 1; |
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=*/2, 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 duplicate pointer ids."; |
| 827 | } |
| 828 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 829 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 830 | |
| 831 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 832 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 833 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 834 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 835 | |
| 836 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 837 | } |
| 838 | |
| 839 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 840 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 841 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 842 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 843 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 844 | |
| 845 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 846 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 847 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 848 | } |
| 849 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 850 | namespace { |
| 851 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 852 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 853 | // Default input dispatching timeout if there is no focused application or paused window |
| 854 | // from which to determine an appropriate dispatching timeout. |
| 855 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 856 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 857 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 858 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 859 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 860 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 861 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 862 | : mConsumer(std::move(clientChannel)), mName(name) {} |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 863 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 864 | InputEvent* consume(std::chrono::milliseconds timeout, bool handled = false) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 865 | InputEvent* event; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 866 | std::optional<uint32_t> consumeSeq = receiveEvent(timeout, &event); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 867 | if (!consumeSeq) { |
| 868 | return nullptr; |
| 869 | } |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 870 | finishEvent(*consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 871 | return event; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Receive an event without acknowledging it. |
| 876 | * Return the sequence number that could later be used to send finished signal. |
| 877 | */ |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 878 | std::optional<uint32_t> receiveEvent(std::chrono::milliseconds timeout, |
| 879 | InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 880 | uint32_t consumeSeq; |
| 881 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 882 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 883 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 884 | status_t status = WOULD_BLOCK; |
| 885 | while (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 886 | status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
| 887 | &event); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 888 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 889 | if (elapsed > timeout) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 890 | break; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | if (status == WOULD_BLOCK) { |
| 895 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 896 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | if (status != OK) { |
| 900 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 901 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 902 | } |
| 903 | if (event == nullptr) { |
| 904 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 905 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 906 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 907 | if (outEvent != nullptr) { |
| 908 | *outEvent = event; |
| 909 | } |
| 910 | return consumeSeq; |
| 911 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 912 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 913 | /** |
| 914 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 915 | */ |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 916 | void finishEvent(uint32_t consumeSeq, bool handled = true) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 917 | const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 918 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 919 | } |
| 920 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 921 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 922 | const status_t status = mConsumer.sendTimeline(inputEventId, timeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 923 | ASSERT_EQ(OK, status); |
| 924 | } |
| 925 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 926 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 927 | std::optional<int32_t> expectedDisplayId, |
| 928 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 929 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 930 | |
| 931 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 932 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 933 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 934 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 935 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 936 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 937 | if (expectedDisplayId.has_value()) { |
| 938 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 939 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 940 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 941 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 942 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 943 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 944 | ASSERT_THAT(keyEvent, WithKeyAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 945 | if (expectedFlags.has_value()) { |
| 946 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 947 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 948 | break; |
| 949 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 950 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 951 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 952 | ASSERT_THAT(motionEvent, WithMotionAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 953 | if (expectedFlags.has_value()) { |
| 954 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 955 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 956 | break; |
| 957 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 958 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 959 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 960 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 961 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 962 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 963 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 964 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 965 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 966 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 967 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 968 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 969 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 970 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 971 | } |
| 972 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 973 | MotionEvent* consumeMotion() { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 974 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 975 | |
| 976 | if (event == nullptr) { |
| 977 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 978 | return nullptr; |
| 979 | } |
| 980 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 981 | if (event->getType() != InputEventType::MOTION) { |
| 982 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 983 | return nullptr; |
| 984 | } |
| 985 | return static_cast<MotionEvent*>(event); |
| 986 | } |
| 987 | |
| 988 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 989 | MotionEvent* motionEvent = consumeMotion(); |
| 990 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 991 | ASSERT_THAT(*motionEvent, matcher); |
| 992 | } |
| 993 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 994 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 995 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 996 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 997 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 998 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 999 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1000 | |
| 1001 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1002 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1003 | |
| 1004 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1005 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1008 | void consumeCaptureEvent(bool hasCapture) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1009 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1010 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1011 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1012 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1013 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1014 | |
| 1015 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1016 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1017 | |
| 1018 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1019 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1020 | } |
| 1021 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1022 | void consumeDragEvent(bool isExiting, float x, float y) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1023 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1024 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1025 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1026 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1027 | |
| 1028 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1029 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1030 | |
| 1031 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1032 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1033 | EXPECT_EQ(x, dragEvent.getX()); |
| 1034 | EXPECT_EQ(y, dragEvent.getY()); |
| 1035 | } |
| 1036 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1037 | void consumeTouchModeEvent(bool inTouchMode) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1038 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1039 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1040 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1041 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1042 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1043 | |
| 1044 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1045 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1046 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1047 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1048 | } |
| 1049 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1050 | void assertNoEvents() { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1051 | InputEvent* event = consume(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1052 | if (event == nullptr) { |
| 1053 | return; |
| 1054 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1055 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1056 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1057 | ADD_FAILURE() << "Received key event " << keyEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1058 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1059 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1060 | ADD_FAILURE() << "Received motion event " << motionEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1061 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1062 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1063 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1064 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1065 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1066 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1067 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1068 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1069 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1070 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1071 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1072 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1073 | } |
| 1074 | FAIL() << mName.c_str() |
| 1075 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1076 | } |
| 1077 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1078 | sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1079 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1080 | int getChannelFd() { return mConsumer.getChannel()->getFd().get(); } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1081 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1082 | private: |
| 1083 | InputConsumer mConsumer; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1084 | PreallocatedInputEventFactory mEventFactory; |
| 1085 | |
| 1086 | std::string mName; |
| 1087 | }; |
| 1088 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1089 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1090 | public: |
| 1091 | static const int32_t WIDTH = 600; |
| 1092 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1093 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1094 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1095 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1096 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1097 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1098 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1099 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1100 | dispatcher->createInputChannel(name); |
| 1101 | token = (*channel)->getConnectionToken(); |
| 1102 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | inputApplicationHandle->updateInfo(); |
| 1106 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1107 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1108 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1109 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1110 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1111 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1112 | mInfo.alpha = 1.0; |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1113 | mInfo.frame = Rect(0, 0, WIDTH, HEIGHT); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1114 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1115 | mInfo.globalScaleFactor = 1.0; |
| 1116 | mInfo.touchableRegion.clear(); |
| 1117 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1118 | mInfo.ownerPid = WINDOW_PID; |
| 1119 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1120 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1121 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1124 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1125 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1126 | handle->mInfo = mInfo; |
| 1127 | handle->mInfo.displayId = displayId; |
| 1128 | handle->mInfo.id = sId++; |
| 1129 | handle->mInputReceiver = mInputReceiver; |
| 1130 | return handle; |
| 1131 | } |
| 1132 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1133 | void setTouchable(bool touchable) { |
| 1134 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1135 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1136 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1137 | void setFocusable(bool focusable) { |
| 1138 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1139 | } |
| 1140 | |
| 1141 | void setVisible(bool visible) { |
| 1142 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1143 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1144 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1145 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1146 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1149 | void setPaused(bool paused) { |
| 1150 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1151 | } |
| 1152 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1153 | void setPreventSplitting(bool preventSplitting) { |
| 1154 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | void setSlippery(bool slippery) { |
| 1158 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1159 | } |
| 1160 | |
| 1161 | void setWatchOutsideTouch(bool watchOutside) { |
| 1162 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1163 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1164 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1165 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1166 | |
| 1167 | void setInterceptsStylus(bool interceptsStylus) { |
| 1168 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1169 | } |
| 1170 | |
| 1171 | void setDropInput(bool dropInput) { |
| 1172 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1173 | } |
| 1174 | |
| 1175 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1176 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1177 | } |
| 1178 | |
| 1179 | void setNoInputChannel(bool noInputChannel) { |
| 1180 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1181 | } |
| 1182 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1183 | void setDisableUserActivity(bool disableUserActivity) { |
| 1184 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1185 | } |
| 1186 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame^] | 1187 | void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) { |
| 1188 | mInfo.setInputConfig(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH, |
| 1189 | shouldGlobalStylusBlockTouch); |
| 1190 | } |
| 1191 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1192 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1193 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1194 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1195 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1196 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1197 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1198 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1199 | mInfo.frame = frame; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1200 | mInfo.touchableRegion.clear(); |
| 1201 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1202 | |
| 1203 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1204 | ui::Transform translate; |
| 1205 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1206 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1209 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1210 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1211 | void setIsWallpaper(bool isWallpaper) { |
| 1212 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1213 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1214 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1215 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1216 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1217 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1218 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1219 | void setTrustedOverlay(bool trustedOverlay) { |
| 1220 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1221 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1222 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1223 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1224 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1225 | } |
| 1226 | |
| 1227 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1228 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1229 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1230 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1231 | const KeyEvent& consumeKey(bool handled = true) { |
| 1232 | const InputEvent& event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled); |
| 1233 | if (event.getType() != InputEventType::KEY) { |
| 1234 | LOG(FATAL) << "Instead of key event, got " << event; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1235 | } |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1236 | return static_cast<const KeyEvent&>(event); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1240 | const KeyEvent& keyEvent = consumeKey(); |
| 1241 | ASSERT_THAT(keyEvent, matcher); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1244 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1245 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_DOWN), WithDisplayId(expectedDisplayId), |
| 1246 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1249 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1250 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1251 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1254 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1255 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1256 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1257 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1261 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1262 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1263 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1267 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1268 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1269 | } |
| 1270 | |
| 1271 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1272 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1273 | consumeMotionEvent( |
| 1274 | AllOf(WithMotionAction(ACTION_DOWN), |
| 1275 | testing::Conditional(expectedDisplayId.has_value(), |
| 1276 | WithDisplayId(*expectedDisplayId), testing::_), |
| 1277 | testing::Conditional(expectedFlags.has_value(), WithFlags(*expectedFlags), |
| 1278 | testing::_))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1281 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1282 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1283 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1284 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1285 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1286 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1287 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1291 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1292 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1293 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1294 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1295 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1299 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1300 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1301 | WithFlags(expectedFlags))); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1304 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1305 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1306 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), |
| 1307 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1308 | } |
| 1309 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1310 | void consumeMotionOutsideWithZeroedCoords() { |
| 1311 | consumeMotionEvent( |
| 1312 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), WithRawCoords(0, 0))); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1315 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1316 | ASSERT_NE(mInputReceiver, nullptr) |
| 1317 | << "Cannot consume events from a window with no receiver"; |
| 1318 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1319 | } |
| 1320 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1321 | void consumeCaptureEvent(bool hasCapture) { |
| 1322 | ASSERT_NE(mInputReceiver, nullptr) |
| 1323 | << "Cannot consume events from a window with no receiver"; |
| 1324 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1325 | } |
| 1326 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1327 | const MotionEvent& consumeMotionEvent( |
| 1328 | const ::testing::Matcher<MotionEvent>& matcher = testing::_) { |
| 1329 | const InputEvent& event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1330 | if (event.getType() != InputEventType::MOTION) { |
| 1331 | LOG(FATAL) << "Instead of motion event, got " << event; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 1332 | } |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1333 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 1334 | EXPECT_THAT(motionEvent, matcher); |
| 1335 | return motionEvent; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1338 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1339 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1340 | } |
| 1341 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1342 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1343 | ASSERT_NE(mInputReceiver, nullptr) |
| 1344 | << "Cannot consume events from a window with no receiver"; |
| 1345 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1346 | } |
| 1347 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1348 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1349 | if (mInputReceiver == nullptr) { |
| 1350 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1351 | return std::nullopt; |
| 1352 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1353 | return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED, outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | void finishEvent(uint32_t sequenceNum) { |
| 1357 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1358 | mInputReceiver->finishEvent(sequenceNum); |
| 1359 | } |
| 1360 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1361 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1362 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1363 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1364 | } |
| 1365 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1366 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1367 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1368 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1369 | return; // Can't receive events if the window does not have input channel |
| 1370 | } |
| 1371 | ASSERT_NE(nullptr, mInputReceiver) |
| 1372 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1373 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1374 | } |
| 1375 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1376 | sp<IBinder> getToken() { return mInfo.token; } |
| 1377 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1378 | const std::string& getName() { return mName; } |
| 1379 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1380 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1381 | mInfo.ownerPid = ownerPid; |
| 1382 | mInfo.ownerUid = ownerUid; |
| 1383 | } |
| 1384 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1385 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1386 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1387 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1388 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1389 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1390 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1391 | private: |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1392 | FakeWindowHandle(std::string name) : mName(name){}; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1393 | const std::string mName; |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1394 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1395 | 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] | 1396 | friend class sp<FakeWindowHandle>; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1397 | |
| 1398 | const InputEvent& consume(std::chrono::milliseconds timeout, bool handled = true) { |
| 1399 | if (mInputReceiver == nullptr) { |
| 1400 | LOG(FATAL) << "Cannot consume event from a window with no input event receiver"; |
| 1401 | } |
| 1402 | InputEvent* event = mInputReceiver->consume(timeout, handled); |
| 1403 | if (event == nullptr) { |
| 1404 | LOG(FATAL) << "Consume failed: no event"; |
| 1405 | } |
| 1406 | return *event; |
| 1407 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1408 | }; |
| 1409 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1410 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1411 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1412 | class FakeMonitorReceiver { |
| 1413 | public: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1414 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId) |
| 1415 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1416 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1417 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1418 | |
| 1419 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1420 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1421 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1425 | return mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1428 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1429 | |
| 1430 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1431 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1432 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1436 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 1437 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1441 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 1442 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1446 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1447 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 1448 | WithDisplayId(expectedDisplayId), |
| 1449 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 1450 | } |
| 1451 | |
| 1452 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 1453 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1454 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1455 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
| 1456 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1460 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1463 | MotionEvent* consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1464 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1465 | void assertNoEvents() { mInputReceiver.assertNoEvents(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1466 | |
| 1467 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1468 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1469 | }; |
| 1470 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1471 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1472 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1473 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1474 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1475 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1476 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1477 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1478 | KeyEvent event; |
| 1479 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1480 | |
| 1481 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1482 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1483 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1484 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1485 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1486 | if (!allowKeyRepeat) { |
| 1487 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1488 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1489 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1490 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1491 | } |
| 1492 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1493 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 1494 | InputEventInjectionResult result = |
| 1495 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
| 1496 | InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
| 1497 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 1498 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1503 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1504 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1507 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1508 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1509 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1510 | static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher, |
| 1511 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1512 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1513 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1514 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1517 | static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1518 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1519 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1522 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1523 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1524 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1525 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1526 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1527 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1528 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1531 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1532 | InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1533 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1534 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1535 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1536 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1537 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1538 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1539 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1540 | MotionEventBuilder motionBuilder = |
| 1541 | MotionEventBuilder(action, source) |
| 1542 | .displayId(displayId) |
| 1543 | .eventTime(eventTime) |
| 1544 | .rawXCursorPosition(cursorPosition.x) |
| 1545 | .rawYCursorPosition(cursorPosition.y) |
| 1546 | .pointer( |
| 1547 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1548 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1549 | motionBuilder.downTime(eventTime); |
| 1550 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1551 | |
| 1552 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1553 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1554 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1555 | } |
| 1556 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1557 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
| 1558 | int32_t displayId, |
| 1559 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1560 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1563 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
| 1564 | int32_t displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1565 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1566 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1569 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1570 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1571 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1572 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1573 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 1574 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1575 | |
| 1576 | return args; |
| 1577 | } |
| 1578 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1579 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1580 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1581 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1582 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1583 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1584 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, |
| 1585 | AMETA_META_ON, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1586 | |
| 1587 | return args; |
| 1588 | } |
| 1589 | |
| 1590 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1591 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1592 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1593 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1594 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1595 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, |
| 1596 | KEY_ASSISTANT, AMETA_NONE, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1597 | |
| 1598 | return args; |
| 1599 | } |
| 1600 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1601 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1602 | int32_t displayId, |
| 1603 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1604 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1605 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1606 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1607 | } |
| 1608 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1609 | PointerProperties pointerProperties[pointerCount]; |
| 1610 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1611 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1612 | for (size_t i = 0; i < pointerCount; i++) { |
| 1613 | pointerProperties[i].clear(); |
| 1614 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1615 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1616 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1617 | pointerCoords[i].clear(); |
| 1618 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1619 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1620 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1621 | |
| 1622 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1623 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1624 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 1625 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 1626 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1627 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1628 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1629 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1630 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1631 | |
| 1632 | return args; |
| 1633 | } |
| 1634 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1635 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1636 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1637 | } |
| 1638 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1639 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1640 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1641 | } |
| 1642 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1643 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1644 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1645 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 1646 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1649 | } // namespace |
| 1650 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1651 | /** |
| 1652 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1653 | * broken channel. |
| 1654 | */ |
| 1655 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1656 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1657 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1658 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1659 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1660 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1661 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1662 | |
| 1663 | // Window closes its channel, but the window remains. |
| 1664 | window->destroyReceiver(); |
| 1665 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1666 | } |
| 1667 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1668 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1669 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1670 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1671 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1672 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1673 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1675 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1676 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1677 | |
| 1678 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1679 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1680 | } |
| 1681 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1682 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1683 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1684 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1685 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1686 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1687 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1688 | // Inject a MotionEvent to an unknown display. |
| 1689 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1690 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1691 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1692 | |
| 1693 | // Window should receive motion event. |
| 1694 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1695 | } |
| 1696 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1697 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1698 | * Calling onWindowInfosChanged once should not cause any issues. |
| 1699 | * 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] | 1700 | * called twice. |
| 1701 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1702 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1704 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1705 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1706 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1707 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1708 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1709 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1710 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1711 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1712 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1713 | |
| 1714 | // Window should receive motion event. |
| 1715 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1716 | } |
| 1717 | |
| 1718 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1719 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1720 | */ |
| 1721 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1722 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1723 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1724 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1725 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1726 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1727 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1728 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1730 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1731 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1732 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1733 | |
| 1734 | // Window should receive motion event. |
| 1735 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1736 | } |
| 1737 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1738 | // The foreground window should receive the first touch down event. |
| 1739 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1740 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1741 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1742 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1743 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1744 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1745 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1746 | mDispatcher->onWindowInfosChanged( |
| 1747 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1748 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1749 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1750 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1751 | |
| 1752 | // 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] | 1753 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1754 | windowSecond->assertNoEvents(); |
| 1755 | } |
| 1756 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1757 | /** |
| 1758 | * Two windows: A top window, and a wallpaper behind the window. |
| 1759 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1760 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1761 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1762 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1763 | */ |
| 1764 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1765 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1766 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1767 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1768 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1769 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1770 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1771 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1772 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1773 | mDispatcher->onWindowInfosChanged( |
| 1774 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1775 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1776 | injectMotionEvent(*mDispatcher, |
| 1777 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1778 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 1779 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1780 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1781 | |
| 1782 | // Both foreground window and its wallpaper should receive the touch down |
| 1783 | foregroundWindow->consumeMotionDown(); |
| 1784 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1785 | |
| 1786 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1787 | injectMotionEvent(*mDispatcher, |
| 1788 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1789 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 1790 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1791 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1792 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1793 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1794 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1795 | |
| 1796 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1797 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1798 | foregroundWindow->consumeMotionCancel(); |
| 1799 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1800 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1801 | } |
| 1802 | |
| 1803 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1804 | * Two fingers down on the window, and lift off the first finger. |
| 1805 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1806 | * contains a single pointer. |
| 1807 | */ |
| 1808 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1809 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1810 | sp<FakeWindowHandle> window = |
| 1811 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1812 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1813 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1814 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1815 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1816 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1817 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1818 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1819 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1820 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1821 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1822 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1823 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1824 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1825 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1826 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1827 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1828 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1829 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1830 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1831 | |
| 1832 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1833 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1834 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1835 | window->consumeMotionEvent( |
| 1836 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1837 | } |
| 1838 | |
| 1839 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1840 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1841 | * with the following differences: |
| 1842 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1843 | * clean up the connection. |
| 1844 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1845 | * Ensure that there's no crash in the dispatcher. |
| 1846 | */ |
| 1847 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1848 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1849 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1850 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1851 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1852 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1853 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1854 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1855 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1856 | mDispatcher->onWindowInfosChanged( |
| 1857 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1858 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1859 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1860 | {100, 200})) |
| 1861 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1862 | |
| 1863 | // Both foreground window and its wallpaper should receive the touch down |
| 1864 | foregroundWindow->consumeMotionDown(); |
| 1865 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1866 | |
| 1867 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1868 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1869 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1870 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1871 | |
| 1872 | foregroundWindow->consumeMotionMove(); |
| 1873 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1874 | |
| 1875 | // Wallpaper closes its channel, but the window remains. |
| 1876 | wallpaperWindow->destroyReceiver(); |
| 1877 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1878 | |
| 1879 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1880 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1881 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1882 | foregroundWindow->consumeMotionCancel(); |
| 1883 | } |
| 1884 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1885 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1886 | public ::testing::WithParamInterface<bool> {}; |
| 1887 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1888 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1889 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1890 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1891 | * The top window gets a multitouch gesture. |
| 1892 | * Ensure that wallpaper gets the same gesture. |
| 1893 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1894 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1895 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1896 | sp<FakeWindowHandle> foregroundWindow = |
| 1897 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1898 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1899 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1900 | |
| 1901 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1902 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1903 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1904 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1905 | mDispatcher->onWindowInfosChanged( |
| 1906 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1907 | |
| 1908 | // Touch down on top window |
| 1909 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1910 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1911 | {100, 100})) |
| 1912 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1913 | |
| 1914 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1915 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1916 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1917 | |
| 1918 | // Second finger down on the top window |
| 1919 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1920 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1921 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1922 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1923 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1924 | .build(); |
| 1925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1926 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1927 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1928 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1929 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1930 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1931 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1932 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1933 | |
| 1934 | const MotionEvent secondFingerUpEvent = |
| 1935 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1936 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1937 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1938 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1939 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1940 | .build(); |
| 1941 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1942 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1943 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1944 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1945 | foregroundWindow->consumeMotionPointerUp(0); |
| 1946 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1947 | |
| 1948 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1949 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1950 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1951 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1952 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1953 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1954 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1955 | .x(100) |
| 1956 | .y(100)) |
| 1957 | .build(), |
| 1958 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1959 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1960 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1961 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | /** |
| 1965 | * Two windows: a window on the left and window on the right. |
| 1966 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1967 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1968 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1969 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1970 | * ACTION_POINTER_DOWN(1). |
| 1971 | */ |
| 1972 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1973 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1974 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1975 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1976 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1977 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1978 | |
| 1979 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1980 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1981 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1982 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1983 | |
| 1984 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1985 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1986 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1987 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1988 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1989 | mDispatcher->onWindowInfosChanged( |
| 1990 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1991 | {}, |
| 1992 | 0, |
| 1993 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1994 | |
| 1995 | // Touch down on left window |
| 1996 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1997 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1998 | {100, 100})) |
| 1999 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2000 | |
| 2001 | // Both foreground window and its wallpaper should receive the touch down |
| 2002 | leftWindow->consumeMotionDown(); |
| 2003 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2004 | |
| 2005 | // Second finger down on the right window |
| 2006 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2007 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2008 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2009 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2010 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2011 | .build(); |
| 2012 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2013 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2014 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2015 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2016 | |
| 2017 | leftWindow->consumeMotionMove(); |
| 2018 | // Since the touch is split, right window gets ACTION_DOWN |
| 2019 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2020 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2021 | expectedWallpaperFlags); |
| 2022 | |
| 2023 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2024 | mDispatcher->onWindowInfosChanged( |
| 2025 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2026 | leftWindow->consumeMotionCancel(); |
| 2027 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2028 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2029 | |
| 2030 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2031 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2032 | const MotionEvent secondFingerMoveEvent = |
| 2033 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2034 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2035 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2036 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2037 | .build(); |
| 2038 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2039 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2040 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2041 | rightWindow->consumeMotionMove(); |
| 2042 | |
| 2043 | leftWindow->assertNoEvents(); |
| 2044 | rightWindow->assertNoEvents(); |
| 2045 | wallpaperWindow->assertNoEvents(); |
| 2046 | } |
| 2047 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2048 | /** |
| 2049 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2050 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2051 | * The right window should receive ACTION_DOWN. |
| 2052 | */ |
| 2053 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2054 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2055 | sp<FakeWindowHandle> leftWindow = |
| 2056 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2057 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2058 | leftWindow->setDupTouchToWallpaper(true); |
| 2059 | leftWindow->setSlippery(true); |
| 2060 | |
| 2061 | sp<FakeWindowHandle> rightWindow = |
| 2062 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2063 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2064 | |
| 2065 | sp<FakeWindowHandle> wallpaperWindow = |
| 2066 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2067 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2068 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2069 | mDispatcher->onWindowInfosChanged( |
| 2070 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2071 | {}, |
| 2072 | 0, |
| 2073 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2074 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2075 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2076 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2077 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2078 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2079 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2080 | |
| 2081 | // Both foreground window and its wallpaper should receive the touch down |
| 2082 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2083 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2084 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2085 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2086 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2087 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2088 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2089 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2090 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2091 | leftWindow->consumeMotionCancel(); |
| 2092 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2093 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2096 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2097 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2098 | * interactive, it might stop sending this flag. |
| 2099 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2100 | * to have a consistent input stream. |
| 2101 | * |
| 2102 | * Test procedure: |
| 2103 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2104 | * DOWN (new gesture). |
| 2105 | * |
| 2106 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2107 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2108 | * |
| 2109 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2110 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2111 | */ |
| 2112 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2113 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2114 | sp<FakeWindowHandle> spyWindow = |
| 2115 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2116 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2117 | spyWindow->setTrustedOverlay(true); |
| 2118 | spyWindow->setSpy(true); |
| 2119 | |
| 2120 | sp<FakeWindowHandle> window = |
| 2121 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2122 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2123 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2124 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2125 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2126 | |
| 2127 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2128 | mDispatcher->notifyMotion( |
| 2129 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2130 | .deviceId(touchDeviceId) |
| 2131 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2132 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2133 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2134 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2135 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2136 | .deviceId(touchDeviceId) |
| 2137 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2138 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2139 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2140 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2141 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2142 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2143 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2144 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2145 | |
| 2146 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2147 | mDispatcher->notifyMotion( |
| 2148 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2149 | .deviceId(touchDeviceId) |
| 2150 | .policyFlags(0) |
| 2151 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2152 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2153 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2154 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2155 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2156 | |
| 2157 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2158 | // 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] | 2159 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2160 | |
| 2161 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2162 | mDispatcher->notifyMotion( |
| 2163 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2164 | .deviceId(touchDeviceId) |
| 2165 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2166 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2167 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2168 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2169 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2170 | |
| 2171 | // No more events |
| 2172 | spyWindow->assertNoEvents(); |
| 2173 | window->assertNoEvents(); |
| 2174 | } |
| 2175 | |
| 2176 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 2177 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 2178 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2179 | * interactive, it might stop sending this flag. |
| 2180 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 2181 | * the consistency of the hover event in this case. |
| 2182 | * |
| 2183 | * Test procedure: |
| 2184 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 2185 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 2186 | * |
| 2187 | * We expect to receive two full streams of hover events. |
| 2188 | */ |
| 2189 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 2190 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2191 | |
| 2192 | sp<FakeWindowHandle> window = |
| 2193 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2194 | window->setFrame(Rect(0, 0, 300, 300)); |
| 2195 | |
| 2196 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2197 | |
| 2198 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2199 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2200 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2201 | .build()); |
| 2202 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2203 | |
| 2204 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2205 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2206 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2207 | .build()); |
| 2208 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2209 | |
| 2210 | // Send hover exit without the default policy flags. |
| 2211 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2212 | .policyFlags(0) |
| 2213 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2214 | .build()); |
| 2215 | |
| 2216 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2217 | |
| 2218 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 2219 | // right event. |
| 2220 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2221 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2222 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 2223 | .build()); |
| 2224 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2225 | |
| 2226 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2227 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2228 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2229 | .build()); |
| 2230 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2231 | |
| 2232 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2233 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2234 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2235 | .build()); |
| 2236 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2237 | } |
| 2238 | |
| 2239 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2240 | * Two windows: a window on the left and a window on the right. |
| 2241 | * Mouse is hovered from the right window into the left window. |
| 2242 | * Next, we tap on the left window, where the cursor was last seen. |
| 2243 | * The second tap is done onto the right window. |
| 2244 | * The mouse and tap are from two different devices. |
| 2245 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2246 | * explicitly helps during debugging. |
| 2247 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2248 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2249 | */ |
| 2250 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2251 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2252 | sp<FakeWindowHandle> leftWindow = |
| 2253 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2254 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2255 | |
| 2256 | sp<FakeWindowHandle> rightWindow = |
| 2257 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2258 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2259 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2260 | mDispatcher->onWindowInfosChanged( |
| 2261 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2262 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2263 | // stale. |
| 2264 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2265 | const int32_t mouseDeviceId = 6; |
| 2266 | const int32_t touchDeviceId = 4; |
| 2267 | // Move the cursor from right |
| 2268 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2269 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2270 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2271 | AINPUT_SOURCE_MOUSE) |
| 2272 | .deviceId(mouseDeviceId) |
| 2273 | .downTime(baseTime + 10) |
| 2274 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2275 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2276 | .build())); |
| 2277 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2278 | |
| 2279 | // .. to the left window |
| 2280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2281 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2282 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2283 | AINPUT_SOURCE_MOUSE) |
| 2284 | .deviceId(mouseDeviceId) |
| 2285 | .downTime(baseTime + 10) |
| 2286 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2287 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2288 | .build())); |
| 2289 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2290 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2291 | // Now tap the left window |
| 2292 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2293 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2294 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2295 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2296 | .deviceId(touchDeviceId) |
| 2297 | .downTime(baseTime + 40) |
| 2298 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2299 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2300 | .build())); |
| 2301 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2302 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2303 | |
| 2304 | // release tap |
| 2305 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2306 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2307 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2308 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2309 | .deviceId(touchDeviceId) |
| 2310 | .downTime(baseTime + 40) |
| 2311 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2312 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2313 | .build())); |
| 2314 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2315 | |
| 2316 | // Tap the window on the right |
| 2317 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2318 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2319 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2320 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2321 | .deviceId(touchDeviceId) |
| 2322 | .downTime(baseTime + 60) |
| 2323 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2324 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2325 | .build())); |
| 2326 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2327 | |
| 2328 | // release tap |
| 2329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2330 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2331 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2332 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2333 | .deviceId(touchDeviceId) |
| 2334 | .downTime(baseTime + 60) |
| 2335 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2336 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2337 | .build())); |
| 2338 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2339 | |
| 2340 | // No more events |
| 2341 | leftWindow->assertNoEvents(); |
| 2342 | rightWindow->assertNoEvents(); |
| 2343 | } |
| 2344 | |
| 2345 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2346 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2347 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2348 | * While the top window is present, the hovering is stopped. |
| 2349 | * Later, hovering gets resumed again. |
| 2350 | * Ensure that new hover gesture is handled correctly. |
| 2351 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2352 | * to the window that's currently being hovered over. |
| 2353 | */ |
| 2354 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2355 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2356 | sp<FakeWindowHandle> window = |
| 2357 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2358 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2359 | |
| 2360 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2361 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2362 | |
| 2363 | // Start hovering in the window |
| 2364 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2365 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2366 | .build()); |
| 2367 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2368 | |
| 2369 | // Now, an obscuring window appears! |
| 2370 | sp<FakeWindowHandle> obscuringWindow = |
| 2371 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2372 | ADISPLAY_ID_DEFAULT, |
| 2373 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2374 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2375 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2376 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2377 | obscuringWindow->setNoInputChannel(true); |
| 2378 | obscuringWindow->setFocusable(false); |
| 2379 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2380 | mDispatcher->onWindowInfosChanged( |
| 2381 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2382 | |
| 2383 | // While this new obscuring window is present, the hovering is stopped |
| 2384 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2385 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2386 | .build()); |
| 2387 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2388 | |
| 2389 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2390 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2391 | |
| 2392 | // And a new hover gesture starts. |
| 2393 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2394 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2395 | .build()); |
| 2396 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2397 | } |
| 2398 | |
| 2399 | /** |
| 2400 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2401 | * the obscuring window. |
| 2402 | */ |
| 2403 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2404 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2405 | sp<FakeWindowHandle> window = |
| 2406 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2407 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2408 | |
| 2409 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2410 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2411 | |
| 2412 | // Start hovering in the window |
| 2413 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2414 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2415 | .build()); |
| 2416 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2417 | |
| 2418 | // Now, an obscuring window appears! |
| 2419 | sp<FakeWindowHandle> obscuringWindow = |
| 2420 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2421 | ADISPLAY_ID_DEFAULT, |
| 2422 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2423 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2424 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2425 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2426 | obscuringWindow->setNoInputChannel(true); |
| 2427 | obscuringWindow->setFocusable(false); |
| 2428 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2429 | mDispatcher->onWindowInfosChanged( |
| 2430 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2431 | |
| 2432 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2433 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2434 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2435 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2436 | .build()); |
| 2437 | obscuringWindow->assertNoEvents(); |
| 2438 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2439 | |
| 2440 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2441 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2442 | |
| 2443 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2444 | // so it should generate a HOVER_ENTER |
| 2445 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2446 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2447 | .build()); |
| 2448 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2449 | |
| 2450 | // Now the MOVE should be getting dispatched normally |
| 2451 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2452 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2453 | .build()); |
| 2454 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2455 | } |
| 2456 | |
| 2457 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2458 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 2459 | * events are delivered to the window. |
| 2460 | */ |
| 2461 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 2462 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2463 | sp<FakeWindowHandle> window = |
| 2464 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2465 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2466 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2467 | |
| 2468 | // Start hovering in the window |
| 2469 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2470 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2471 | .build()); |
| 2472 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2473 | |
| 2474 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2475 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2476 | .build()); |
| 2477 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2478 | |
| 2479 | // Scroll with the mouse |
| 2480 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 2481 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2482 | .build()); |
| 2483 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 2484 | } |
| 2485 | |
| 2486 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2487 | |
| 2488 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2489 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2490 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2491 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2492 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2493 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2494 | sp<FakeWindowHandle> window = |
| 2495 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2496 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2497 | |
| 2498 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2499 | |
| 2500 | constexpr int32_t touchDeviceId = 4; |
| 2501 | constexpr int32_t stylusDeviceId = 2; |
| 2502 | |
| 2503 | // Stylus down |
| 2504 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2505 | .deviceId(stylusDeviceId) |
| 2506 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2507 | .build()); |
| 2508 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2509 | |
| 2510 | // Touch down |
| 2511 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2512 | .deviceId(touchDeviceId) |
| 2513 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2514 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2515 | |
| 2516 | // Touch move |
| 2517 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2518 | .deviceId(touchDeviceId) |
| 2519 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2520 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2521 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2522 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2523 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2524 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2525 | .deviceId(stylusDeviceId) |
| 2526 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2527 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2528 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2529 | WithCoords(101, 111))); |
| 2530 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2531 | window->assertNoEvents(); |
| 2532 | } |
| 2533 | |
| 2534 | /** |
| 2535 | * 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] | 2536 | * 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] | 2537 | * Similar test as above, but with added SPY window. |
| 2538 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2539 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2540 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2541 | sp<FakeWindowHandle> window = |
| 2542 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2543 | sp<FakeWindowHandle> spyWindow = |
| 2544 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2545 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2546 | spyWindow->setTrustedOverlay(true); |
| 2547 | spyWindow->setSpy(true); |
| 2548 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2549 | |
| 2550 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2551 | |
| 2552 | constexpr int32_t touchDeviceId = 4; |
| 2553 | constexpr int32_t stylusDeviceId = 2; |
| 2554 | |
| 2555 | // Stylus down |
| 2556 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2557 | .deviceId(stylusDeviceId) |
| 2558 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2559 | .build()); |
| 2560 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2561 | spyWindow->consumeMotionEvent( |
| 2562 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2563 | |
| 2564 | // Touch down |
| 2565 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2566 | .deviceId(touchDeviceId) |
| 2567 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2568 | .build()); |
| 2569 | |
| 2570 | // Touch move |
| 2571 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2572 | .deviceId(touchDeviceId) |
| 2573 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2574 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2575 | |
| 2576 | // Touch is ignored because stylus is already down |
| 2577 | |
| 2578 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2579 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2580 | .deviceId(stylusDeviceId) |
| 2581 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2582 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2583 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2584 | WithCoords(101, 111))); |
| 2585 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2586 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2587 | |
| 2588 | window->assertNoEvents(); |
| 2589 | spyWindow->assertNoEvents(); |
| 2590 | } |
| 2591 | |
| 2592 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2593 | * 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] | 2594 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2595 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2596 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2597 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2598 | sp<FakeWindowHandle> window = |
| 2599 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2600 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2601 | |
| 2602 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2603 | |
| 2604 | constexpr int32_t touchDeviceId = 4; |
| 2605 | constexpr int32_t stylusDeviceId = 2; |
| 2606 | |
| 2607 | // Stylus down on the window |
| 2608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2609 | .deviceId(stylusDeviceId) |
| 2610 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2611 | .build()); |
| 2612 | window->consumeMotionEvent( |
| 2613 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2614 | |
| 2615 | // Touch down on window |
| 2616 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2617 | .deviceId(touchDeviceId) |
| 2618 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2619 | .build()); |
| 2620 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2621 | .deviceId(touchDeviceId) |
| 2622 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2623 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2624 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2625 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2626 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2627 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2628 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2629 | .deviceId(stylusDeviceId) |
| 2630 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2631 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2632 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2633 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2634 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2635 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2636 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2637 | .deviceId(touchDeviceId) |
| 2638 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2639 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2640 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | /** |
| 2644 | * 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] | 2645 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2646 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2647 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2648 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2649 | sp<FakeWindowHandle> window = |
| 2650 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2651 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2652 | |
| 2653 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2654 | |
| 2655 | constexpr int32_t touchDeviceId = 4; |
| 2656 | constexpr int32_t stylusDeviceId = 2; |
| 2657 | |
| 2658 | // Touch down on window |
| 2659 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2660 | .deviceId(touchDeviceId) |
| 2661 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2662 | .build()); |
| 2663 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2664 | .deviceId(touchDeviceId) |
| 2665 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2666 | .build()); |
| 2667 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2668 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2669 | |
| 2670 | // Stylus hover on the window |
| 2671 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2672 | .deviceId(stylusDeviceId) |
| 2673 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2674 | .build()); |
| 2675 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2676 | .deviceId(stylusDeviceId) |
| 2677 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2678 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2679 | // Stylus hover movement causes touch to be canceled |
| 2680 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2681 | WithCoords(141, 146))); |
| 2682 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2683 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2684 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2685 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2686 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2687 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2688 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2689 | .deviceId(touchDeviceId) |
| 2690 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2691 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2692 | |
| 2693 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | /** |
| 2697 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2698 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2699 | * become active. |
| 2700 | */ |
| 2701 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
| 2702 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2703 | sp<FakeWindowHandle> window = |
| 2704 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2705 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2706 | |
| 2707 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2708 | |
| 2709 | constexpr int32_t stylusDeviceId1 = 3; |
| 2710 | constexpr int32_t stylusDeviceId2 = 5; |
| 2711 | |
| 2712 | // Touch down on window |
| 2713 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2714 | .deviceId(stylusDeviceId1) |
| 2715 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2716 | .build()); |
| 2717 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2718 | .deviceId(stylusDeviceId1) |
| 2719 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2720 | .build()); |
| 2721 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2722 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2723 | |
| 2724 | // Second stylus down |
| 2725 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2726 | .deviceId(stylusDeviceId2) |
| 2727 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2728 | .build()); |
| 2729 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2730 | .deviceId(stylusDeviceId2) |
| 2731 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2732 | .build()); |
| 2733 | |
| 2734 | // First stylus is canceled, second one takes over. |
| 2735 | window->consumeMotionEvent( |
| 2736 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2737 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2738 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2739 | |
| 2740 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2741 | .deviceId(stylusDeviceId1) |
| 2742 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2743 | .build()); |
| 2744 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2745 | window->assertNoEvents(); |
| 2746 | } |
| 2747 | |
| 2748 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2749 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2750 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2751 | */ |
| 2752 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
| 2753 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2754 | sp<FakeWindowHandle> window = |
| 2755 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2756 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2757 | |
| 2758 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2759 | |
| 2760 | constexpr int32_t touchDeviceId = 4; |
| 2761 | constexpr int32_t stylusDeviceId = 2; |
| 2762 | |
| 2763 | // Touch down on window |
| 2764 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2765 | .deviceId(touchDeviceId) |
| 2766 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2767 | .build()); |
| 2768 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2769 | .deviceId(touchDeviceId) |
| 2770 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2771 | .build()); |
| 2772 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2773 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2774 | |
| 2775 | // Stylus down on the window |
| 2776 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2777 | .deviceId(stylusDeviceId) |
| 2778 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2779 | .build()); |
| 2780 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2781 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2782 | |
| 2783 | // Subsequent stylus movements are delivered correctly |
| 2784 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2785 | .deviceId(stylusDeviceId) |
| 2786 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2787 | .build()); |
| 2788 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2789 | WithCoords(101, 111))); |
| 2790 | } |
| 2791 | |
| 2792 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2793 | * Two windows: a window on the left and a window on the right. |
| 2794 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2795 | * down. Then, on the left window, also place second touch pointer down. |
| 2796 | * This test tries to reproduce a crash. |
| 2797 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2798 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2799 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2800 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2801 | sp<FakeWindowHandle> leftWindow = |
| 2802 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2803 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2804 | |
| 2805 | sp<FakeWindowHandle> rightWindow = |
| 2806 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2807 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2808 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2809 | mDispatcher->onWindowInfosChanged( |
| 2810 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2811 | |
| 2812 | const int32_t touchDeviceId = 4; |
| 2813 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2814 | |
| 2815 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2816 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2817 | .deviceId(mouseDeviceId) |
| 2818 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2819 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2820 | leftWindow->consumeMotionEvent( |
| 2821 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2822 | |
| 2823 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2824 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2825 | .deviceId(mouseDeviceId) |
| 2826 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2827 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2828 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2829 | |
| 2830 | leftWindow->consumeMotionEvent( |
| 2831 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2832 | leftWindow->consumeMotionEvent( |
| 2833 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2834 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2835 | mDispatcher->notifyMotion( |
| 2836 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2837 | .deviceId(mouseDeviceId) |
| 2838 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2839 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2840 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2841 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2842 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2843 | |
| 2844 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2845 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2846 | .deviceId(touchDeviceId) |
| 2847 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2848 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2849 | leftWindow->assertNoEvents(); |
| 2850 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2851 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2852 | |
| 2853 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2854 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2855 | .deviceId(touchDeviceId) |
| 2856 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2857 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2858 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2859 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2860 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2861 | // be canceled. |
| 2862 | leftWindow->consumeMotionEvent( |
| 2863 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2864 | leftWindow->consumeMotionEvent( |
| 2865 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2866 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2867 | // current implementation. |
| 2868 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2869 | rightWindow->consumeMotionEvent( |
| 2870 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2871 | |
| 2872 | leftWindow->assertNoEvents(); |
| 2873 | rightWindow->assertNoEvents(); |
| 2874 | } |
| 2875 | |
| 2876 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2877 | * Two windows: a window on the left and a window on the right. |
| 2878 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2879 | */ |
| 2880 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2881 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2882 | sp<FakeWindowHandle> leftWindow = |
| 2883 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2884 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2885 | |
| 2886 | sp<FakeWindowHandle> rightWindow = |
| 2887 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2888 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2889 | |
| 2890 | mDispatcher->onWindowInfosChanged( |
| 2891 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2892 | |
| 2893 | const int32_t stylusDeviceId = 3; |
| 2894 | const int32_t mouseDeviceId = 6; |
| 2895 | |
| 2896 | // Start hovering over the left window |
| 2897 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2898 | .deviceId(mouseDeviceId) |
| 2899 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2900 | .build()); |
| 2901 | leftWindow->consumeMotionEvent( |
| 2902 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2903 | |
| 2904 | // Stylus hovered on right window |
| 2905 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2906 | .deviceId(stylusDeviceId) |
| 2907 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2908 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2909 | rightWindow->consumeMotionEvent( |
| 2910 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2911 | |
| 2912 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2913 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2914 | .deviceId(mouseDeviceId) |
| 2915 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2916 | .build()); |
| 2917 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2918 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2919 | |
| 2920 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2921 | .deviceId(stylusDeviceId) |
| 2922 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2923 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2924 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2925 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2926 | |
| 2927 | leftWindow->assertNoEvents(); |
| 2928 | rightWindow->assertNoEvents(); |
| 2929 | } |
| 2930 | |
| 2931 | /** |
| 2932 | * Three windows: a window on the left and a window on the right. |
| 2933 | * And a spy window that's positioned above all of them. |
| 2934 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2935 | * Check the stream that's received by the spy. |
| 2936 | */ |
| 2937 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 2938 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2939 | |
| 2940 | sp<FakeWindowHandle> spyWindow = |
| 2941 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2942 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2943 | spyWindow->setTrustedOverlay(true); |
| 2944 | spyWindow->setSpy(true); |
| 2945 | |
| 2946 | sp<FakeWindowHandle> leftWindow = |
| 2947 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2948 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2949 | |
| 2950 | sp<FakeWindowHandle> rightWindow = |
| 2951 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2952 | |
| 2953 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2954 | |
| 2955 | mDispatcher->onWindowInfosChanged( |
| 2956 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2957 | |
| 2958 | const int32_t stylusDeviceId = 1; |
| 2959 | const int32_t touchDeviceId = 2; |
| 2960 | |
| 2961 | // Stylus down on the left window |
| 2962 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2963 | .deviceId(stylusDeviceId) |
| 2964 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2965 | .build()); |
| 2966 | leftWindow->consumeMotionEvent( |
| 2967 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2968 | spyWindow->consumeMotionEvent( |
| 2969 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2970 | |
| 2971 | // Touch down on the right window |
| 2972 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2973 | .deviceId(touchDeviceId) |
| 2974 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2975 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2976 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2977 | rightWindow->consumeMotionEvent( |
| 2978 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2979 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2980 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 2981 | // already has an active stylus gesture. |
| 2982 | |
| 2983 | // 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] | 2984 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2985 | .deviceId(stylusDeviceId) |
| 2986 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2987 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2988 | leftWindow->consumeMotionEvent( |
| 2989 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2990 | spyWindow->consumeMotionEvent( |
| 2991 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2992 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2993 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2994 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2995 | .deviceId(touchDeviceId) |
| 2996 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2997 | .build()); |
| 2998 | rightWindow->consumeMotionEvent( |
| 2999 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3000 | |
| 3001 | spyWindow->assertNoEvents(); |
| 3002 | leftWindow->assertNoEvents(); |
| 3003 | rightWindow->assertNoEvents(); |
| 3004 | } |
| 3005 | |
| 3006 | /** |
| 3007 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3008 | * both. |
| 3009 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3010 | * 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] | 3011 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3012 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3013 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3014 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3015 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3016 | |
| 3017 | sp<FakeWindowHandle> spyWindow = |
| 3018 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3019 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3020 | spyWindow->setTrustedOverlay(true); |
| 3021 | spyWindow->setSpy(true); |
| 3022 | |
| 3023 | sp<FakeWindowHandle> leftWindow = |
| 3024 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3025 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3026 | |
| 3027 | sp<FakeWindowHandle> rightWindow = |
| 3028 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3029 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3030 | |
| 3031 | mDispatcher->onWindowInfosChanged( |
| 3032 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3033 | |
| 3034 | const int32_t stylusDeviceId = 1; |
| 3035 | const int32_t touchDeviceId = 2; |
| 3036 | |
| 3037 | // Stylus hover on the left window |
| 3038 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3039 | .deviceId(stylusDeviceId) |
| 3040 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3041 | .build()); |
| 3042 | leftWindow->consumeMotionEvent( |
| 3043 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3044 | spyWindow->consumeMotionEvent( |
| 3045 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3046 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3047 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 3048 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3049 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3050 | .deviceId(touchDeviceId) |
| 3051 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3052 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3053 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3054 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3055 | rightWindow->consumeMotionEvent( |
| 3056 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3057 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3058 | // 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] | 3059 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3060 | .deviceId(stylusDeviceId) |
| 3061 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3062 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3063 | leftWindow->consumeMotionEvent( |
| 3064 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3065 | spyWindow->consumeMotionEvent( |
| 3066 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3067 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3068 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3069 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3070 | .deviceId(touchDeviceId) |
| 3071 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3072 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3073 | rightWindow->consumeMotionEvent( |
| 3074 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3075 | |
| 3076 | spyWindow->assertNoEvents(); |
| 3077 | leftWindow->assertNoEvents(); |
| 3078 | rightWindow->assertNoEvents(); |
| 3079 | } |
| 3080 | |
| 3081 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3082 | * On a single window, use two different devices: mouse and touch. |
| 3083 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3084 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3085 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3086 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3087 | * represent a new gesture. |
| 3088 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3089 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3090 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3091 | sp<FakeWindowHandle> window = |
| 3092 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3093 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3094 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3095 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3096 | |
| 3097 | const int32_t touchDeviceId = 4; |
| 3098 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3099 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3100 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3101 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3102 | .deviceId(touchDeviceId) |
| 3103 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3104 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3105 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3106 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3107 | .deviceId(touchDeviceId) |
| 3108 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3109 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3110 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3111 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3112 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3113 | .deviceId(touchDeviceId) |
| 3114 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3115 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3116 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3117 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3118 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3119 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3120 | |
| 3121 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3122 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3123 | .deviceId(mouseDeviceId) |
| 3124 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3125 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3126 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3127 | |
| 3128 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3129 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3130 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3131 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3132 | mDispatcher->notifyMotion( |
| 3133 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3134 | .deviceId(mouseDeviceId) |
| 3135 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3136 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3137 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3138 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3139 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3140 | |
| 3141 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3142 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3143 | .deviceId(touchDeviceId) |
| 3144 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3145 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3146 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3147 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3148 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3149 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3150 | // However, mouse movements should continue to work. |
| 3151 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3152 | .deviceId(mouseDeviceId) |
| 3153 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3154 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3155 | .build()); |
| 3156 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3157 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3158 | window->assertNoEvents(); |
| 3159 | } |
| 3160 | |
| 3161 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3162 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3163 | * the injected event. |
| 3164 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3165 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3166 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3167 | sp<FakeWindowHandle> window = |
| 3168 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3169 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3170 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3171 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3172 | |
| 3173 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3174 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3175 | // completion. |
| 3176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3177 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3178 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3179 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3180 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3181 | .build())); |
| 3182 | window->consumeMotionEvent( |
| 3183 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3184 | |
| 3185 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3186 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3187 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3188 | .deviceId(touchDeviceId) |
| 3189 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3190 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3191 | |
| 3192 | window->consumeMotionEvent( |
| 3193 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3194 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3195 | } |
| 3196 | |
| 3197 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3198 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3199 | * |
| 3200 | * Two windows: a window on the left and a window on the right. |
| 3201 | * Mouse is hovered over the left window. |
| 3202 | * Next, we tap on the left window, where the cursor was last seen. |
| 3203 | * |
| 3204 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3205 | * the left window. |
| 3206 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3207 | * window (first), and then another on the left window (second). |
| 3208 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3209 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3210 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3211 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3212 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3213 | sp<FakeWindowHandle> leftWindow = |
| 3214 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3215 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3216 | |
| 3217 | sp<FakeWindowHandle> rightWindow = |
| 3218 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3219 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3220 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3221 | mDispatcher->onWindowInfosChanged( |
| 3222 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3223 | |
| 3224 | const int32_t mouseDeviceId = 6; |
| 3225 | const int32_t touchDeviceId = 4; |
| 3226 | // Hover over the left window. Keep the cursor there. |
| 3227 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3228 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3229 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3230 | AINPUT_SOURCE_MOUSE) |
| 3231 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3232 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3233 | .build())); |
| 3234 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3235 | |
| 3236 | // Tap on left window |
| 3237 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3238 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3239 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3240 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3241 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3242 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3243 | .build())); |
| 3244 | |
| 3245 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3246 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3247 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3248 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3249 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3250 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3251 | .build())); |
| 3252 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3253 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3254 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3255 | |
| 3256 | // First finger down on right window |
| 3257 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3258 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3259 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3260 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3261 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3262 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3263 | .build())); |
| 3264 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3265 | |
| 3266 | // Second finger down on the left window |
| 3267 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3268 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3269 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3270 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3271 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3272 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3273 | .build())); |
| 3274 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3275 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3276 | |
| 3277 | // No more events |
| 3278 | leftWindow->assertNoEvents(); |
| 3279 | rightWindow->assertNoEvents(); |
| 3280 | } |
| 3281 | |
| 3282 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3283 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3284 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3285 | * touch is gone, stylus hovering should start working again. |
| 3286 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3287 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3288 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3289 | sp<FakeWindowHandle> window = |
| 3290 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3291 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3292 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3293 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3294 | |
| 3295 | const int32_t stylusDeviceId = 5; |
| 3296 | const int32_t touchDeviceId = 4; |
| 3297 | // Start hovering with stylus |
| 3298 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3299 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3300 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3301 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3302 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3303 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3304 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3305 | |
| 3306 | // Finger down on the window |
| 3307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3308 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3309 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3310 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3311 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3312 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3313 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3314 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3315 | // Continue hovering with stylus. |
| 3316 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3317 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3318 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3319 | AINPUT_SOURCE_STYLUS) |
| 3320 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3321 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3322 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3323 | // Hovers continue to work |
| 3324 | window->consumeMotionEvent( |
| 3325 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3326 | |
| 3327 | // Lift up the finger |
| 3328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3329 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3330 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3331 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3332 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3333 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3334 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3335 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3336 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3337 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3338 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3339 | AINPUT_SOURCE_STYLUS) |
| 3340 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3341 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3342 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3343 | window->consumeMotionEvent( |
| 3344 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3345 | window->assertNoEvents(); |
| 3346 | } |
| 3347 | |
| 3348 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame^] | 3349 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3350 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3351 | * |
| 3352 | * Two windows: one on the left and one on the right. |
| 3353 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3354 | * Stylus down on the left window, and then touch down on the right window. |
| 3355 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3356 | */ |
| 3357 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3359 | sp<FakeWindowHandle> leftWindow = |
| 3360 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3361 | ADISPLAY_ID_DEFAULT); |
| 3362 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3363 | |
| 3364 | sp<FakeWindowHandle> sbtRightWindow = |
| 3365 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3366 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3367 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3368 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3369 | |
| 3370 | mDispatcher->onWindowInfosChanged( |
| 3371 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3372 | |
| 3373 | const int32_t stylusDeviceId = 5; |
| 3374 | const int32_t touchDeviceId = 4; |
| 3375 | |
| 3376 | // Stylus down in the left window |
| 3377 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3378 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3379 | .deviceId(stylusDeviceId) |
| 3380 | .build()); |
| 3381 | leftWindow->consumeMotionEvent( |
| 3382 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3383 | |
| 3384 | // Finger tap on the right window |
| 3385 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3386 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3387 | .deviceId(touchDeviceId) |
| 3388 | .build()); |
| 3389 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3390 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3391 | .deviceId(touchDeviceId) |
| 3392 | .build()); |
| 3393 | |
| 3394 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3395 | sbtRightWindow->assertNoEvents(); |
| 3396 | |
| 3397 | // Continue stylus motion, and ensure it's not impacted. |
| 3398 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3399 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3400 | .deviceId(stylusDeviceId) |
| 3401 | .build()); |
| 3402 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3403 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3404 | .deviceId(stylusDeviceId) |
| 3405 | .build()); |
| 3406 | leftWindow->consumeMotionEvent( |
| 3407 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3408 | leftWindow->consumeMotionEvent( |
| 3409 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3410 | |
| 3411 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3412 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3413 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3414 | .deviceId(touchDeviceId) |
| 3415 | .build()); |
| 3416 | sbtRightWindow->consumeMotionEvent( |
| 3417 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3418 | } |
| 3419 | |
| 3420 | /** |
| 3421 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3422 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3423 | * |
| 3424 | * Two windows: one on the left and one on the right. |
| 3425 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3426 | * Stylus hover on the left window, and then touch down on the right window. |
| 3427 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3428 | */ |
| 3429 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3430 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3431 | sp<FakeWindowHandle> leftWindow = |
| 3432 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3433 | ADISPLAY_ID_DEFAULT); |
| 3434 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3435 | |
| 3436 | sp<FakeWindowHandle> sbtRightWindow = |
| 3437 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3438 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3439 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3440 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3441 | |
| 3442 | mDispatcher->onWindowInfosChanged( |
| 3443 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3444 | |
| 3445 | const int32_t stylusDeviceId = 5; |
| 3446 | const int32_t touchDeviceId = 4; |
| 3447 | |
| 3448 | // Stylus hover in the left window |
| 3449 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3450 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3451 | .deviceId(stylusDeviceId) |
| 3452 | .build()); |
| 3453 | leftWindow->consumeMotionEvent( |
| 3454 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3455 | |
| 3456 | // Finger tap on the right window |
| 3457 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3458 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3459 | .deviceId(touchDeviceId) |
| 3460 | .build()); |
| 3461 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3462 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3463 | .deviceId(touchDeviceId) |
| 3464 | .build()); |
| 3465 | |
| 3466 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3467 | sbtRightWindow->assertNoEvents(); |
| 3468 | |
| 3469 | // Continue stylus motion, and ensure it's not impacted. |
| 3470 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3471 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3472 | .deviceId(stylusDeviceId) |
| 3473 | .build()); |
| 3474 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3475 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3476 | .deviceId(stylusDeviceId) |
| 3477 | .build()); |
| 3478 | leftWindow->consumeMotionEvent( |
| 3479 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3480 | leftWindow->consumeMotionEvent( |
| 3481 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3482 | |
| 3483 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3484 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3485 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3486 | .deviceId(touchDeviceId) |
| 3487 | .build()); |
| 3488 | sbtRightWindow->consumeMotionEvent( |
| 3489 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3490 | } |
| 3491 | |
| 3492 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3493 | * A spy window above a window with no input channel. |
| 3494 | * Start hovering with a stylus device, and then tap with it. |
| 3495 | * Ensure spy window receives the entire sequence. |
| 3496 | */ |
| 3497 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3498 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3499 | sp<FakeWindowHandle> spyWindow = |
| 3500 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3501 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3502 | spyWindow->setTrustedOverlay(true); |
| 3503 | spyWindow->setSpy(true); |
| 3504 | sp<FakeWindowHandle> window = |
| 3505 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3506 | window->setNoInputChannel(true); |
| 3507 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3508 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3509 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3510 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3511 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3512 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3513 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3514 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3515 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3516 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3517 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3518 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3519 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3520 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3521 | |
| 3522 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3523 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3524 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3525 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3526 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3527 | |
| 3528 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3529 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3530 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3531 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3532 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3533 | |
| 3534 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3535 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3536 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3537 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3538 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3539 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3540 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3541 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3542 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3543 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3544 | |
| 3545 | // No more events |
| 3546 | spyWindow->assertNoEvents(); |
| 3547 | window->assertNoEvents(); |
| 3548 | } |
| 3549 | |
| 3550 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3551 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3552 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3553 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3554 | */ |
| 3555 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3556 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3557 | |
| 3558 | sp<FakeWindowHandle> window = |
| 3559 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3560 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3561 | |
| 3562 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3563 | |
| 3564 | // Start hovering with stylus |
| 3565 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3566 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3567 | .build()); |
| 3568 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3569 | |
| 3570 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3571 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3572 | .build(); |
| 3573 | // Make this 'hoverExit' event stale |
| 3574 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3575 | std::this_thread::sleep_for(100ms); |
| 3576 | |
| 3577 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3578 | mDispatcher->notifyMotion(hoverExit); |
| 3579 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3580 | |
| 3581 | // Stylus starts hovering again! There should be no crash. |
| 3582 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3583 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3584 | .build()); |
| 3585 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3586 | } |
| 3587 | |
| 3588 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3589 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3590 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3591 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3592 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3593 | */ |
| 3594 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3595 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3596 | sp<FakeWindowHandle> spyWindow = |
| 3597 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3598 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3599 | spyWindow->setTrustedOverlay(true); |
| 3600 | spyWindow->setSpy(true); |
| 3601 | sp<FakeWindowHandle> window = |
| 3602 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3603 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3604 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3605 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3606 | |
| 3607 | const int32_t mouseDeviceId = 7; |
| 3608 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3609 | |
| 3610 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3611 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3612 | .deviceId(mouseDeviceId) |
| 3613 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3614 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3615 | spyWindow->consumeMotionEvent( |
| 3616 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3617 | window->consumeMotionEvent( |
| 3618 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3619 | |
| 3620 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3621 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3622 | .deviceId(touchDeviceId) |
| 3623 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3624 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3625 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3626 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3627 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3628 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3629 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3630 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3631 | .deviceId(touchDeviceId) |
| 3632 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3633 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3634 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3635 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3636 | |
| 3637 | // Pilfer the stream |
| 3638 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3639 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3640 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3641 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3642 | .deviceId(touchDeviceId) |
| 3643 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3644 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3645 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3646 | |
| 3647 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3648 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3649 | .deviceId(mouseDeviceId) |
| 3650 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3651 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3652 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3653 | |
| 3654 | spyWindow->consumeMotionEvent( |
| 3655 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3656 | spyWindow->consumeMotionEvent( |
| 3657 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3658 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3659 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3660 | mDispatcher->notifyMotion( |
| 3661 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3662 | .deviceId(mouseDeviceId) |
| 3663 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3664 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3665 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3666 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3667 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3668 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3669 | |
| 3670 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3671 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3672 | .deviceId(mouseDeviceId) |
| 3673 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3674 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3675 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3676 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3677 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3678 | |
| 3679 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3680 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3681 | .deviceId(touchDeviceId) |
| 3682 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3683 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3684 | |
| 3685 | // No more events |
| 3686 | spyWindow->assertNoEvents(); |
| 3687 | window->assertNoEvents(); |
| 3688 | } |
| 3689 | |
| 3690 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3691 | * On the display, have a single window, and also an area where there's no window. |
| 3692 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3693 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3694 | */ |
| 3695 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3696 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3697 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3698 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3699 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3700 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3701 | |
| 3702 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3703 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3704 | |
| 3705 | mDispatcher->waitForIdle(); |
| 3706 | window->assertNoEvents(); |
| 3707 | |
| 3708 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3709 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3710 | mDispatcher->waitForIdle(); |
| 3711 | window->consumeMotionDown(); |
| 3712 | } |
| 3713 | |
| 3714 | /** |
| 3715 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3716 | * non-touchable window. |
| 3717 | */ |
| 3718 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3719 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3720 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3721 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3722 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3723 | window1->setTouchable(false); |
| 3724 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3725 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3726 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3727 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3728 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3729 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3730 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3731 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3732 | |
| 3733 | mDispatcher->waitForIdle(); |
| 3734 | window1->assertNoEvents(); |
| 3735 | window2->assertNoEvents(); |
| 3736 | |
| 3737 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3738 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3739 | mDispatcher->waitForIdle(); |
| 3740 | window2->consumeMotionDown(); |
| 3741 | } |
| 3742 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3743 | /** |
| 3744 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3745 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3746 | */ |
| 3747 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3748 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3749 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3750 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3751 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3752 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3753 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3754 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3755 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3756 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3757 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3758 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3759 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3760 | |
| 3761 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3762 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 3763 | const MotionEvent& motionEvent1 = window1->consumeMotionEvent(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3764 | window2->assertNoEvents(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 3765 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 3766 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3767 | |
| 3768 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3769 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3770 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 3771 | const MotionEvent& motionEvent2 = window2->consumeMotionEvent(); |
| 3772 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3773 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 3774 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3775 | |
| 3776 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3777 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3778 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3779 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3780 | |
| 3781 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3782 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3783 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3784 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3785 | |
| 3786 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3787 | window1->consumeMotionMove(); |
| 3788 | window1->assertNoEvents(); |
| 3789 | |
| 3790 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3791 | mDispatcher->notifyMotion( |
| 3792 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3793 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3794 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3795 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3796 | mDispatcher->notifyMotion( |
| 3797 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3798 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3799 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3802 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3803 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3804 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3805 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3806 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3807 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3808 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3809 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3810 | |
| 3811 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3812 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3813 | mDispatcher->onWindowInfosChanged( |
| 3814 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3815 | |
| 3816 | // 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] | 3817 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3818 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3819 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3820 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3821 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3822 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3823 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3824 | |
| 3825 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3826 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3827 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3828 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3829 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3830 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3831 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3832 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3833 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3834 | |
| 3835 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3837 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3838 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3839 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3840 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3841 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3842 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3843 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3844 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3846 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3847 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3848 | AINPUT_SOURCE_MOUSE) |
| 3849 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3850 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3851 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3852 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3853 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3854 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3855 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3856 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3857 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3858 | AINPUT_SOURCE_MOUSE) |
| 3859 | .buttonState(0) |
| 3860 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3861 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3862 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3863 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3864 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3865 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3866 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3867 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3868 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3869 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3870 | .build())); |
| 3871 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3872 | |
| 3873 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3874 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3875 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3876 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3877 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3878 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3879 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3880 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3881 | |
| 3882 | // No more events |
| 3883 | windowLeft->assertNoEvents(); |
| 3884 | windowRight->assertNoEvents(); |
| 3885 | } |
| 3886 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3887 | /** |
| 3888 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3889 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3890 | * currently active gesture should be canceled, and the new one should proceed. |
| 3891 | */ |
| 3892 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3893 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3894 | sp<FakeWindowHandle> window = |
| 3895 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3896 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3897 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3898 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3899 | |
| 3900 | const int32_t touchDeviceId = 4; |
| 3901 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3902 | |
| 3903 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3904 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3905 | .deviceId(touchDeviceId) |
| 3906 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3907 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3908 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3909 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3910 | .deviceId(touchDeviceId) |
| 3911 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3912 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3913 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3914 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3915 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3916 | |
| 3917 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3918 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3919 | .deviceId(mouseDeviceId) |
| 3920 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3921 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3922 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3923 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3924 | WithPointerCount(2u))); |
| 3925 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3926 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3927 | mDispatcher->notifyMotion( |
| 3928 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3929 | .deviceId(mouseDeviceId) |
| 3930 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3931 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3932 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3933 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3934 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3935 | |
| 3936 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3937 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3938 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3939 | .deviceId(touchDeviceId) |
| 3940 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3941 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3942 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3943 | window->assertNoEvents(); |
| 3944 | } |
| 3945 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3946 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3947 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3948 | |
| 3949 | sp<FakeWindowHandle> spyWindow = |
| 3950 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3951 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3952 | spyWindow->setTrustedOverlay(true); |
| 3953 | spyWindow->setSpy(true); |
| 3954 | sp<FakeWindowHandle> window = |
| 3955 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3956 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3957 | |
| 3958 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3959 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3960 | |
| 3961 | // Send mouse cursor to the window |
| 3962 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3963 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3964 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3965 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3966 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3967 | .build())); |
| 3968 | |
| 3969 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3970 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3971 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3972 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3973 | |
| 3974 | window->assertNoEvents(); |
| 3975 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3976 | } |
| 3977 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3978 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3979 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3980 | |
| 3981 | sp<FakeWindowHandle> spyWindow = |
| 3982 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3983 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3984 | spyWindow->setTrustedOverlay(true); |
| 3985 | spyWindow->setSpy(true); |
| 3986 | sp<FakeWindowHandle> window = |
| 3987 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3988 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3989 | |
| 3990 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3991 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3992 | |
| 3993 | // Send mouse cursor to the window |
| 3994 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3995 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3996 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3997 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3998 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3999 | .build())); |
| 4000 | |
| 4001 | // Move mouse cursor |
| 4002 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4003 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4004 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4005 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4006 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4007 | .build())); |
| 4008 | |
| 4009 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4010 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4011 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4012 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4013 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4014 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4015 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4016 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4017 | // Touch down on the window |
| 4018 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4019 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4020 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4021 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4022 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4023 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4024 | .build())); |
| 4025 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4026 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4027 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4028 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4029 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4030 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4031 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4032 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4033 | |
| 4034 | // pilfer the motion, retaining the gesture on the spy window. |
| 4035 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4036 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4037 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4038 | |
| 4039 | // Touch UP on the window |
| 4040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4041 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4042 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4043 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4044 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4045 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4046 | .build())); |
| 4047 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4048 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4049 | |
| 4050 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4051 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4052 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4053 | |
| 4054 | // One more tap - DOWN |
| 4055 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4056 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4057 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4058 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4059 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4060 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4061 | .build())); |
| 4062 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4063 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4064 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4065 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4066 | |
| 4067 | // Touch UP on the window |
| 4068 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4069 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4070 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4071 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4072 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4073 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4074 | .build())); |
| 4075 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4076 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4077 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4078 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4079 | |
| 4080 | window->assertNoEvents(); |
| 4081 | spyWindow->assertNoEvents(); |
| 4082 | } |
| 4083 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4084 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 4085 | // directly in this test. |
| 4086 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4087 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4088 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4089 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4090 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4091 | |
| 4092 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4093 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4094 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4095 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4096 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4097 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4098 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4099 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4100 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4101 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4102 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4103 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4104 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4105 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4106 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4107 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4108 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4109 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4110 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4111 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4112 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4113 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4114 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4115 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4116 | AINPUT_SOURCE_MOUSE) |
| 4117 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4118 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4119 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4120 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4121 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4122 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4123 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4124 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4125 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4126 | AINPUT_SOURCE_MOUSE) |
| 4127 | .buttonState(0) |
| 4128 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4129 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4130 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4131 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4132 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4133 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4134 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4135 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4136 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4137 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4138 | .build())); |
| 4139 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4140 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 4141 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 4142 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 4143 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4144 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4145 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 4146 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4147 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4148 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4149 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4150 | } |
| 4151 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4152 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4153 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 4154 | * is generated. |
| 4155 | */ |
| 4156 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 4157 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4158 | sp<FakeWindowHandle> window = |
| 4159 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4160 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 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}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4165 | |
| 4166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4167 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4168 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4169 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4170 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4171 | .build())); |
| 4172 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4173 | |
| 4174 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4175 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4176 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4177 | } |
| 4178 | |
| 4179 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4180 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4181 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4182 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4183 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4184 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4185 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4186 | sp<FakeWindowHandle> window = |
| 4187 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4188 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4189 | |
| 4190 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4191 | |
| 4192 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4193 | |
| 4194 | MotionEventBuilder hoverEnterBuilder = |
| 4195 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4196 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4197 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4198 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4199 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4201 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4202 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4203 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4204 | } |
| 4205 | |
| 4206 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4207 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4208 | */ |
| 4209 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4210 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4211 | sp<FakeWindowHandle> window = |
| 4212 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4213 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4214 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4215 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4216 | |
| 4217 | const int32_t mouseDeviceId = 7; |
| 4218 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4219 | |
| 4220 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4221 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4222 | .deviceId(mouseDeviceId) |
| 4223 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4224 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4225 | window->consumeMotionEvent( |
| 4226 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4227 | |
| 4228 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4229 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4230 | .deviceId(touchDeviceId) |
| 4231 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4232 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4233 | |
| 4234 | window->consumeMotionEvent( |
| 4235 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4236 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4237 | } |
| 4238 | |
| 4239 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4240 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4241 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4242 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4243 | */ |
| 4244 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4245 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4246 | sp<FakeWindowHandle> window = |
| 4247 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4248 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4249 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4250 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4251 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4252 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4253 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4254 | ASSERT_NO_FATAL_FAILURE( |
| 4255 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4256 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4257 | |
| 4258 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4259 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4260 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4261 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4262 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4263 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4264 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4265 | |
| 4266 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4267 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4268 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4269 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4270 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4271 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4272 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4273 | ASSERT_NO_FATAL_FAILURE( |
| 4274 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4275 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4276 | } |
| 4277 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4278 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4279 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4280 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4281 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4282 | ADISPLAY_ID_DEFAULT); |
| 4283 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4284 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4285 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4286 | SECOND_DISPLAY_ID); |
| 4287 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4288 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4289 | mDispatcher->onWindowInfosChanged( |
| 4290 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4291 | |
| 4292 | // Set cursor position in window in default display and check that hover enter and move |
| 4293 | // events are generated. |
| 4294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4295 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4296 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4297 | AINPUT_SOURCE_MOUSE) |
| 4298 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4299 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4300 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4301 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4302 | |
| 4303 | // Remove all windows in secondary display and check that no event happens on window in |
| 4304 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4305 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4306 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4307 | windowDefaultDisplay->assertNoEvents(); |
| 4308 | |
| 4309 | // Move cursor position in window in default display and check that only hover move |
| 4310 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4311 | mDispatcher->onWindowInfosChanged( |
| 4312 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4313 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4314 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4315 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4316 | AINPUT_SOURCE_MOUSE) |
| 4317 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4318 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4319 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4320 | windowDefaultDisplay->consumeMotionEvent( |
| 4321 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4322 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4323 | windowDefaultDisplay->assertNoEvents(); |
| 4324 | } |
| 4325 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4326 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4327 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4328 | |
| 4329 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4330 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4331 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4332 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4333 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4334 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4335 | |
| 4336 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4337 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4338 | mDispatcher->onWindowInfosChanged( |
| 4339 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4340 | |
| 4341 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4342 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4343 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4344 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4345 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4346 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4347 | windowRight->assertNoEvents(); |
| 4348 | } |
| 4349 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4350 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4351 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4352 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4353 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4354 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4355 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4356 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4357 | setFocusedWindow(window); |
| 4358 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4359 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4360 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4361 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4362 | |
| 4363 | // Window should receive key down event. |
| 4364 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4365 | |
| 4366 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4367 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4368 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 4369 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4373 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4374 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4375 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4376 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4377 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4378 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4379 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4380 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4381 | |
| 4382 | // Window should receive motion down event. |
| 4383 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4384 | |
| 4385 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4386 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4387 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4388 | window->consumeMotionEvent( |
| 4389 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4390 | } |
| 4391 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4392 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4393 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4394 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4395 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4396 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4397 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4398 | |
| 4399 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4400 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 4401 | .build()); |
| 4402 | |
| 4403 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4404 | |
| 4405 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 4406 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 4407 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4408 | |
| 4409 | // After the device has been reset, a new hovering stream can be sent to the window |
| 4410 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4411 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 4412 | .build()); |
| 4413 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4414 | } |
| 4415 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4416 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 4417 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4418 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4419 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4420 | window->setFocusable(true); |
| 4421 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4422 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4423 | setFocusedWindow(window); |
| 4424 | |
| 4425 | window->consumeFocusEvent(true); |
| 4426 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4427 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4428 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 4429 | const nsecs_t injectTime = keyArgs.eventTime; |
| 4430 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4431 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4432 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 4433 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4434 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 4435 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 4436 | } |
| 4437 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4438 | /** |
| 4439 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 4440 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4441 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 4442 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4443 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4444 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4445 | window->setFocusable(true); |
| 4446 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4447 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4448 | setFocusedWindow(window); |
| 4449 | |
| 4450 | window->consumeFocusEvent(true); |
| 4451 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4452 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4453 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4454 | |
| 4455 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 4456 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 4457 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 4458 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 4459 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4460 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4461 | } |
| 4462 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4463 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4464 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 4465 | * WATCH_OUTSIDE_TOUCH. |
| 4466 | * Both windows are owned by the same UID. |
| 4467 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 4468 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 4469 | */ |
| 4470 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 4471 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4472 | sp<FakeWindowHandle> window = |
| 4473 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4474 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4475 | |
| 4476 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4477 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4478 | ADISPLAY_ID_DEFAULT); |
| 4479 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4480 | outsideWindow->setWatchOutsideTouch(true); |
| 4481 | // 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] | 4482 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4483 | |
| 4484 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4485 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4486 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4487 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4488 | window->consumeMotionDown(); |
| 4489 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 4490 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 4491 | outsideWindow->consumeMotionEvent( |
| 4492 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 4493 | |
| 4494 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 4495 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4496 | ADISPLAY_ID_DEFAULT, {PointF{51, 51}})); |
| 4497 | window->consumeMotionMove(); |
| 4498 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4502 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 4503 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 4504 | * ACTION_OUTSIDE event is sent per gesture. |
| 4505 | */ |
| 4506 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 4507 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 4508 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4509 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4510 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4511 | window->setWatchOutsideTouch(true); |
| 4512 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4513 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4514 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4515 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4516 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4517 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4518 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 4519 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4520 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4521 | mDispatcher->onWindowInfosChanged( |
| 4522 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4523 | |
| 4524 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4525 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4526 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4527 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4528 | window->assertNoEvents(); |
| 4529 | secondWindow->assertNoEvents(); |
| 4530 | |
| 4531 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 4532 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4533 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4534 | ADISPLAY_ID_DEFAULT, |
| 4535 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4536 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 4537 | window->consumeMotionEvent( |
| 4538 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4539 | secondWindow->consumeMotionDown(); |
| 4540 | thirdWindow->assertNoEvents(); |
| 4541 | |
| 4542 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 4543 | // 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] | 4544 | mDispatcher->notifyMotion( |
| 4545 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4546 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4547 | window->assertNoEvents(); |
| 4548 | secondWindow->consumeMotionMove(); |
| 4549 | thirdWindow->consumeMotionDown(); |
| 4550 | } |
| 4551 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4552 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 4553 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4554 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4555 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4556 | window->setFocusable(true); |
| 4557 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4558 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4559 | setFocusedWindow(window); |
| 4560 | |
| 4561 | window->consumeFocusEvent(true); |
| 4562 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4563 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4564 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 4565 | mDispatcher->notifyKey(keyDown); |
| 4566 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4567 | |
| 4568 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4569 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4570 | |
| 4571 | // 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] | 4572 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4573 | |
| 4574 | window->consumeFocusEvent(false); |
| 4575 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4576 | mDispatcher->notifyKey(keyDown); |
| 4577 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4578 | window->assertNoEvents(); |
| 4579 | } |
| 4580 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4581 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 4582 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4583 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4584 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4585 | // Ensure window is non-split and have some transform. |
| 4586 | window->setPreventSplitting(true); |
| 4587 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4588 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4589 | |
| 4590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4591 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4592 | {50, 50})) |
| 4593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4594 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4595 | |
| 4596 | const MotionEvent secondFingerDownEvent = |
| 4597 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4598 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4599 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4600 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4601 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4602 | .build(); |
| 4603 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4604 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4605 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4606 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4607 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 4608 | const MotionEvent& event = window->consumeMotionEvent(); |
| 4609 | EXPECT_EQ(POINTER_1_DOWN, event.getAction()); |
| 4610 | EXPECT_EQ(70, event.getX(0)); // 50 + 20 |
| 4611 | EXPECT_EQ(90, event.getY(0)); // 50 + 40 |
| 4612 | EXPECT_EQ(-10, event.getX(1)); // -30 + 20 |
| 4613 | EXPECT_EQ(-10, event.getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 4616 | /** |
| 4617 | * Two windows: a splittable and a non-splittable. |
| 4618 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 4619 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 4620 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 4621 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 4622 | * "incomplete" gestures. |
| 4623 | */ |
| 4624 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 4625 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4626 | sp<FakeWindowHandle> leftWindow = |
| 4627 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 4628 | ADISPLAY_ID_DEFAULT); |
| 4629 | leftWindow->setPreventSplitting(false); |
| 4630 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4631 | sp<FakeWindowHandle> rightWindow = |
| 4632 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 4633 | ADISPLAY_ID_DEFAULT); |
| 4634 | rightWindow->setPreventSplitting(true); |
| 4635 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4636 | mDispatcher->onWindowInfosChanged( |
| 4637 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 4638 | |
| 4639 | // Touch down on left, splittable window |
| 4640 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4641 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4642 | .build()); |
| 4643 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4644 | |
| 4645 | mDispatcher->notifyMotion( |
| 4646 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4647 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4648 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 4649 | .build()); |
| 4650 | leftWindow->assertNoEvents(); |
| 4651 | rightWindow->assertNoEvents(); |
| 4652 | } |
| 4653 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4654 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4655 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4656 | sp<FakeWindowHandle> window = |
| 4657 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4658 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4659 | sp<FakeWindowHandle> trustedOverlay = |
| 4660 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4661 | ADISPLAY_ID_DEFAULT); |
| 4662 | trustedOverlay->setSpy(true); |
| 4663 | trustedOverlay->setTrustedOverlay(true); |
| 4664 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4665 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4666 | |
| 4667 | // Start a three-finger touchpad swipe |
| 4668 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4669 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4670 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4671 | .build()); |
| 4672 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4673 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4674 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4675 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4676 | .build()); |
| 4677 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4678 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4679 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4680 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4681 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4682 | .build()); |
| 4683 | |
| 4684 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4685 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4686 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4687 | |
| 4688 | // Move the swipe a bit |
| 4689 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4690 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4691 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4692 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4693 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4694 | .build()); |
| 4695 | |
| 4696 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4697 | |
| 4698 | // End the swipe |
| 4699 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4700 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4701 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4702 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4703 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4704 | .build()); |
| 4705 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4706 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4707 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4708 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4709 | .build()); |
| 4710 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4711 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4712 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4713 | .build()); |
| 4714 | |
| 4715 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4716 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4717 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4718 | |
| 4719 | window->assertNoEvents(); |
| 4720 | } |
| 4721 | |
| 4722 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4723 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4724 | sp<FakeWindowHandle> window = |
| 4725 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4726 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4727 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4728 | |
| 4729 | // Start a three-finger touchpad swipe |
| 4730 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4731 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4732 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4733 | .build()); |
| 4734 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4735 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4736 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4737 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4738 | .build()); |
| 4739 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4740 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4741 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4742 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4743 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4744 | .build()); |
| 4745 | |
| 4746 | // Move the swipe a bit |
| 4747 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4748 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4749 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4750 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4751 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4752 | .build()); |
| 4753 | |
| 4754 | // End the swipe |
| 4755 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4756 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4757 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4758 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4759 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4760 | .build()); |
| 4761 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4762 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4763 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4764 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4765 | .build()); |
| 4766 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4767 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4768 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4769 | .build()); |
| 4770 | |
| 4771 | window->assertNoEvents(); |
| 4772 | } |
| 4773 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4774 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4775 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 4776 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4777 | * 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] | 4778 | */ |
| 4779 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 4780 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4781 | sp<FakeWindowHandle> window = |
| 4782 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4783 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4784 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4785 | |
| 4786 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4787 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4788 | .downTime(baseTime + 10) |
| 4789 | .eventTime(baseTime + 10) |
| 4790 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4791 | .build()); |
| 4792 | |
| 4793 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4794 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4795 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4796 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4797 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4798 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4799 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4800 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4801 | .downTime(baseTime + 10) |
| 4802 | .eventTime(baseTime + 30) |
| 4803 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4804 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4805 | .build()); |
| 4806 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4807 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4808 | |
| 4809 | // 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] | 4810 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4811 | .downTime(baseTime + 10) |
| 4812 | .eventTime(baseTime + 40) |
| 4813 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4814 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4815 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4816 | |
| 4817 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4818 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4819 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4820 | .downTime(baseTime + 10) |
| 4821 | .eventTime(baseTime + 50) |
| 4822 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4823 | .build()); |
| 4824 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4825 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4826 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4827 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4828 | .downTime(baseTime + 60) |
| 4829 | .eventTime(baseTime + 60) |
| 4830 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4831 | .build()); |
| 4832 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4833 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4834 | } |
| 4835 | |
| 4836 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4837 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 4838 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 4839 | * its coordinates should be converted by the transform of the windows of target screen. |
| 4840 | */ |
| 4841 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 4842 | // This case will create a window and a spy window on the default display and mirror |
| 4843 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 4844 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4845 | |
| 4846 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
| 4847 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4848 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 4849 | spyWindowDefaultDisplay->setSpy(true); |
| 4850 | |
| 4851 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4852 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4853 | ADISPLAY_ID_DEFAULT); |
| 4854 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 4855 | |
| 4856 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 4857 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 4858 | |
| 4859 | // Add the windows to the dispatcher |
| 4860 | mDispatcher->onWindowInfosChanged( |
| 4861 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 4862 | *windowSecondDisplay->getInfo()}, |
| 4863 | {}, |
| 4864 | 0, |
| 4865 | 0}); |
| 4866 | |
| 4867 | // Send down to ADISPLAY_ID_DEFAULT |
| 4868 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4869 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4870 | {100, 100})) |
| 4871 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4872 | |
| 4873 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 4874 | windowDefaultDisplay->consumeMotionDown(); |
| 4875 | |
| 4876 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 4877 | |
| 4878 | // windowDefaultDisplay gets cancel |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 4879 | const MotionEvent& event = windowDefaultDisplay->consumeMotionEvent(); |
| 4880 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event.getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4881 | |
| 4882 | // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the |
| 4883 | // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y |
| 4884 | // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of |
| 4885 | // SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 4886 | EXPECT_EQ(100, event.getX(0)); |
| 4887 | EXPECT_EQ(100, event.getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4891 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4892 | * |
| 4893 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4894 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4895 | * space. |
| 4896 | */ |
| 4897 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4898 | public: |
| 4899 | void SetUp() override { |
| 4900 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4901 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4905 | gui::DisplayInfo info; |
| 4906 | info.displayId = displayId; |
| 4907 | info.transform = transform; |
| 4908 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4909 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4913 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4914 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4917 | void removeAllWindowsAndDisplays() { |
| 4918 | mDisplayInfos.clear(); |
| 4919 | mWindowInfos.clear(); |
| 4920 | } |
| 4921 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4922 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4923 | // on the display. |
| 4924 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4925 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4926 | // respectively. |
| 4927 | ui::Transform displayTransform; |
| 4928 | displayTransform.set(2, 0, 0, 4); |
| 4929 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4930 | |
| 4931 | std::shared_ptr<FakeApplicationHandle> application = |
| 4932 | std::make_shared<FakeApplicationHandle>(); |
| 4933 | |
| 4934 | // Add two windows to the display. Their frames are represented in the display space. |
| 4935 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4936 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4937 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4938 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 4939 | addWindow(firstWindow); |
| 4940 | |
| 4941 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4942 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4943 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4944 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 4945 | addWindow(secondWindow); |
| 4946 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 4947 | } |
| 4948 | |
| 4949 | private: |
| 4950 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 4951 | std::vector<gui::WindowInfo> mWindowInfos; |
| 4952 | }; |
| 4953 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4954 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4955 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4956 | // 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] | 4957 | // selected so that if the hit test was performed with the point and the bounds being in |
| 4958 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4959 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4960 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4961 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4962 | |
| 4963 | firstWindow->consumeMotionDown(); |
| 4964 | secondWindow->assertNoEvents(); |
| 4965 | } |
| 4966 | |
| 4967 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 4968 | // the event should be treated as being in the logical display space. |
| 4969 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 4970 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4971 | // Send down to the first window. The point is represented in the logical display space. The |
| 4972 | // point is selected so that if the hit test was done in logical display space, then it would |
| 4973 | // end up in the incorrect window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4974 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4975 | PointF{75 * 2, 55 * 4}); |
| 4976 | |
| 4977 | firstWindow->consumeMotionDown(); |
| 4978 | secondWindow->assertNoEvents(); |
| 4979 | } |
| 4980 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4981 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 4982 | // event should be treated as being in the logical display space. |
| 4983 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 4984 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4985 | |
| 4986 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4987 | ui::Transform injectedEventTransform; |
| 4988 | injectedEventTransform.set(matrix); |
| 4989 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 4990 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 4991 | |
| 4992 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4993 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4994 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4995 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4996 | .x(untransformedPoint.x) |
| 4997 | .y(untransformedPoint.y)) |
| 4998 | .build(); |
| 4999 | event.transform(matrix); |
| 5000 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5001 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5002 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 5003 | |
| 5004 | firstWindow->consumeMotionDown(); |
| 5005 | secondWindow->assertNoEvents(); |
| 5006 | } |
| 5007 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5008 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 5009 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5010 | |
| 5011 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5012 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5013 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5014 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5015 | |
| 5016 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 5017 | const MotionEvent& event = secondWindow->consumeMotionEvent(); |
| 5018 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event.getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5019 | |
| 5020 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 5021 | EXPECT_EQ(300, event.getRawX(0)); |
| 5022 | EXPECT_EQ(880, event.getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5023 | |
| 5024 | // Ensure that the x and y values are in the window's coordinate space. |
| 5025 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 5026 | // the logical display space. This will be the origin of the window space. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 5027 | EXPECT_EQ(100, event.getX(0)); |
| 5028 | EXPECT_EQ(80, event.getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5029 | } |
| 5030 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5031 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 5032 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5033 | // The monitor will always receive events in the logical display's coordinate space, because |
| 5034 | // it does not have a window. |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5035 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5036 | |
| 5037 | // Send down to the first window. |
| 5038 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5039 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5040 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5041 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5042 | |
| 5043 | // Second pointer goes down on second window. |
| 5044 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5045 | ADISPLAY_ID_DEFAULT, |
| 5046 | {PointF{50, 100}, PointF{150, 220}})); |
| 5047 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 5048 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 5049 | {1, PointF{300, 880}}}; |
| 5050 | monitor.consumeMotionEvent( |
| 5051 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 5052 | |
| 5053 | mDispatcher->cancelCurrentTouch(); |
| 5054 | |
| 5055 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5056 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 5057 | monitor.consumeMotionEvent( |
| 5058 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 5059 | } |
| 5060 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5061 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 5062 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5063 | |
| 5064 | // Send down to the first window. |
| 5065 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5066 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5067 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5068 | |
| 5069 | // The pointer is transferred to the second window, and the second window receives it in the |
| 5070 | // correct coordinate space. |
| 5071 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5072 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5073 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 5074 | } |
| 5075 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5076 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 5077 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5078 | |
| 5079 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5080 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5081 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5082 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5083 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5084 | |
| 5085 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 5086 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5087 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5088 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5089 | WithRawCoords(300, 880))); |
| 5090 | secondWindow->consumeMotionEvent( |
| 5091 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5092 | secondWindow->assertNoEvents(); |
| 5093 | firstWindow->assertNoEvents(); |
| 5094 | } |
| 5095 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5096 | // Same as above, but while the window is being mirrored. |
| 5097 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5098 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 5099 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5100 | |
| 5101 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5102 | ui::Transform secondDisplayTransform; |
| 5103 | secondDisplayTransform.set(matrix); |
| 5104 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5105 | |
| 5106 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5107 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5108 | addWindow(secondWindowClone); |
| 5109 | |
| 5110 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5111 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5112 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5113 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5114 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5115 | |
| 5116 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 5117 | // display. |
| 5118 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5119 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5120 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5121 | WithRawCoords(300, 880))); |
| 5122 | secondWindow->consumeMotionEvent( |
| 5123 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5124 | secondWindow->assertNoEvents(); |
| 5125 | firstWindow->assertNoEvents(); |
| 5126 | } |
| 5127 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5128 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 5129 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5130 | |
| 5131 | // Send hover enter to second window |
| 5132 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5133 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5134 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5135 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5136 | |
| 5137 | mDispatcher->cancelCurrentTouch(); |
| 5138 | |
| 5139 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5140 | WithRawCoords(300, 880))); |
| 5141 | secondWindow->assertNoEvents(); |
| 5142 | firstWindow->assertNoEvents(); |
| 5143 | } |
| 5144 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5145 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5146 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5147 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 5148 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5149 | |
| 5150 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5151 | ui::Transform secondDisplayTransform; |
| 5152 | secondDisplayTransform.set(matrix); |
| 5153 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5154 | |
| 5155 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5156 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5157 | addWindow(secondWindowClone); |
| 5158 | |
| 5159 | // Send hover enter to second window |
| 5160 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5161 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5162 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5163 | WithCoords(100, 80), WithRawCoords(300, 880), |
| 5164 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5165 | |
| 5166 | mDispatcher->cancelCurrentTouch(); |
| 5167 | |
| 5168 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 5169 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5170 | WithRawCoords(300, 880), |
| 5171 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5172 | secondWindow->assertNoEvents(); |
| 5173 | firstWindow->assertNoEvents(); |
| 5174 | } |
| 5175 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5176 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 5177 | class InputDispatcherDisplayOrientationFixture |
| 5178 | : public InputDispatcherDisplayProjectionTest, |
| 5179 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 5180 | |
| 5181 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 5182 | // in different locations on the display, specifically points close to the four corners of a |
| 5183 | // window. |
| 5184 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 5185 | constexpr static int32_t displayWidth = 400; |
| 5186 | constexpr static int32_t displayHeight = 800; |
| 5187 | |
| 5188 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5189 | |
| 5190 | const auto rotation = GetParam(); |
| 5191 | |
| 5192 | // Set up the display with the specified rotation. |
| 5193 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5194 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5195 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5196 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5197 | logicalDisplayWidth, logicalDisplayHeight); |
| 5198 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5199 | |
| 5200 | // Create a window with its bounds determined in the logical display. |
| 5201 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 5202 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 5203 | sp<FakeWindowHandle> window = |
| 5204 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5205 | window->setFrame(frameInDisplay, displayTransform); |
| 5206 | addWindow(window); |
| 5207 | |
| 5208 | // The following points in logical display space should be inside the window. |
| 5209 | static const std::array<vec2, 4> insidePoints{ |
| 5210 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5211 | for (const auto pointInsideWindow : insidePoints) { |
| 5212 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 5213 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5214 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5215 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5216 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5217 | window->consumeMotionDown(); |
| 5218 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5219 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5220 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5221 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5222 | window->consumeMotionUp(); |
| 5223 | } |
| 5224 | |
| 5225 | // The following points in logical display space should be outside the window. |
| 5226 | static const std::array<vec2, 5> outsidePoints{ |
| 5227 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5228 | for (const auto pointOutsideWindow : outsidePoints) { |
| 5229 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 5230 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5231 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5232 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5233 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5234 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5235 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5236 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5237 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5238 | } |
| 5239 | window->assertNoEvents(); |
| 5240 | } |
| 5241 | |
| 5242 | // Run the precision tests for all rotations. |
| 5243 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 5244 | InputDispatcherDisplayOrientationFixture, |
| 5245 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 5246 | ui::ROTATION_270), |
| 5247 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 5248 | return ftl::enum_string(testParamInfo.param); |
| 5249 | }); |
| 5250 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5251 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5252 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5253 | |
| 5254 | class TransferTouchFixture : public InputDispatcherTest, |
| 5255 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 5256 | |
| 5257 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5258 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5259 | |
| 5260 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5261 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5262 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5263 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5264 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5265 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5266 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5267 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5268 | sp<FakeWindowHandle> wallpaper = |
| 5269 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 5270 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5271 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5272 | mDispatcher->onWindowInfosChanged( |
| 5273 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5274 | |
| 5275 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5276 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5277 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5278 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5279 | // Only the first window should get the down event |
| 5280 | firstWindow->consumeMotionDown(); |
| 5281 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5282 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5283 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5284 | // Transfer touch to the second window |
| 5285 | TransferFunction f = GetParam(); |
| 5286 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5287 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5288 | // The first window gets cancel and the second gets down |
| 5289 | firstWindow->consumeMotionCancel(); |
| 5290 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5291 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5292 | |
| 5293 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5294 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5295 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5296 | // The first window gets no events and the second gets up |
| 5297 | firstWindow->assertNoEvents(); |
| 5298 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5299 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5300 | } |
| 5301 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5302 | /** |
| 5303 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 5304 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 5305 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 5306 | * natural to the user. |
| 5307 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 5308 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 5309 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 5310 | * the first window to the second. |
| 5311 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 5312 | * the other API, as well. |
| 5313 | */ |
| 5314 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 5315 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5316 | |
| 5317 | // Create a couple of windows + a spy window |
| 5318 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5319 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5320 | spyWindow->setTrustedOverlay(true); |
| 5321 | spyWindow->setSpy(true); |
| 5322 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5323 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5324 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5325 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5326 | |
| 5327 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5328 | mDispatcher->onWindowInfosChanged( |
| 5329 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5330 | |
| 5331 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5332 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5333 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5334 | // Only the first window and spy should get the down event |
| 5335 | spyWindow->consumeMotionDown(); |
| 5336 | firstWindow->consumeMotionDown(); |
| 5337 | |
| 5338 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 5339 | // if f === 'transferTouch'. |
| 5340 | TransferFunction f = GetParam(); |
| 5341 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5342 | ASSERT_TRUE(success); |
| 5343 | // The first window gets cancel and the second gets down |
| 5344 | firstWindow->consumeMotionCancel(); |
| 5345 | secondWindow->consumeMotionDown(); |
| 5346 | |
| 5347 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5348 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5349 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5350 | // The first window gets no events and the second+spy get up |
| 5351 | firstWindow->assertNoEvents(); |
| 5352 | spyWindow->consumeMotionUp(); |
| 5353 | secondWindow->consumeMotionUp(); |
| 5354 | } |
| 5355 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5356 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5357 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5358 | |
| 5359 | PointF touchPoint = {10, 10}; |
| 5360 | |
| 5361 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5362 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5363 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5364 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5365 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5366 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5367 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5368 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5369 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5370 | |
| 5371 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5372 | mDispatcher->onWindowInfosChanged( |
| 5373 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5374 | |
| 5375 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5376 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5377 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5378 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5379 | // Only the first window should get the down event |
| 5380 | firstWindow->consumeMotionDown(); |
| 5381 | secondWindow->assertNoEvents(); |
| 5382 | |
| 5383 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5384 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5385 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5386 | // Only the first window should get the pointer down event |
| 5387 | firstWindow->consumeMotionPointerDown(1); |
| 5388 | secondWindow->assertNoEvents(); |
| 5389 | |
| 5390 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5391 | TransferFunction f = GetParam(); |
| 5392 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5393 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5394 | // The first window gets cancel and the second gets down and pointer down |
| 5395 | firstWindow->consumeMotionCancel(); |
| 5396 | secondWindow->consumeMotionDown(); |
| 5397 | secondWindow->consumeMotionPointerDown(1); |
| 5398 | |
| 5399 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5400 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5401 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5402 | // The first window gets nothing and the second gets pointer up |
| 5403 | firstWindow->assertNoEvents(); |
| 5404 | secondWindow->consumeMotionPointerUp(1); |
| 5405 | |
| 5406 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5407 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5408 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5409 | // The first window gets nothing and the second gets up |
| 5410 | firstWindow->assertNoEvents(); |
| 5411 | secondWindow->consumeMotionUp(); |
| 5412 | } |
| 5413 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5414 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 5415 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5416 | |
| 5417 | // Create a couple of windows |
| 5418 | sp<FakeWindowHandle> firstWindow = |
| 5419 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5420 | ADISPLAY_ID_DEFAULT); |
| 5421 | firstWindow->setDupTouchToWallpaper(true); |
| 5422 | sp<FakeWindowHandle> secondWindow = |
| 5423 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5424 | ADISPLAY_ID_DEFAULT); |
| 5425 | secondWindow->setDupTouchToWallpaper(true); |
| 5426 | |
| 5427 | sp<FakeWindowHandle> wallpaper1 = |
| 5428 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 5429 | wallpaper1->setIsWallpaper(true); |
| 5430 | |
| 5431 | sp<FakeWindowHandle> wallpaper2 = |
| 5432 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 5433 | wallpaper2->setIsWallpaper(true); |
| 5434 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5435 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 5436 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 5437 | {}, |
| 5438 | 0, |
| 5439 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5440 | |
| 5441 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5442 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5443 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5444 | |
| 5445 | // Only the first window should get the down event |
| 5446 | firstWindow->consumeMotionDown(); |
| 5447 | secondWindow->assertNoEvents(); |
| 5448 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5449 | wallpaper2->assertNoEvents(); |
| 5450 | |
| 5451 | // Transfer touch focus to the second window |
| 5452 | TransferFunction f = GetParam(); |
| 5453 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5454 | ASSERT_TRUE(success); |
| 5455 | |
| 5456 | // The first window gets cancel and the second gets down |
| 5457 | firstWindow->consumeMotionCancel(); |
| 5458 | secondWindow->consumeMotionDown(); |
| 5459 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5460 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5461 | |
| 5462 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5463 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5464 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5465 | // The first window gets no events and the second gets up |
| 5466 | firstWindow->assertNoEvents(); |
| 5467 | secondWindow->consumeMotionUp(); |
| 5468 | wallpaper1->assertNoEvents(); |
| 5469 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5470 | } |
| 5471 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5472 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 5473 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 5474 | // for the case where there are multiple pointers split across several windows. |
| 5475 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 5476 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5477 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5478 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5479 | return dispatcher->transferTouch(destChannelToken, |
| 5480 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5481 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5482 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5483 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5484 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5485 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5486 | })); |
| 5487 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5488 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5489 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5490 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5491 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5492 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5493 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5494 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5495 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5496 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5497 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5498 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5499 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5500 | |
| 5501 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5502 | mDispatcher->onWindowInfosChanged( |
| 5503 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5504 | |
| 5505 | PointF pointInFirst = {300, 200}; |
| 5506 | PointF pointInSecond = {300, 600}; |
| 5507 | |
| 5508 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5509 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5510 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5511 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5512 | // Only the first window should get the down event |
| 5513 | firstWindow->consumeMotionDown(); |
| 5514 | secondWindow->assertNoEvents(); |
| 5515 | |
| 5516 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5517 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5518 | ADISPLAY_ID_DEFAULT, |
| 5519 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5520 | // The first window gets a move and the second a down |
| 5521 | firstWindow->consumeMotionMove(); |
| 5522 | secondWindow->consumeMotionDown(); |
| 5523 | |
| 5524 | // Transfer touch focus to the second window |
| 5525 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5526 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 5527 | firstWindow->consumeMotionCancel(); |
| 5528 | secondWindow->consumeMotionPointerDown(1); |
| 5529 | |
| 5530 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5531 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5532 | ADISPLAY_ID_DEFAULT, |
| 5533 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5534 | // The first window gets nothing and the second gets pointer up |
| 5535 | firstWindow->assertNoEvents(); |
| 5536 | secondWindow->consumeMotionPointerUp(1); |
| 5537 | |
| 5538 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5539 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5540 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5541 | // The first window gets nothing and the second gets up |
| 5542 | firstWindow->assertNoEvents(); |
| 5543 | secondWindow->consumeMotionUp(); |
| 5544 | } |
| 5545 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5546 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 5547 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 5548 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 5549 | // window should get nothing. |
| 5550 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 5551 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5552 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5553 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5554 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5555 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5556 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5557 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5558 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5559 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5560 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5561 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5562 | |
| 5563 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5564 | mDispatcher->onWindowInfosChanged( |
| 5565 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5566 | |
| 5567 | PointF pointInFirst = {300, 200}; |
| 5568 | PointF pointInSecond = {300, 600}; |
| 5569 | |
| 5570 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5571 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5572 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5573 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5574 | // Only the first window should get the down event |
| 5575 | firstWindow->consumeMotionDown(); |
| 5576 | secondWindow->assertNoEvents(); |
| 5577 | |
| 5578 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5579 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5580 | ADISPLAY_ID_DEFAULT, |
| 5581 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5582 | // The first window gets a move and the second a down |
| 5583 | firstWindow->consumeMotionMove(); |
| 5584 | secondWindow->consumeMotionDown(); |
| 5585 | |
| 5586 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5587 | const bool transferred = |
| 5588 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5589 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 5590 | ASSERT_FALSE(transferred); |
| 5591 | firstWindow->assertNoEvents(); |
| 5592 | secondWindow->assertNoEvents(); |
| 5593 | |
| 5594 | // The rest of the dispatch should proceed as normal |
| 5595 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5596 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5597 | ADISPLAY_ID_DEFAULT, |
| 5598 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5599 | // The first window gets MOVE and the second gets pointer up |
| 5600 | firstWindow->consumeMotionMove(); |
| 5601 | secondWindow->consumeMotionUp(); |
| 5602 | |
| 5603 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5604 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5605 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5606 | // The first window gets nothing and the second gets up |
| 5607 | firstWindow->consumeMotionUp(); |
| 5608 | secondWindow->assertNoEvents(); |
| 5609 | } |
| 5610 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5611 | // This case will create two windows and one mirrored window on the default display and mirror |
| 5612 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 5613 | // the windows info of second display before default display. |
| 5614 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 5615 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5616 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5617 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5618 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5619 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5620 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5621 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5622 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5623 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5624 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5625 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5626 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5627 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5628 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5629 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5630 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5631 | |
| 5632 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5633 | mDispatcher->onWindowInfosChanged( |
| 5634 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5635 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5636 | *secondWindowInPrimary->getInfo()}, |
| 5637 | {}, |
| 5638 | 0, |
| 5639 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5640 | |
| 5641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5642 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5643 | {50, 50})) |
| 5644 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5645 | |
| 5646 | // Window should receive motion event. |
| 5647 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5648 | |
| 5649 | // Transfer touch focus |
| 5650 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 5651 | secondWindowInPrimary->getToken())); |
| 5652 | // The first window gets cancel. |
| 5653 | firstWindowInPrimary->consumeMotionCancel(); |
| 5654 | secondWindowInPrimary->consumeMotionDown(); |
| 5655 | |
| 5656 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5657 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5658 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5659 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5660 | firstWindowInPrimary->assertNoEvents(); |
| 5661 | secondWindowInPrimary->consumeMotionMove(); |
| 5662 | |
| 5663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5664 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5665 | {150, 50})) |
| 5666 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5667 | firstWindowInPrimary->assertNoEvents(); |
| 5668 | secondWindowInPrimary->consumeMotionUp(); |
| 5669 | } |
| 5670 | |
| 5671 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 5672 | // 'transferTouch' api. |
| 5673 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 5674 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5675 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5676 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5677 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5678 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5679 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5680 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5681 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5682 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5683 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5684 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5685 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5686 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5687 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5688 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5689 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5690 | |
| 5691 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5692 | mDispatcher->onWindowInfosChanged( |
| 5693 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5694 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5695 | *secondWindowInPrimary->getInfo()}, |
| 5696 | {}, |
| 5697 | 0, |
| 5698 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5699 | |
| 5700 | // Touch on second display. |
| 5701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5702 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5703 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5704 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5705 | |
| 5706 | // Window should receive motion event. |
| 5707 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5708 | |
| 5709 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5710 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5711 | |
| 5712 | // The first window gets cancel. |
| 5713 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5714 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5715 | |
| 5716 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5717 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5718 | SECOND_DISPLAY_ID, {150, 50})) |
| 5719 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5720 | firstWindowInPrimary->assertNoEvents(); |
| 5721 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 5722 | |
| 5723 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5724 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5725 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5726 | firstWindowInPrimary->assertNoEvents(); |
| 5727 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5728 | } |
| 5729 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5730 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5731 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5732 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5733 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5734 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5735 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5736 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5737 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5738 | |
| 5739 | window->consumeFocusEvent(true); |
| 5740 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5741 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5742 | |
| 5743 | // Window should receive key down event. |
| 5744 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5745 | |
| 5746 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5747 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5748 | mFakePolicy->assertUserActivityPoked(); |
| 5749 | } |
| 5750 | |
| 5751 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 5752 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5753 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5754 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5755 | |
| 5756 | window->setDisableUserActivity(true); |
| 5757 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5758 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5759 | setFocusedWindow(window); |
| 5760 | |
| 5761 | window->consumeFocusEvent(true); |
| 5762 | |
| 5763 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5764 | |
| 5765 | // Window should receive key down event. |
| 5766 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5767 | |
| 5768 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5769 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5770 | mFakePolicy->assertUserActivityNotPoked(); |
| 5771 | } |
| 5772 | |
| 5773 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 5774 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5775 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5776 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5777 | |
| 5778 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5779 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5780 | setFocusedWindow(window); |
| 5781 | |
| 5782 | window->consumeFocusEvent(true); |
| 5783 | |
| 5784 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5785 | mDispatcher->waitForIdle(); |
| 5786 | |
| 5787 | // System key is not passed down |
| 5788 | window->assertNoEvents(); |
| 5789 | |
| 5790 | // Should have poked user activity |
| 5791 | mFakePolicy->assertUserActivityPoked(); |
| 5792 | } |
| 5793 | |
| 5794 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 5795 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5796 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5797 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5798 | |
| 5799 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5800 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5801 | setFocusedWindow(window); |
| 5802 | |
| 5803 | window->consumeFocusEvent(true); |
| 5804 | |
| 5805 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5806 | mDispatcher->waitForIdle(); |
| 5807 | |
| 5808 | // System key is not passed down |
| 5809 | window->assertNoEvents(); |
| 5810 | |
| 5811 | // Should have poked user activity |
| 5812 | mFakePolicy->assertUserActivityPoked(); |
| 5813 | } |
| 5814 | |
| 5815 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 5816 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5817 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5818 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5819 | |
| 5820 | window->setDisableUserActivity(true); |
| 5821 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5822 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5823 | setFocusedWindow(window); |
| 5824 | |
| 5825 | window->consumeFocusEvent(true); |
| 5826 | |
| 5827 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5828 | mDispatcher->waitForIdle(); |
| 5829 | |
| 5830 | // System key is not passed down |
| 5831 | window->assertNoEvents(); |
| 5832 | |
| 5833 | // Should have poked user activity |
| 5834 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5835 | } |
| 5836 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5837 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 5838 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5839 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5840 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5841 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5842 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5843 | |
| 5844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5845 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5846 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 5847 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5848 | |
| 5849 | window->consumeMotionEvent( |
| 5850 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5851 | |
| 5852 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5853 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5854 | mFakePolicy->assertUserActivityPoked(); |
| 5855 | } |
| 5856 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5857 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5858 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5859 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5860 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5861 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5862 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5863 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5864 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5865 | mDispatcher->waitForIdle(); |
| 5866 | |
| 5867 | window->assertNoEvents(); |
| 5868 | } |
| 5869 | |
| 5870 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 5871 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5872 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5873 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5874 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5875 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5876 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5877 | |
| 5878 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5879 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5880 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5881 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5882 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5883 | |
| 5884 | // Window should receive only the motion event |
| 5885 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5886 | window->assertNoEvents(); // Key event or focus event will not be received |
| 5887 | } |
| 5888 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5889 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 5890 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5891 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5892 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5893 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5894 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5895 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5896 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5897 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5898 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5899 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5900 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5901 | |
| 5902 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5903 | mDispatcher->onWindowInfosChanged( |
| 5904 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5905 | |
| 5906 | PointF pointInFirst = {300, 200}; |
| 5907 | PointF pointInSecond = {300, 600}; |
| 5908 | |
| 5909 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5910 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5911 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5912 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5913 | // Only the first window should get the down event |
| 5914 | firstWindow->consumeMotionDown(); |
| 5915 | secondWindow->assertNoEvents(); |
| 5916 | |
| 5917 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5918 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5919 | ADISPLAY_ID_DEFAULT, |
| 5920 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5921 | // The first window gets a move and the second a down |
| 5922 | firstWindow->consumeMotionMove(); |
| 5923 | secondWindow->consumeMotionDown(); |
| 5924 | |
| 5925 | // Send pointer cancel to the second window |
| 5926 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5927 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5928 | {pointInFirst, pointInSecond}); |
| 5929 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5930 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5931 | // The first window gets move and the second gets cancel. |
| 5932 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5933 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5934 | |
| 5935 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5936 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5937 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5938 | // The first window gets up and the second gets nothing. |
| 5939 | firstWindow->consumeMotionUp(); |
| 5940 | secondWindow->assertNoEvents(); |
| 5941 | } |
| 5942 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5943 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 5944 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5945 | |
| 5946 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5947 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5948 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5949 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 5950 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 5951 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 5952 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5953 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5954 | window->assertNoEvents(); |
| 5955 | mDispatcher->waitForIdle(); |
| 5956 | } |
| 5957 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5958 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 5959 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5960 | /** |
| 5961 | * Two entities that receive touch: A window, and a global monitor. |
| 5962 | * The touch goes to the window, and then the window disappears. |
| 5963 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 5964 | * for the monitor, as well. |
| 5965 | * 1. foregroundWindow |
| 5966 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 5967 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5968 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5969 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5970 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5971 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5972 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5973 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5974 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5975 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5976 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5977 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5978 | {100, 200})) |
| 5979 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5980 | |
| 5981 | // Both the foreground window and the global monitor should receive the touch down |
| 5982 | window->consumeMotionDown(); |
| 5983 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5984 | |
| 5985 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5986 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5987 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5988 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5989 | |
| 5990 | window->consumeMotionMove(); |
| 5991 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5992 | |
| 5993 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5994 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5995 | window->consumeMotionCancel(); |
| 5996 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 5997 | |
| 5998 | // If more events come in, there will be no more foreground window to send them to. This will |
| 5999 | // cause a cancel for the monitor, as well. |
| 6000 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6001 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6002 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 6003 | << "Injection should fail because the window was removed"; |
| 6004 | window->assertNoEvents(); |
| 6005 | // Global monitor now gets the cancel |
| 6006 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6007 | } |
| 6008 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6009 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6010 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6011 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6012 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6013 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6014 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6015 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6016 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6017 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6018 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6019 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6020 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6021 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6022 | } |
| 6023 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6024 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6025 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6026 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6027 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6028 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6029 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6030 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6031 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6033 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6034 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6035 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6036 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6037 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6038 | // Pilfer pointers from the monitor. |
| 6039 | // This should not do anything and the window should continue to receive events. |
| 6040 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6041 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6042 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6043 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6044 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6045 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6046 | |
| 6047 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6048 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6049 | } |
| 6050 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6051 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6052 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6053 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6054 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6055 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6056 | window->setWindowOffset(20, 40); |
| 6057 | window->setWindowTransform(0, 1, -1, 0); |
| 6058 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6059 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6060 | |
| 6061 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6062 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6063 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6064 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6065 | MotionEvent* event = monitor.consumeMotion(); |
| 6066 | // Even though window has transform, gesture monitor must not. |
| 6067 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 6068 | } |
| 6069 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6070 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6071 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6072 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6073 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6074 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6075 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6076 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 6077 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6080 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6081 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6082 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6083 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6084 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6085 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6086 | |
| 6087 | NotifyMotionArgs motionArgs = |
| 6088 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6089 | ADISPLAY_ID_DEFAULT); |
| 6090 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6091 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6092 | // Window should receive motion down event. |
| 6093 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6094 | |
| 6095 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6096 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6097 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6098 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 6099 | motionArgs.pointerCoords[0].getX() - 10); |
| 6100 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6101 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6102 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6103 | } |
| 6104 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6105 | /** |
| 6106 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 6107 | * the device default right away. In the test scenario, we check both the default value, |
| 6108 | * and the action of enabling / disabling. |
| 6109 | */ |
| 6110 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6111 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6112 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6113 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6114 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6115 | |
| 6116 | // Set focused application. |
| 6117 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6118 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6119 | |
| 6120 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6121 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6122 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6123 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6124 | |
| 6125 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6126 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6127 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6128 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6129 | |
| 6130 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6131 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6132 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6133 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6134 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6135 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6136 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6137 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6138 | |
| 6139 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6140 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6141 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6142 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6143 | |
| 6144 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6145 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6146 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6147 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6148 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6149 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6150 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6151 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6152 | |
| 6153 | window->assertNoEvents(); |
| 6154 | } |
| 6155 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6156 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6157 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6158 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6159 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6160 | |
| 6161 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6162 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6163 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6164 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6165 | setFocusedWindow(window); |
| 6166 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6167 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6168 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6169 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 6170 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6171 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6172 | const KeyEvent& event = window->consumeKey(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6173 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6174 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6175 | ASSERT_NE(verified, nullptr); |
| 6176 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 6177 | |
| 6178 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 6179 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 6180 | ASSERT_EQ(keyArgs.source, verified->source); |
| 6181 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 6182 | |
| 6183 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 6184 | |
| 6185 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6186 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6187 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6188 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 6189 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 6190 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 6191 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 6192 | } |
| 6193 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6194 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6195 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6196 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6197 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6198 | |
| 6199 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6200 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6201 | ui::Transform transform; |
| 6202 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6203 | |
| 6204 | gui::DisplayInfo displayInfo; |
| 6205 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6206 | displayInfo.transform = transform; |
| 6207 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6208 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6209 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6210 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6211 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6212 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6213 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6214 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6215 | const MotionEvent& event = window->consumeMotionEvent(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6216 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6217 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6218 | ASSERT_NE(verified, nullptr); |
| 6219 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 6220 | |
| 6221 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 6222 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 6223 | EXPECT_EQ(motionArgs.source, verified->source); |
| 6224 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 6225 | |
| 6226 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 6227 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6228 | const vec2 rawXY = |
| 6229 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 6230 | motionArgs.pointerCoords[0].getXYValue()); |
| 6231 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 6232 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6233 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6234 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6235 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6236 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 6237 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 6238 | } |
| 6239 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 6240 | /** |
| 6241 | * Ensure that separate calls to sign the same data are generating the same key. |
| 6242 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 6243 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 6244 | * tests. |
| 6245 | */ |
| 6246 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 6247 | KeyEvent event = getTestKeyEvent(); |
| 6248 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6249 | |
| 6250 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 6251 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 6252 | ASSERT_EQ(hmac1, hmac2); |
| 6253 | } |
| 6254 | |
| 6255 | /** |
| 6256 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 6257 | */ |
| 6258 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 6259 | KeyEvent event = getTestKeyEvent(); |
| 6260 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6261 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 6262 | |
| 6263 | verifiedEvent.deviceId += 1; |
| 6264 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6265 | |
| 6266 | verifiedEvent.source += 1; |
| 6267 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6268 | |
| 6269 | verifiedEvent.eventTimeNanos += 1; |
| 6270 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6271 | |
| 6272 | verifiedEvent.displayId += 1; |
| 6273 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6274 | |
| 6275 | verifiedEvent.action += 1; |
| 6276 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6277 | |
| 6278 | verifiedEvent.downTimeNanos += 1; |
| 6279 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6280 | |
| 6281 | verifiedEvent.flags += 1; |
| 6282 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6283 | |
| 6284 | verifiedEvent.keyCode += 1; |
| 6285 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6286 | |
| 6287 | verifiedEvent.scanCode += 1; |
| 6288 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6289 | |
| 6290 | verifiedEvent.metaState += 1; |
| 6291 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6292 | |
| 6293 | verifiedEvent.repeatCount += 1; |
| 6294 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6295 | } |
| 6296 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6297 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 6298 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6299 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6300 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6301 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6302 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6303 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6304 | |
| 6305 | // Top window is also focusable but is not granted focus. |
| 6306 | windowTop->setFocusable(true); |
| 6307 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6308 | mDispatcher->onWindowInfosChanged( |
| 6309 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6310 | setFocusedWindow(windowSecond); |
| 6311 | |
| 6312 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6313 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6314 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6315 | |
| 6316 | // Focused window should receive event. |
| 6317 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6318 | windowTop->assertNoEvents(); |
| 6319 | } |
| 6320 | |
| 6321 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 6322 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6323 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6324 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6325 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6326 | |
| 6327 | window->setFocusable(true); |
| 6328 | // Release channel for window is no longer valid. |
| 6329 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6330 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6331 | setFocusedWindow(window); |
| 6332 | |
| 6333 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6334 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6335 | |
| 6336 | // window channel is invalid, so it should not receive any input event. |
| 6337 | window->assertNoEvents(); |
| 6338 | } |
| 6339 | |
| 6340 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 6341 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6342 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6343 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6344 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6345 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6346 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6347 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6348 | setFocusedWindow(window); |
| 6349 | |
| 6350 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6351 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6352 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6353 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6354 | window->assertNoEvents(); |
| 6355 | } |
| 6356 | |
| 6357 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 6358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6359 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6360 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6361 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6362 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6363 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6364 | |
| 6365 | windowTop->setFocusable(true); |
| 6366 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6367 | mDispatcher->onWindowInfosChanged( |
| 6368 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6369 | setFocusedWindow(windowTop); |
| 6370 | windowTop->consumeFocusEvent(true); |
| 6371 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6372 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6373 | mDispatcher->onWindowInfosChanged( |
| 6374 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6375 | windowSecond->consumeFocusEvent(true); |
| 6376 | windowTop->consumeFocusEvent(false); |
| 6377 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6378 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6379 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6380 | |
| 6381 | // Focused window should receive event. |
| 6382 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6383 | } |
| 6384 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6385 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6386 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6387 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6388 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6389 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6390 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6391 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6392 | |
| 6393 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6394 | windowSecond->setFocusable(false); |
| 6395 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6396 | mDispatcher->onWindowInfosChanged( |
| 6397 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6398 | setFocusedWindow(windowTop); |
| 6399 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6400 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6401 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6402 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6403 | |
| 6404 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6405 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6406 | windowSecond->assertNoEvents(); |
| 6407 | } |
| 6408 | |
| 6409 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 6410 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6411 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6412 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6413 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6414 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 6415 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6416 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6417 | |
| 6418 | window->setFocusable(true); |
| 6419 | previousFocusedWindow->setFocusable(true); |
| 6420 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6421 | mDispatcher->onWindowInfosChanged( |
| 6422 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6423 | setFocusedWindow(previousFocusedWindow); |
| 6424 | previousFocusedWindow->consumeFocusEvent(true); |
| 6425 | |
| 6426 | // Requesting focus on invisible window takes focus from currently focused window. |
| 6427 | setFocusedWindow(window); |
| 6428 | previousFocusedWindow->consumeFocusEvent(false); |
| 6429 | |
| 6430 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6431 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6432 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 6433 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6434 | |
| 6435 | // Window does not get focus event or key down. |
| 6436 | window->assertNoEvents(); |
| 6437 | |
| 6438 | // Window becomes visible. |
| 6439 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6440 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6441 | |
| 6442 | // Window receives focus event. |
| 6443 | window->consumeFocusEvent(true); |
| 6444 | // Focused window receives key down. |
| 6445 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6446 | } |
| 6447 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6448 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 6449 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6450 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6451 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6452 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6453 | |
| 6454 | // window is granted focus. |
| 6455 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6456 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6457 | setFocusedWindow(window); |
| 6458 | window->consumeFocusEvent(true); |
| 6459 | |
| 6460 | // When a display is removed window loses focus. |
| 6461 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 6462 | window->consumeFocusEvent(false); |
| 6463 | } |
| 6464 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6465 | /** |
| 6466 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 6467 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 6468 | * of the 'slipperyEnterWindow'. |
| 6469 | * |
| 6470 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 6471 | * a way so that the touched location is no longer covered by the top window. |
| 6472 | * |
| 6473 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 6474 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 6475 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 6476 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 6477 | * with ACTION_DOWN). |
| 6478 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 6479 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 6480 | * |
| 6481 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 6482 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 6483 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 6484 | * |
| 6485 | * In this test, we ensure that the event received by the bottom window has |
| 6486 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 6487 | */ |
| 6488 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6489 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6490 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6491 | |
| 6492 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6493 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6494 | |
| 6495 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6496 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6497 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6498 | // Make sure this one overlaps the bottom window |
| 6499 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 6500 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 6501 | // one. Windows with the same owner are not considered to be occluding each other. |
| 6502 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 6503 | |
| 6504 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6505 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6506 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6507 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6508 | mDispatcher->onWindowInfosChanged( |
| 6509 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6510 | |
| 6511 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6512 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6513 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6514 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6515 | slipperyExitWindow->consumeMotionDown(); |
| 6516 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6517 | mDispatcher->onWindowInfosChanged( |
| 6518 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6519 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6520 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 6521 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6522 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6523 | |
| 6524 | slipperyExitWindow->consumeMotionCancel(); |
| 6525 | |
| 6526 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 6527 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6528 | } |
| 6529 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6530 | /** |
| 6531 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 6532 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 6533 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 6534 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 6535 | * reproduces a crash. |
| 6536 | */ |
| 6537 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 6538 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6539 | |
| 6540 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 6541 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6542 | leftSlipperyWindow->setSlippery(true); |
| 6543 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6544 | |
| 6545 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 6546 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6547 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6548 | rightDropTouchesWindow->setDropInput(true); |
| 6549 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6550 | mDispatcher->onWindowInfosChanged( |
| 6551 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6552 | |
| 6553 | // Start touch in the left window |
| 6554 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6555 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6556 | .build()); |
| 6557 | leftSlipperyWindow->consumeMotionDown(); |
| 6558 | |
| 6559 | // And move it into the right window |
| 6560 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6561 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6562 | .build()); |
| 6563 | |
| 6564 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 6565 | // The left window continues to receive the gesture. |
| 6566 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6567 | rightDropTouchesWindow->assertNoEvents(); |
| 6568 | } |
| 6569 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 6570 | /** |
| 6571 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 6572 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 6573 | */ |
| 6574 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 6575 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6576 | sp<FakeWindowHandle> originalWindow = |
| 6577 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT); |
| 6578 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6579 | originalWindow->setSlippery(true); |
| 6580 | |
| 6581 | sp<FakeWindowHandle> appearingWindow = |
| 6582 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT); |
| 6583 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6584 | |
| 6585 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 6586 | |
| 6587 | // Touch down on the original window |
| 6588 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6589 | injectMotionEvent(*mDispatcher, |
| 6590 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6591 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 6592 | .build())); |
| 6593 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6594 | |
| 6595 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 6596 | // after user starts to drag down on the launcher window. |
| 6597 | mDispatcher->onWindowInfosChanged( |
| 6598 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 6599 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6600 | injectMotionEvent(*mDispatcher, |
| 6601 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6602 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 6603 | .build())); |
| 6604 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 6605 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6606 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6607 | injectMotionEvent(*mDispatcher, |
| 6608 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6609 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6610 | .build())); |
| 6611 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6612 | |
| 6613 | originalWindow->assertNoEvents(); |
| 6614 | appearingWindow->assertNoEvents(); |
| 6615 | } |
| 6616 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6617 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6618 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6619 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6620 | |
| 6621 | sp<FakeWindowHandle> leftWindow = |
| 6622 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6623 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6624 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6625 | |
| 6626 | sp<FakeWindowHandle> rightSpy = |
| 6627 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 6628 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6629 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6630 | rightSpy->setSpy(true); |
| 6631 | rightSpy->setTrustedOverlay(true); |
| 6632 | |
| 6633 | sp<FakeWindowHandle> rightWindow = |
| 6634 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6635 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6636 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6637 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6638 | mDispatcher->onWindowInfosChanged( |
| 6639 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6640 | |
| 6641 | // Touch in the left window |
| 6642 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6643 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6644 | .build()); |
| 6645 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 6646 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6647 | ASSERT_NO_FATAL_FAILURE( |
| 6648 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6649 | |
| 6650 | // Touch another finger over the right windows |
| 6651 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6652 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6653 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6654 | .build()); |
| 6655 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 6656 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 6657 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 6658 | mDispatcher->waitForIdle(); |
| 6659 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6660 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 6661 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6662 | |
| 6663 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 6664 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 6665 | // is part of the UP action, we do not treat this as device interaction. |
| 6666 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6667 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6668 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6669 | .build()); |
| 6670 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 6671 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6672 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6673 | mDispatcher->waitForIdle(); |
| 6674 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6675 | |
| 6676 | // Move remaining finger |
| 6677 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6678 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6679 | .build()); |
| 6680 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6681 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6682 | mDispatcher->waitForIdle(); |
| 6683 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6684 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6685 | |
| 6686 | // Release all fingers |
| 6687 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6688 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6689 | .build()); |
| 6690 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 6691 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 6692 | mDispatcher->waitForIdle(); |
| 6693 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6694 | } |
| 6695 | |
| 6696 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 6697 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6698 | |
| 6699 | sp<FakeWindowHandle> window = |
| 6700 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 6701 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6702 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6703 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6704 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6705 | setFocusedWindow(window); |
| 6706 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 6707 | |
| 6708 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 6709 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 6710 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6711 | ASSERT_NO_FATAL_FAILURE( |
| 6712 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6713 | |
| 6714 | // The UP actions are not treated as device interaction. |
| 6715 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 6716 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 6717 | mDispatcher->waitForIdle(); |
| 6718 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6719 | } |
| 6720 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 6721 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 6722 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6723 | |
| 6724 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 6725 | ADISPLAY_ID_DEFAULT); |
| 6726 | left->setFrame(Rect(0, 0, 100, 100)); |
| 6727 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6728 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 6729 | right->setFrame(Rect(100, 0, 200, 100)); |
| 6730 | sp<FakeWindowHandle> spy = |
| 6731 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 6732 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 6733 | spy->setTrustedOverlay(true); |
| 6734 | spy->setSpy(true); |
| 6735 | |
| 6736 | mDispatcher->onWindowInfosChanged( |
| 6737 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 6738 | |
| 6739 | // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId. |
| 6740 | NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 6741 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 6742 | mDispatcher->notifyMotion(notifyArgs); |
| 6743 | |
| 6744 | const MotionEvent& leftEnter = left->consumeMotionEvent( |
| 6745 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 6746 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 6747 | |
| 6748 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 6749 | Not(WithEventId(notifyArgs.id)), |
| 6750 | Not(WithEventId(leftEnter.getId())), |
| 6751 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 6752 | |
| 6753 | // Send move to the right window, and ensure hover exit and enter are synthesized with new ids. |
| 6754 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 6755 | {PointF{150, 50}}); |
| 6756 | mDispatcher->notifyMotion(notifyArgs); |
| 6757 | |
| 6758 | const MotionEvent& leftExit = left->consumeMotionEvent( |
| 6759 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 6760 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 6761 | |
| 6762 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 6763 | Not(WithEventId(notifyArgs.id)), |
| 6764 | Not(WithEventId(leftExit.getId())), |
| 6765 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 6766 | |
| 6767 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 6768 | } |
| 6769 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 6770 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 6771 | protected: |
| 6772 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6773 | sp<FakeWindowHandle> mWindow; |
| 6774 | |
| 6775 | virtual void SetUp() override { |
| 6776 | InputDispatcherTest::SetUp(); |
| 6777 | |
| 6778 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 6779 | |
| 6780 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 6781 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6782 | |
| 6783 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 6784 | setFocusedWindow(mWindow); |
| 6785 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 6786 | } |
| 6787 | |
| 6788 | void setFallback(int32_t keycode) { |
| 6789 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 6790 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 6791 | }); |
| 6792 | } |
| 6793 | |
| 6794 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6795 | const KeyEvent& event = mWindow->consumeKey(handled); |
| 6796 | ASSERT_THAT(event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 6797 | } |
| 6798 | }; |
| 6799 | |
| 6800 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 6801 | mDispatcher->notifyKey( |
| 6802 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6803 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 6804 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6805 | } |
| 6806 | |
| 6807 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 6808 | mDispatcher->notifyKey( |
| 6809 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6810 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 6811 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6812 | } |
| 6813 | |
| 6814 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 6815 | mDispatcher->notifyKey( |
| 6816 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6817 | |
| 6818 | // Do not handle this key event. |
| 6819 | consumeKey(/*handled=*/false, |
| 6820 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6821 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6822 | |
| 6823 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 6824 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6825 | } |
| 6826 | |
| 6827 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 6828 | setFallback(AKEYCODE_B); |
| 6829 | mDispatcher->notifyKey( |
| 6830 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6831 | |
| 6832 | // Do not handle this key event. |
| 6833 | consumeKey(/*handled=*/false, |
| 6834 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6835 | |
| 6836 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 6837 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6838 | consumeKey(/*handled=*/true, |
| 6839 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6840 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6841 | |
| 6842 | // Release the original key, and ensure the fallback key is also released. |
| 6843 | mDispatcher->notifyKey( |
| 6844 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6845 | consumeKey(/*handled=*/false, |
| 6846 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6847 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6848 | consumeKey(/*handled=*/true, |
| 6849 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6850 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6851 | |
| 6852 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6853 | mWindow->assertNoEvents(); |
| 6854 | } |
| 6855 | |
| 6856 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 6857 | setFallback(AKEYCODE_B); |
| 6858 | mDispatcher->notifyKey( |
| 6859 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6860 | |
| 6861 | // Do not handle this key event, but handle the fallback. |
| 6862 | consumeKey(/*handled=*/false, |
| 6863 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6864 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6865 | consumeKey(/*handled=*/true, |
| 6866 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6867 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6868 | |
| 6869 | // Release the original key, and ensure the fallback key is also released. |
| 6870 | mDispatcher->notifyKey( |
| 6871 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6872 | // But this time, the app handles the original key. |
| 6873 | consumeKey(/*handled=*/true, |
| 6874 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6875 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6876 | // Ensure the fallback key is canceled. |
| 6877 | consumeKey(/*handled=*/true, |
| 6878 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6879 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6880 | |
| 6881 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6882 | mWindow->assertNoEvents(); |
| 6883 | } |
| 6884 | |
| 6885 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 6886 | setFallback(AKEYCODE_B); |
| 6887 | mDispatcher->notifyKey( |
| 6888 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6889 | |
| 6890 | // Do not handle this key event. |
| 6891 | consumeKey(/*handled=*/false, |
| 6892 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6893 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6894 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 6895 | setFallback(AKEYCODE_C); |
| 6896 | consumeKey(/*handled=*/false, |
| 6897 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6898 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6899 | |
| 6900 | // Release the original key, and ensure the fallback key is also released. |
| 6901 | setFallback(AKEYCODE_B); |
| 6902 | mDispatcher->notifyKey( |
| 6903 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6904 | consumeKey(/*handled=*/false, |
| 6905 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6906 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6907 | consumeKey(/*handled=*/false, |
| 6908 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6909 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6910 | |
| 6911 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6912 | mWindow->assertNoEvents(); |
| 6913 | } |
| 6914 | |
| 6915 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 6916 | setFallback(AKEYCODE_B); |
| 6917 | mDispatcher->notifyKey( |
| 6918 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6919 | |
| 6920 | // Do not handle this key event, so fallback is generated. |
| 6921 | consumeKey(/*handled=*/false, |
| 6922 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6923 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6924 | consumeKey(/*handled=*/true, |
| 6925 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6926 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6927 | |
| 6928 | // Release the original key, but assume the policy is misbehaving and it |
| 6929 | // generates an inconsistent fallback to the one from the DOWN event. |
| 6930 | setFallback(AKEYCODE_C); |
| 6931 | mDispatcher->notifyKey( |
| 6932 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6933 | consumeKey(/*handled=*/false, |
| 6934 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6935 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6936 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 6937 | consumeKey(/*handled=*/true, |
| 6938 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6939 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6940 | |
| 6941 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6942 | mWindow->assertNoEvents(); |
| 6943 | } |
| 6944 | |
| 6945 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 6946 | setFallback(AKEYCODE_B); |
| 6947 | mDispatcher->notifyKey( |
| 6948 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6949 | |
| 6950 | // Do not handle this key event, so fallback is generated. |
| 6951 | consumeKey(/*handled=*/false, |
| 6952 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6953 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6954 | consumeKey(/*handled=*/true, |
| 6955 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6956 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6957 | |
| 6958 | // The original key is canceled. |
| 6959 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 6960 | .keyCode(AKEYCODE_A) |
| 6961 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 6962 | .build()); |
| 6963 | consumeKey(/*handled=*/false, |
| 6964 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 6965 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 6966 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6967 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 6968 | consumeKey(/*handled=*/true, |
| 6969 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6970 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6971 | |
| 6972 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6973 | mWindow->assertNoEvents(); |
| 6974 | } |
| 6975 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6976 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 6977 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 6978 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 6979 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6980 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6981 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6982 | sp<FakeWindowHandle> mWindow; |
| 6983 | |
| 6984 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6985 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6986 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6987 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 6988 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6989 | ASSERT_EQ(OK, mDispatcher->start()); |
| 6990 | |
| 6991 | setUpWindow(); |
| 6992 | } |
| 6993 | |
| 6994 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6995 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6996 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6997 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6998 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6999 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7000 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7001 | mWindow->consumeFocusEvent(true); |
| 7002 | } |
| 7003 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7004 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7005 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7006 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7007 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7008 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7009 | |
| 7010 | // Window should receive key down event. |
| 7011 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7012 | } |
| 7013 | |
| 7014 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 7015 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7016 | mWindow->consumeKeyEvent( |
| 7017 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7018 | } |
| 7019 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7020 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7021 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7022 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7023 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7024 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7025 | |
| 7026 | // Window should receive key down event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7027 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7028 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7029 | } |
| 7030 | }; |
| 7031 | |
| 7032 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7033 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7034 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7035 | expectKeyRepeatOnce(repeatCount); |
| 7036 | } |
| 7037 | } |
| 7038 | |
| 7039 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7040 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7041 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7042 | expectKeyRepeatOnce(repeatCount); |
| 7043 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7044 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7045 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7046 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7047 | expectKeyRepeatOnce(repeatCount); |
| 7048 | } |
| 7049 | } |
| 7050 | |
| 7051 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7052 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7053 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7054 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7055 | mWindow->assertNoEvents(); |
| 7056 | } |
| 7057 | |
| 7058 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7059 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7060 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7061 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7062 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7063 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7064 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7065 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7066 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 7067 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7068 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7069 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7070 | mWindow->assertNoEvents(); |
| 7071 | } |
| 7072 | |
| 7073 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7074 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7075 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7076 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7077 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7078 | // 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] | 7079 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7080 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7081 | mWindow->assertNoEvents(); |
| 7082 | } |
| 7083 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7084 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 7085 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7086 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7087 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7088 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 7089 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 7090 | mWindow->assertNoEvents(); |
| 7091 | } |
| 7092 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7093 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7094 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7095 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7096 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7097 | const KeyEvent& repeatEvent = mWindow->consumeKey(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7098 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7099 | IdGenerator::getSource(repeatEvent.getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7100 | } |
| 7101 | } |
| 7102 | |
| 7103 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7104 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7105 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7106 | |
| 7107 | std::unordered_set<int32_t> idSet; |
| 7108 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7109 | const KeyEvent& repeatEvent = mWindow->consumeKey(); |
| 7110 | int32_t id = repeatEvent.getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7111 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 7112 | idSet.insert(id); |
| 7113 | } |
| 7114 | } |
| 7115 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7116 | /* Test InputDispatcher for MultiDisplay */ |
| 7117 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 7118 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7119 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7120 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7121 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7122 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7123 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7124 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7125 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7126 | // Set focus window for primary display, but focused display would be second one. |
| 7127 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7128 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7129 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 7130 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7131 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7132 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7133 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7134 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7135 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7136 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7137 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7138 | // Set focus display to second one. |
| 7139 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 7140 | // Set focus window for second display. |
| 7141 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7142 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7143 | mDispatcher->onWindowInfosChanged( |
| 7144 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7145 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7146 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7147 | } |
| 7148 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7149 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7150 | InputDispatcherTest::TearDown(); |
| 7151 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7152 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7153 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7154 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7155 | windowInSecondary.clear(); |
| 7156 | } |
| 7157 | |
| 7158 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7159 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7160 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7161 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7162 | sp<FakeWindowHandle> windowInSecondary; |
| 7163 | }; |
| 7164 | |
| 7165 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 7166 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7167 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7168 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7169 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7170 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7171 | windowInSecondary->assertNoEvents(); |
| 7172 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7173 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7175 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7176 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7177 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7178 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7179 | } |
| 7180 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7181 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7182 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7183 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7184 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7185 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7186 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7187 | windowInSecondary->assertNoEvents(); |
| 7188 | |
| 7189 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7190 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7191 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7192 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7193 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7194 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7195 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7196 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7197 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7198 | // Old focus should receive a cancel event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7199 | windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7200 | |
| 7201 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7202 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7203 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7204 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7205 | windowInSecondary->assertNoEvents(); |
| 7206 | } |
| 7207 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7208 | // Test per-display input monitors for motion event. |
| 7209 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7210 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7211 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7212 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7213 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7214 | |
| 7215 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7217 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7218 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7219 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7220 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7221 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7222 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7223 | |
| 7224 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7226 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7227 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7228 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7229 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7230 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7231 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7232 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7233 | // Lift up the touch from the second display |
| 7234 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7235 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7236 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7237 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 7238 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 7239 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7240 | // Test inject a non-pointer motion event. |
| 7241 | // If specific a display, it will dispatch to the focused window of particular display, |
| 7242 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7244 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7245 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7246 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7247 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7248 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7249 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | // Test per-display input monitors for key event. |
| 7253 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7254 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7255 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7256 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7257 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7258 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7259 | |
| 7260 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7261 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7262 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7263 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7264 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7265 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7266 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7267 | } |
| 7268 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7269 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 7270 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7271 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7272 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7273 | mDispatcher->onWindowInfosChanged( |
| 7274 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 7275 | *windowInSecondary->getInfo()}, |
| 7276 | {}, |
| 7277 | 0, |
| 7278 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7279 | setFocusedWindow(secondWindowInPrimary); |
| 7280 | windowInPrimary->consumeFocusEvent(false); |
| 7281 | secondWindowInPrimary->consumeFocusEvent(true); |
| 7282 | |
| 7283 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7284 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7285 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7286 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7287 | windowInPrimary->assertNoEvents(); |
| 7288 | windowInSecondary->assertNoEvents(); |
| 7289 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7290 | } |
| 7291 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7292 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 7293 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7294 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7295 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7296 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7297 | |
| 7298 | // Test touch down on primary display. |
| 7299 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7300 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7301 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7302 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7303 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7304 | |
| 7305 | // Test touch down on second display. |
| 7306 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7307 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7308 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7309 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7310 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7311 | |
| 7312 | // Trigger cancel touch. |
| 7313 | mDispatcher->cancelCurrentTouch(); |
| 7314 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7315 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7316 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7317 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7318 | |
| 7319 | // Test inject a move motion event, no window/monitor should receive the event. |
| 7320 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7321 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7322 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 7323 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7324 | windowInPrimary->assertNoEvents(); |
| 7325 | monitorInPrimary.assertNoEvents(); |
| 7326 | |
| 7327 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7328 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7329 | SECOND_DISPLAY_ID, {110, 200})) |
| 7330 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7331 | windowInSecondary->assertNoEvents(); |
| 7332 | monitorInSecondary.assertNoEvents(); |
| 7333 | } |
| 7334 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7335 | class InputFilterTest : public InputDispatcherTest { |
| 7336 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7337 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 7338 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7339 | NotifyMotionArgs motionArgs; |
| 7340 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7341 | motionArgs = |
| 7342 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7343 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7344 | motionArgs = |
| 7345 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7346 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7347 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7348 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 7349 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7350 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7351 | } else { |
| 7352 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7353 | } |
| 7354 | } |
| 7355 | |
| 7356 | void testNotifyKey(bool expectToBeFiltered) { |
| 7357 | NotifyKeyArgs keyArgs; |
| 7358 | |
| 7359 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7360 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7361 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7362 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7363 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7364 | |
| 7365 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 7366 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7367 | } else { |
| 7368 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7369 | } |
| 7370 | } |
| 7371 | }; |
| 7372 | |
| 7373 | // Test InputFilter for MotionEvent |
| 7374 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 7375 | // 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] | 7376 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7377 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7378 | |
| 7379 | // Enable InputFilter |
| 7380 | mDispatcher->setInputFilterEnabled(true); |
| 7381 | // 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] | 7382 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 7383 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7384 | |
| 7385 | // Disable InputFilter |
| 7386 | mDispatcher->setInputFilterEnabled(false); |
| 7387 | // 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] | 7388 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7389 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7390 | } |
| 7391 | |
| 7392 | // Test InputFilter for KeyEvent |
| 7393 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 7394 | // 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] | 7395 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7396 | |
| 7397 | // Enable InputFilter |
| 7398 | mDispatcher->setInputFilterEnabled(true); |
| 7399 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7400 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7401 | |
| 7402 | // Disable InputFilter |
| 7403 | mDispatcher->setInputFilterEnabled(false); |
| 7404 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7405 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7406 | } |
| 7407 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7408 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 7409 | // logical display coordinate space. |
| 7410 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 7411 | ui::Transform firstDisplayTransform; |
| 7412 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7413 | ui::Transform secondDisplayTransform; |
| 7414 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 7415 | |
| 7416 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 7417 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 7418 | displayInfos[0].transform = firstDisplayTransform; |
| 7419 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 7420 | displayInfos[1].transform = secondDisplayTransform; |
| 7421 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7422 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7423 | |
| 7424 | // Enable InputFilter |
| 7425 | mDispatcher->setInputFilterEnabled(true); |
| 7426 | |
| 7427 | // Ensure the correct transforms are used for the displays. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7428 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 7429 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7430 | } |
| 7431 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7432 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 7433 | protected: |
| 7434 | virtual void SetUp() override { |
| 7435 | InputDispatcherTest::SetUp(); |
| 7436 | |
| 7437 | /** |
| 7438 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 7439 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 7440 | * on. |
| 7441 | */ |
| 7442 | mDispatcher->setInputFilterEnabled(true); |
| 7443 | |
| 7444 | std::shared_ptr<InputApplicationHandle> application = |
| 7445 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7446 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 7447 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7448 | |
| 7449 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7450 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7451 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7452 | setFocusedWindow(mWindow); |
| 7453 | mWindow->consumeFocusEvent(true); |
| 7454 | } |
| 7455 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7456 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7457 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7458 | KeyEvent event; |
| 7459 | |
| 7460 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7461 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 7462 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7463 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7464 | const int32_t additionalPolicyFlags = |
| 7465 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 7466 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7467 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7468 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7469 | policyFlags | additionalPolicyFlags)); |
| 7470 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7471 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7472 | } |
| 7473 | |
| 7474 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7475 | int32_t flags) { |
| 7476 | MotionEvent event; |
| 7477 | PointerProperties pointerProperties[1]; |
| 7478 | PointerCoords pointerCoords[1]; |
| 7479 | pointerProperties[0].clear(); |
| 7480 | pointerProperties[0].id = 0; |
| 7481 | pointerCoords[0].clear(); |
| 7482 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 7483 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 7484 | |
| 7485 | ui::Transform identityTransform; |
| 7486 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7487 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 7488 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 7489 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 7490 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 7491 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 7492 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7493 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7494 | |
| 7495 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 7496 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7497 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7498 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7499 | policyFlags | additionalPolicyFlags)); |
| 7500 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7501 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7502 | } |
| 7503 | |
| 7504 | private: |
| 7505 | sp<FakeWindowHandle> mWindow; |
| 7506 | }; |
| 7507 | |
| 7508 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7509 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 7510 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 7511 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7512 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 7513 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7514 | } |
| 7515 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7516 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7517 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7518 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7519 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 7520 | } |
| 7521 | |
| 7522 | TEST_F(InputFilterInjectionPolicyTest, |
| 7523 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 7524 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7525 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7526 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
| 7529 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7530 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 7531 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7532 | } |
| 7533 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7534 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7535 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7536 | InputDispatcherTest::SetUp(); |
| 7537 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7538 | std::shared_ptr<FakeApplicationHandle> application = |
| 7539 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7540 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7541 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7542 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7543 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7544 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7545 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7546 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7547 | |
| 7548 | // Set focused application. |
| 7549 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7550 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7551 | |
| 7552 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7553 | mDispatcher->onWindowInfosChanged( |
| 7554 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7555 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7556 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7557 | } |
| 7558 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7559 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7560 | InputDispatcherTest::TearDown(); |
| 7561 | |
| 7562 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7563 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7564 | } |
| 7565 | |
| 7566 | protected: |
| 7567 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7568 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7569 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7570 | }; |
| 7571 | |
| 7572 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 7573 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 7574 | // the onPointerDownOutsideFocus callback. |
| 7575 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7577 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7578 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7579 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7580 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7581 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7582 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7583 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 7584 | } |
| 7585 | |
| 7586 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 7587 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 7588 | // onPointerDownOutsideFocus callback. |
| 7589 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7591 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, |
| 7592 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7594 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7595 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7596 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7597 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7598 | } |
| 7599 | |
| 7600 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 7601 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 7602 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7603 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7604 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7605 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7606 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7607 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7608 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7609 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7610 | } |
| 7611 | |
| 7612 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 7613 | // DOWN on the window that already has focus. Ensure no window received the |
| 7614 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7615 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7617 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7618 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7619 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7620 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7621 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7622 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7623 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7624 | } |
| 7625 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7626 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 7627 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 7628 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 7629 | const MotionEvent event = |
| 7630 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7631 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7632 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7633 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 7634 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7635 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7636 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7637 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 7638 | |
| 7639 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7640 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 7641 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 7642 | mUnfocusedWindow->assertNoEvents(); |
| 7643 | } |
| 7644 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7645 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 7646 | // windows that point to the same client token. |
| 7647 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 7648 | virtual void SetUp() override { |
| 7649 | InputDispatcherTest::SetUp(); |
| 7650 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7651 | std::shared_ptr<FakeApplicationHandle> application = |
| 7652 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7653 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 7654 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7655 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 7656 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7657 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 7658 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7659 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 7660 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7661 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | protected: |
| 7665 | sp<FakeWindowHandle> mWindow1; |
| 7666 | sp<FakeWindowHandle> mWindow2; |
| 7667 | |
| 7668 | // 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] | 7669 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 7670 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 7671 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7672 | } |
| 7673 | |
| 7674 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 7675 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7676 | const std::string name = window->getName(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7677 | const MotionEvent& motionEvent = |
| 7678 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7679 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7680 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7681 | |
| 7682 | for (size_t i = 0; i < points.size(); i++) { |
| 7683 | float expectedX = points[i].x; |
| 7684 | float expectedY = points[i].y; |
| 7685 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7686 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7687 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7688 | << ", got " << motionEvent.getX(i); |
| 7689 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7690 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7691 | << ", got " << motionEvent.getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7692 | } |
| 7693 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7694 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7695 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7696 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7697 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 7698 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7699 | |
| 7700 | // Always consume from window1 since it's the window that has the InputReceiver |
| 7701 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 7702 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7703 | }; |
| 7704 | |
| 7705 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 7706 | // Touch Window 1 |
| 7707 | PointF touchedPoint = {10, 10}; |
| 7708 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7709 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7710 | |
| 7711 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7712 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7713 | |
| 7714 | // Touch Window 2 |
| 7715 | touchedPoint = {150, 150}; |
| 7716 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7717 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7718 | } |
| 7719 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7720 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 7721 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7722 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7723 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7724 | |
| 7725 | // Touch Window 1 |
| 7726 | PointF touchedPoint = {10, 10}; |
| 7727 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7728 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7729 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7730 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7731 | |
| 7732 | // Touch Window 2 |
| 7733 | touchedPoint = {150, 150}; |
| 7734 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7735 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 7736 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7737 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7738 | // Update the transform so rotation is set |
| 7739 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7740 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7741 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 7742 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7743 | } |
| 7744 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7745 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7746 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7747 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7748 | |
| 7749 | // Touch Window 1 |
| 7750 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7751 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7752 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7753 | |
| 7754 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7755 | touchedPoints.push_back(PointF{150, 150}); |
| 7756 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7757 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7758 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7759 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7760 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7761 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7762 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7763 | // Update the transform so rotation is set for Window 2 |
| 7764 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7765 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7766 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7767 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7768 | } |
| 7769 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7770 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7771 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7772 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7773 | |
| 7774 | // Touch Window 1 |
| 7775 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7776 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7777 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7778 | |
| 7779 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7780 | touchedPoints.push_back(PointF{150, 150}); |
| 7781 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7782 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7783 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7784 | |
| 7785 | // Move both windows |
| 7786 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7787 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7788 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7789 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7790 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7791 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7792 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7793 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7794 | expectedPoints.pop_back(); |
| 7795 | |
| 7796 | // Touch Window 2 |
| 7797 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7798 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7799 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7800 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7801 | |
| 7802 | // Move both windows |
| 7803 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7804 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7805 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7806 | |
| 7807 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7808 | } |
| 7809 | |
| 7810 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 7811 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7812 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7813 | |
| 7814 | // Touch Window 1 |
| 7815 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7816 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7817 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7818 | |
| 7819 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7820 | touchedPoints.push_back(PointF{150, 150}); |
| 7821 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7822 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7823 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7824 | |
| 7825 | // Move both windows |
| 7826 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7827 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7828 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7829 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7830 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7831 | } |
| 7832 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 7833 | /** |
| 7834 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 7835 | * same input channel. |
| 7836 | */ |
| 7837 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 7838 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7839 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 7840 | |
| 7841 | // Touch down in window 1 |
| 7842 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7843 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 7844 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 7845 | |
| 7846 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 7847 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 7848 | // getting generated. |
| 7849 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7850 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 7851 | |
| 7852 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 7853 | } |
| 7854 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7855 | /** |
| 7856 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 7857 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 7858 | * that the pointer is hovering over may have a different transform. |
| 7859 | */ |
| 7860 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7861 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7862 | |
| 7863 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7864 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 7865 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7866 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7867 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 7868 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7869 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7870 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7871 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 7872 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7873 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 7874 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 7875 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 7876 | } |
| 7877 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7878 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 7879 | virtual void SetUp() override { |
| 7880 | InputDispatcherTest::SetUp(); |
| 7881 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7882 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7883 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7884 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 7885 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7886 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7887 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7888 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7889 | |
| 7890 | // Set focused application. |
| 7891 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 7892 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7893 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7894 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7895 | mWindow->consumeFocusEvent(true); |
| 7896 | } |
| 7897 | |
| 7898 | virtual void TearDown() override { |
| 7899 | InputDispatcherTest::TearDown(); |
| 7900 | mWindow.clear(); |
| 7901 | } |
| 7902 | |
| 7903 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7904 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7905 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7906 | sp<FakeWindowHandle> mWindow; |
| 7907 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 7908 | |
| 7909 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 7910 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 7911 | .x(WINDOW_LOCATION.x) |
| 7912 | .y(WINDOW_LOCATION.y); |
| 7913 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7914 | .pointer(touchingPointer) |
| 7915 | .build()); |
| 7916 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7917 | .pointer(touchingPointer) |
| 7918 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7919 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7920 | |
| 7921 | sp<FakeWindowHandle> addSpyWindow() { |
| 7922 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7923 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7924 | spy->setTrustedOverlay(true); |
| 7925 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7926 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7927 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7928 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7929 | return spy; |
| 7930 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7931 | }; |
| 7932 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7933 | // Send a tap and respond, which should not cause an ANR. |
| 7934 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 7935 | tapOnWindow(); |
| 7936 | mWindow->consumeMotionDown(); |
| 7937 | mWindow->consumeMotionUp(); |
| 7938 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7939 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7940 | } |
| 7941 | |
| 7942 | // Send a regular key and respond, which should not cause an ANR. |
| 7943 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7944 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7945 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7946 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7947 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7948 | } |
| 7949 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7950 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 7951 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7952 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7953 | mWindow->consumeFocusEvent(false); |
| 7954 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7955 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7956 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7957 | InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7958 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7959 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7960 | // Key will not go to window because we have no focused window. |
| 7961 | // The 'no focused window' ANR timer should start instead. |
| 7962 | |
| 7963 | // Now, the focused application goes away. |
| 7964 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 7965 | // The key should get dropped and there should be no ANR. |
| 7966 | |
| 7967 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7968 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7969 | } |
| 7970 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7971 | // 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] | 7972 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 7973 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7974 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7975 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7976 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7977 | WINDOW_LOCATION)); |
| 7978 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7979 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 7980 | ASSERT_TRUE(sequenceNum); |
| 7981 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7982 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7983 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7984 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7985 | mWindow->consumeMotionEvent( |
| 7986 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7987 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7988 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7989 | } |
| 7990 | |
| 7991 | // Send a key to the app and have the app not respond right away. |
| 7992 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 7993 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7994 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7995 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 7996 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7997 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7998 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7999 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8000 | } |
| 8001 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8002 | // We have a focused application, but no focused window |
| 8003 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8004 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8005 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8006 | mWindow->consumeFocusEvent(false); |
| 8007 | |
| 8008 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8009 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8010 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8011 | WINDOW_LOCATION)); |
| 8012 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 8013 | mDispatcher->waitForIdle(); |
| 8014 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8015 | |
| 8016 | // Once a focused event arrives, we get an ANR for this application |
| 8017 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8018 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8019 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8020 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8021 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8022 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8023 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8024 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8025 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8026 | } |
| 8027 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8028 | /** |
| 8029 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 8030 | * there will not be an ANR. |
| 8031 | */ |
| 8032 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 8033 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8034 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8035 | mWindow->consumeFocusEvent(false); |
| 8036 | |
| 8037 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 8038 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 8039 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8040 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 8041 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 8042 | |
| 8043 | // Define a valid key down event that is stale (too old). |
| 8044 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8045 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8046 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8047 | |
| 8048 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 8049 | |
| 8050 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8051 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8052 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8053 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 8054 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 8055 | << "Injection should fail because the event is stale"; |
| 8056 | |
| 8057 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8058 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8059 | mWindow->assertNoEvents(); |
| 8060 | } |
| 8061 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8062 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8063 | // Make sure that we don't notify policy twice about the same ANR. |
| 8064 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8065 | const std::chrono::duration appTimeout = 400ms; |
| 8066 | mApplication->setDispatchingTimeout(appTimeout); |
| 8067 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8068 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8069 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8070 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8071 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8072 | |
| 8073 | // Once a focused event arrives, we get an ANR for this application |
| 8074 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8075 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8076 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 8077 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8078 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8079 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8080 | InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout, |
| 8081 | /*allowKeyRepeat=*/false); |
| 8082 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 8083 | << "result=" << ftl::enum_string(result); |
| 8084 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 8085 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 8086 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 8087 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8088 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8089 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8090 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 8091 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8092 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8093 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 8094 | // '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] | 8095 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8096 | } |
| 8097 | |
| 8098 | // We have a focused application, but no focused window |
| 8099 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8100 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8101 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8102 | mWindow->consumeFocusEvent(false); |
| 8103 | |
| 8104 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8105 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8106 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8107 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8108 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8109 | |
| 8110 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8111 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8112 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8113 | mWindow->assertNoEvents(); |
| 8114 | } |
| 8115 | |
| 8116 | /** |
| 8117 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 8118 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 8119 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 8120 | * the ANR mechanism should still work. |
| 8121 | * |
| 8122 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 8123 | * DOWN event, while not responding on the second one. |
| 8124 | */ |
| 8125 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 8126 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8127 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8128 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8129 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8130 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8131 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8132 | |
| 8133 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8134 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8135 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8136 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8137 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8138 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8139 | |
| 8140 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 8141 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8142 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8143 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8144 | } |
| 8145 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8146 | // A spy window can receive an ANR |
| 8147 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 8148 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 8149 | |
| 8150 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8151 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8152 | WINDOW_LOCATION)); |
| 8153 | mWindow->consumeMotionDown(); |
| 8154 | |
| 8155 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 8156 | ASSERT_TRUE(sequenceNum); |
| 8157 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8158 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8159 | |
| 8160 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8161 | spy->consumeMotionEvent( |
| 8162 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8163 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8164 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | // 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] | 8168 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8169 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 8170 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8171 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8172 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8173 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8174 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8175 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8176 | |
| 8177 | // Stuck on the ACTION_UP |
| 8178 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8179 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8180 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8181 | // 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] | 8182 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8183 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8184 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8185 | |
| 8186 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8187 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8188 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8189 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8190 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8191 | } |
| 8192 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8193 | // 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] | 8194 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8195 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 8196 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8197 | |
| 8198 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8199 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8200 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8201 | |
| 8202 | mWindow->consumeMotionDown(); |
| 8203 | // Stuck on the ACTION_UP |
| 8204 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8205 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8206 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8207 | // 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] | 8208 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8209 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8210 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8211 | |
| 8212 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8213 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8214 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8215 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8216 | spy->assertNoEvents(); |
| 8217 | } |
| 8218 | |
| 8219 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8220 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8221 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8222 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8223 | |
| 8224 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8225 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8226 | WINDOW_LOCATION)); |
| 8227 | |
| 8228 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8229 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 8230 | ASSERT_TRUE(consumeSeq); |
| 8231 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8232 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 8233 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8234 | |
| 8235 | monitor.finishEvent(*consumeSeq); |
| 8236 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 8237 | |
| 8238 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8239 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8240 | } |
| 8241 | |
| 8242 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 8243 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 8244 | // get an ANR again. |
| 8245 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 8246 | // 2. consume all pending events (= queue becomes healthy again) |
| 8247 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 8248 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 8249 | tapOnWindow(); |
| 8250 | |
| 8251 | mWindow->consumeMotionDown(); |
| 8252 | // Block on ACTION_UP |
| 8253 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8254 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8255 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 8256 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8257 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8258 | mWindow->assertNoEvents(); |
| 8259 | |
| 8260 | tapOnWindow(); |
| 8261 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8262 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8263 | mWindow->consumeMotionUp(); |
| 8264 | |
| 8265 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8266 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8267 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8268 | mWindow->assertNoEvents(); |
| 8269 | } |
| 8270 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8271 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 8272 | // it. |
| 8273 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8275 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8276 | WINDOW_LOCATION)); |
| 8277 | |
| 8278 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8279 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8280 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8281 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 8282 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8283 | // 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] | 8284 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8285 | mWindow->consumeMotionEvent( |
| 8286 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8287 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8288 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8289 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8290 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8291 | } |
| 8292 | |
| 8293 | /** |
| 8294 | * 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] | 8295 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8296 | * |
| 8297 | * Warning!!! |
| 8298 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 8299 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8300 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8301 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 8302 | * |
| 8303 | * If that value changes, this test should also change. |
| 8304 | */ |
| 8305 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 8306 | 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] | 8307 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8308 | |
| 8309 | tapOnWindow(); |
| 8310 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 8311 | ASSERT_TRUE(downSequenceNum); |
| 8312 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 8313 | ASSERT_TRUE(upSequenceNum); |
| 8314 | // Don't finish the events yet, and send a key |
| 8315 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 8316 | // window even if motions are still being processed. But because the injection timeout is short, |
| 8317 | // we will receive INJECTION_TIMED_OUT as the result. |
| 8318 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8319 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8320 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8321 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8322 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8323 | // Key will not be sent to the window, yet, because the window is still processing events |
| 8324 | // 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] | 8325 | // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR. |
| 8326 | // Rely here on the fact that it uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 8327 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8328 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8329 | |
| 8330 | std::this_thread::sleep_for(500ms); |
| 8331 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 8332 | // to the focused window, even though we have not yet finished the motion event |
| 8333 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8334 | mWindow->finishEvent(*downSequenceNum); |
| 8335 | mWindow->finishEvent(*upSequenceNum); |
| 8336 | } |
| 8337 | |
| 8338 | /** |
| 8339 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 8340 | * not go to the focused window until the motion is processed. |
| 8341 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 8342 | * focused window right away. |
| 8343 | */ |
| 8344 | TEST_F(InputDispatcherSingleWindowAnr, |
| 8345 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 8346 | 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] | 8347 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8348 | |
| 8349 | tapOnWindow(); |
| 8350 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 8351 | ASSERT_TRUE(downSequenceNum); |
| 8352 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 8353 | ASSERT_TRUE(upSequenceNum); |
| 8354 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8355 | mDispatcher->notifyKey( |
| 8356 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8357 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8358 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8359 | // 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] | 8360 | // Make sure the `assertNoEvents` check doesn't take too long. It uses |
| 8361 | // CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 8362 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8363 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8364 | |
| 8365 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 8366 | tapOnWindow(); |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8367 | mWindow->consumeKeyEvent(WithKeyAction(AKEY_EVENT_ACTION_DOWN)); // it doesn't matter that we |
| 8368 | // 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] | 8369 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 8370 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8371 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8372 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8373 | mWindow->assertNoEvents(); |
| 8374 | } |
| 8375 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8376 | /** |
| 8377 | * Send an event to the app and have the app not respond right away. |
| 8378 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8379 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 8380 | * At some point, the window becomes responsive again. |
| 8381 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 8382 | */ |
| 8383 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 8384 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8385 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 8386 | .build()); |
| 8387 | |
| 8388 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 8389 | ASSERT_TRUE(sequenceNum); |
| 8390 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8391 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 8392 | |
| 8393 | mWindow->finishEvent(*sequenceNum); |
| 8394 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 8395 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8396 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 8397 | |
| 8398 | // Now that the window is responsive, let's continue the gesture. |
| 8399 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8400 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8401 | .build()); |
| 8402 | |
| 8403 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8404 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8405 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8406 | .build()); |
| 8407 | |
| 8408 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8409 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8410 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8411 | .build()); |
| 8412 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8413 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8414 | .build()); |
| 8415 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 8416 | mWindow->assertNoEvents(); |
| 8417 | |
| 8418 | // Start another one. |
| 8419 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8420 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 8421 | .build()); |
| 8422 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8423 | } |
| 8424 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8425 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 8426 | virtual void SetUp() override { |
| 8427 | InputDispatcherTest::SetUp(); |
| 8428 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8429 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8430 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8431 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 8432 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8433 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8434 | // 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] | 8435 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8436 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8437 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 8438 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8439 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8440 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8441 | |
| 8442 | // Set focused application. |
| 8443 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8444 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8445 | |
| 8446 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8447 | mDispatcher->onWindowInfosChanged( |
| 8448 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8449 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8450 | mFocusedWindow->consumeFocusEvent(true); |
| 8451 | } |
| 8452 | |
| 8453 | virtual void TearDown() override { |
| 8454 | InputDispatcherTest::TearDown(); |
| 8455 | |
| 8456 | mUnfocusedWindow.clear(); |
| 8457 | mFocusedWindow.clear(); |
| 8458 | } |
| 8459 | |
| 8460 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8461 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8462 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 8463 | sp<FakeWindowHandle> mFocusedWindow; |
| 8464 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 8465 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 8466 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 8467 | |
| 8468 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 8469 | |
| 8470 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 8471 | |
| 8472 | private: |
| 8473 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8474 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8475 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8476 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8477 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8478 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8479 | location)); |
| 8480 | } |
| 8481 | }; |
| 8482 | |
| 8483 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 8484 | // should be ANR'd first. |
| 8485 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8486 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8487 | injectMotionEvent(*mDispatcher, |
| 8488 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8489 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8490 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8491 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8492 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8493 | .build())); |
| 8494 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8495 | injectMotionEvent(*mDispatcher, |
| 8496 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 8497 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8498 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8499 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8500 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8501 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8502 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8503 | mFocusedWindow->consumeMotionUp(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8504 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8505 | // We consumed all events, so no ANR |
| 8506 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8507 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8508 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8509 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8510 | injectMotionEvent(*mDispatcher, |
| 8511 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8512 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8513 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8514 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8515 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8516 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8517 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8518 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8519 | |
| 8520 | const std::chrono::duration timeout = |
| 8521 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8522 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8523 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8524 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8525 | mFocusedWindow->consumeMotionDown(); |
| 8526 | // This cancel is generated because the connection was unresponsive |
| 8527 | mFocusedWindow->consumeMotionCancel(); |
| 8528 | mFocusedWindow->assertNoEvents(); |
| 8529 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8530 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8531 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8532 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8533 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8534 | } |
| 8535 | |
| 8536 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 8537 | // it doesn't matter which order they should have ANR. |
| 8538 | // But we should receive ANR for both. |
| 8539 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 8540 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8541 | mUnfocusedWindow->setDispatchingTimeout( |
| 8542 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8543 | mDispatcher->onWindowInfosChanged( |
| 8544 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8545 | |
| 8546 | tapOnFocusedWindow(); |
| 8547 | // 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] | 8548 | // 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] | 8549 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 8550 | mFocusedWindow->getDispatchingTimeout( |
| 8551 | DISPATCHING_TIMEOUT)), |
| 8552 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 8553 | |
| 8554 | ASSERT_THAT(anrConnectionTokens, |
| 8555 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 8556 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8557 | |
| 8558 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8559 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8560 | |
| 8561 | mFocusedWindow->consumeMotionDown(); |
| 8562 | mFocusedWindow->consumeMotionUp(); |
| 8563 | mUnfocusedWindow->consumeMotionOutside(); |
| 8564 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8565 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 8566 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8567 | |
| 8568 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8569 | ASSERT_THAT(responsiveTokens, |
| 8570 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 8571 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8572 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8573 | } |
| 8574 | |
| 8575 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 8576 | // We should also log an error to account for the dropped event (not tested here). |
| 8577 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 8578 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 8579 | tapOnFocusedWindow(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8580 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8581 | // Receive the events, but don't respond |
| 8582 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 8583 | ASSERT_TRUE(downEventSequenceNum); |
| 8584 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 8585 | ASSERT_TRUE(upEventSequenceNum); |
| 8586 | const std::chrono::duration timeout = |
| 8587 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8588 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8589 | |
| 8590 | // Tap once again |
| 8591 | // 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] | 8592 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8593 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8594 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8595 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8596 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8597 | FOCUSED_WINDOW_LOCATION)); |
| 8598 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 8599 | // valid touch target |
| 8600 | mUnfocusedWindow->assertNoEvents(); |
| 8601 | |
| 8602 | // Consume the first tap |
| 8603 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 8604 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 8605 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8606 | // The second tap did not go to the focused window |
| 8607 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8608 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8609 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8610 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8611 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8612 | } |
| 8613 | |
| 8614 | // If you tap outside of all windows, there will not be ANR |
| 8615 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8616 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8617 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8618 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 8619 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8620 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8621 | } |
| 8622 | |
| 8623 | // Since the focused window is paused, tapping on it should not produce any events |
| 8624 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 8625 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8626 | mDispatcher->onWindowInfosChanged( |
| 8627 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8628 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8629 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8630 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8631 | FOCUSED_WINDOW_LOCATION)); |
| 8632 | |
| 8633 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 8634 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8635 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 8636 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8637 | |
| 8638 | mFocusedWindow->assertNoEvents(); |
| 8639 | mUnfocusedWindow->assertNoEvents(); |
| 8640 | } |
| 8641 | |
| 8642 | /** |
| 8643 | * 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] | 8644 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8645 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 8646 | * |
| 8647 | * Warning!!! |
| 8648 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 8649 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8650 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8651 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 8652 | * |
| 8653 | * If that value changes, this test should also change. |
| 8654 | */ |
| 8655 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 8656 | // Set a long ANR timeout to prevent it from triggering |
| 8657 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8658 | mDispatcher->onWindowInfosChanged( |
| 8659 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8660 | |
| 8661 | tapOnUnfocusedWindow(); |
| 8662 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8663 | ASSERT_TRUE(downSequenceNum); |
| 8664 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8665 | ASSERT_TRUE(upSequenceNum); |
| 8666 | // Don't finish the events yet, and send a key |
| 8667 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 8668 | // window even if motions are still being processed. |
| 8669 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8670 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8671 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8672 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8674 | // 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] | 8675 | // and the key remains pending, waiting for the touch events to be processed. |
| 8676 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 8677 | // under the hood. |
| 8678 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8679 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8680 | |
| 8681 | // 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] | 8682 | mFocusedWindow->setFocusable(false); |
| 8683 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8684 | mDispatcher->onWindowInfosChanged( |
| 8685 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8686 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8687 | |
| 8688 | // Focus events should precede the key events |
| 8689 | mUnfocusedWindow->consumeFocusEvent(true); |
| 8690 | mFocusedWindow->consumeFocusEvent(false); |
| 8691 | |
| 8692 | // Finish the tap events, which should unblock dispatcher |
| 8693 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 8694 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 8695 | |
| 8696 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 8697 | // can finally go to the newly focused "mUnfocusedWindow". |
| 8698 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8699 | mFocusedWindow->assertNoEvents(); |
| 8700 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8701 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8702 | } |
| 8703 | |
| 8704 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 8705 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 8706 | // The other window should not be affected by that. |
| 8707 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 8708 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8709 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8710 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8711 | {FOCUSED_WINDOW_LOCATION})); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8712 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8713 | |
| 8714 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8715 | mDispatcher->notifyMotion( |
| 8716 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8717 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8718 | |
| 8719 | const std::chrono::duration timeout = |
| 8720 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8721 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8722 | |
| 8723 | mUnfocusedWindow->consumeMotionDown(); |
| 8724 | mFocusedWindow->consumeMotionDown(); |
| 8725 | // Focused window may or may not receive ACTION_MOVE |
| 8726 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 8727 | InputEvent* event; |
| 8728 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 8729 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 8730 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 8731 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8732 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8733 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 8734 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 8735 | mFocusedWindow->consumeMotionCancel(); |
| 8736 | } else { |
| 8737 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 8738 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8739 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8740 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8741 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8742 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8743 | mUnfocusedWindow->assertNoEvents(); |
| 8744 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8745 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8746 | } |
| 8747 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8748 | /** |
| 8749 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 8750 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 8751 | * |
| 8752 | * If the user touches another application during this time, the key should be dropped. |
| 8753 | * Next, if a new focused window comes in, without toggling the focused application, |
| 8754 | * then no ANR should occur. |
| 8755 | * |
| 8756 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 8757 | * but in some cases the policy may not update the focused application. |
| 8758 | */ |
| 8759 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 8760 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 8761 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 8762 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8763 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 8764 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 8765 | mFocusedWindow->setFocusable(false); |
| 8766 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8767 | mDispatcher->onWindowInfosChanged( |
| 8768 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8769 | mFocusedWindow->consumeFocusEvent(false); |
| 8770 | |
| 8771 | // Send a key. The ANR timer should start because there is no focused window. |
| 8772 | // 'focusedApplication' will get blamed if this timer completes. |
| 8773 | // 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] | 8774 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8775 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8776 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8777 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8779 | |
| 8780 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 8781 | // then the injected touches won't cause the focused event to get dropped. |
| 8782 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 8783 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 8784 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 8785 | // For this test, it means that the key would get delivered to the window once it becomes |
| 8786 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8787 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8788 | |
| 8789 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8790 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8791 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8792 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8793 | |
| 8794 | // We do not consume the motion right away, because that would require dispatcher to first |
| 8795 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 8796 | // Set the focused window first. |
| 8797 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8798 | mDispatcher->onWindowInfosChanged( |
| 8799 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8800 | setFocusedWindow(mFocusedWindow); |
| 8801 | mFocusedWindow->consumeFocusEvent(true); |
| 8802 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 8803 | // to another application. This could be a bug / behaviour in the policy. |
| 8804 | |
| 8805 | mUnfocusedWindow->consumeMotionDown(); |
| 8806 | |
| 8807 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8808 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 8809 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 8810 | } |
| 8811 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8812 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 8813 | // that has feature NO_INPUT_CHANNEL. |
| 8814 | // Layout: |
| 8815 | // Top (closest to user) |
| 8816 | // mNoInputWindow (above all windows) |
| 8817 | // mBottomWindow |
| 8818 | // Bottom (furthest from user) |
| 8819 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 8820 | virtual void SetUp() override { |
| 8821 | InputDispatcherTest::SetUp(); |
| 8822 | |
| 8823 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8824 | mNoInputWindow = |
| 8825 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 8826 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8827 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8828 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8829 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8830 | // It's perfectly valid for this window to not have an associated input channel |
| 8831 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8832 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 8833 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8834 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8835 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8836 | mDispatcher->onWindowInfosChanged( |
| 8837 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8838 | } |
| 8839 | |
| 8840 | protected: |
| 8841 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 8842 | sp<FakeWindowHandle> mNoInputWindow; |
| 8843 | sp<FakeWindowHandle> mBottomWindow; |
| 8844 | }; |
| 8845 | |
| 8846 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 8847 | PointF touchedPoint = {10, 10}; |
| 8848 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8849 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8850 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8851 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8852 | |
| 8853 | mNoInputWindow->assertNoEvents(); |
| 8854 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 8855 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 8856 | // and therefore should prevent mBottomWindow from receiving touches |
| 8857 | mBottomWindow->assertNoEvents(); |
| 8858 | } |
| 8859 | |
| 8860 | /** |
| 8861 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 8862 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 8863 | */ |
| 8864 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 8865 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8866 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 8867 | "Window with input channel and NO_INPUT_CHANNEL", |
| 8868 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8869 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8870 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8871 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8872 | mDispatcher->onWindowInfosChanged( |
| 8873 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8874 | |
| 8875 | PointF touchedPoint = {10, 10}; |
| 8876 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8877 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8878 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8879 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8880 | |
| 8881 | mNoInputWindow->assertNoEvents(); |
| 8882 | mBottomWindow->assertNoEvents(); |
| 8883 | } |
| 8884 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8885 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 8886 | protected: |
| 8887 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8888 | sp<FakeWindowHandle> mWindow; |
| 8889 | sp<FakeWindowHandle> mMirror; |
| 8890 | |
| 8891 | virtual void SetUp() override { |
| 8892 | InputDispatcherTest::SetUp(); |
| 8893 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8894 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 8895 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 8896 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8897 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 8898 | mWindow->setFocusable(true); |
| 8899 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8900 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8901 | } |
| 8902 | }; |
| 8903 | |
| 8904 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 8905 | // Request focus on a mirrored window |
| 8906 | setFocusedWindow(mMirror); |
| 8907 | |
| 8908 | // window gets focused |
| 8909 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8911 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8912 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8913 | } |
| 8914 | |
| 8915 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 8916 | // focusable. |
| 8917 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 8918 | setFocusedWindow(mMirror); |
| 8919 | |
| 8920 | // window gets focused |
| 8921 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8922 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8923 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8924 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8926 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8927 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8928 | |
| 8929 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8930 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8931 | |
| 8932 | // window loses focus since one of the windows associated with the token in not focusable |
| 8933 | mWindow->consumeFocusEvent(false); |
| 8934 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8935 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8936 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8937 | mWindow->assertNoEvents(); |
| 8938 | } |
| 8939 | |
| 8940 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 8941 | // invisible. |
| 8942 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 8943 | setFocusedWindow(mMirror); |
| 8944 | |
| 8945 | // window gets focused |
| 8946 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8947 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8948 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8949 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8950 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8951 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8952 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8953 | |
| 8954 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8955 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8956 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8957 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8958 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8959 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8960 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8961 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8962 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8963 | |
| 8964 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8965 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8966 | |
| 8967 | // window loses focus only after all windows associated with the token become invisible. |
| 8968 | mWindow->consumeFocusEvent(false); |
| 8969 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8970 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8971 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8972 | mWindow->assertNoEvents(); |
| 8973 | } |
| 8974 | |
| 8975 | // A focused & mirrored window remains focused until both windows are removed. |
| 8976 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 8977 | setFocusedWindow(mMirror); |
| 8978 | |
| 8979 | // window gets focused |
| 8980 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8981 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8982 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8983 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8984 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8985 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8986 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8987 | |
| 8988 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8989 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8990 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8991 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8992 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8993 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8994 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8995 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8996 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8997 | |
| 8998 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8999 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9000 | mWindow->consumeFocusEvent(false); |
| 9001 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9002 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9003 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9004 | mWindow->assertNoEvents(); |
| 9005 | } |
| 9006 | |
| 9007 | // Focus request can be pending until one window becomes visible. |
| 9008 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 9009 | // Request focus on an invisible mirror. |
| 9010 | mWindow->setVisible(false); |
| 9011 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9012 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9013 | setFocusedWindow(mMirror); |
| 9014 | |
| 9015 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9017 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9018 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9019 | |
| 9020 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9021 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9022 | |
| 9023 | // window gets focused |
| 9024 | mWindow->consumeFocusEvent(true); |
| 9025 | // window gets the pending key event |
| 9026 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9027 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9028 | |
| 9029 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 9030 | protected: |
| 9031 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9032 | sp<FakeWindowHandle> mWindow; |
| 9033 | sp<FakeWindowHandle> mSecondWindow; |
| 9034 | |
| 9035 | void SetUp() override { |
| 9036 | InputDispatcherTest::SetUp(); |
| 9037 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9038 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9039 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9040 | mSecondWindow = |
| 9041 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9042 | mSecondWindow->setFocusable(true); |
| 9043 | |
| 9044 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9045 | mDispatcher->onWindowInfosChanged( |
| 9046 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9047 | |
| 9048 | setFocusedWindow(mWindow); |
| 9049 | mWindow->consumeFocusEvent(true); |
| 9050 | } |
| 9051 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9052 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9053 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9054 | } |
| 9055 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9056 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 9057 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9058 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9059 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 9060 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9061 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9062 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9063 | } |
| 9064 | }; |
| 9065 | |
| 9066 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 9067 | // Ensure that capture cannot be obtained for unfocused windows. |
| 9068 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 9069 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9070 | mSecondWindow->assertNoEvents(); |
| 9071 | |
| 9072 | // Ensure that capture can be enabled from the focus window. |
| 9073 | requestAndVerifyPointerCapture(mWindow, true); |
| 9074 | |
| 9075 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 9076 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 9077 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9078 | |
| 9079 | // Ensure that capture can be disabled from the window with capture. |
| 9080 | requestAndVerifyPointerCapture(mWindow, false); |
| 9081 | } |
| 9082 | |
| 9083 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9084 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9085 | |
| 9086 | setFocusedWindow(mSecondWindow); |
| 9087 | |
| 9088 | // Ensure that the capture disabled event was sent first. |
| 9089 | mWindow->consumeCaptureEvent(false); |
| 9090 | mWindow->consumeFocusEvent(false); |
| 9091 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9092 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9093 | |
| 9094 | // 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] | 9095 | notifyPointerCaptureChanged({}); |
| 9096 | notifyPointerCaptureChanged(request); |
| 9097 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9098 | mWindow->assertNoEvents(); |
| 9099 | mSecondWindow->assertNoEvents(); |
| 9100 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9101 | } |
| 9102 | |
| 9103 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9104 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9105 | |
| 9106 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9107 | notifyPointerCaptureChanged({}); |
| 9108 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9109 | |
| 9110 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9111 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9112 | mWindow->consumeCaptureEvent(false); |
| 9113 | mWindow->assertNoEvents(); |
| 9114 | } |
| 9115 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9116 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 9117 | requestAndVerifyPointerCapture(mWindow, true); |
| 9118 | |
| 9119 | // The first window loses focus. |
| 9120 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9121 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9122 | mWindow->consumeCaptureEvent(false); |
| 9123 | |
| 9124 | // Request Pointer Capture from the second window before the notification from InputReader |
| 9125 | // arrives. |
| 9126 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9127 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9128 | |
| 9129 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9130 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9131 | |
| 9132 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9133 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9134 | |
| 9135 | mSecondWindow->consumeFocusEvent(true); |
| 9136 | mSecondWindow->consumeCaptureEvent(true); |
| 9137 | } |
| 9138 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9139 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 9140 | // App repeatedly enables and disables capture. |
| 9141 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9142 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9143 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 9144 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 9145 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9146 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9147 | |
| 9148 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 9149 | // first request is now stale, this should do nothing. |
| 9150 | notifyPointerCaptureChanged(firstRequest); |
| 9151 | mWindow->assertNoEvents(); |
| 9152 | |
| 9153 | // InputReader notifies that the second request was enabled. |
| 9154 | notifyPointerCaptureChanged(secondRequest); |
| 9155 | mWindow->consumeCaptureEvent(true); |
| 9156 | } |
| 9157 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 9158 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 9159 | requestAndVerifyPointerCapture(mWindow, true); |
| 9160 | |
| 9161 | // App toggles pointer capture off and on. |
| 9162 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 9163 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 9164 | |
| 9165 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9166 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9167 | |
| 9168 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 9169 | // preceding "disable" request. |
| 9170 | notifyPointerCaptureChanged(enableRequest); |
| 9171 | |
| 9172 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 9173 | // any notifications. |
| 9174 | mWindow->assertNoEvents(); |
| 9175 | } |
| 9176 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9177 | /** |
| 9178 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 9179 | * mouse movements don't affect the previous mouse hovering state. |
| 9180 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 9181 | * HOVER_MOVE events). |
| 9182 | */ |
| 9183 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 9184 | // Mouse hover on the window |
| 9185 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 9186 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9187 | .build()); |
| 9188 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9189 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9190 | .build()); |
| 9191 | |
| 9192 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 9193 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 9194 | |
| 9195 | // Start pointer capture |
| 9196 | requestAndVerifyPointerCapture(mWindow, true); |
| 9197 | |
| 9198 | // Send some relative mouse movements and receive them in the window. |
| 9199 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 9200 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 9201 | .build()); |
| 9202 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 9203 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 9204 | |
| 9205 | // Stop pointer capture |
| 9206 | requestAndVerifyPointerCapture(mWindow, false); |
| 9207 | |
| 9208 | // Continue hovering on the window |
| 9209 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9210 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 9211 | .build()); |
| 9212 | mWindow->consumeMotionEvent( |
| 9213 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 9214 | |
| 9215 | mWindow->assertNoEvents(); |
| 9216 | } |
| 9217 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9218 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 9219 | protected: |
| 9220 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9221 | |
| 9222 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 9223 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 9224 | |
| 9225 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 9226 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9227 | |
| 9228 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 9229 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 9230 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9231 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 9232 | MAXIMUM_OBSCURING_OPACITY); |
| 9233 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9234 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 9235 | static constexpr gui::Uid APP_B_UID{10002}; |
| 9236 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9237 | |
| 9238 | sp<FakeWindowHandle> mTouchWindow; |
| 9239 | |
| 9240 | virtual void SetUp() override { |
| 9241 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9242 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9243 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 9244 | } |
| 9245 | |
| 9246 | virtual void TearDown() override { |
| 9247 | InputDispatcherTest::TearDown(); |
| 9248 | mTouchWindow.clear(); |
| 9249 | } |
| 9250 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9251 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 9252 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9253 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9254 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9255 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9256 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9257 | return window; |
| 9258 | } |
| 9259 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9260 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9261 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 9262 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9263 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9264 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9265 | 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] | 9266 | return window; |
| 9267 | } |
| 9268 | |
| 9269 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9270 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9271 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9272 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9273 | } |
| 9274 | }; |
| 9275 | |
| 9276 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9277 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9278 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9279 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9280 | |
| 9281 | touch(); |
| 9282 | |
| 9283 | mTouchWindow->assertNoEvents(); |
| 9284 | } |
| 9285 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9286 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9287 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 9288 | const sp<FakeWindowHandle>& w = |
| 9289 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9290 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9291 | |
| 9292 | touch(); |
| 9293 | |
| 9294 | mTouchWindow->assertNoEvents(); |
| 9295 | } |
| 9296 | |
| 9297 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9298 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 9299 | const sp<FakeWindowHandle>& w = |
| 9300 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9301 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9302 | |
| 9303 | touch(); |
| 9304 | |
| 9305 | w->assertNoEvents(); |
| 9306 | } |
| 9307 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9308 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9309 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9310 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9311 | |
| 9312 | touch(); |
| 9313 | |
| 9314 | mTouchWindow->consumeAnyMotionDown(); |
| 9315 | } |
| 9316 | |
| 9317 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9318 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9319 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9320 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9321 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9322 | |
| 9323 | touch({PointF{100, 100}}); |
| 9324 | |
| 9325 | mTouchWindow->consumeAnyMotionDown(); |
| 9326 | } |
| 9327 | |
| 9328 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9329 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9330 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9331 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9332 | |
| 9333 | touch(); |
| 9334 | |
| 9335 | mTouchWindow->consumeAnyMotionDown(); |
| 9336 | } |
| 9337 | |
| 9338 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 9339 | const sp<FakeWindowHandle>& w = |
| 9340 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9341 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9342 | |
| 9343 | touch(); |
| 9344 | |
| 9345 | mTouchWindow->consumeAnyMotionDown(); |
| 9346 | } |
| 9347 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9348 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 9349 | const sp<FakeWindowHandle>& w = |
| 9350 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9351 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9352 | |
| 9353 | touch(); |
| 9354 | |
| 9355 | w->assertNoEvents(); |
| 9356 | } |
| 9357 | |
| 9358 | /** |
| 9359 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 9360 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 9361 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 9362 | */ |
| 9363 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9364 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 9365 | const sp<FakeWindowHandle>& w = |
| 9366 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9367 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9368 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9369 | |
| 9370 | touch(); |
| 9371 | |
| 9372 | w->consumeMotionOutside(); |
| 9373 | } |
| 9374 | |
| 9375 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 9376 | const sp<FakeWindowHandle>& w = |
| 9377 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9378 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9379 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9380 | |
| 9381 | touch(); |
| 9382 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9383 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9384 | } |
| 9385 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9386 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9387 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9388 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9389 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9390 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9391 | |
| 9392 | touch(); |
| 9393 | |
| 9394 | mTouchWindow->consumeAnyMotionDown(); |
| 9395 | } |
| 9396 | |
| 9397 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 9398 | const sp<FakeWindowHandle>& w = |
| 9399 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9400 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9401 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9402 | |
| 9403 | touch(); |
| 9404 | |
| 9405 | mTouchWindow->consumeAnyMotionDown(); |
| 9406 | } |
| 9407 | |
| 9408 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9409 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9410 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9411 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9412 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9413 | |
| 9414 | touch(); |
| 9415 | |
| 9416 | mTouchWindow->assertNoEvents(); |
| 9417 | } |
| 9418 | |
| 9419 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 9420 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 9421 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9422 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 9423 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9424 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9425 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 9426 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9427 | mDispatcher->onWindowInfosChanged( |
| 9428 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9429 | |
| 9430 | touch(); |
| 9431 | |
| 9432 | mTouchWindow->assertNoEvents(); |
| 9433 | } |
| 9434 | |
| 9435 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 9436 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 9437 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9438 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 9439 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9440 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9441 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 9442 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9443 | mDispatcher->onWindowInfosChanged( |
| 9444 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9445 | |
| 9446 | touch(); |
| 9447 | |
| 9448 | mTouchWindow->consumeAnyMotionDown(); |
| 9449 | } |
| 9450 | |
| 9451 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9452 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 9453 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9454 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9455 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9456 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9457 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9458 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9459 | mDispatcher->onWindowInfosChanged( |
| 9460 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9461 | |
| 9462 | touch(); |
| 9463 | |
| 9464 | mTouchWindow->consumeAnyMotionDown(); |
| 9465 | } |
| 9466 | |
| 9467 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 9468 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9469 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9470 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9471 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9472 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9473 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9474 | mDispatcher->onWindowInfosChanged( |
| 9475 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9476 | |
| 9477 | touch(); |
| 9478 | |
| 9479 | mTouchWindow->assertNoEvents(); |
| 9480 | } |
| 9481 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9482 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9483 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 9484 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9485 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9486 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9487 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9488 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9489 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9490 | mDispatcher->onWindowInfosChanged( |
| 9491 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9492 | |
| 9493 | touch(); |
| 9494 | |
| 9495 | mTouchWindow->assertNoEvents(); |
| 9496 | } |
| 9497 | |
| 9498 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9499 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 9500 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9501 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9502 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9503 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9504 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9505 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9506 | mDispatcher->onWindowInfosChanged( |
| 9507 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9508 | |
| 9509 | touch(); |
| 9510 | |
| 9511 | mTouchWindow->consumeAnyMotionDown(); |
| 9512 | } |
| 9513 | |
| 9514 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 9515 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9516 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9517 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9518 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9519 | |
| 9520 | touch(); |
| 9521 | |
| 9522 | mTouchWindow->consumeAnyMotionDown(); |
| 9523 | } |
| 9524 | |
| 9525 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 9526 | const sp<FakeWindowHandle>& w = |
| 9527 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9528 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9529 | |
| 9530 | touch(); |
| 9531 | |
| 9532 | mTouchWindow->consumeAnyMotionDown(); |
| 9533 | } |
| 9534 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9535 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9536 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 9537 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 9538 | const sp<FakeWindowHandle>& w = |
| 9539 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9540 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9541 | |
| 9542 | touch(); |
| 9543 | |
| 9544 | mTouchWindow->assertNoEvents(); |
| 9545 | } |
| 9546 | |
| 9547 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 9548 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 9549 | const sp<FakeWindowHandle>& w = |
| 9550 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9551 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9552 | |
| 9553 | touch(); |
| 9554 | |
| 9555 | mTouchWindow->consumeAnyMotionDown(); |
| 9556 | } |
| 9557 | |
| 9558 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9559 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 9560 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 9561 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9562 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9563 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9564 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9565 | |
| 9566 | touch(); |
| 9567 | |
| 9568 | mTouchWindow->consumeAnyMotionDown(); |
| 9569 | } |
| 9570 | |
| 9571 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9572 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 9573 | const sp<FakeWindowHandle>& w1 = |
| 9574 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 9575 | OPACITY_BELOW_THRESHOLD); |
| 9576 | const sp<FakeWindowHandle>& w2 = |
| 9577 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9578 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9579 | mDispatcher->onWindowInfosChanged( |
| 9580 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9581 | |
| 9582 | touch(); |
| 9583 | |
| 9584 | mTouchWindow->assertNoEvents(); |
| 9585 | } |
| 9586 | |
| 9587 | /** |
| 9588 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 9589 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 9590 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 9591 | */ |
| 9592 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9593 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 9594 | const sp<FakeWindowHandle>& wB = |
| 9595 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 9596 | OPACITY_BELOW_THRESHOLD); |
| 9597 | const sp<FakeWindowHandle>& wC = |
| 9598 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9599 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9600 | mDispatcher->onWindowInfosChanged( |
| 9601 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9602 | |
| 9603 | touch(); |
| 9604 | |
| 9605 | mTouchWindow->assertNoEvents(); |
| 9606 | } |
| 9607 | |
| 9608 | /** |
| 9609 | * This test is testing that a window from a different UID but with same application token doesn't |
| 9610 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 9611 | */ |
| 9612 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9613 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 9614 | const sp<FakeWindowHandle>& w = |
| 9615 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 9616 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9617 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9618 | |
| 9619 | touch(); |
| 9620 | |
| 9621 | mTouchWindow->consumeAnyMotionDown(); |
| 9622 | } |
| 9623 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9624 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 9625 | protected: |
| 9626 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9627 | sp<FakeWindowHandle> mWindow; |
| 9628 | sp<FakeWindowHandle> mSecondWindow; |
| 9629 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9630 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9631 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 9632 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9633 | |
| 9634 | void SetUp() override { |
| 9635 | InputDispatcherTest::SetUp(); |
| 9636 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9637 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9638 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9639 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9640 | mSecondWindow = |
| 9641 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9642 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9643 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9644 | mSpyWindow = |
| 9645 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9646 | mSpyWindow->setSpy(true); |
| 9647 | mSpyWindow->setTrustedOverlay(true); |
| 9648 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 9649 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9650 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9651 | mDispatcher->onWindowInfosChanged( |
| 9652 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 9653 | {}, |
| 9654 | 0, |
| 9655 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9656 | } |
| 9657 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9658 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 9659 | switch (fromSource) { |
| 9660 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 9661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9662 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9663 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9664 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9665 | break; |
| 9666 | case AINPUT_SOURCE_STYLUS: |
| 9667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9668 | injectMotionEvent(*mDispatcher, |
| 9669 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9670 | AINPUT_SOURCE_STYLUS) |
| 9671 | .buttonState( |
| 9672 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 9673 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 9674 | .x(50) |
| 9675 | .y(50)) |
| 9676 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9677 | break; |
| 9678 | case AINPUT_SOURCE_MOUSE: |
| 9679 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9680 | injectMotionEvent(*mDispatcher, |
| 9681 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9682 | AINPUT_SOURCE_MOUSE) |
| 9683 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 9684 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 9685 | ToolType::MOUSE) |
| 9686 | .x(50) |
| 9687 | .y(50)) |
| 9688 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9689 | break; |
| 9690 | default: |
| 9691 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 9692 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9693 | |
| 9694 | // Window should receive motion event. |
| 9695 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9696 | // Spy window should also receive motion event |
| 9697 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9698 | } |
| 9699 | |
| 9700 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 9701 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 9702 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9703 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9704 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9705 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9706 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9707 | |
| 9708 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9709 | mDragWindow = |
| 9710 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9711 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9712 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 9713 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 9714 | {}, |
| 9715 | 0, |
| 9716 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9717 | |
| 9718 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9719 | bool transferred = |
| 9720 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9721 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9722 | if (transferred) { |
| 9723 | mWindow->consumeMotionCancel(); |
| 9724 | mDragWindow->consumeMotionDown(); |
| 9725 | } |
| 9726 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9727 | } |
| 9728 | }; |
| 9729 | |
| 9730 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9731 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9732 | |
| 9733 | // Move on window. |
| 9734 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9735 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9736 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9737 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9738 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9739 | mWindow->consumeDragEvent(false, 50, 50); |
| 9740 | mSecondWindow->assertNoEvents(); |
| 9741 | |
| 9742 | // Move to another window. |
| 9743 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9744 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9745 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9746 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9747 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9748 | mWindow->consumeDragEvent(true, 150, 50); |
| 9749 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9750 | |
| 9751 | // Move back to original window. |
| 9752 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9753 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9754 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9755 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9756 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9757 | mWindow->consumeDragEvent(false, 50, 50); |
| 9758 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 9759 | |
| 9760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9761 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9762 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9763 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9764 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9765 | mWindow->assertNoEvents(); |
| 9766 | mSecondWindow->assertNoEvents(); |
| 9767 | } |
| 9768 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9769 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9770 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9771 | |
| 9772 | // No cancel event after drag start |
| 9773 | mSpyWindow->assertNoEvents(); |
| 9774 | |
| 9775 | const MotionEvent secondFingerDownEvent = |
| 9776 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9777 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9778 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9779 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9780 | .build(); |
| 9781 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9782 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9783 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9784 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9785 | |
| 9786 | // Receives cancel for first pointer after next pointer down |
| 9787 | mSpyWindow->consumeMotionCancel(); |
| 9788 | mSpyWindow->consumeMotionDown(); |
| 9789 | |
| 9790 | mSpyWindow->assertNoEvents(); |
| 9791 | } |
| 9792 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9793 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9794 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9795 | |
| 9796 | // Move on window. |
| 9797 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9798 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9799 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9800 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9801 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9802 | mWindow->consumeDragEvent(false, 50, 50); |
| 9803 | mSecondWindow->assertNoEvents(); |
| 9804 | |
| 9805 | // Move to another window. |
| 9806 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9807 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9808 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9809 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9810 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9811 | mWindow->consumeDragEvent(true, 150, 50); |
| 9812 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9813 | |
| 9814 | // drop to another window. |
| 9815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9816 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9817 | {150, 50})) |
| 9818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9819 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9820 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9821 | mWindow->assertNoEvents(); |
| 9822 | mSecondWindow->assertNoEvents(); |
| 9823 | } |
| 9824 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 9825 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 9826 | startDrag(); |
| 9827 | |
| 9828 | // No cancel event after drag start |
| 9829 | mSpyWindow->assertNoEvents(); |
| 9830 | |
| 9831 | const MotionEvent secondFingerDownEvent = |
| 9832 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9833 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 9834 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9835 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 9836 | .build(); |
| 9837 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9838 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9839 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9840 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9841 | |
| 9842 | // Receives cancel for first pointer after next pointer down |
| 9843 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 9844 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 9845 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 9846 | |
| 9847 | mSpyWindow->assertNoEvents(); |
| 9848 | |
| 9849 | // Spy window calls pilfer pointers |
| 9850 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 9851 | mDragWindow->assertNoEvents(); |
| 9852 | |
| 9853 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 9854 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 9855 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 9856 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 9857 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 9858 | .build(); |
| 9859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 9860 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 9861 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9862 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9863 | |
| 9864 | // Drag window should still receive the new event |
| 9865 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 9866 | mDragWindow->assertNoEvents(); |
| 9867 | } |
| 9868 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9869 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9870 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9871 | |
| 9872 | // Move on window and keep button pressed. |
| 9873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9874 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9875 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 9876 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9877 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9878 | .build())) |
| 9879 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9880 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9881 | mWindow->consumeDragEvent(false, 50, 50); |
| 9882 | mSecondWindow->assertNoEvents(); |
| 9883 | |
| 9884 | // Move to another window and release button, expect to drop item. |
| 9885 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9886 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9887 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 9888 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9889 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9890 | .build())) |
| 9891 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9892 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9893 | mWindow->assertNoEvents(); |
| 9894 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9895 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9896 | |
| 9897 | // nothing to the window. |
| 9898 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9899 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9900 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 9901 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9902 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9903 | .build())) |
| 9904 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9905 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9906 | mWindow->assertNoEvents(); |
| 9907 | mSecondWindow->assertNoEvents(); |
| 9908 | } |
| 9909 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9910 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9911 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9912 | |
| 9913 | // Set second window invisible. |
| 9914 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9915 | mDispatcher->onWindowInfosChanged( |
| 9916 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9917 | |
| 9918 | // Move on window. |
| 9919 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9920 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9921 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9922 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9923 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9924 | mWindow->consumeDragEvent(false, 50, 50); |
| 9925 | mSecondWindow->assertNoEvents(); |
| 9926 | |
| 9927 | // Move to another window. |
| 9928 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9929 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9930 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9931 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9932 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9933 | mWindow->consumeDragEvent(true, 150, 50); |
| 9934 | mSecondWindow->assertNoEvents(); |
| 9935 | |
| 9936 | // drop to another window. |
| 9937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9938 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9939 | {150, 50})) |
| 9940 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9941 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9942 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9943 | mWindow->assertNoEvents(); |
| 9944 | mSecondWindow->assertNoEvents(); |
| 9945 | } |
| 9946 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9947 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9948 | // Ensure window could track pointerIds if it didn't support split touch. |
| 9949 | mWindow->setPreventSplitting(true); |
| 9950 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9951 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9952 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9953 | {50, 50})) |
| 9954 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9955 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9956 | |
| 9957 | const MotionEvent secondFingerDownEvent = |
| 9958 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9959 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9960 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9961 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9962 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9963 | .build(); |
| 9964 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9965 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9966 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9967 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9968 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9969 | |
| 9970 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9971 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9972 | } |
| 9973 | |
| 9974 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 9975 | // First down on second window. |
| 9976 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9977 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9978 | {150, 50})) |
| 9979 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9980 | |
| 9981 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9982 | |
| 9983 | // Second down on first window. |
| 9984 | const MotionEvent secondFingerDownEvent = |
| 9985 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9986 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9987 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9988 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9989 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9990 | .build(); |
| 9991 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9992 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9993 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9994 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9995 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9996 | |
| 9997 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9998 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9999 | |
| 10000 | // Move on window. |
| 10001 | const MotionEvent secondFingerMoveEvent = |
| 10002 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10003 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10004 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10005 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10006 | .build(); |
| 10007 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10008 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10009 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 10010 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10011 | mWindow->consumeDragEvent(false, 50, 50); |
| 10012 | mSecondWindow->consumeMotionMove(); |
| 10013 | |
| 10014 | // Release the drag pointer should perform drop. |
| 10015 | const MotionEvent secondFingerUpEvent = |
| 10016 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10017 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10018 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10019 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10020 | .build(); |
| 10021 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10022 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10023 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 10024 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10025 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10026 | mWindow->assertNoEvents(); |
| 10027 | mSecondWindow->consumeMotionMove(); |
| 10028 | } |
| 10029 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10030 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10031 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10032 | |
| 10033 | // Update window of second display. |
| 10034 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10035 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10036 | mDispatcher->onWindowInfosChanged( |
| 10037 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10038 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10039 | {}, |
| 10040 | 0, |
| 10041 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10042 | |
| 10043 | // Let second display has a touch state. |
| 10044 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10045 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10046 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10047 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10048 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10049 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10050 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 10051 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10052 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10053 | mDispatcher->onWindowInfosChanged( |
| 10054 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10055 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10056 | {}, |
| 10057 | 0, |
| 10058 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10059 | |
| 10060 | // Move on window. |
| 10061 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10062 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10063 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10064 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10065 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10066 | mWindow->consumeDragEvent(false, 50, 50); |
| 10067 | mSecondWindow->assertNoEvents(); |
| 10068 | |
| 10069 | // Move to another window. |
| 10070 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10071 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10072 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10073 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10074 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10075 | mWindow->consumeDragEvent(true, 150, 50); |
| 10076 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10077 | |
| 10078 | // drop to another window. |
| 10079 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10080 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10081 | {150, 50})) |
| 10082 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10083 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10084 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10085 | mWindow->assertNoEvents(); |
| 10086 | mSecondWindow->assertNoEvents(); |
| 10087 | } |
| 10088 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10089 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 10090 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 10091 | // Move on window. |
| 10092 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10093 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10094 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10095 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10096 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10097 | .x(50) |
| 10098 | .y(50)) |
| 10099 | .build())) |
| 10100 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10101 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10102 | mWindow->consumeDragEvent(false, 50, 50); |
| 10103 | mSecondWindow->assertNoEvents(); |
| 10104 | |
| 10105 | // Move to another window. |
| 10106 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10107 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10108 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10109 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10110 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10111 | .x(150) |
| 10112 | .y(50)) |
| 10113 | .build())) |
| 10114 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10115 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10116 | mWindow->consumeDragEvent(true, 150, 50); |
| 10117 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10118 | |
| 10119 | // drop to another window. |
| 10120 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10121 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10122 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 10123 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10124 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10125 | .x(150) |
| 10126 | .y(50)) |
| 10127 | .build())) |
| 10128 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10129 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10130 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10131 | mWindow->assertNoEvents(); |
| 10132 | mSecondWindow->assertNoEvents(); |
| 10133 | } |
| 10134 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 10135 | /** |
| 10136 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 10137 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 10138 | */ |
| 10139 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 10140 | // Down on second window |
| 10141 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10142 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10143 | {150, 50})) |
| 10144 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10145 | |
| 10146 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 10147 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 10148 | |
| 10149 | // Down on first window |
| 10150 | const MotionEvent secondFingerDownEvent = |
| 10151 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10152 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10153 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10154 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 10155 | .build(); |
| 10156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10157 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10158 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10159 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10160 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10161 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 10162 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 10163 | |
| 10164 | // Start drag on first window |
| 10165 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 10166 | |
| 10167 | // Trigger cancel |
| 10168 | mDispatcher->cancelCurrentTouch(); |
| 10169 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
| 10170 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel()); |
| 10171 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 10172 | |
| 10173 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10174 | // The D&D finished with nullptr |
| 10175 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 10176 | |
| 10177 | // Remove drag window |
| 10178 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 10179 | |
| 10180 | // Inject a simple gesture, ensure dispatcher not crashed |
| 10181 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10182 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10183 | PointF{50, 50})) |
| 10184 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10185 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10186 | |
| 10187 | const MotionEvent moveEvent = |
| 10188 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10189 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10190 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10191 | .build(); |
| 10192 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10193 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 10194 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10195 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10196 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 10197 | |
| 10198 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10199 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10200 | {50, 50})) |
| 10201 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10202 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 10203 | } |
| 10204 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 10205 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 10206 | // Start hovering over the window. |
| 10207 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10208 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
| 10209 | ADISPLAY_ID_DEFAULT, {50, 50})); |
| 10210 | |
| 10211 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10212 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10213 | |
| 10214 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 10215 | << "Drag and drop should not work with a hovering pointer"; |
| 10216 | } |
| 10217 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10218 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 10219 | |
| 10220 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 10221 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10222 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10223 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10224 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10225 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10226 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10227 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10228 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10229 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10230 | |
| 10231 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10232 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10233 | window->assertNoEvents(); |
| 10234 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10235 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10236 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10237 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10238 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 10239 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10240 | window->assertNoEvents(); |
| 10241 | |
| 10242 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10243 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10244 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10245 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10246 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10247 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10248 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10249 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10250 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10251 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10252 | window->assertNoEvents(); |
| 10253 | } |
| 10254 | |
| 10255 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 10256 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10257 | std::make_shared<FakeApplicationHandle>(); |
| 10258 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10259 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10260 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10261 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10262 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10263 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10264 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10265 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10266 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10267 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10268 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10269 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10270 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10271 | mDispatcher->onWindowInfosChanged( |
| 10272 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10273 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10274 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10275 | |
| 10276 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10277 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10278 | window->assertNoEvents(); |
| 10279 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10280 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10281 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10282 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10283 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10284 | window->assertNoEvents(); |
| 10285 | |
| 10286 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10287 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10288 | mDispatcher->onWindowInfosChanged( |
| 10289 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10290 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10291 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10292 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10293 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10294 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10295 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10296 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 10297 | window->assertNoEvents(); |
| 10298 | } |
| 10299 | |
| 10300 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 10301 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10302 | std::make_shared<FakeApplicationHandle>(); |
| 10303 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10304 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10305 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10306 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10307 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10308 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10309 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10310 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10311 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10312 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10313 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10314 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10315 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10316 | mDispatcher->onWindowInfosChanged( |
| 10317 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10318 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10319 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10320 | |
| 10321 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10322 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10323 | window->assertNoEvents(); |
| 10324 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10325 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10326 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10327 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10328 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10329 | window->assertNoEvents(); |
| 10330 | |
| 10331 | // 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] | 10332 | mDispatcher->onWindowInfosChanged( |
| 10333 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10334 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10335 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10336 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10337 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10338 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10339 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10340 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10341 | window->assertNoEvents(); |
| 10342 | } |
| 10343 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10344 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 10345 | protected: |
| 10346 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10347 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10348 | sp<FakeWindowHandle> mWindow; |
| 10349 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10350 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10351 | |
| 10352 | void SetUp() override { |
| 10353 | InputDispatcherTest::SetUp(); |
| 10354 | |
| 10355 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10356 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10357 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10358 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10359 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10360 | mSecondWindow = |
| 10361 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10362 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10363 | mThirdWindow = |
| 10364 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 10365 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 10366 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10367 | |
| 10368 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10369 | mDispatcher->onWindowInfosChanged( |
| 10370 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 10371 | {}, |
| 10372 | 0, |
| 10373 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10374 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10375 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10376 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10377 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10378 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10379 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10380 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 10381 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10382 | mThirdWindow->assertNoEvents(); |
| 10383 | } |
| 10384 | |
| 10385 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 10386 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10387 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10388 | SECOND_DISPLAY_ID)) { |
| 10389 | mWindow->assertNoEvents(); |
| 10390 | mSecondWindow->assertNoEvents(); |
| 10391 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10392 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10393 | } |
| 10394 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10395 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10396 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 10397 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 10398 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10399 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 10400 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10401 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10402 | } |
| 10403 | }; |
| 10404 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10405 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 10406 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10407 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 10408 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10409 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10410 | } |
| 10411 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10412 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 10413 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10414 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10415 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10416 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10417 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10418 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 10419 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10420 | mWindow->assertNoEvents(); |
| 10421 | mSecondWindow->assertNoEvents(); |
| 10422 | } |
| 10423 | |
| 10424 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 10425 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10426 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10427 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10428 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10429 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10430 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10431 | } |
| 10432 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10433 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 10434 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10435 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 10436 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10437 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10438 | mWindow->assertNoEvents(); |
| 10439 | mSecondWindow->assertNoEvents(); |
| 10440 | } |
| 10441 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10442 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 10443 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 10444 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 10445 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10446 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10447 | mWindow->assertNoEvents(); |
| 10448 | mSecondWindow->assertNoEvents(); |
| 10449 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 10450 | } |
| 10451 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10452 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 10453 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10454 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10455 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10456 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10457 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 10458 | |
| 10459 | // Then remove focus. |
| 10460 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10461 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10462 | |
| 10463 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 10464 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 10465 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 10466 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10467 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10468 | } |
| 10469 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10470 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 10471 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10472 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10473 | std::shared_ptr<FakeApplicationHandle> application = |
| 10474 | std::make_shared<FakeApplicationHandle>(); |
| 10475 | std::string name = "Fake Spy "; |
| 10476 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10477 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10478 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10479 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10480 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10481 | return spy; |
| 10482 | } |
| 10483 | |
| 10484 | sp<FakeWindowHandle> createForeground() { |
| 10485 | std::shared_ptr<FakeApplicationHandle> application = |
| 10486 | std::make_shared<FakeApplicationHandle>(); |
| 10487 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10488 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 10489 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10490 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10491 | return window; |
| 10492 | } |
| 10493 | |
| 10494 | private: |
| 10495 | int mSpyCount{0}; |
| 10496 | }; |
| 10497 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10498 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10499 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10500 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 10501 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10502 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 10503 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10504 | ScopedSilentDeath _silentDeath; |
| 10505 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10506 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10507 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10508 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10509 | ".* not a trusted overlay"); |
| 10510 | } |
| 10511 | |
| 10512 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10513 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 10514 | */ |
| 10515 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10516 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10517 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10518 | |
| 10519 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10520 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10521 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10522 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10523 | } |
| 10524 | |
| 10525 | /** |
| 10526 | * Verify the order in which different input windows receive events. The touched foreground window |
| 10527 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 10528 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 10529 | * receive events before ones belows it. |
| 10530 | * |
| 10531 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 10532 | * spy1, spy2, window, spy3. |
| 10533 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 10534 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 10535 | * window. |
| 10536 | */ |
| 10537 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 10538 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10539 | auto spy1 = createSpy(); |
| 10540 | auto spy2 = createSpy(); |
| 10541 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10542 | mDispatcher->onWindowInfosChanged( |
| 10543 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10544 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 10545 | const size_t numChannels = channels.size(); |
| 10546 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 10547 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10548 | if (!epollFd.ok()) { |
| 10549 | FAIL() << "Failed to create epoll fd"; |
| 10550 | } |
| 10551 | |
| 10552 | for (size_t i = 0; i < numChannels; i++) { |
| 10553 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 10554 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 10555 | FAIL() << "Failed to add fd to epoll"; |
| 10556 | } |
| 10557 | } |
| 10558 | |
| 10559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10560 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10561 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10562 | |
| 10563 | std::vector<size_t> eventOrder; |
| 10564 | std::vector<struct epoll_event> events(numChannels); |
| 10565 | for (;;) { |
| 10566 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 10567 | (100ms).count()); |
| 10568 | if (nFds < 0) { |
| 10569 | FAIL() << "Failed to call epoll_wait"; |
| 10570 | } |
| 10571 | if (nFds == 0) { |
| 10572 | break; // epoll_wait timed out |
| 10573 | } |
| 10574 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 10575 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 10576 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10577 | channels[i]->consumeMotionDown(); |
| 10578 | } |
| 10579 | } |
| 10580 | |
| 10581 | // Verify the order in which the events were received. |
| 10582 | EXPECT_EQ(3u, eventOrder.size()); |
| 10583 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 10584 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 10585 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 10586 | } |
| 10587 | |
| 10588 | /** |
| 10589 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 10590 | */ |
| 10591 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 10592 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10593 | auto spy = createSpy(); |
| 10594 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10595 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10596 | |
| 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)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10599 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10600 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10601 | spy->assertNoEvents(); |
| 10602 | } |
| 10603 | |
| 10604 | /** |
| 10605 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 10606 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 10607 | * to the window. |
| 10608 | */ |
| 10609 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 10610 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10611 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10612 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10613 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10614 | |
| 10615 | // Inject an event outside the spy window's touchable region. |
| 10616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10617 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10618 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10619 | window->consumeMotionDown(); |
| 10620 | spy->assertNoEvents(); |
| 10621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10622 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10623 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10624 | window->consumeMotionUp(); |
| 10625 | spy->assertNoEvents(); |
| 10626 | |
| 10627 | // Inject an event inside the spy window's touchable region. |
| 10628 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10629 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10630 | {5, 10})) |
| 10631 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10632 | window->consumeMotionDown(); |
| 10633 | spy->consumeMotionDown(); |
| 10634 | } |
| 10635 | |
| 10636 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10637 | * 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] | 10638 | * 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] | 10639 | */ |
| 10640 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 10641 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10642 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10643 | auto spy = createSpy(); |
| 10644 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10645 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10646 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10647 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10648 | |
| 10649 | // Inject an event outside the spy window's frame and touchable region. |
| 10650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10651 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10652 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10653 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10654 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10655 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10656 | } |
| 10657 | |
| 10658 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10659 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 10660 | * pointers that are down within its bounds. |
| 10661 | */ |
| 10662 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 10663 | auto windowLeft = createForeground(); |
| 10664 | windowLeft->setFrame({0, 0, 100, 200}); |
| 10665 | auto windowRight = createForeground(); |
| 10666 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10667 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10668 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10669 | mDispatcher->onWindowInfosChanged( |
| 10670 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10671 | |
| 10672 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10673 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10674 | {50, 50})) |
| 10675 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10676 | windowLeft->consumeMotionDown(); |
| 10677 | spy->consumeMotionDown(); |
| 10678 | |
| 10679 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10680 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10681 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10682 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10683 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10684 | .build(); |
| 10685 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10686 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10687 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10688 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10689 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10690 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10691 | } |
| 10692 | |
| 10693 | /** |
| 10694 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 10695 | * the spy should receive the second pointer with ACTION_DOWN. |
| 10696 | */ |
| 10697 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 10698 | auto window = createForeground(); |
| 10699 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10700 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10701 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10702 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10703 | |
| 10704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10705 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10706 | {50, 50})) |
| 10707 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10708 | window->consumeMotionDown(); |
| 10709 | spyRight->assertNoEvents(); |
| 10710 | |
| 10711 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10712 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10713 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10714 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10715 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10716 | .build(); |
| 10717 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10718 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10719 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10720 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10721 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10722 | spyRight->consumeMotionDown(); |
| 10723 | } |
| 10724 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10725 | /** |
| 10726 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 10727 | * windows should be allowed to control split touch. |
| 10728 | */ |
| 10729 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 10730 | // 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] | 10731 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10732 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 10733 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10734 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10735 | auto window = createForeground(); |
| 10736 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10737 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10738 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10739 | |
| 10740 | // First finger down, no window touched. |
| 10741 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10742 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10743 | {100, 200})) |
| 10744 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10745 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10746 | window->assertNoEvents(); |
| 10747 | |
| 10748 | // Second finger down on window, the window should receive touch down. |
| 10749 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10750 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10751 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10752 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10753 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10754 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10755 | .build(); |
| 10756 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10757 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10758 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10759 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10760 | |
| 10761 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10762 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10763 | } |
| 10764 | |
| 10765 | /** |
| 10766 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 10767 | * do not receive key events. |
| 10768 | */ |
| 10769 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10770 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10771 | spy->setFocusable(false); |
| 10772 | |
| 10773 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10774 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10775 | setFocusedWindow(window); |
| 10776 | window->consumeFocusEvent(true); |
| 10777 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10779 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10780 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 10781 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10782 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10783 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10784 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 10785 | |
| 10786 | spy->assertNoEvents(); |
| 10787 | } |
| 10788 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10789 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 10790 | |
| 10791 | /** |
| 10792 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 10793 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 10794 | */ |
| 10795 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 10796 | auto window = createForeground(); |
| 10797 | auto spy1 = createSpy(); |
| 10798 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10799 | mDispatcher->onWindowInfosChanged( |
| 10800 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10801 | |
| 10802 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10803 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10804 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10805 | window->consumeMotionDown(); |
| 10806 | spy1->consumeMotionDown(); |
| 10807 | spy2->consumeMotionDown(); |
| 10808 | |
| 10809 | // Pilfer pointers from the second spy window. |
| 10810 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 10811 | spy2->assertNoEvents(); |
| 10812 | spy1->consumeMotionCancel(); |
| 10813 | window->consumeMotionCancel(); |
| 10814 | |
| 10815 | // The rest of the gesture should only be sent to the second spy window. |
| 10816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10817 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10818 | ADISPLAY_ID_DEFAULT)) |
| 10819 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10820 | spy2->consumeMotionMove(); |
| 10821 | spy1->assertNoEvents(); |
| 10822 | window->assertNoEvents(); |
| 10823 | } |
| 10824 | |
| 10825 | /** |
| 10826 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 10827 | * in the middle of the gesture. |
| 10828 | */ |
| 10829 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 10830 | auto window = createForeground(); |
| 10831 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10832 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10833 | |
| 10834 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10835 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10836 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10837 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10838 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10839 | |
| 10840 | window->releaseChannel(); |
| 10841 | |
| 10842 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10843 | |
| 10844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10845 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10846 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10847 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10848 | } |
| 10849 | |
| 10850 | /** |
| 10851 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 10852 | * the spy, but not to any other windows. |
| 10853 | */ |
| 10854 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 10855 | auto spy = createSpy(); |
| 10856 | auto window = createForeground(); |
| 10857 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10858 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10859 | |
| 10860 | // First finger down on the window and the spy. |
| 10861 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10862 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10863 | {100, 200})) |
| 10864 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10865 | spy->consumeMotionDown(); |
| 10866 | window->consumeMotionDown(); |
| 10867 | |
| 10868 | // Spy window pilfers the pointers. |
| 10869 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10870 | window->consumeMotionCancel(); |
| 10871 | |
| 10872 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 10873 | const MotionEvent secondFingerDownEvent = |
| 10874 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10875 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10876 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10877 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10878 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10879 | .build(); |
| 10880 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10881 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10882 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10883 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10884 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10885 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10886 | |
| 10887 | // Third finger goes down outside all windows, so injection should fail. |
| 10888 | const MotionEvent thirdFingerDownEvent = |
| 10889 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10890 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10891 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10892 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10893 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 10894 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10895 | .build(); |
| 10896 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10897 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10898 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 10899 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10900 | |
| 10901 | spy->assertNoEvents(); |
| 10902 | window->assertNoEvents(); |
| 10903 | } |
| 10904 | |
| 10905 | /** |
| 10906 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 10907 | */ |
| 10908 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 10909 | auto spy = createSpy(); |
| 10910 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 10911 | auto window = createForeground(); |
| 10912 | window->setFrame(Rect(0, 0, 200, 200)); |
| 10913 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10914 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10915 | |
| 10916 | // First finger down on the window only |
| 10917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10918 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10919 | {150, 150})) |
| 10920 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10921 | window->consumeMotionDown(); |
| 10922 | |
| 10923 | // Second finger down on the spy and window |
| 10924 | const MotionEvent secondFingerDownEvent = |
| 10925 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10926 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10927 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10928 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 10929 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10930 | .build(); |
| 10931 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10932 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10933 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10934 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10935 | spy->consumeMotionDown(); |
| 10936 | window->consumeMotionPointerDown(1); |
| 10937 | |
| 10938 | // Third finger down on the spy and window |
| 10939 | const MotionEvent thirdFingerDownEvent = |
| 10940 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10941 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10942 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10943 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 10944 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 10945 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10946 | .build(); |
| 10947 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10948 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10949 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10950 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10951 | spy->consumeMotionPointerDown(1); |
| 10952 | window->consumeMotionPointerDown(2); |
| 10953 | |
| 10954 | // Spy window pilfers the pointers. |
| 10955 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 10956 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 10957 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10958 | |
| 10959 | spy->assertNoEvents(); |
| 10960 | window->assertNoEvents(); |
| 10961 | } |
| 10962 | |
| 10963 | /** |
| 10964 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 10965 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 10966 | * window, then that window should receive ACTION_CANCEL. |
| 10967 | */ |
| 10968 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 10969 | auto spy = createSpy(); |
| 10970 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 10971 | auto window = createForeground(); |
| 10972 | window->setFrame(Rect(0, 0, 200, 200)); |
| 10973 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10974 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10975 | |
| 10976 | // First finger down on both spy and window |
| 10977 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10978 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10979 | {10, 10})) |
| 10980 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10981 | window->consumeMotionDown(); |
| 10982 | spy->consumeMotionDown(); |
| 10983 | |
| 10984 | // Second finger down on the spy and window |
| 10985 | const MotionEvent secondFingerDownEvent = |
| 10986 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10987 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10988 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10989 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 10990 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10991 | .build(); |
| 10992 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10993 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10994 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10995 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10996 | spy->consumeMotionPointerDown(1); |
| 10997 | window->consumeMotionPointerDown(1); |
| 10998 | |
| 10999 | // Spy window pilfers the pointers. |
| 11000 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11001 | window->consumeMotionCancel(); |
| 11002 | |
| 11003 | spy->assertNoEvents(); |
| 11004 | window->assertNoEvents(); |
| 11005 | } |
| 11006 | |
| 11007 | /** |
| 11008 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 11009 | * be sent to other windows |
| 11010 | */ |
| 11011 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 11012 | auto spy = createSpy(); |
| 11013 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11014 | auto window = createForeground(); |
| 11015 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11016 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11017 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11018 | |
| 11019 | // First finger down on both window and spy |
| 11020 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11021 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11022 | {10, 10})) |
| 11023 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11024 | window->consumeMotionDown(); |
| 11025 | spy->consumeMotionDown(); |
| 11026 | |
| 11027 | // Spy window pilfers the pointers. |
| 11028 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11029 | window->consumeMotionCancel(); |
| 11030 | |
| 11031 | // Second finger down on the window only |
| 11032 | const MotionEvent secondFingerDownEvent = |
| 11033 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11034 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11035 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11036 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11037 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11038 | .build(); |
| 11039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11040 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11041 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11042 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11043 | window->consumeMotionDown(); |
| 11044 | window->assertNoEvents(); |
| 11045 | |
| 11046 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 11047 | spy->consumeMotionMove(); |
| 11048 | spy->assertNoEvents(); |
| 11049 | } |
| 11050 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11051 | /** |
| 11052 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 11053 | * windows, and spanning both left and right windows. |
| 11054 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 11055 | * to the right window. |
| 11056 | * Pilfer from spy window. |
| 11057 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 11058 | */ |
| 11059 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 11060 | sp<FakeWindowHandle> spy = createSpy(); |
| 11061 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 11062 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 11063 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 11064 | |
| 11065 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 11066 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 11067 | |
| 11068 | constexpr int32_t stylusDeviceId = 1; |
| 11069 | constexpr int32_t touchDeviceId = 2; |
| 11070 | |
| 11071 | mDispatcher->onWindowInfosChanged( |
| 11072 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 11073 | |
| 11074 | // Stylus down on left window and spy |
| 11075 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 11076 | .deviceId(stylusDeviceId) |
| 11077 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 11078 | .build()); |
| 11079 | leftWindow->consumeMotionEvent( |
| 11080 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11081 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11082 | |
| 11083 | // Finger down on right window and spy - but spy already has stylus |
| 11084 | mDispatcher->notifyMotion( |
| 11085 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11086 | .deviceId(touchDeviceId) |
| 11087 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 11088 | .build()); |
| 11089 | rightWindow->consumeMotionEvent( |
| 11090 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11091 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11092 | |
| 11093 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 11094 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11095 | leftWindow->consumeMotionEvent( |
| 11096 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11097 | rightWindow->consumeMotionEvent( |
| 11098 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 11099 | |
| 11100 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 11101 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11102 | .deviceId(stylusDeviceId) |
| 11103 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 11104 | .build()); |
| 11105 | mDispatcher->notifyMotion( |
| 11106 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 11107 | .deviceId(touchDeviceId) |
| 11108 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 11109 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11110 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11111 | |
| 11112 | spy->assertNoEvents(); |
| 11113 | leftWindow->assertNoEvents(); |
| 11114 | rightWindow->assertNoEvents(); |
| 11115 | } |
| 11116 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 11117 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 11118 | auto window = createForeground(); |
| 11119 | auto spy = createSpy(); |
| 11120 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 11121 | |
| 11122 | mDispatcher->notifyMotion( |
| 11123 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11124 | .deviceId(1) |
| 11125 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 11126 | .build()); |
| 11127 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11128 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11129 | |
| 11130 | // Pilfer pointers from the spy window should fail. |
| 11131 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11132 | spy->assertNoEvents(); |
| 11133 | window->assertNoEvents(); |
| 11134 | } |
| 11135 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11136 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 11137 | public: |
| 11138 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 11139 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11140 | std::make_shared<FakeApplicationHandle>(); |
| 11141 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11142 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11143 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11144 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11145 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11146 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11147 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11148 | overlay->setTrustedOverlay(true); |
| 11149 | |
| 11150 | std::shared_ptr<FakeApplicationHandle> application = |
| 11151 | std::make_shared<FakeApplicationHandle>(); |
| 11152 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11153 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 11154 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11155 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11156 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11157 | |
| 11158 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11159 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11160 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11161 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11162 | return {std::move(overlay), std::move(window)}; |
| 11163 | } |
| 11164 | |
| 11165 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11166 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11167 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11168 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11169 | } |
| 11170 | |
| 11171 | void sendStylusEvent(int32_t action) { |
| 11172 | NotifyMotionArgs motionArgs = |
| 11173 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 11174 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11175 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11176 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11177 | } |
| 11178 | }; |
| 11179 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11180 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 11181 | |
| 11182 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 11183 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11184 | ScopedSilentDeath _silentDeath; |
| 11185 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11186 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11187 | overlay->setTrustedOverlay(false); |
| 11188 | // 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] | 11189 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 11190 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11191 | ".* not a trusted overlay"); |
| 11192 | } |
| 11193 | |
| 11194 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 11195 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11196 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11197 | |
| 11198 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11199 | overlay->consumeMotionDown(); |
| 11200 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11201 | overlay->consumeMotionUp(); |
| 11202 | |
| 11203 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11204 | window->consumeMotionDown(); |
| 11205 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11206 | window->consumeMotionUp(); |
| 11207 | |
| 11208 | overlay->assertNoEvents(); |
| 11209 | window->assertNoEvents(); |
| 11210 | } |
| 11211 | |
| 11212 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 11213 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11214 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11215 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11216 | |
| 11217 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11218 | overlay->consumeMotionDown(); |
| 11219 | window->consumeMotionDown(); |
| 11220 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11221 | overlay->consumeMotionUp(); |
| 11222 | window->consumeMotionUp(); |
| 11223 | |
| 11224 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11225 | window->consumeMotionDown(); |
| 11226 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11227 | window->consumeMotionUp(); |
| 11228 | |
| 11229 | overlay->assertNoEvents(); |
| 11230 | window->assertNoEvents(); |
| 11231 | } |
| 11232 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11233 | /** |
| 11234 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 11235 | * The scenario is as follows: |
| 11236 | * - The stylus interceptor overlay is configured as a spy window. |
| 11237 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 11238 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 11239 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 11240 | */ |
| 11241 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 11242 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11243 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11244 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11245 | |
| 11246 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11247 | overlay->consumeMotionDown(); |
| 11248 | window->consumeMotionDown(); |
| 11249 | |
| 11250 | // The interceptor pilfers the pointers. |
| 11251 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 11252 | window->consumeMotionCancel(); |
| 11253 | |
| 11254 | // The interceptor configures itself so that it is no longer a spy. |
| 11255 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11256 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11257 | |
| 11258 | // It continues to receive the rest of the stylus gesture. |
| 11259 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 11260 | overlay->consumeMotionMove(); |
| 11261 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11262 | overlay->consumeMotionUp(); |
| 11263 | |
| 11264 | window->assertNoEvents(); |
| 11265 | } |
| 11266 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11267 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11268 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11269 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11270 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 11271 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 11272 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11273 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11274 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 11275 | |
| 11276 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11277 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11278 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 11279 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 11280 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 11281 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 11282 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 11283 | } |
| 11284 | |
| 11285 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11286 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11287 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11288 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11289 | mPolicyFlags); |
| 11290 | } |
| 11291 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11292 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11293 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11294 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11295 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11296 | name, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11297 | window->setOwnerInfo(mPid, mUid); |
| 11298 | return window; |
| 11299 | } |
| 11300 | }; |
| 11301 | |
| 11302 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 11303 | |
| 11304 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11305 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11306 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11307 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11308 | |
| 11309 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11310 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11311 | window->consumeMotionDown(); |
| 11312 | |
| 11313 | setFocusedWindow(window); |
| 11314 | window->consumeFocusEvent(true); |
| 11315 | |
| 11316 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11317 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11318 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11319 | } |
| 11320 | |
| 11321 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11322 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11323 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11324 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11325 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11326 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11327 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11328 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11329 | |
| 11330 | setFocusedWindow(window); |
| 11331 | window->consumeFocusEvent(true); |
| 11332 | |
| 11333 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11334 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11335 | window->assertNoEvents(); |
| 11336 | } |
| 11337 | |
| 11338 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11339 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11340 | auto window = owner.createWindow("Owned window"); |
| 11341 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11342 | spy->setSpy(true); |
| 11343 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11344 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11345 | |
| 11346 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11347 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11348 | spy->consumeMotionDown(); |
| 11349 | window->consumeMotionDown(); |
| 11350 | } |
| 11351 | |
| 11352 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11353 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11354 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11355 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11356 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11357 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11358 | randosSpy->setSpy(true); |
| 11359 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11360 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11361 | |
| 11362 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 11363 | // not receive the event. |
| 11364 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11365 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11366 | randosSpy->assertNoEvents(); |
| 11367 | window->consumeMotionDown(); |
| 11368 | } |
| 11369 | |
| 11370 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11371 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11372 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11373 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11374 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11375 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11376 | randosSpy->setSpy(true); |
| 11377 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11378 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11379 | |
| 11380 | // A user that has injection permission can inject into any window. |
| 11381 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11382 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11383 | ADISPLAY_ID_DEFAULT)); |
| 11384 | randosSpy->consumeMotionDown(); |
| 11385 | window->consumeMotionDown(); |
| 11386 | |
| 11387 | setFocusedWindow(randosSpy); |
| 11388 | randosSpy->consumeFocusEvent(true); |
| 11389 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11390 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11391 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11392 | window->assertNoEvents(); |
| 11393 | } |
| 11394 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11395 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11396 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11397 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11398 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11399 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11400 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11401 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 11402 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11403 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11404 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11405 | // 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] | 11406 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11407 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11408 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11409 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11410 | } |
| 11411 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 11412 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 11413 | |
| 11414 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 11415 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 11416 | |
| 11417 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 11418 | ADISPLAY_ID_DEFAULT); |
| 11419 | left->setFrame(Rect(0, 0, 100, 100)); |
| 11420 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11421 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 11422 | right->setFrame(Rect(100, 0, 200, 100)); |
| 11423 | sp<FakeWindowHandle> spy = |
| 11424 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 11425 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 11426 | spy->setTrustedOverlay(true); |
| 11427 | spy->setSpy(true); |
| 11428 | |
| 11429 | mDispatcher->onWindowInfosChanged( |
| 11430 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 11431 | |
| 11432 | // Hover into the left window. |
| 11433 | mDispatcher->notifyMotion( |
| 11434 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 11435 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 11436 | .build()); |
| 11437 | |
| 11438 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11439 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11440 | |
| 11441 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11442 | /*pointerId=*/0)); |
| 11443 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11444 | /*pointerId=*/0)); |
| 11445 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11446 | /*pointerId=*/0)); |
| 11447 | |
| 11448 | // Hover move to the right window. |
| 11449 | mDispatcher->notifyMotion( |
| 11450 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 11451 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 11452 | .build()); |
| 11453 | |
| 11454 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 11455 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11456 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 11457 | |
| 11458 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11459 | /*pointerId=*/0)); |
| 11460 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11461 | /*pointerId=*/0)); |
| 11462 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11463 | /*pointerId=*/0)); |
| 11464 | |
| 11465 | // Stop hovering. |
| 11466 | mDispatcher->notifyMotion( |
| 11467 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 11468 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 11469 | .build()); |
| 11470 | |
| 11471 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 11472 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 11473 | |
| 11474 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11475 | /*pointerId=*/0)); |
| 11476 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11477 | /*pointerId=*/0)); |
| 11478 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11479 | /*pointerId=*/0)); |
| 11480 | } |
| 11481 | |
| 11482 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 11483 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 11484 | |
| 11485 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 11486 | ADISPLAY_ID_DEFAULT); |
| 11487 | left->setFrame(Rect(0, 0, 100, 100)); |
| 11488 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11489 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 11490 | right->setFrame(Rect(100, 0, 200, 100)); |
| 11491 | sp<FakeWindowHandle> spy = |
| 11492 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 11493 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 11494 | spy->setTrustedOverlay(true); |
| 11495 | spy->setSpy(true); |
| 11496 | |
| 11497 | mDispatcher->onWindowInfosChanged( |
| 11498 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 11499 | |
| 11500 | // First pointer down on left window. |
| 11501 | mDispatcher->notifyMotion( |
| 11502 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11503 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11504 | .build()); |
| 11505 | |
| 11506 | left->consumeMotionDown(); |
| 11507 | spy->consumeMotionDown(); |
| 11508 | |
| 11509 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11510 | /*pointerId=*/0)); |
| 11511 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11512 | /*pointerId=*/0)); |
| 11513 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11514 | /*pointerId=*/0)); |
| 11515 | |
| 11516 | // Second pointer down on right window. |
| 11517 | mDispatcher->notifyMotion( |
| 11518 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11519 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11520 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 11521 | .build()); |
| 11522 | |
| 11523 | left->consumeMotionMove(); |
| 11524 | right->consumeMotionDown(); |
| 11525 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 11526 | |
| 11527 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11528 | /*pointerId=*/0)); |
| 11529 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11530 | /*pointerId=*/0)); |
| 11531 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11532 | /*pointerId=*/0)); |
| 11533 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11534 | /*pointerId=*/1)); |
| 11535 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11536 | /*pointerId=*/1)); |
| 11537 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11538 | /*pointerId=*/1)); |
| 11539 | |
| 11540 | // Second pointer up. |
| 11541 | mDispatcher->notifyMotion( |
| 11542 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 11543 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11544 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 11545 | .build()); |
| 11546 | |
| 11547 | left->consumeMotionMove(); |
| 11548 | right->consumeMotionUp(); |
| 11549 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 11550 | |
| 11551 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11552 | /*pointerId=*/0)); |
| 11553 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11554 | /*pointerId=*/0)); |
| 11555 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11556 | /*pointerId=*/0)); |
| 11557 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11558 | /*pointerId=*/1)); |
| 11559 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11560 | /*pointerId=*/1)); |
| 11561 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11562 | /*pointerId=*/1)); |
| 11563 | |
| 11564 | // First pointer up. |
| 11565 | mDispatcher->notifyMotion( |
| 11566 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 11567 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11568 | .build()); |
| 11569 | |
| 11570 | left->consumeMotionUp(); |
| 11571 | spy->consumeMotionUp(); |
| 11572 | |
| 11573 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11574 | /*pointerId=*/0)); |
| 11575 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11576 | /*pointerId=*/0)); |
| 11577 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11578 | /*pointerId=*/0)); |
| 11579 | } |
| 11580 | |
| 11581 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 11582 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 11583 | |
| 11584 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 11585 | ADISPLAY_ID_DEFAULT); |
| 11586 | left->setFrame(Rect(0, 0, 100, 100)); |
| 11587 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11588 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 11589 | right->setFrame(Rect(100, 0, 200, 100)); |
| 11590 | |
| 11591 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 11592 | |
| 11593 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11594 | /*pointerId=*/0)); |
| 11595 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11596 | /*pointerId=*/0)); |
| 11597 | |
| 11598 | // Hover move into the window. |
| 11599 | mDispatcher->notifyMotion( |
| 11600 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11601 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 11602 | .rawXCursorPosition(50) |
| 11603 | .rawYCursorPosition(50) |
| 11604 | .deviceId(DEVICE_ID) |
| 11605 | .build()); |
| 11606 | |
| 11607 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11608 | |
| 11609 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11610 | /*pointerId=*/0)); |
| 11611 | |
| 11612 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 11613 | mDispatcher->notifyMotion( |
| 11614 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11615 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 11616 | .rawXCursorPosition(51) |
| 11617 | .rawYCursorPosition(50) |
| 11618 | .deviceId(SECOND_DEVICE_ID) |
| 11619 | .build()); |
| 11620 | |
| 11621 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 11622 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11623 | |
| 11624 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 11625 | // a HOVER_EXIT from the first device. |
| 11626 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11627 | /*pointerId=*/0)); |
| 11628 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 11629 | SECOND_DEVICE_ID, |
| 11630 | /*pointerId=*/0)); |
| 11631 | |
| 11632 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 11633 | // receive HOVER_EXIT even though the mouse left the window. |
| 11634 | mDispatcher->notifyMotion( |
| 11635 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11636 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 11637 | .rawXCursorPosition(150) |
| 11638 | .rawYCursorPosition(50) |
| 11639 | .deviceId(SECOND_DEVICE_ID) |
| 11640 | .build()); |
| 11641 | |
| 11642 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 11643 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11644 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11645 | /*pointerId=*/0)); |
| 11646 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 11647 | SECOND_DEVICE_ID, |
| 11648 | /*pointerId=*/0)); |
| 11649 | } |
| 11650 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 11651 | } // namespace android::inputdispatcher |