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" |
Prabir Pradhan | e3b28dd | 2023-10-06 04:19:29 +0000 | [diff] [blame] | 19 | #include "TestEventMatchers.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 20 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 21 | #include <NotifyArgsBuilders.h> |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 22 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 23 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 25 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 26 | #include <binder/Binder.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 27 | #include <com_android_input_flags.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 28 | #include <fcntl.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 29 | #include <flag_macros.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 30 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 31 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 32 | #include <input/Input.h> |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 33 | #include <input/PrintTools.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 35 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 36 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 37 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 38 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 39 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 40 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 41 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 42 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 44 | using android::gui::FocusRequest; |
| 45 | using android::gui::TouchOcclusionMode; |
| 46 | using android::gui::WindowInfo; |
| 47 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 48 | using android::os::InputEventInjectionResult; |
| 49 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 50 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 51 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 53 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 54 | using testing::AllOf; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 55 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 56 | namespace { |
| 57 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 59 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 60 | |
| 61 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 62 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 63 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 65 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 66 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 67 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 68 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 69 | // Ensure common actions are interchangeable between keys and motions for convenience. |
| 70 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); |
| 71 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 72 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 73 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 74 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 75 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 76 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 77 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 78 | static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 79 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 80 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 81 | /** |
| 82 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 83 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 84 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 85 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 86 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 87 | * pointer id=0 leaves but the pointer id=1 remains. |
| 88 | */ |
| 89 | static constexpr int32_t POINTER_0_DOWN = |
| 90 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 91 | static constexpr int32_t POINTER_1_DOWN = |
| 92 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 93 | static constexpr int32_t POINTER_2_DOWN = |
| 94 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 95 | static constexpr int32_t POINTER_3_DOWN = |
| 96 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 97 | static constexpr int32_t POINTER_0_UP = |
| 98 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 99 | static constexpr int32_t POINTER_1_UP = |
| 100 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 101 | static constexpr int32_t POINTER_2_UP = |
| 102 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 103 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 104 | // 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] | 105 | static constexpr gui::Pid WINDOW_PID{999}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 106 | static constexpr gui::Uid WINDOW_UID{1001}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 107 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 108 | // 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] | 109 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 110 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 111 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 112 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 113 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 114 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 115 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 116 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 117 | /** |
| 118 | * If we expect to receive the event, the timeout can be made very long. When the test are running |
| 119 | * correctly, we will actually never wait until the end of the timeout because the wait will end |
| 120 | * when the event comes in. Still, this value shouldn't be infinite. During development, a local |
| 121 | * change may cause the test to fail. This timeout should be short enough to not annoy so that the |
| 122 | * developer can see the failure quickly (on human scale). |
| 123 | */ |
| 124 | static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms; |
| 125 | /** |
| 126 | * When no event is expected, we can have a very short timeout. A large value here would slow down |
| 127 | * the tests. In the unlikely event of system being too slow, the event may still be present but the |
| 128 | * timeout would complete before it is consumed. This would result in test flakiness. If this |
| 129 | * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this |
| 130 | * would get noticed and addressed quickly. |
| 131 | */ |
| 132 | static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms; |
| 133 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 134 | static constexpr int expectedWallpaperFlags = |
| 135 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 136 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 137 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 138 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 139 | /** |
| 140 | * Return a DOWN key event with KEYCODE_A. |
| 141 | */ |
| 142 | static KeyEvent getTestKeyEvent() { |
| 143 | KeyEvent event; |
| 144 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 145 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 146 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 147 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 148 | return event; |
| 149 | } |
| 150 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 151 | // --- FakeInputDispatcherPolicy --- |
| 152 | |
| 153 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 154 | struct AnrResult { |
| 155 | sp<IBinder> token{}; |
| 156 | gui::Pid pid{gui::Pid::INVALID}; |
| 157 | }; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 158 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 159 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 160 | FakeInputDispatcherPolicy() = default; |
| 161 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 162 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 163 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 164 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 165 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 166 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 167 | |
| 168 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 169 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 170 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 171 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 174 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 175 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 176 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 177 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 178 | |
| 179 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 180 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 181 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 182 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 183 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 184 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 185 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 186 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 189 | void assertFilterInputEventWasNotCalled() { |
| 190 | std::scoped_lock lock(mLock); |
| 191 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 192 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 193 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 194 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 195 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 196 | ASSERT_TRUE(mConfigurationChangedTime) |
| 197 | << "Timed out waiting for configuration changed call"; |
| 198 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 199 | mConfigurationChangedTime = std::nullopt; |
| 200 | } |
| 201 | |
| 202 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 203 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 204 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 205 | // 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] | 206 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 207 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 208 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 209 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 210 | mLastNotifySwitch = std::nullopt; |
| 211 | } |
| 212 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 213 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 214 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 215 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 216 | mOnPointerDownToken.clear(); |
| 217 | } |
| 218 | |
| 219 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 220 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 221 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 222 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 225 | // This function must be called soon after the expected ANR timer starts, |
| 226 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 227 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 228 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 229 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 230 | std::unique_lock lock(mLock); |
| 231 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 232 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 233 | ASSERT_NO_FATAL_FAILURE( |
| 234 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 235 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 238 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 239 | const sp<WindowInfoHandle>& window) { |
| 240 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 241 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 242 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 243 | } |
| 244 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 245 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 246 | const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 247 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 248 | std::unique_lock lock(mLock); |
| 249 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 250 | AnrResult result; |
| 251 | ASSERT_NO_FATAL_FAILURE(result = |
| 252 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 253 | ASSERT_EQ(expectedToken, result.token); |
| 254 | ASSERT_EQ(expectedPid, result.pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 255 | } |
| 256 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 257 | /** 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] | 258 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 259 | std::unique_lock lock(mLock); |
| 260 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 261 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 262 | const auto& [token, _] = result; |
| 263 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 266 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 267 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 268 | std::unique_lock lock(mLock); |
| 269 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 270 | AnrResult result; |
| 271 | ASSERT_NO_FATAL_FAILURE( |
| 272 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 273 | ASSERT_EQ(expectedToken, result.token); |
| 274 | ASSERT_EQ(expectedPid, result.pid); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** 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] | 278 | sp<IBinder> getResponsiveWindowToken() { |
| 279 | std::unique_lock lock(mLock); |
| 280 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 281 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 282 | const auto& [token, _] = result; |
| 283 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void assertNotifyAnrWasNotCalled() { |
| 287 | std::scoped_lock lock(mLock); |
| 288 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 289 | ASSERT_TRUE(mAnrWindows.empty()); |
| 290 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 291 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 292 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 295 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 296 | std::unique_lock lock(mLock); |
| 297 | base::ScopedLockAssertion assumeLocked(mLock); |
| 298 | |
| 299 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 300 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 301 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 302 | enabled; |
| 303 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 304 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 305 | << ") to be called."; |
| 306 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 307 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 308 | auto request = *mPointerCaptureRequest; |
| 309 | mPointerCaptureRequest.reset(); |
| 310 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void assertSetPointerCaptureNotCalled() { |
| 314 | std::unique_lock lock(mLock); |
| 315 | base::ScopedLockAssertion assumeLocked(mLock); |
| 316 | |
| 317 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 318 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 319 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 320 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 321 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 322 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 325 | void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, |
| 326 | const sp<IBinder>& targetToken) { |
| 327 | dispatcher.waitForIdle(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 328 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 329 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 330 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 331 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 334 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 335 | std::unique_lock lock(mLock); |
| 336 | base::ScopedLockAssertion assumeLocked(mLock); |
| 337 | std::optional<sp<IBinder>> receivedToken = |
| 338 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 339 | mNotifyInputChannelBroken); |
| 340 | ASSERT_TRUE(receivedToken.has_value()); |
| 341 | ASSERT_EQ(token, *receivedToken); |
| 342 | } |
| 343 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 344 | /** |
| 345 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 346 | */ |
| 347 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 348 | mInterceptKeyTimeout = timeout; |
| 349 | } |
| 350 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 351 | void assertUserActivityPoked() { |
| 352 | std::scoped_lock lock(mLock); |
| 353 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; |
| 354 | } |
| 355 | |
| 356 | void assertUserActivityNotPoked() { |
| 357 | std::scoped_lock lock(mLock); |
| 358 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; |
| 359 | } |
| 360 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 361 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 362 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 363 | } |
| 364 | |
| 365 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 366 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 367 | } |
| 368 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 369 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 370 | std::mutex mLock; |
| 371 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 372 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 373 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 374 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 375 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 376 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 377 | |
| 378 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 379 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 380 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 381 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 382 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 383 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 384 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 385 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 386 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 387 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 388 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 389 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 390 | bool mPokedUserActivity GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 391 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 392 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 393 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 394 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 395 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 396 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 397 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 398 | // first entry from the container and erase it. |
| 399 | template <class T> |
| 400 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 401 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 402 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 403 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 404 | // before checking if ANR was called. |
| 405 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 406 | // to provide it some time to act. 100ms seems reasonable. |
| 407 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 408 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 409 | std::optional<T> token = |
| 410 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 411 | if (!token.has_value()) { |
| 412 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 413 | return {}; |
| 414 | } |
| 415 | |
| 416 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 417 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 418 | // the dispatcher started counting before this function was called |
| 419 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 420 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 421 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 422 | << "ms, but waited " |
| 423 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 424 | << "ms instead"; |
| 425 | } |
| 426 | return *token; |
| 427 | } |
| 428 | |
| 429 | template <class T> |
| 430 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 431 | std::queue<T>& storage, |
| 432 | std::unique_lock<std::mutex>& lock, |
| 433 | std::condition_variable& condition) |
| 434 | REQUIRES(mLock) { |
| 435 | condition.wait_for(lock, timeout, |
| 436 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 437 | if (storage.empty()) { |
| 438 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 439 | return std::nullopt; |
| 440 | } |
| 441 | T item = storage.front(); |
| 442 | storage.pop(); |
| 443 | return std::make_optional(item); |
| 444 | } |
| 445 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 446 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 447 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 448 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 451 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 452 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 453 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 454 | ASSERT_TRUE(pid.has_value()); |
| 455 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 456 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 459 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 460 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 461 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 462 | ASSERT_TRUE(pid.has_value()); |
| 463 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 464 | mNotifyAnr.notify_all(); |
| 465 | } |
| 466 | |
| 467 | void notifyNoFocusedWindowAnr( |
| 468 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 469 | std::scoped_lock lock(mLock); |
| 470 | mAnrApplications.push(applicationHandle); |
| 471 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 474 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 475 | std::scoped_lock lock(mLock); |
| 476 | mBrokenInputChannels.push(connectionToken); |
| 477 | mNotifyInputChannelBroken.notify_all(); |
| 478 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 479 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 480 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 481 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 482 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 483 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 484 | const std::vector<float>& values) override {} |
| 485 | |
| 486 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 487 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 488 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 489 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 490 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 491 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 492 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 493 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 494 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 495 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 496 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 497 | break; |
| 498 | } |
| 499 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 500 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 501 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 502 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 503 | break; |
| 504 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 505 | default: { |
| 506 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 507 | break; |
| 508 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 509 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 510 | return true; |
| 511 | } |
| 512 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 513 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 514 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 515 | // Clear intercept state when we handled the event. |
| 516 | mInterceptKeyTimeout = 0ms; |
| 517 | } |
| 518 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 519 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 520 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 521 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 522 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 523 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 524 | // Clear intercept state so we could dispatch the event in next wake. |
| 525 | mInterceptKeyTimeout = 0ms; |
| 526 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 529 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&, |
| 530 | uint32_t) override { |
| 531 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 534 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 535 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 536 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 537 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 538 | * essentially a passthrough for notifySwitch. |
| 539 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 540 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 543 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { |
| 544 | std::scoped_lock lock(mLock); |
| 545 | mPokedUserActivity = true; |
| 546 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 547 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 548 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 549 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 550 | mOnPointerDownToken = newToken; |
| 551 | } |
| 552 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 553 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 554 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 555 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 556 | mPointerCaptureChangedCondition.notify_all(); |
| 557 | } |
| 558 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 559 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 560 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 561 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 562 | mDropTargetWindowToken = token; |
| 563 | } |
| 564 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 565 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 566 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 567 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 568 | } |
| 569 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 570 | void assertFilterInputEventWasCalledInternal( |
| 571 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 572 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 573 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 574 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 575 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 576 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 577 | }; |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 578 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 579 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | // --- InputDispatcherTest --- |
| 581 | |
| 582 | class InputDispatcherTest : public testing::Test { |
| 583 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 584 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 585 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 586 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 587 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 588 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
| 589 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 590 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 591 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 592 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 595 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 596 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 597 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 598 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 599 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 600 | |
| 601 | /** |
| 602 | * Used for debugging when writing the test |
| 603 | */ |
| 604 | void dumpDispatcherState() { |
| 605 | std::string dump; |
| 606 | mDispatcher->dump(dump); |
| 607 | std::stringstream ss(dump); |
| 608 | std::string to; |
| 609 | |
| 610 | while (std::getline(ss, to, '\n')) { |
| 611 | ALOGE("%s", to.c_str()); |
| 612 | } |
| 613 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 614 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 615 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 616 | FocusRequest request; |
| 617 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 618 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 619 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 620 | request.displayId = window->getInfo()->displayId; |
| 621 | mDispatcher->setFocusedWindow(request); |
| 622 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | }; |
| 624 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 625 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 626 | KeyEvent event; |
| 627 | |
| 628 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 629 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 630 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 631 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 632 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 633 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 634 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 635 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 636 | << "Should reject key events with undefined action."; |
| 637 | |
| 638 | // 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] | 639 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 640 | 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] | 641 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 642 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 643 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 644 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 645 | << "Should reject key events with ACTION_MULTIPLE."; |
| 646 | } |
| 647 | |
| 648 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 649 | MotionEvent event; |
| 650 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 651 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 652 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 653 | pointerProperties[i].clear(); |
| 654 | pointerProperties[i].id = i; |
| 655 | pointerCoords[i].clear(); |
| 656 | } |
| 657 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 658 | // Some constants commonly used below |
| 659 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 660 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 661 | constexpr int32_t metaState = AMETA_NONE; |
| 662 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 663 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 664 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 665 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 666 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 667 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 668 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 669 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 670 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 671 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 672 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 673 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 674 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | << "Should reject motion events with undefined action."; |
| 676 | |
| 677 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 678 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 679 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 680 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 681 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 682 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 683 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 684 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 685 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 686 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 687 | << "Should reject motion events with pointer down index too large."; |
| 688 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 689 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 690 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 691 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 692 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 693 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 694 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 695 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 696 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 697 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 698 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 699 | << "Should reject motion events with pointer down index too small."; |
| 700 | |
| 701 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 702 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 703 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 704 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 705 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 706 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 707 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 708 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 709 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 710 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 711 | << "Should reject motion events with pointer up index too large."; |
| 712 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 713 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 714 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 715 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 716 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 717 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 718 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 719 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 720 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 721 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 722 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 723 | << "Should reject motion events with pointer up index too small."; |
| 724 | |
| 725 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 726 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 727 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 728 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 729 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 730 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 731 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 732 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 733 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 734 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 735 | << "Should reject motion events with 0 pointers."; |
| 736 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 737 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 738 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 739 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 740 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 741 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 742 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 743 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 744 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 745 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 746 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 747 | |
| 748 | // Rejects motion events with invalid pointer ids. |
| 749 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 750 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 751 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 752 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 753 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 754 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 755 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 756 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 757 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 758 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 759 | << "Should reject motion events with pointer ids less than 0."; |
| 760 | |
| 761 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 762 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 763 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 764 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 765 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 766 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 767 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 768 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 769 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 770 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 772 | |
| 773 | // Rejects motion events with duplicate pointer ids. |
| 774 | pointerProperties[0].id = 1; |
| 775 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 776 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 777 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 778 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 779 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 780 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 781 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 782 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 783 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 784 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | << "Should reject motion events with duplicate pointer ids."; |
| 786 | } |
| 787 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 788 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 789 | |
| 790 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 791 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 792 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 793 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 794 | |
| 795 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 796 | } |
| 797 | |
| 798 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 799 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 800 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 801 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 802 | |
| 803 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 804 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 805 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 806 | } |
| 807 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 808 | namespace { |
| 809 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 810 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 811 | // Default input dispatching timeout if there is no focused application or paused window |
| 812 | // from which to determine an appropriate dispatching timeout. |
| 813 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 814 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 815 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 816 | |
| 817 | class FakeApplicationHandle : public InputApplicationHandle { |
| 818 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 819 | FakeApplicationHandle() { |
| 820 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 821 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 822 | mInfo.dispatchingTimeoutMillis = |
| 823 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 824 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 825 | virtual ~FakeApplicationHandle() {} |
| 826 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 827 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 828 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 829 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 830 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 831 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 832 | }; |
| 833 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 834 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 835 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 836 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 837 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 838 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 839 | } |
| 840 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 841 | InputEvent* consume(std::chrono::milliseconds timeout) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 842 | InputEvent* event; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 843 | std::optional<uint32_t> consumeSeq = receiveEvent(timeout, &event); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 844 | if (!consumeSeq) { |
| 845 | return nullptr; |
| 846 | } |
| 847 | finishEvent(*consumeSeq); |
| 848 | return event; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Receive an event without acknowledging it. |
| 853 | * Return the sequence number that could later be used to send finished signal. |
| 854 | */ |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 855 | std::optional<uint32_t> receiveEvent(std::chrono::milliseconds timeout, |
| 856 | InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 857 | uint32_t consumeSeq; |
| 858 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 859 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 860 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 861 | status_t status = WOULD_BLOCK; |
| 862 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 863 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 864 | &event); |
| 865 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 866 | if (elapsed > timeout) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 867 | break; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if (status == WOULD_BLOCK) { |
| 872 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 873 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | if (status != OK) { |
| 877 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 878 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 879 | } |
| 880 | if (event == nullptr) { |
| 881 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 882 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 883 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 884 | if (outEvent != nullptr) { |
| 885 | *outEvent = event; |
| 886 | } |
| 887 | return consumeSeq; |
| 888 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 889 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 890 | /** |
| 891 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 892 | */ |
| 893 | void finishEvent(uint32_t consumeSeq) { |
| 894 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 895 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 896 | } |
| 897 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 898 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 899 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 900 | ASSERT_EQ(OK, status); |
| 901 | } |
| 902 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 903 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 904 | std::optional<int32_t> expectedDisplayId, |
| 905 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 906 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 907 | |
| 908 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 909 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 910 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 911 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 912 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 913 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 914 | if (expectedDisplayId.has_value()) { |
| 915 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 916 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 917 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 918 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 919 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 920 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 921 | ASSERT_THAT(keyEvent, WithKeyAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 922 | if (expectedFlags.has_value()) { |
| 923 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 924 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 925 | break; |
| 926 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 927 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 928 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 929 | ASSERT_THAT(motionEvent, WithMotionAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 930 | if (expectedFlags.has_value()) { |
| 931 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 932 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 933 | break; |
| 934 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 935 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 936 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 937 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 938 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 939 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 940 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 941 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 942 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 943 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 944 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 945 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 946 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 947 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 948 | } |
| 949 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 950 | MotionEvent* consumeMotion() { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 951 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 952 | |
| 953 | if (event == nullptr) { |
| 954 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 955 | return nullptr; |
| 956 | } |
| 957 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 958 | if (event->getType() != InputEventType::MOTION) { |
| 959 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 960 | return nullptr; |
| 961 | } |
| 962 | return static_cast<MotionEvent*>(event); |
| 963 | } |
| 964 | |
| 965 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 966 | MotionEvent* motionEvent = consumeMotion(); |
| 967 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 968 | ASSERT_THAT(*motionEvent, matcher); |
| 969 | } |
| 970 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 971 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 972 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 973 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 974 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 975 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 976 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 977 | |
| 978 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 979 | << mName.c_str() << ": event displayId should always be NONE."; |
| 980 | |
| 981 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 982 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 983 | } |
| 984 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 985 | void consumeCaptureEvent(bool hasCapture) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 986 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 987 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 988 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 989 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 990 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 991 | |
| 992 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 993 | << mName.c_str() << ": event displayId should always be NONE."; |
| 994 | |
| 995 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 996 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 997 | } |
| 998 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 999 | void consumeDragEvent(bool isExiting, float x, float y) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1000 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1001 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1002 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1003 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1004 | |
| 1005 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1006 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1007 | |
| 1008 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1009 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1010 | EXPECT_EQ(x, dragEvent.getX()); |
| 1011 | EXPECT_EQ(y, dragEvent.getY()); |
| 1012 | } |
| 1013 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1014 | void consumeTouchModeEvent(bool inTouchMode) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1015 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1016 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1017 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1018 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1019 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1020 | |
| 1021 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1022 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1023 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1024 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1025 | } |
| 1026 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1027 | void assertNoEvents() { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1028 | InputEvent* event = consume(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1029 | if (event == nullptr) { |
| 1030 | return; |
| 1031 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1032 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1033 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1034 | ADD_FAILURE() << "Received key event " |
| 1035 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1036 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1037 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1038 | ADD_FAILURE() << "Received motion event " |
| 1039 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1040 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1041 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1042 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1043 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1044 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1045 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1046 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1047 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1048 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1049 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1050 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1051 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1052 | } |
| 1053 | FAIL() << mName.c_str() |
| 1054 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1058 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1059 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1060 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1061 | protected: |
| 1062 | std::unique_ptr<InputConsumer> mConsumer; |
| 1063 | PreallocatedInputEventFactory mEventFactory; |
| 1064 | |
| 1065 | std::string mName; |
| 1066 | }; |
| 1067 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1068 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1069 | public: |
| 1070 | static const int32_t WIDTH = 600; |
| 1071 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1072 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1073 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1074 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1075 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1076 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1077 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1078 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1079 | dispatcher->createInputChannel(name); |
| 1080 | token = (*channel)->getConnectionToken(); |
| 1081 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | inputApplicationHandle->updateInfo(); |
| 1085 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1086 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1087 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1088 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1089 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1090 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1091 | mInfo.alpha = 1.0; |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1092 | mInfo.frame = Rect(0, 0, WIDTH, HEIGHT); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1093 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1094 | mInfo.globalScaleFactor = 1.0; |
| 1095 | mInfo.touchableRegion.clear(); |
| 1096 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1097 | mInfo.ownerPid = WINDOW_PID; |
| 1098 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1099 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1100 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1103 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1104 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1105 | handle->mInfo = mInfo; |
| 1106 | handle->mInfo.displayId = displayId; |
| 1107 | handle->mInfo.id = sId++; |
| 1108 | handle->mInputReceiver = mInputReceiver; |
| 1109 | return handle; |
| 1110 | } |
| 1111 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1112 | void setTouchable(bool touchable) { |
| 1113 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1114 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1115 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1116 | void setFocusable(bool focusable) { |
| 1117 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1118 | } |
| 1119 | |
| 1120 | void setVisible(bool visible) { |
| 1121 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1122 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1123 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1124 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1125 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1128 | void setPaused(bool paused) { |
| 1129 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1130 | } |
| 1131 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1132 | void setPreventSplitting(bool preventSplitting) { |
| 1133 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | void setSlippery(bool slippery) { |
| 1137 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1138 | } |
| 1139 | |
| 1140 | void setWatchOutsideTouch(bool watchOutside) { |
| 1141 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1142 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1143 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1144 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1145 | |
| 1146 | void setInterceptsStylus(bool interceptsStylus) { |
| 1147 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1148 | } |
| 1149 | |
| 1150 | void setDropInput(bool dropInput) { |
| 1151 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1152 | } |
| 1153 | |
| 1154 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1155 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1156 | } |
| 1157 | |
| 1158 | void setNoInputChannel(bool noInputChannel) { |
| 1159 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1160 | } |
| 1161 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1162 | void setDisableUserActivity(bool disableUserActivity) { |
| 1163 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1164 | } |
| 1165 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1166 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1167 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1168 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1169 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1170 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1171 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1172 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1173 | mInfo.frame = frame; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1174 | mInfo.touchableRegion.clear(); |
| 1175 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1176 | |
| 1177 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1178 | ui::Transform translate; |
| 1179 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1180 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1183 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1184 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1185 | void setIsWallpaper(bool isWallpaper) { |
| 1186 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1187 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1188 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1189 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1190 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1191 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1192 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1193 | void setTrustedOverlay(bool trustedOverlay) { |
| 1194 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1195 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1196 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1197 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1198 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1199 | } |
| 1200 | |
| 1201 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1202 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1203 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1204 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1205 | KeyEvent* consumeKey() { |
| 1206 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1207 | if (event == nullptr) { |
| 1208 | ADD_FAILURE() << "Consume failed : no event"; |
| 1209 | return nullptr; |
| 1210 | } |
| 1211 | if (event->getType() != InputEventType::KEY) { |
| 1212 | ADD_FAILURE() << "Instead of key event, got " << *event; |
| 1213 | return nullptr; |
| 1214 | } |
| 1215 | return static_cast<KeyEvent*>(event); |
| 1216 | } |
| 1217 | |
| 1218 | void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
| 1219 | KeyEvent* keyEvent = consumeKey(); |
| 1220 | ASSERT_NE(nullptr, keyEvent) << "Did not get a key event, but expected " << matcher; |
| 1221 | ASSERT_THAT(*keyEvent, matcher); |
| 1222 | } |
| 1223 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1224 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1225 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1226 | } |
| 1227 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1228 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1229 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1232 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1233 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1234 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1235 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1239 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1240 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1241 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1245 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1246 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1247 | } |
| 1248 | |
| 1249 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1250 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1251 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1252 | expectedFlags); |
| 1253 | } |
| 1254 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1255 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1256 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1257 | int32_t expectedFlags = 0) { |
| 1258 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1259 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1260 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1264 | int32_t expectedFlags = 0) { |
| 1265 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1266 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1267 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1271 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1272 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1273 | expectedFlags); |
| 1274 | } |
| 1275 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1276 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1277 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1278 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1279 | expectedFlags); |
| 1280 | } |
| 1281 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1282 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1283 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1284 | MotionEvent* motionEvent = consumeMotion(); |
| 1285 | ASSERT_NE(nullptr, motionEvent); |
| 1286 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent->getActionMasked()); |
| 1287 | EXPECT_EQ(0.f, motionEvent->getRawPointerCoords(0)->getX()); |
| 1288 | EXPECT_EQ(0.f, motionEvent->getRawPointerCoords(0)->getY()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1291 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1292 | ASSERT_NE(mInputReceiver, nullptr) |
| 1293 | << "Cannot consume events from a window with no receiver"; |
| 1294 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1295 | } |
| 1296 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1297 | void consumeCaptureEvent(bool hasCapture) { |
| 1298 | ASSERT_NE(mInputReceiver, nullptr) |
| 1299 | << "Cannot consume events from a window with no receiver"; |
| 1300 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1301 | } |
| 1302 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1303 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1304 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1305 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1306 | ASSERT_THAT(*motionEvent, matcher); |
| 1307 | } |
| 1308 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1309 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1310 | std::optional<int32_t> expectedDisplayId, |
| 1311 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1312 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1313 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1314 | expectedFlags); |
| 1315 | } |
| 1316 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1317 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1318 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1319 | } |
| 1320 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1321 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1322 | ASSERT_NE(mInputReceiver, nullptr) |
| 1323 | << "Cannot consume events from a window with no receiver"; |
| 1324 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1325 | } |
| 1326 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1327 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1328 | if (mInputReceiver == nullptr) { |
| 1329 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1330 | return std::nullopt; |
| 1331 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1332 | return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED, outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | void finishEvent(uint32_t sequenceNum) { |
| 1336 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1337 | mInputReceiver->finishEvent(sequenceNum); |
| 1338 | } |
| 1339 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1340 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1341 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1342 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1343 | } |
| 1344 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1345 | InputEvent* consume(std::chrono::milliseconds timeout) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1346 | if (mInputReceiver == nullptr) { |
| 1347 | return nullptr; |
| 1348 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1349 | return mInputReceiver->consume(timeout); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1352 | MotionEvent* consumeMotion() { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1353 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1354 | if (event == nullptr) { |
| 1355 | ADD_FAILURE() << "Consume failed : no event"; |
| 1356 | return nullptr; |
| 1357 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1358 | if (event->getType() != InputEventType::MOTION) { |
| 1359 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1360 | return nullptr; |
| 1361 | } |
| 1362 | return static_cast<MotionEvent*>(event); |
| 1363 | } |
| 1364 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1365 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1366 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1367 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1368 | return; // Can't receive events if the window does not have input channel |
| 1369 | } |
| 1370 | ASSERT_NE(nullptr, mInputReceiver) |
| 1371 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1372 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1373 | } |
| 1374 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1375 | sp<IBinder> getToken() { return mInfo.token; } |
| 1376 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1377 | const std::string& getName() { return mName; } |
| 1378 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1379 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1380 | mInfo.ownerPid = ownerPid; |
| 1381 | mInfo.ownerUid = ownerUid; |
| 1382 | } |
| 1383 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1384 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1385 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1386 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1387 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1388 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1389 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1390 | private: |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1391 | FakeWindowHandle(std::string name) : mName(name){}; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1392 | const std::string mName; |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1393 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1394 | 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] | 1395 | friend class sp<FakeWindowHandle>; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1396 | }; |
| 1397 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1398 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1399 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1400 | class FakeMonitorReceiver { |
| 1401 | public: |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 1402 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId) { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1403 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 1404 | dispatcher.createInputMonitor(displayId, name, MONITOR_PID); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1405 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
| 1406 | } |
| 1407 | |
| 1408 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 1409 | |
| 1410 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1411 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1412 | expectedFlags); |
| 1413 | } |
| 1414 | |
| 1415 | std::optional<int32_t> receiveEvent() { |
| 1416 | return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1417 | } |
| 1418 | |
| 1419 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 1420 | |
| 1421 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1422 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1423 | expectedDisplayId, expectedFlags); |
| 1424 | } |
| 1425 | |
| 1426 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1427 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 1428 | expectedDisplayId, expectedFlags); |
| 1429 | } |
| 1430 | |
| 1431 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1432 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 1433 | expectedDisplayId, expectedFlags); |
| 1434 | } |
| 1435 | |
| 1436 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1437 | mInputReceiver->consumeMotionEvent( |
| 1438 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 1439 | WithDisplayId(expectedDisplayId), |
| 1440 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 1441 | } |
| 1442 | |
| 1443 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 1444 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1445 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 1446 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
| 1447 | /*expectedFlags=*/0); |
| 1448 | } |
| 1449 | |
| 1450 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1451 | mInputReceiver->consumeMotionEvent(matcher); |
| 1452 | } |
| 1453 | |
| 1454 | MotionEvent* consumeMotion() { return mInputReceiver->consumeMotion(); } |
| 1455 | |
| 1456 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 1457 | |
| 1458 | private: |
| 1459 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
| 1460 | }; |
| 1461 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1462 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1463 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1464 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1465 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1466 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1467 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1468 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1469 | KeyEvent event; |
| 1470 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1471 | |
| 1472 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1473 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1474 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1475 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1476 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1477 | if (!allowKeyRepeat) { |
| 1478 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1479 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1480 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1481 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1484 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 1485 | InputEventInjectionResult result = |
| 1486 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
| 1487 | InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
| 1488 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 1489 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1494 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1495 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1498 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1499 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1500 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1501 | static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher, |
| 1502 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1503 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1504 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1505 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1508 | static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1509 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1510 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1513 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1514 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1515 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1516 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1517 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1518 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1519 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -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, int32_t action, int32_t source, int32_t displayId, |
| 1524 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1525 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1526 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1527 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1528 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1529 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1530 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1531 | MotionEventBuilder motionBuilder = |
| 1532 | MotionEventBuilder(action, source) |
| 1533 | .displayId(displayId) |
| 1534 | .eventTime(eventTime) |
| 1535 | .rawXCursorPosition(cursorPosition.x) |
| 1536 | .rawYCursorPosition(cursorPosition.y) |
| 1537 | .pointer( |
| 1538 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1539 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1540 | motionBuilder.downTime(eventTime); |
| 1541 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1542 | |
| 1543 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1544 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1545 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1546 | } |
| 1547 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1548 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
| 1549 | int32_t displayId, |
| 1550 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1551 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1554 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
| 1555 | int32_t displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1556 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1557 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1560 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1561 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1562 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1563 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1564 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*flags=*/0, AKEYCODE_A, KEY_A, |
| 1565 | AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1566 | |
| 1567 | return args; |
| 1568 | } |
| 1569 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1570 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1571 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1572 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1573 | // Define a valid key event. |
| 1574 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1575 | displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, AMETA_META_ON, |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1576 | currentTime); |
| 1577 | |
| 1578 | return args; |
| 1579 | } |
| 1580 | |
| 1581 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1582 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1583 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1584 | // Define a valid key event. |
| 1585 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1586 | displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, KEY_ASSISTANT, |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1587 | AMETA_NONE, currentTime); |
| 1588 | |
| 1589 | return args; |
| 1590 | } |
| 1591 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1592 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1593 | int32_t displayId, |
| 1594 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1595 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1596 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1597 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1598 | } |
| 1599 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1600 | PointerProperties pointerProperties[pointerCount]; |
| 1601 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1602 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1603 | for (size_t i = 0; i < pointerCount; i++) { |
| 1604 | pointerProperties[i].clear(); |
| 1605 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1606 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1607 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1608 | pointerCoords[i].clear(); |
| 1609 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1610 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1611 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1612 | |
| 1613 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1614 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1615 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1616 | POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, /*flags=*/0, |
| 1617 | AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1618 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1619 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1620 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1621 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1622 | |
| 1623 | return args; |
| 1624 | } |
| 1625 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1626 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1627 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1628 | } |
| 1629 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1630 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1631 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1632 | } |
| 1633 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1634 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1635 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1636 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1637 | } |
| 1638 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1639 | } // namespace |
| 1640 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1641 | /** |
| 1642 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1643 | * broken channel. |
| 1644 | */ |
| 1645 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1646 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1647 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1648 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1649 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1650 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1651 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1652 | |
| 1653 | // Window closes its channel, but the window remains. |
| 1654 | window->destroyReceiver(); |
| 1655 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1656 | } |
| 1657 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1658 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1659 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1660 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1661 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1662 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1663 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1664 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1665 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1666 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1667 | |
| 1668 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1669 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1670 | } |
| 1671 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1672 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1673 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1674 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1675 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1676 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1677 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1678 | // Inject a MotionEvent to an unknown display. |
| 1679 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1680 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1681 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1682 | |
| 1683 | // Window should receive motion event. |
| 1684 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1685 | } |
| 1686 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1687 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1688 | * Calling onWindowInfosChanged once should not cause any issues. |
| 1689 | * 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] | 1690 | * called twice. |
| 1691 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1692 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1693 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1694 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1695 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1696 | window->setFrame(Rect(0, 0, 100, 100)); |
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 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1699 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1700 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1701 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1702 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1703 | |
| 1704 | // Window should receive motion event. |
| 1705 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1706 | } |
| 1707 | |
| 1708 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1709 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1710 | */ |
| 1711 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1712 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1713 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1714 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1715 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1716 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1717 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1718 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1720 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1721 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1722 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1723 | |
| 1724 | // Window should receive motion event. |
| 1725 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1726 | } |
| 1727 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1728 | // The foreground window should receive the first touch down event. |
| 1729 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1730 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1731 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1732 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1733 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1734 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1735 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1736 | mDispatcher->onWindowInfosChanged( |
| 1737 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1738 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1739 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1740 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1741 | |
| 1742 | // 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] | 1743 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1744 | windowSecond->assertNoEvents(); |
| 1745 | } |
| 1746 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1747 | /** |
| 1748 | * Two windows: A top window, and a wallpaper behind the window. |
| 1749 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1750 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1751 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1752 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1753 | */ |
| 1754 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1755 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1756 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1757 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1758 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1759 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1760 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1761 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1762 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1763 | mDispatcher->onWindowInfosChanged( |
| 1764 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1766 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1767 | {100, 200})) |
| 1768 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1769 | |
| 1770 | // Both foreground window and its wallpaper should receive the touch down |
| 1771 | foregroundWindow->consumeMotionDown(); |
| 1772 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1773 | |
| 1774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1775 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1776 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1777 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1778 | |
| 1779 | foregroundWindow->consumeMotionMove(); |
| 1780 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1781 | |
| 1782 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1783 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1784 | foregroundWindow->consumeMotionCancel(); |
| 1785 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1786 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1787 | } |
| 1788 | |
| 1789 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1790 | * Two fingers down on the window, and lift off the first finger. |
| 1791 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1792 | * contains a single pointer. |
| 1793 | */ |
| 1794 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1795 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1796 | sp<FakeWindowHandle> window = |
| 1797 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1798 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1799 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1800 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1801 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1802 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1803 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1804 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1805 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1806 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1807 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1808 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1809 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1810 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1811 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1812 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1813 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1814 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1815 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1816 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1817 | |
| 1818 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1819 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1820 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1821 | window->consumeMotionEvent( |
| 1822 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1823 | } |
| 1824 | |
| 1825 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1826 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1827 | * with the following differences: |
| 1828 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1829 | * clean up the connection. |
| 1830 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1831 | * Ensure that there's no crash in the dispatcher. |
| 1832 | */ |
| 1833 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1834 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1835 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1836 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1837 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1838 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1839 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1840 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1841 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1842 | mDispatcher->onWindowInfosChanged( |
| 1843 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1845 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1846 | {100, 200})) |
| 1847 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1848 | |
| 1849 | // Both foreground window and its wallpaper should receive the touch down |
| 1850 | foregroundWindow->consumeMotionDown(); |
| 1851 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1852 | |
| 1853 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1854 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1855 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1856 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1857 | |
| 1858 | foregroundWindow->consumeMotionMove(); |
| 1859 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1860 | |
| 1861 | // Wallpaper closes its channel, but the window remains. |
| 1862 | wallpaperWindow->destroyReceiver(); |
| 1863 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1864 | |
| 1865 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1866 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1867 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1868 | foregroundWindow->consumeMotionCancel(); |
| 1869 | } |
| 1870 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1871 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1872 | public ::testing::WithParamInterface<bool> {}; |
| 1873 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1874 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1875 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1876 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1877 | * The top window gets a multitouch gesture. |
| 1878 | * Ensure that wallpaper gets the same gesture. |
| 1879 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1880 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1881 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1882 | sp<FakeWindowHandle> foregroundWindow = |
| 1883 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1884 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1885 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1886 | |
| 1887 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1888 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1889 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1890 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1891 | mDispatcher->onWindowInfosChanged( |
| 1892 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1893 | |
| 1894 | // Touch down on top window |
| 1895 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1896 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1897 | {100, 100})) |
| 1898 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1899 | |
| 1900 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1901 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1902 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1903 | |
| 1904 | // Second finger down on the top window |
| 1905 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1906 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1907 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1908 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1909 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1910 | .build(); |
| 1911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1912 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1913 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1914 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1915 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1916 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1917 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1918 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1919 | |
| 1920 | const MotionEvent secondFingerUpEvent = |
| 1921 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1922 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1923 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1924 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1925 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1926 | .build(); |
| 1927 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1928 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1929 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1930 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1931 | foregroundWindow->consumeMotionPointerUp(0); |
| 1932 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1933 | |
| 1934 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1935 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1936 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1937 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1938 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1939 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1940 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1941 | .x(100) |
| 1942 | .y(100)) |
| 1943 | .build(), |
| 1944 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1945 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1946 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1947 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * Two windows: a window on the left and window on the right. |
| 1952 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1953 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1954 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1955 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1956 | * ACTION_POINTER_DOWN(1). |
| 1957 | */ |
| 1958 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1959 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1960 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1961 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1962 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1963 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1964 | |
| 1965 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1966 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1967 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1968 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1969 | |
| 1970 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1971 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1972 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1973 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1974 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1975 | mDispatcher->onWindowInfosChanged( |
| 1976 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1977 | {}, |
| 1978 | 0, |
| 1979 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1980 | |
| 1981 | // Touch down on left window |
| 1982 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1983 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1984 | {100, 100})) |
| 1985 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1986 | |
| 1987 | // Both foreground window and its wallpaper should receive the touch down |
| 1988 | leftWindow->consumeMotionDown(); |
| 1989 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1990 | |
| 1991 | // Second finger down on the right window |
| 1992 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1993 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1994 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1995 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1996 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1997 | .build(); |
| 1998 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1999 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2000 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2001 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2002 | |
| 2003 | leftWindow->consumeMotionMove(); |
| 2004 | // Since the touch is split, right window gets ACTION_DOWN |
| 2005 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2006 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2007 | expectedWallpaperFlags); |
| 2008 | |
| 2009 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2010 | mDispatcher->onWindowInfosChanged( |
| 2011 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2012 | leftWindow->consumeMotionCancel(); |
| 2013 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2014 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2015 | |
| 2016 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2017 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2018 | const MotionEvent secondFingerMoveEvent = |
| 2019 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2020 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2021 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2022 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2023 | .build(); |
| 2024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2025 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2026 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2027 | rightWindow->consumeMotionMove(); |
| 2028 | |
| 2029 | leftWindow->assertNoEvents(); |
| 2030 | rightWindow->assertNoEvents(); |
| 2031 | wallpaperWindow->assertNoEvents(); |
| 2032 | } |
| 2033 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2034 | /** |
| 2035 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2036 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2037 | * The right window should receive ACTION_DOWN. |
| 2038 | */ |
| 2039 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2040 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2041 | sp<FakeWindowHandle> leftWindow = |
| 2042 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2043 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2044 | leftWindow->setDupTouchToWallpaper(true); |
| 2045 | leftWindow->setSlippery(true); |
| 2046 | |
| 2047 | sp<FakeWindowHandle> rightWindow = |
| 2048 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2049 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2050 | |
| 2051 | sp<FakeWindowHandle> wallpaperWindow = |
| 2052 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2053 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2054 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2055 | mDispatcher->onWindowInfosChanged( |
| 2056 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2057 | {}, |
| 2058 | 0, |
| 2059 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2060 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2061 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2062 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2063 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2064 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2065 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2066 | |
| 2067 | // Both foreground window and its wallpaper should receive the touch down |
| 2068 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2069 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2070 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2071 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2073 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2074 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2075 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2076 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2077 | leftWindow->consumeMotionCancel(); |
| 2078 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2079 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2082 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2083 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2084 | * interactive, it might stop sending this flag. |
| 2085 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2086 | * to have a consistent input stream. |
| 2087 | * |
| 2088 | * Test procedure: |
| 2089 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2090 | * DOWN (new gesture). |
| 2091 | * |
| 2092 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2093 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2094 | * |
| 2095 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2096 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2097 | */ |
| 2098 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2099 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2100 | sp<FakeWindowHandle> spyWindow = |
| 2101 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2102 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2103 | spyWindow->setTrustedOverlay(true); |
| 2104 | spyWindow->setSpy(true); |
| 2105 | |
| 2106 | sp<FakeWindowHandle> window = |
| 2107 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2108 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2109 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2110 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2111 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2112 | |
| 2113 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2114 | mDispatcher->notifyMotion( |
| 2115 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2116 | .deviceId(touchDeviceId) |
| 2117 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2118 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2119 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2120 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2121 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2122 | .deviceId(touchDeviceId) |
| 2123 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2124 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2125 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2126 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2127 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2128 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2129 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2130 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2131 | |
| 2132 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2133 | mDispatcher->notifyMotion( |
| 2134 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2135 | .deviceId(touchDeviceId) |
| 2136 | .policyFlags(0) |
| 2137 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2138 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2139 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2140 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2141 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2142 | |
| 2143 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2144 | // 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] | 2145 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2146 | |
| 2147 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2148 | mDispatcher->notifyMotion( |
| 2149 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2150 | .deviceId(touchDeviceId) |
| 2151 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2152 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2153 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2154 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2155 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2156 | |
| 2157 | // No more events |
| 2158 | spyWindow->assertNoEvents(); |
| 2159 | window->assertNoEvents(); |
| 2160 | } |
| 2161 | |
| 2162 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2163 | * Two windows: a window on the left and a window on the right. |
| 2164 | * Mouse is hovered from the right window into the left window. |
| 2165 | * Next, we tap on the left window, where the cursor was last seen. |
| 2166 | * The second tap is done onto the right window. |
| 2167 | * The mouse and tap are from two different devices. |
| 2168 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2169 | * explicitly helps during debugging. |
| 2170 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2171 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2172 | */ |
| 2173 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2174 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2175 | sp<FakeWindowHandle> leftWindow = |
| 2176 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2177 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2178 | |
| 2179 | sp<FakeWindowHandle> rightWindow = |
| 2180 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2181 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2182 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2183 | mDispatcher->onWindowInfosChanged( |
| 2184 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2185 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2186 | // stale. |
| 2187 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2188 | const int32_t mouseDeviceId = 6; |
| 2189 | const int32_t touchDeviceId = 4; |
| 2190 | // Move the cursor from right |
| 2191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2192 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2193 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2194 | AINPUT_SOURCE_MOUSE) |
| 2195 | .deviceId(mouseDeviceId) |
| 2196 | .downTime(baseTime + 10) |
| 2197 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2198 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2199 | .build())); |
| 2200 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2201 | |
| 2202 | // .. to the left window |
| 2203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2204 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2205 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2206 | AINPUT_SOURCE_MOUSE) |
| 2207 | .deviceId(mouseDeviceId) |
| 2208 | .downTime(baseTime + 10) |
| 2209 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2210 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2211 | .build())); |
| 2212 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2213 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2214 | // Now tap the left window |
| 2215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2216 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2217 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2218 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2219 | .deviceId(touchDeviceId) |
| 2220 | .downTime(baseTime + 40) |
| 2221 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2222 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2223 | .build())); |
| 2224 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2225 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2226 | |
| 2227 | // release tap |
| 2228 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2229 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2230 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2231 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2232 | .deviceId(touchDeviceId) |
| 2233 | .downTime(baseTime + 40) |
| 2234 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2235 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2236 | .build())); |
| 2237 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2238 | |
| 2239 | // Tap the window on the right |
| 2240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2241 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2242 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2243 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2244 | .deviceId(touchDeviceId) |
| 2245 | .downTime(baseTime + 60) |
| 2246 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2247 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2248 | .build())); |
| 2249 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2250 | |
| 2251 | // release tap |
| 2252 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2253 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2254 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2255 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2256 | .deviceId(touchDeviceId) |
| 2257 | .downTime(baseTime + 60) |
| 2258 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2259 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2260 | .build())); |
| 2261 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2262 | |
| 2263 | // No more events |
| 2264 | leftWindow->assertNoEvents(); |
| 2265 | rightWindow->assertNoEvents(); |
| 2266 | } |
| 2267 | |
| 2268 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2269 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2270 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2271 | * While the top window is present, the hovering is stopped. |
| 2272 | * Later, hovering gets resumed again. |
| 2273 | * Ensure that new hover gesture is handled correctly. |
| 2274 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2275 | * to the window that's currently being hovered over. |
| 2276 | */ |
| 2277 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2278 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2279 | sp<FakeWindowHandle> window = |
| 2280 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2281 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2282 | |
| 2283 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2284 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2285 | |
| 2286 | // Start hovering in the window |
| 2287 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2288 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2289 | .build()); |
| 2290 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2291 | |
| 2292 | // Now, an obscuring window appears! |
| 2293 | sp<FakeWindowHandle> obscuringWindow = |
| 2294 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2295 | ADISPLAY_ID_DEFAULT, |
| 2296 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2297 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2298 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2299 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2300 | obscuringWindow->setNoInputChannel(true); |
| 2301 | obscuringWindow->setFocusable(false); |
| 2302 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2303 | mDispatcher->onWindowInfosChanged( |
| 2304 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2305 | |
| 2306 | // While this new obscuring window is present, the hovering is stopped |
| 2307 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2308 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2309 | .build()); |
| 2310 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2311 | |
| 2312 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2313 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2314 | |
| 2315 | // And a new hover gesture starts. |
| 2316 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2317 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2318 | .build()); |
| 2319 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2320 | } |
| 2321 | |
| 2322 | /** |
| 2323 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2324 | * the obscuring window. |
| 2325 | */ |
| 2326 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2327 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2328 | sp<FakeWindowHandle> window = |
| 2329 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2330 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2331 | |
| 2332 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2333 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2334 | |
| 2335 | // Start hovering in the window |
| 2336 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2337 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2338 | .build()); |
| 2339 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2340 | |
| 2341 | // Now, an obscuring window appears! |
| 2342 | sp<FakeWindowHandle> obscuringWindow = |
| 2343 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2344 | ADISPLAY_ID_DEFAULT, |
| 2345 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2346 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2347 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2348 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2349 | obscuringWindow->setNoInputChannel(true); |
| 2350 | obscuringWindow->setFocusable(false); |
| 2351 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2352 | mDispatcher->onWindowInfosChanged( |
| 2353 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2354 | |
| 2355 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2356 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2357 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2358 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2359 | .build()); |
| 2360 | obscuringWindow->assertNoEvents(); |
| 2361 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2362 | |
| 2363 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2364 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2365 | |
| 2366 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2367 | // so it should generate a HOVER_ENTER |
| 2368 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2369 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2370 | .build()); |
| 2371 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2372 | |
| 2373 | // Now the MOVE should be getting dispatched normally |
| 2374 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2375 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2376 | .build()); |
| 2377 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2378 | } |
| 2379 | |
| 2380 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2381 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 2382 | * events are delivered to the window. |
| 2383 | */ |
| 2384 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 2385 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2386 | sp<FakeWindowHandle> window = |
| 2387 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2388 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2389 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2390 | |
| 2391 | // Start hovering in the window |
| 2392 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2393 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2394 | .build()); |
| 2395 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2396 | |
| 2397 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2398 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2399 | .build()); |
| 2400 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2401 | |
| 2402 | // Scroll with the mouse |
| 2403 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 2404 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2405 | .build()); |
| 2406 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 2407 | } |
| 2408 | |
| 2409 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2410 | |
| 2411 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2412 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2413 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2414 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2415 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2416 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2417 | sp<FakeWindowHandle> window = |
| 2418 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2419 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2420 | |
| 2421 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2422 | |
| 2423 | constexpr int32_t touchDeviceId = 4; |
| 2424 | constexpr int32_t stylusDeviceId = 2; |
| 2425 | |
| 2426 | // Stylus down |
| 2427 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2428 | .deviceId(stylusDeviceId) |
| 2429 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2430 | .build()); |
| 2431 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2432 | |
| 2433 | // Touch down |
| 2434 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2435 | .deviceId(touchDeviceId) |
| 2436 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2437 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2438 | |
| 2439 | // Touch move |
| 2440 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2441 | .deviceId(touchDeviceId) |
| 2442 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2443 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2444 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2445 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2446 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2447 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2448 | .deviceId(stylusDeviceId) |
| 2449 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2450 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2451 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2452 | WithCoords(101, 111))); |
| 2453 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2454 | window->assertNoEvents(); |
| 2455 | } |
| 2456 | |
| 2457 | /** |
| 2458 | * 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^] | 2459 | * 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] | 2460 | * Similar test as above, but with added SPY window. |
| 2461 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2462 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2464 | sp<FakeWindowHandle> window = |
| 2465 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2466 | sp<FakeWindowHandle> spyWindow = |
| 2467 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2468 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2469 | spyWindow->setTrustedOverlay(true); |
| 2470 | spyWindow->setSpy(true); |
| 2471 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2472 | |
| 2473 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2474 | |
| 2475 | constexpr int32_t touchDeviceId = 4; |
| 2476 | constexpr int32_t stylusDeviceId = 2; |
| 2477 | |
| 2478 | // Stylus down |
| 2479 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2480 | .deviceId(stylusDeviceId) |
| 2481 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2482 | .build()); |
| 2483 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2484 | spyWindow->consumeMotionEvent( |
| 2485 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2486 | |
| 2487 | // Touch down |
| 2488 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2489 | .deviceId(touchDeviceId) |
| 2490 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2491 | .build()); |
| 2492 | |
| 2493 | // Touch move |
| 2494 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2495 | .deviceId(touchDeviceId) |
| 2496 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2497 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2498 | |
| 2499 | // Touch is ignored because stylus is already down |
| 2500 | |
| 2501 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2502 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2503 | .deviceId(stylusDeviceId) |
| 2504 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2505 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2506 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2507 | WithCoords(101, 111))); |
| 2508 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2509 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2510 | |
| 2511 | window->assertNoEvents(); |
| 2512 | spyWindow->assertNoEvents(); |
| 2513 | } |
| 2514 | |
| 2515 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2516 | * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that |
| 2517 | * touch is not dropped, because stylus hover should be ignored. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2518 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2519 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDoesNotBlockTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2520 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2521 | sp<FakeWindowHandle> window = |
| 2522 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2523 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2524 | |
| 2525 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2526 | |
| 2527 | constexpr int32_t touchDeviceId = 4; |
| 2528 | constexpr int32_t stylusDeviceId = 2; |
| 2529 | |
| 2530 | // Stylus down on the window |
| 2531 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2532 | .deviceId(stylusDeviceId) |
| 2533 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2534 | .build()); |
| 2535 | window->consumeMotionEvent( |
| 2536 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2537 | |
| 2538 | // Touch down on window |
| 2539 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2540 | .deviceId(touchDeviceId) |
| 2541 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2542 | .build()); |
| 2543 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2544 | .deviceId(touchDeviceId) |
| 2545 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2546 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2547 | |
| 2548 | // Stylus hover is canceled because touch is down |
| 2549 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), |
| 2550 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2551 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId), |
| 2552 | WithCoords(140, 145))); |
| 2553 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2554 | WithCoords(141, 146))); |
| 2555 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2556 | // Subsequent stylus movements are ignored |
| 2557 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2558 | .deviceId(stylusDeviceId) |
| 2559 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2560 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2561 | |
| 2562 | // but subsequent touches continue to be delivered |
| 2563 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2564 | .deviceId(touchDeviceId) |
| 2565 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2566 | .build()); |
| 2567 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2568 | WithCoords(142, 147))); |
| 2569 | } |
| 2570 | |
| 2571 | /** |
| 2572 | * One window. Touch down on the window. Then, stylus hover on the window from another device. |
| 2573 | * Ensure that touch is not canceled, because stylus hover should be dropped. |
| 2574 | */ |
| 2575 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusHover) { |
| 2576 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2577 | sp<FakeWindowHandle> window = |
| 2578 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2579 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2580 | |
| 2581 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2582 | |
| 2583 | constexpr int32_t touchDeviceId = 4; |
| 2584 | constexpr int32_t stylusDeviceId = 2; |
| 2585 | |
| 2586 | // Touch down on window |
| 2587 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2588 | .deviceId(touchDeviceId) |
| 2589 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2590 | .build()); |
| 2591 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2592 | .deviceId(touchDeviceId) |
| 2593 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2594 | .build()); |
| 2595 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2596 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2597 | |
| 2598 | // Stylus hover on the window |
| 2599 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2600 | .deviceId(stylusDeviceId) |
| 2601 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2602 | .build()); |
| 2603 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2604 | .deviceId(stylusDeviceId) |
| 2605 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2606 | .build()); |
| 2607 | // Stylus hover movement is dropped |
| 2608 | |
| 2609 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2610 | .deviceId(touchDeviceId) |
| 2611 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2612 | .build()); |
| 2613 | // Subsequent touch movements are delivered correctly |
| 2614 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2615 | WithCoords(142, 147))); |
| 2616 | } |
| 2617 | |
| 2618 | /** |
| 2619 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2620 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2621 | * become active. |
| 2622 | */ |
| 2623 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
| 2624 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2625 | sp<FakeWindowHandle> window = |
| 2626 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2627 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2628 | |
| 2629 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2630 | |
| 2631 | constexpr int32_t stylusDeviceId1 = 3; |
| 2632 | constexpr int32_t stylusDeviceId2 = 5; |
| 2633 | |
| 2634 | // Touch down on window |
| 2635 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2636 | .deviceId(stylusDeviceId1) |
| 2637 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2638 | .build()); |
| 2639 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2640 | .deviceId(stylusDeviceId1) |
| 2641 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2642 | .build()); |
| 2643 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2644 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2645 | |
| 2646 | // Second stylus down |
| 2647 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2648 | .deviceId(stylusDeviceId2) |
| 2649 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2650 | .build()); |
| 2651 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2652 | .deviceId(stylusDeviceId2) |
| 2653 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2654 | .build()); |
| 2655 | |
| 2656 | // First stylus is canceled, second one takes over. |
| 2657 | window->consumeMotionEvent( |
| 2658 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2659 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2660 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2661 | |
| 2662 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2663 | .deviceId(stylusDeviceId1) |
| 2664 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2665 | .build()); |
| 2666 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2667 | window->assertNoEvents(); |
| 2668 | } |
| 2669 | |
| 2670 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2671 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2672 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2673 | */ |
| 2674 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
| 2675 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2676 | sp<FakeWindowHandle> window = |
| 2677 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2678 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2679 | |
| 2680 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2681 | |
| 2682 | constexpr int32_t touchDeviceId = 4; |
| 2683 | constexpr int32_t stylusDeviceId = 2; |
| 2684 | |
| 2685 | // Touch down on window |
| 2686 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2687 | .deviceId(touchDeviceId) |
| 2688 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2689 | .build()); |
| 2690 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2691 | .deviceId(touchDeviceId) |
| 2692 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2693 | .build()); |
| 2694 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2695 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2696 | |
| 2697 | // Stylus down on the window |
| 2698 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2699 | .deviceId(stylusDeviceId) |
| 2700 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2701 | .build()); |
| 2702 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2703 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2704 | |
| 2705 | // Subsequent stylus movements are delivered correctly |
| 2706 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2707 | .deviceId(stylusDeviceId) |
| 2708 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2709 | .build()); |
| 2710 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2711 | WithCoords(101, 111))); |
| 2712 | } |
| 2713 | |
| 2714 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2715 | * Two windows: a window on the left and a window on the right. |
| 2716 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2717 | * down. Then, on the left window, also place second touch pointer down. |
| 2718 | * This test tries to reproduce a crash. |
| 2719 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2720 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2721 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2722 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2723 | sp<FakeWindowHandle> leftWindow = |
| 2724 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2725 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2726 | |
| 2727 | sp<FakeWindowHandle> rightWindow = |
| 2728 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2729 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2730 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2731 | mDispatcher->onWindowInfosChanged( |
| 2732 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2733 | |
| 2734 | const int32_t touchDeviceId = 4; |
| 2735 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2736 | |
| 2737 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2738 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2739 | .deviceId(mouseDeviceId) |
| 2740 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2741 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2742 | leftWindow->consumeMotionEvent( |
| 2743 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2744 | |
| 2745 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2746 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2747 | .deviceId(mouseDeviceId) |
| 2748 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2749 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2750 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2751 | |
| 2752 | leftWindow->consumeMotionEvent( |
| 2753 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2754 | leftWindow->consumeMotionEvent( |
| 2755 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2756 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2757 | mDispatcher->notifyMotion( |
| 2758 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2759 | .deviceId(mouseDeviceId) |
| 2760 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2761 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2762 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2763 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2764 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2765 | |
| 2766 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2767 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2768 | .deviceId(touchDeviceId) |
| 2769 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2770 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2771 | leftWindow->assertNoEvents(); |
| 2772 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2773 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2774 | |
| 2775 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2776 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2777 | .deviceId(touchDeviceId) |
| 2778 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2779 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2780 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2781 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2782 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2783 | // be canceled. |
| 2784 | leftWindow->consumeMotionEvent( |
| 2785 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2786 | leftWindow->consumeMotionEvent( |
| 2787 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2788 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2789 | // current implementation. |
| 2790 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2791 | rightWindow->consumeMotionEvent( |
| 2792 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2793 | |
| 2794 | leftWindow->assertNoEvents(); |
| 2795 | rightWindow->assertNoEvents(); |
| 2796 | } |
| 2797 | |
| 2798 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2799 | * Two windows: a window on the left and a window on the right. |
| 2800 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2801 | */ |
| 2802 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2803 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2804 | sp<FakeWindowHandle> leftWindow = |
| 2805 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2806 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2807 | |
| 2808 | sp<FakeWindowHandle> rightWindow = |
| 2809 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2810 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2811 | |
| 2812 | mDispatcher->onWindowInfosChanged( |
| 2813 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2814 | |
| 2815 | const int32_t stylusDeviceId = 3; |
| 2816 | const int32_t mouseDeviceId = 6; |
| 2817 | |
| 2818 | // Start hovering over the left window |
| 2819 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2820 | .deviceId(mouseDeviceId) |
| 2821 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2822 | .build()); |
| 2823 | leftWindow->consumeMotionEvent( |
| 2824 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2825 | |
| 2826 | // Stylus hovered on right window |
| 2827 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2828 | .deviceId(stylusDeviceId) |
| 2829 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2830 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2831 | rightWindow->consumeMotionEvent( |
| 2832 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2833 | |
| 2834 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2835 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2836 | .deviceId(mouseDeviceId) |
| 2837 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2838 | .build()); |
| 2839 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2840 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2841 | |
| 2842 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2843 | .deviceId(stylusDeviceId) |
| 2844 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2845 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2846 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2847 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2848 | |
| 2849 | leftWindow->assertNoEvents(); |
| 2850 | rightWindow->assertNoEvents(); |
| 2851 | } |
| 2852 | |
| 2853 | /** |
| 2854 | * Three windows: a window on the left and a window on the right. |
| 2855 | * And a spy window that's positioned above all of them. |
| 2856 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2857 | * Check the stream that's received by the spy. |
| 2858 | */ |
| 2859 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 2860 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2861 | |
| 2862 | sp<FakeWindowHandle> spyWindow = |
| 2863 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2864 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2865 | spyWindow->setTrustedOverlay(true); |
| 2866 | spyWindow->setSpy(true); |
| 2867 | |
| 2868 | sp<FakeWindowHandle> leftWindow = |
| 2869 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2870 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2871 | |
| 2872 | sp<FakeWindowHandle> rightWindow = |
| 2873 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2874 | |
| 2875 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2876 | |
| 2877 | mDispatcher->onWindowInfosChanged( |
| 2878 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2879 | |
| 2880 | const int32_t stylusDeviceId = 1; |
| 2881 | const int32_t touchDeviceId = 2; |
| 2882 | |
| 2883 | // Stylus down on the left window |
| 2884 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2885 | .deviceId(stylusDeviceId) |
| 2886 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2887 | .build()); |
| 2888 | leftWindow->consumeMotionEvent( |
| 2889 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2890 | spyWindow->consumeMotionEvent( |
| 2891 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2892 | |
| 2893 | // Touch down on the right window |
| 2894 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2895 | .deviceId(touchDeviceId) |
| 2896 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2897 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2898 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2899 | rightWindow->consumeMotionEvent( |
| 2900 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2901 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2902 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 2903 | // already has an active stylus gesture. |
| 2904 | |
| 2905 | // 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] | 2906 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2907 | .deviceId(stylusDeviceId) |
| 2908 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2909 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2910 | leftWindow->consumeMotionEvent( |
| 2911 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2912 | spyWindow->consumeMotionEvent( |
| 2913 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2914 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2915 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2916 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2917 | .deviceId(touchDeviceId) |
| 2918 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2919 | .build()); |
| 2920 | rightWindow->consumeMotionEvent( |
| 2921 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2922 | |
| 2923 | spyWindow->assertNoEvents(); |
| 2924 | leftWindow->assertNoEvents(); |
| 2925 | rightWindow->assertNoEvents(); |
| 2926 | } |
| 2927 | |
| 2928 | /** |
| 2929 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 2930 | * both. |
| 2931 | * Check hover in left window and touch down in the right window. |
| 2932 | * At first, spy should receive hover, but the touch down should cancel hovering inside spy. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2933 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 2934 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2935 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2936 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlockedByTouchWithSpy) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2937 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2938 | |
| 2939 | sp<FakeWindowHandle> spyWindow = |
| 2940 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2941 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2942 | spyWindow->setTrustedOverlay(true); |
| 2943 | spyWindow->setSpy(true); |
| 2944 | |
| 2945 | sp<FakeWindowHandle> leftWindow = |
| 2946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2947 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2948 | |
| 2949 | sp<FakeWindowHandle> rightWindow = |
| 2950 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2951 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2952 | |
| 2953 | mDispatcher->onWindowInfosChanged( |
| 2954 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2955 | |
| 2956 | const int32_t stylusDeviceId = 1; |
| 2957 | const int32_t touchDeviceId = 2; |
| 2958 | |
| 2959 | // Stylus hover on the left window |
| 2960 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2961 | .deviceId(stylusDeviceId) |
| 2962 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2963 | .build()); |
| 2964 | leftWindow->consumeMotionEvent( |
| 2965 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2966 | spyWindow->consumeMotionEvent( |
| 2967 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2968 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2969 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 2970 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2971 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2972 | .deviceId(touchDeviceId) |
| 2973 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2974 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2975 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2976 | spyWindow->consumeMotionEvent( |
| 2977 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 2978 | spyWindow->consumeMotionEvent( |
| 2979 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2980 | rightWindow->consumeMotionEvent( |
| 2981 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2982 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 2983 | // Stylus movements continue. They should be delivered to the left window only. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2984 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_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_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2990 | |
| 2991 | // Touch movements continue. They should be delivered to the right window and to the spy |
| 2992 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2993 | .deviceId(touchDeviceId) |
| 2994 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 2995 | .build()); |
| 2996 | spyWindow->consumeMotionEvent( |
| 2997 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2998 | rightWindow->consumeMotionEvent( |
| 2999 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3000 | |
| 3001 | spyWindow->assertNoEvents(); |
| 3002 | leftWindow->assertNoEvents(); |
| 3003 | rightWindow->assertNoEvents(); |
| 3004 | } |
| 3005 | |
| 3006 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3007 | * On a single window, use two different devices: mouse and touch. |
| 3008 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3009 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3010 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3011 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3012 | * represent a new gesture. |
| 3013 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3014 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3015 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3016 | sp<FakeWindowHandle> window = |
| 3017 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3018 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3019 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3020 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3021 | |
| 3022 | const int32_t touchDeviceId = 4; |
| 3023 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3024 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3025 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3026 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3027 | .deviceId(touchDeviceId) |
| 3028 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3029 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3030 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3031 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3032 | .deviceId(touchDeviceId) |
| 3033 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3034 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3035 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3036 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3037 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3038 | .deviceId(touchDeviceId) |
| 3039 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3040 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3041 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3042 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3043 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3044 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3045 | |
| 3046 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3047 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3048 | .deviceId(mouseDeviceId) |
| 3049 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3050 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3051 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3052 | |
| 3053 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3054 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3055 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3056 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3057 | mDispatcher->notifyMotion( |
| 3058 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3059 | .deviceId(mouseDeviceId) |
| 3060 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3061 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3062 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3063 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3064 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3065 | |
| 3066 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3067 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3068 | .deviceId(touchDeviceId) |
| 3069 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3070 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3071 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3072 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3073 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3074 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3075 | // However, mouse movements should continue to work. |
| 3076 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3077 | .deviceId(mouseDeviceId) |
| 3078 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3079 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3080 | .build()); |
| 3081 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3082 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3083 | window->assertNoEvents(); |
| 3084 | } |
| 3085 | |
| 3086 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3087 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3088 | * the injected event. |
| 3089 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3090 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3091 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3092 | sp<FakeWindowHandle> window = |
| 3093 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3094 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3095 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3096 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3097 | |
| 3098 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3099 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3100 | // completion. |
| 3101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3102 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3103 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3104 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3105 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3106 | .build())); |
| 3107 | window->consumeMotionEvent( |
| 3108 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3109 | |
| 3110 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3111 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3112 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3113 | .deviceId(touchDeviceId) |
| 3114 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3115 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3116 | |
| 3117 | window->consumeMotionEvent( |
| 3118 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3119 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3120 | } |
| 3121 | |
| 3122 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3123 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3124 | * |
| 3125 | * Two windows: a window on the left and a window on the right. |
| 3126 | * Mouse is hovered over the left window. |
| 3127 | * Next, we tap on the left window, where the cursor was last seen. |
| 3128 | * |
| 3129 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3130 | * the left window. |
| 3131 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3132 | * window (first), and then another on the left window (second). |
| 3133 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3134 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3135 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3136 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3137 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3138 | sp<FakeWindowHandle> leftWindow = |
| 3139 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3140 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3141 | |
| 3142 | sp<FakeWindowHandle> rightWindow = |
| 3143 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3144 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3145 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3146 | mDispatcher->onWindowInfosChanged( |
| 3147 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3148 | |
| 3149 | const int32_t mouseDeviceId = 6; |
| 3150 | const int32_t touchDeviceId = 4; |
| 3151 | // Hover over the left window. Keep the cursor there. |
| 3152 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3153 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3154 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3155 | AINPUT_SOURCE_MOUSE) |
| 3156 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3157 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3158 | .build())); |
| 3159 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3160 | |
| 3161 | // Tap on left window |
| 3162 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3163 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3164 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3165 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3166 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3167 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3168 | .build())); |
| 3169 | |
| 3170 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3171 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3172 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3173 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3174 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3175 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3176 | .build())); |
| 3177 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3178 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3179 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3180 | |
| 3181 | // First finger down on right window |
| 3182 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3183 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3184 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3185 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3186 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3187 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3188 | .build())); |
| 3189 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3190 | |
| 3191 | // Second finger down on the left window |
| 3192 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3193 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3194 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3195 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3196 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3197 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3198 | .build())); |
| 3199 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3200 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3201 | |
| 3202 | // No more events |
| 3203 | leftWindow->assertNoEvents(); |
| 3204 | rightWindow->assertNoEvents(); |
| 3205 | } |
| 3206 | |
| 3207 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3208 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3209 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3210 | * touch is gone, stylus hovering should start working again. |
| 3211 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 3212 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDroppedWhenTouchTap) { |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3213 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3214 | sp<FakeWindowHandle> window = |
| 3215 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3216 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3217 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3218 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3219 | |
| 3220 | const int32_t stylusDeviceId = 5; |
| 3221 | const int32_t touchDeviceId = 4; |
| 3222 | // Start hovering with stylus |
| 3223 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3224 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3225 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3226 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3227 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3228 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3229 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3230 | |
| 3231 | // Finger down on the window |
| 3232 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3233 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3234 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3235 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3236 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3237 | .build())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 3238 | // The touch device should cause hover to stop! |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3239 | window->consumeMotionEvent( |
| 3240 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3241 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3242 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 3243 | // Continue hovering with stylus. |
| 3244 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3245 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3246 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3247 | AINPUT_SOURCE_STYLUS) |
| 3248 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3249 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3250 | .build())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 3251 | // Hovers are now ignored |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3252 | |
| 3253 | // Lift up the finger |
| 3254 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3255 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3256 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3257 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3258 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3259 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3260 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3261 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3262 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3263 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3264 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3265 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3266 | AINPUT_SOURCE_STYLUS) |
| 3267 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3268 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3269 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3270 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3271 | WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3272 | window->assertNoEvents(); |
| 3273 | } |
| 3274 | |
| 3275 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3276 | * A spy window above a window with no input channel. |
| 3277 | * Start hovering with a stylus device, and then tap with it. |
| 3278 | * Ensure spy window receives the entire sequence. |
| 3279 | */ |
| 3280 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3281 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3282 | sp<FakeWindowHandle> spyWindow = |
| 3283 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3284 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3285 | spyWindow->setTrustedOverlay(true); |
| 3286 | spyWindow->setSpy(true); |
| 3287 | sp<FakeWindowHandle> window = |
| 3288 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3289 | window->setNoInputChannel(true); |
| 3290 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3291 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3292 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3293 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3294 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3295 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3296 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3297 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3298 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3299 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3300 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3301 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3302 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3303 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3304 | |
| 3305 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3307 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3308 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3309 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3310 | |
| 3311 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3312 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3313 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3314 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3315 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3316 | |
| 3317 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3318 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3319 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3320 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3321 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3322 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3323 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3324 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3325 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3326 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3327 | |
| 3328 | // No more events |
| 3329 | spyWindow->assertNoEvents(); |
| 3330 | window->assertNoEvents(); |
| 3331 | } |
| 3332 | |
| 3333 | /** |
| 3334 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3335 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3336 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3337 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3338 | */ |
| 3339 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3340 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3341 | sp<FakeWindowHandle> spyWindow = |
| 3342 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3343 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3344 | spyWindow->setTrustedOverlay(true); |
| 3345 | spyWindow->setSpy(true); |
| 3346 | sp<FakeWindowHandle> window = |
| 3347 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3348 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3349 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3350 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3351 | |
| 3352 | const int32_t mouseDeviceId = 7; |
| 3353 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3354 | |
| 3355 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3356 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3357 | .deviceId(mouseDeviceId) |
| 3358 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3359 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3360 | spyWindow->consumeMotionEvent( |
| 3361 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3362 | window->consumeMotionEvent( |
| 3363 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3364 | |
| 3365 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3366 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3367 | .deviceId(touchDeviceId) |
| 3368 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3369 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3370 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3371 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3372 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3373 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3374 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3375 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3376 | .deviceId(touchDeviceId) |
| 3377 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3378 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3379 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3380 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3381 | |
| 3382 | // Pilfer the stream |
| 3383 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3384 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3385 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3386 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3387 | .deviceId(touchDeviceId) |
| 3388 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3389 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3390 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3391 | |
| 3392 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3393 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3394 | .deviceId(mouseDeviceId) |
| 3395 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3396 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3397 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3398 | |
| 3399 | spyWindow->consumeMotionEvent( |
| 3400 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3401 | spyWindow->consumeMotionEvent( |
| 3402 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3403 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3404 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3405 | mDispatcher->notifyMotion( |
| 3406 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3407 | .deviceId(mouseDeviceId) |
| 3408 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3409 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3410 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3411 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3412 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3413 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3414 | |
| 3415 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3416 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3417 | .deviceId(mouseDeviceId) |
| 3418 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3419 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3420 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3421 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3422 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3423 | |
| 3424 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3425 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3426 | .deviceId(touchDeviceId) |
| 3427 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3428 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3429 | |
| 3430 | // No more events |
| 3431 | spyWindow->assertNoEvents(); |
| 3432 | window->assertNoEvents(); |
| 3433 | } |
| 3434 | |
| 3435 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3436 | * On the display, have a single window, and also an area where there's no window. |
| 3437 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3438 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3439 | */ |
| 3440 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3441 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3442 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3443 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3444 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3445 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3446 | |
| 3447 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3448 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3449 | |
| 3450 | mDispatcher->waitForIdle(); |
| 3451 | window->assertNoEvents(); |
| 3452 | |
| 3453 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3454 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3455 | mDispatcher->waitForIdle(); |
| 3456 | window->consumeMotionDown(); |
| 3457 | } |
| 3458 | |
| 3459 | /** |
| 3460 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3461 | * non-touchable window. |
| 3462 | */ |
| 3463 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3464 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3465 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3466 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3467 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3468 | window1->setTouchable(false); |
| 3469 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3470 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3471 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3472 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3473 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3474 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3475 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3476 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3477 | |
| 3478 | mDispatcher->waitForIdle(); |
| 3479 | window1->assertNoEvents(); |
| 3480 | window2->assertNoEvents(); |
| 3481 | |
| 3482 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3483 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3484 | mDispatcher->waitForIdle(); |
| 3485 | window2->consumeMotionDown(); |
| 3486 | } |
| 3487 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3488 | /** |
| 3489 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3490 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3491 | */ |
| 3492 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3493 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3494 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3495 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3496 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3497 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3498 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3499 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3500 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3501 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3502 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3503 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3504 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3505 | |
| 3506 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3507 | |
| 3508 | MotionEvent* motionEvent1 = window1->consumeMotion(); |
| 3509 | ASSERT_NE(motionEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3510 | window2->assertNoEvents(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3511 | nsecs_t downTimeForWindow1 = motionEvent1->getDownTime(); |
| 3512 | ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3513 | |
| 3514 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3515 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3516 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3517 | MotionEvent* motionEvent2 = window2->consumeMotion(); |
| 3518 | ASSERT_NE(motionEvent2, nullptr); |
| 3519 | nsecs_t downTimeForWindow2 = motionEvent2->getDownTime(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3520 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3521 | ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3522 | |
| 3523 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3524 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3525 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3526 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3527 | |
| 3528 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3529 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3530 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3531 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3532 | |
| 3533 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3534 | window1->consumeMotionMove(); |
| 3535 | window1->assertNoEvents(); |
| 3536 | |
| 3537 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3538 | mDispatcher->notifyMotion( |
| 3539 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3540 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3541 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3542 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3543 | mDispatcher->notifyMotion( |
| 3544 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3545 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3546 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3549 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3550 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3551 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3552 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3553 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3554 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3555 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3556 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3557 | |
| 3558 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3559 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3560 | mDispatcher->onWindowInfosChanged( |
| 3561 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3562 | |
| 3563 | // 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] | 3564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3565 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3566 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3567 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3568 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3569 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3570 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3571 | |
| 3572 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3574 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3575 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3576 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3577 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3578 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3579 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3580 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3581 | |
| 3582 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3584 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3585 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3586 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3587 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3588 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3589 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3590 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3591 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3592 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3593 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3594 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3595 | AINPUT_SOURCE_MOUSE) |
| 3596 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3597 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3598 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3599 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3600 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3601 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3603 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3604 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3605 | AINPUT_SOURCE_MOUSE) |
| 3606 | .buttonState(0) |
| 3607 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3608 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3609 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3610 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3611 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3612 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3613 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3614 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3615 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3616 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3617 | .build())); |
| 3618 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3619 | |
| 3620 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3622 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3623 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3624 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3625 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3626 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3627 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3628 | |
| 3629 | // No more events |
| 3630 | windowLeft->assertNoEvents(); |
| 3631 | windowRight->assertNoEvents(); |
| 3632 | } |
| 3633 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3634 | /** |
| 3635 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3636 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3637 | * currently active gesture should be canceled, and the new one should proceed. |
| 3638 | */ |
| 3639 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3640 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3641 | sp<FakeWindowHandle> window = |
| 3642 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3643 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3644 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3645 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3646 | |
| 3647 | const int32_t touchDeviceId = 4; |
| 3648 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3649 | |
| 3650 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3651 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3652 | .deviceId(touchDeviceId) |
| 3653 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3654 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3655 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3656 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3657 | .deviceId(touchDeviceId) |
| 3658 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3659 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3660 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3661 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3662 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3663 | |
| 3664 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3665 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3666 | .deviceId(mouseDeviceId) |
| 3667 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3668 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3669 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3670 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3671 | WithPointerCount(2u))); |
| 3672 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3673 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3674 | mDispatcher->notifyMotion( |
| 3675 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3676 | .deviceId(mouseDeviceId) |
| 3677 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3678 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3679 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3680 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3681 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3682 | |
| 3683 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3684 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3685 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3686 | .deviceId(touchDeviceId) |
| 3687 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3688 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3689 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3690 | window->assertNoEvents(); |
| 3691 | } |
| 3692 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3693 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3694 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3695 | |
| 3696 | sp<FakeWindowHandle> spyWindow = |
| 3697 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3698 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3699 | spyWindow->setTrustedOverlay(true); |
| 3700 | spyWindow->setSpy(true); |
| 3701 | sp<FakeWindowHandle> window = |
| 3702 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3703 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3704 | |
| 3705 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3706 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3707 | |
| 3708 | // Send mouse cursor to the window |
| 3709 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3710 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3711 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3712 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3713 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3714 | .build())); |
| 3715 | |
| 3716 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3717 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3718 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3719 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3720 | |
| 3721 | window->assertNoEvents(); |
| 3722 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3723 | } |
| 3724 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3725 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3726 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3727 | |
| 3728 | sp<FakeWindowHandle> spyWindow = |
| 3729 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3730 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3731 | spyWindow->setTrustedOverlay(true); |
| 3732 | spyWindow->setSpy(true); |
| 3733 | sp<FakeWindowHandle> window = |
| 3734 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3735 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3736 | |
| 3737 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3738 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3739 | |
| 3740 | // Send mouse cursor to the window |
| 3741 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3742 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3743 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3744 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3745 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3746 | .build())); |
| 3747 | |
| 3748 | // Move mouse cursor |
| 3749 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3750 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3751 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3752 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3753 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3754 | .build())); |
| 3755 | |
| 3756 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3757 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3758 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3759 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3760 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3761 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3762 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3763 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3764 | // Touch down on the window |
| 3765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3766 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3767 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3768 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3769 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3770 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3771 | .build())); |
| 3772 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3773 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3774 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3775 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3776 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3777 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3778 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3779 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3780 | |
| 3781 | // pilfer the motion, retaining the gesture on the spy window. |
| 3782 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3783 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3784 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3785 | |
| 3786 | // Touch UP on the window |
| 3787 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3788 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3789 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3790 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3791 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3792 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3793 | .build())); |
| 3794 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3795 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3796 | |
| 3797 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3798 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3799 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3800 | |
| 3801 | // One more tap - DOWN |
| 3802 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3803 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3804 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3805 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3806 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3807 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3808 | .build())); |
| 3809 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3810 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3811 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3812 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3813 | |
| 3814 | // Touch UP on the window |
| 3815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3816 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3817 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3818 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3819 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3820 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3821 | .build())); |
| 3822 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3823 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3824 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3825 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3826 | |
| 3827 | window->assertNoEvents(); |
| 3828 | spyWindow->assertNoEvents(); |
| 3829 | } |
| 3830 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3831 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3832 | // directly in this test. |
| 3833 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3834 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3835 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3836 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3837 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3838 | |
| 3839 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3840 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3841 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3842 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3843 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3844 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3845 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3846 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3847 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3848 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3849 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3850 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3851 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3852 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3853 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3854 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3855 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3856 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3857 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3858 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3859 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3860 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3861 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3862 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3863 | AINPUT_SOURCE_MOUSE) |
| 3864 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3865 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3866 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3867 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3868 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3869 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3871 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3872 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3873 | AINPUT_SOURCE_MOUSE) |
| 3874 | .buttonState(0) |
| 3875 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3876 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3877 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3878 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3879 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3880 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3881 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3882 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3883 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3884 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3885 | .build())); |
| 3886 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3887 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3888 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 3889 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 3890 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3891 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3892 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3893 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3894 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3895 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3896 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3897 | } |
| 3898 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3899 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3900 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3901 | * is generated. |
| 3902 | */ |
| 3903 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3904 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3905 | sp<FakeWindowHandle> window = |
| 3906 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3907 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3908 | |
| 3909 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3910 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3911 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3912 | |
| 3913 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3914 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3915 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3916 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3917 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3918 | .build())); |
| 3919 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3920 | |
| 3921 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3922 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3923 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3924 | } |
| 3925 | |
| 3926 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 3927 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 3928 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 3929 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 3930 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 3931 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 3932 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3933 | sp<FakeWindowHandle> window = |
| 3934 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3935 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3936 | |
| 3937 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3938 | |
| 3939 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3940 | |
| 3941 | MotionEventBuilder hoverEnterBuilder = |
| 3942 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3943 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3944 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 3945 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3946 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 3947 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3948 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 3949 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3950 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3951 | } |
| 3952 | |
| 3953 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3954 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3955 | */ |
| 3956 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3957 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3958 | sp<FakeWindowHandle> window = |
| 3959 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3960 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3961 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3962 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3963 | |
| 3964 | const int32_t mouseDeviceId = 7; |
| 3965 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3966 | |
| 3967 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3968 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3969 | .deviceId(mouseDeviceId) |
| 3970 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 3971 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3972 | window->consumeMotionEvent( |
| 3973 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3974 | |
| 3975 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3976 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3977 | .deviceId(touchDeviceId) |
| 3978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3979 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3980 | |
| 3981 | window->consumeMotionEvent( |
| 3982 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3983 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3984 | } |
| 3985 | |
| 3986 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3987 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3988 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3989 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3990 | */ |
| 3991 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3992 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3993 | sp<FakeWindowHandle> window = |
| 3994 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3995 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3996 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3997 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3998 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 3999 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4000 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4001 | ASSERT_NO_FATAL_FAILURE( |
| 4002 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4003 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4004 | |
| 4005 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4006 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4007 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4008 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4009 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4010 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4011 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4012 | |
| 4013 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4014 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4015 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4016 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4017 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4018 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4019 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4020 | ASSERT_NO_FATAL_FAILURE( |
| 4021 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4022 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4023 | } |
| 4024 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4025 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4026 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4027 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4028 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4029 | ADISPLAY_ID_DEFAULT); |
| 4030 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4031 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4032 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4033 | SECOND_DISPLAY_ID); |
| 4034 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4035 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4036 | mDispatcher->onWindowInfosChanged( |
| 4037 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4038 | |
| 4039 | // Set cursor position in window in default display and check that hover enter and move |
| 4040 | // events are generated. |
| 4041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4042 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4043 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4044 | AINPUT_SOURCE_MOUSE) |
| 4045 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4046 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4047 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4048 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4049 | |
| 4050 | // Remove all windows in secondary display and check that no event happens on window in |
| 4051 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4052 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4053 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4054 | windowDefaultDisplay->assertNoEvents(); |
| 4055 | |
| 4056 | // Move cursor position in window in default display and check that only hover move |
| 4057 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4058 | mDispatcher->onWindowInfosChanged( |
| 4059 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4060 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4061 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4062 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4063 | AINPUT_SOURCE_MOUSE) |
| 4064 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4065 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4066 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4067 | windowDefaultDisplay->consumeMotionEvent( |
| 4068 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4069 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4070 | windowDefaultDisplay->assertNoEvents(); |
| 4071 | } |
| 4072 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4073 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4074 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4075 | |
| 4076 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4077 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4078 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4079 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4080 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4081 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4082 | |
| 4083 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4084 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4085 | mDispatcher->onWindowInfosChanged( |
| 4086 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4087 | |
| 4088 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4089 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4090 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4091 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4092 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4093 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4094 | windowRight->assertNoEvents(); |
| 4095 | } |
| 4096 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4097 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4098 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4099 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4100 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4101 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4102 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4103 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4104 | setFocusedWindow(window); |
| 4105 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4106 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4107 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4108 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4109 | |
| 4110 | // Window should receive key down event. |
| 4111 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4112 | |
| 4113 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4114 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4115 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4116 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4117 | AKEY_EVENT_FLAG_CANCELED); |
| 4118 | } |
| 4119 | |
| 4120 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4121 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4122 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4123 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4124 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4125 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4126 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4127 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4128 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4129 | |
| 4130 | // Window should receive motion down event. |
| 4131 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4132 | |
| 4133 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4134 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4135 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4136 | window->consumeMotionEvent( |
| 4137 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4138 | } |
| 4139 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4140 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4141 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4142 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4143 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4144 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4145 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4146 | |
| 4147 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4148 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 4149 | .build()); |
| 4150 | |
| 4151 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4152 | |
| 4153 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 4154 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 4155 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4156 | |
| 4157 | // After the device has been reset, a new hovering stream can be sent to the window |
| 4158 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4159 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 4160 | .build()); |
| 4161 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4162 | } |
| 4163 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4164 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 4165 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4166 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4167 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4168 | window->setFocusable(true); |
| 4169 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4170 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4171 | setFocusedWindow(window); |
| 4172 | |
| 4173 | window->consumeFocusEvent(true); |
| 4174 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4175 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4176 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 4177 | const nsecs_t injectTime = keyArgs.eventTime; |
| 4178 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4179 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4180 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 4181 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4182 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 4183 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 4184 | } |
| 4185 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4186 | /** |
| 4187 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 4188 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4189 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 4190 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4191 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4192 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4193 | window->setFocusable(true); |
| 4194 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4195 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4196 | setFocusedWindow(window); |
| 4197 | |
| 4198 | window->consumeFocusEvent(true); |
| 4199 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4200 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4201 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4202 | |
| 4203 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 4204 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 4205 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 4206 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 4207 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4208 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4209 | } |
| 4210 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4211 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4212 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 4213 | * WATCH_OUTSIDE_TOUCH. |
| 4214 | * Both windows are owned by the same UID. |
| 4215 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 4216 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 4217 | */ |
| 4218 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 4219 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4220 | sp<FakeWindowHandle> window = |
| 4221 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4222 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4223 | |
| 4224 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4225 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4226 | ADISPLAY_ID_DEFAULT); |
| 4227 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4228 | outsideWindow->setWatchOutsideTouch(true); |
| 4229 | // 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] | 4230 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4231 | |
| 4232 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4233 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4234 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4235 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4236 | window->consumeMotionDown(); |
| 4237 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 4238 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 4239 | outsideWindow->consumeMotionEvent( |
| 4240 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 4241 | } |
| 4242 | |
| 4243 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4244 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 4245 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 4246 | * ACTION_OUTSIDE event is sent per gesture. |
| 4247 | */ |
| 4248 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 4249 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 4250 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4251 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4252 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4253 | window->setWatchOutsideTouch(true); |
| 4254 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4255 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4256 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4257 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4258 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4259 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4260 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 4261 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4262 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4263 | mDispatcher->onWindowInfosChanged( |
| 4264 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4265 | |
| 4266 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4267 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4268 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4269 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4270 | window->assertNoEvents(); |
| 4271 | secondWindow->assertNoEvents(); |
| 4272 | |
| 4273 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 4274 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4275 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4276 | ADISPLAY_ID_DEFAULT, |
| 4277 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4278 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 4279 | window->consumeMotionEvent( |
| 4280 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4281 | secondWindow->consumeMotionDown(); |
| 4282 | thirdWindow->assertNoEvents(); |
| 4283 | |
| 4284 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 4285 | // 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] | 4286 | mDispatcher->notifyMotion( |
| 4287 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4288 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4289 | window->assertNoEvents(); |
| 4290 | secondWindow->consumeMotionMove(); |
| 4291 | thirdWindow->consumeMotionDown(); |
| 4292 | } |
| 4293 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4294 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 4295 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4296 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4297 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4298 | window->setFocusable(true); |
| 4299 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4300 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4301 | setFocusedWindow(window); |
| 4302 | |
| 4303 | window->consumeFocusEvent(true); |
| 4304 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4305 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4306 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 4307 | mDispatcher->notifyKey(keyDown); |
| 4308 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4309 | |
| 4310 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4311 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4312 | |
| 4313 | // 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] | 4314 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4315 | |
| 4316 | window->consumeFocusEvent(false); |
| 4317 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4318 | mDispatcher->notifyKey(keyDown); |
| 4319 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4320 | window->assertNoEvents(); |
| 4321 | } |
| 4322 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4323 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 4324 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4325 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4326 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4327 | // Ensure window is non-split and have some transform. |
| 4328 | window->setPreventSplitting(true); |
| 4329 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4330 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4331 | |
| 4332 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4333 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4334 | {50, 50})) |
| 4335 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4336 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4337 | |
| 4338 | const MotionEvent secondFingerDownEvent = |
| 4339 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4340 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4341 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4342 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4343 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4344 | .build(); |
| 4345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4346 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4347 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4348 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4349 | |
| 4350 | const MotionEvent* event = window->consumeMotion(); |
| 4351 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 4352 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 4353 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 4354 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 4355 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 4356 | } |
| 4357 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 4358 | /** |
| 4359 | * Two windows: a splittable and a non-splittable. |
| 4360 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 4361 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 4362 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 4363 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 4364 | * "incomplete" gestures. |
| 4365 | */ |
| 4366 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 4367 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4368 | sp<FakeWindowHandle> leftWindow = |
| 4369 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 4370 | ADISPLAY_ID_DEFAULT); |
| 4371 | leftWindow->setPreventSplitting(false); |
| 4372 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4373 | sp<FakeWindowHandle> rightWindow = |
| 4374 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 4375 | ADISPLAY_ID_DEFAULT); |
| 4376 | rightWindow->setPreventSplitting(true); |
| 4377 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4378 | mDispatcher->onWindowInfosChanged( |
| 4379 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 4380 | |
| 4381 | // Touch down on left, splittable window |
| 4382 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4383 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4384 | .build()); |
| 4385 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4386 | |
| 4387 | mDispatcher->notifyMotion( |
| 4388 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4389 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4390 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 4391 | .build()); |
| 4392 | leftWindow->assertNoEvents(); |
| 4393 | rightWindow->assertNoEvents(); |
| 4394 | } |
| 4395 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4396 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4397 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4398 | sp<FakeWindowHandle> window = |
| 4399 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4400 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4401 | sp<FakeWindowHandle> trustedOverlay = |
| 4402 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4403 | ADISPLAY_ID_DEFAULT); |
| 4404 | trustedOverlay->setSpy(true); |
| 4405 | trustedOverlay->setTrustedOverlay(true); |
| 4406 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4407 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4408 | |
| 4409 | // Start a three-finger touchpad swipe |
| 4410 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4411 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4412 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4413 | .build()); |
| 4414 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4415 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4416 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4417 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4418 | .build()); |
| 4419 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4420 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4421 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4422 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4423 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4424 | .build()); |
| 4425 | |
| 4426 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4427 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4428 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4429 | |
| 4430 | // Move the swipe a bit |
| 4431 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4432 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4433 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4434 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4435 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4436 | .build()); |
| 4437 | |
| 4438 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4439 | |
| 4440 | // End the swipe |
| 4441 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4442 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4443 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4444 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4445 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4446 | .build()); |
| 4447 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4448 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4449 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4450 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4451 | .build()); |
| 4452 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4453 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4454 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4455 | .build()); |
| 4456 | |
| 4457 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4458 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4459 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4460 | |
| 4461 | window->assertNoEvents(); |
| 4462 | } |
| 4463 | |
| 4464 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4465 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4466 | sp<FakeWindowHandle> window = |
| 4467 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4468 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4469 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4470 | |
| 4471 | // Start a three-finger touchpad swipe |
| 4472 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4473 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4474 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4475 | .build()); |
| 4476 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4477 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4478 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4479 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4480 | .build()); |
| 4481 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4482 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4483 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4484 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4485 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4486 | .build()); |
| 4487 | |
| 4488 | // Move the swipe a bit |
| 4489 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4490 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4491 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4492 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4493 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4494 | .build()); |
| 4495 | |
| 4496 | // End the swipe |
| 4497 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4498 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4499 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4500 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4501 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4502 | .build()); |
| 4503 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4504 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4505 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4506 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4507 | .build()); |
| 4508 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4509 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4510 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4511 | .build()); |
| 4512 | |
| 4513 | window->assertNoEvents(); |
| 4514 | } |
| 4515 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4516 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4517 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 4518 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4519 | * 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] | 4520 | */ |
| 4521 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 4522 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4523 | sp<FakeWindowHandle> window = |
| 4524 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4525 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4526 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4527 | |
| 4528 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4529 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4530 | .downTime(baseTime + 10) |
| 4531 | .eventTime(baseTime + 10) |
| 4532 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4533 | .build()); |
| 4534 | |
| 4535 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4536 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4537 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4538 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4539 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4540 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4541 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4542 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4543 | .downTime(baseTime + 10) |
| 4544 | .eventTime(baseTime + 30) |
| 4545 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4546 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4547 | .build()); |
| 4548 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4549 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4550 | |
| 4551 | // 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] | 4552 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4553 | .downTime(baseTime + 10) |
| 4554 | .eventTime(baseTime + 40) |
| 4555 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4556 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4557 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4558 | |
| 4559 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4560 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4561 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4562 | .downTime(baseTime + 10) |
| 4563 | .eventTime(baseTime + 50) |
| 4564 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4565 | .build()); |
| 4566 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4567 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4568 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4569 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4570 | .downTime(baseTime + 60) |
| 4571 | .eventTime(baseTime + 60) |
| 4572 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4573 | .build()); |
| 4574 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4575 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4579 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 4580 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 4581 | * its coordinates should be converted by the transform of the windows of target screen. |
| 4582 | */ |
| 4583 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 4584 | // This case will create a window and a spy window on the default display and mirror |
| 4585 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 4586 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4587 | |
| 4588 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
| 4589 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4590 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 4591 | spyWindowDefaultDisplay->setSpy(true); |
| 4592 | |
| 4593 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4594 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4595 | ADISPLAY_ID_DEFAULT); |
| 4596 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 4597 | |
| 4598 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 4599 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 4600 | |
| 4601 | // Add the windows to the dispatcher |
| 4602 | mDispatcher->onWindowInfosChanged( |
| 4603 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 4604 | *windowSecondDisplay->getInfo()}, |
| 4605 | {}, |
| 4606 | 0, |
| 4607 | 0}); |
| 4608 | |
| 4609 | // Send down to ADISPLAY_ID_DEFAULT |
| 4610 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4611 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4612 | {100, 100})) |
| 4613 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4614 | |
| 4615 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 4616 | windowDefaultDisplay->consumeMotionDown(); |
| 4617 | |
| 4618 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 4619 | |
| 4620 | // windowDefaultDisplay gets cancel |
| 4621 | MotionEvent* event = windowDefaultDisplay->consumeMotion(); |
| 4622 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
| 4623 | |
| 4624 | // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the |
| 4625 | // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y |
| 4626 | // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of |
| 4627 | // SECOND_DISPLAY_ID, the x and y coordinates are 200 |
| 4628 | EXPECT_EQ(100, event->getX(0)); |
| 4629 | EXPECT_EQ(100, event->getY(0)); |
| 4630 | } |
| 4631 | |
| 4632 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4633 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4634 | * |
| 4635 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4636 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4637 | * space. |
| 4638 | */ |
| 4639 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4640 | public: |
| 4641 | void SetUp() override { |
| 4642 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4643 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4647 | gui::DisplayInfo info; |
| 4648 | info.displayId = displayId; |
| 4649 | info.transform = transform; |
| 4650 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4651 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4655 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4656 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4659 | void removeAllWindowsAndDisplays() { |
| 4660 | mDisplayInfos.clear(); |
| 4661 | mWindowInfos.clear(); |
| 4662 | } |
| 4663 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4664 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4665 | // on the display. |
| 4666 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4667 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4668 | // respectively. |
| 4669 | ui::Transform displayTransform; |
| 4670 | displayTransform.set(2, 0, 0, 4); |
| 4671 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4672 | |
| 4673 | std::shared_ptr<FakeApplicationHandle> application = |
| 4674 | std::make_shared<FakeApplicationHandle>(); |
| 4675 | |
| 4676 | // Add two windows to the display. Their frames are represented in the display space. |
| 4677 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4678 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4679 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4680 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 4681 | addWindow(firstWindow); |
| 4682 | |
| 4683 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4684 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4685 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4686 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 4687 | addWindow(secondWindow); |
| 4688 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 4689 | } |
| 4690 | |
| 4691 | private: |
| 4692 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 4693 | std::vector<gui::WindowInfo> mWindowInfos; |
| 4694 | }; |
| 4695 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4696 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4697 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4698 | // 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] | 4699 | // selected so that if the hit test was performed with the point and the bounds being in |
| 4700 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4701 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4702 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4703 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4704 | |
| 4705 | firstWindow->consumeMotionDown(); |
| 4706 | secondWindow->assertNoEvents(); |
| 4707 | } |
| 4708 | |
| 4709 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 4710 | // the event should be treated as being in the logical display space. |
| 4711 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 4712 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4713 | // Send down to the first window. The point is represented in the logical display space. The |
| 4714 | // point is selected so that if the hit test was done in logical display space, then it would |
| 4715 | // end up in the incorrect window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4716 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4717 | PointF{75 * 2, 55 * 4}); |
| 4718 | |
| 4719 | firstWindow->consumeMotionDown(); |
| 4720 | secondWindow->assertNoEvents(); |
| 4721 | } |
| 4722 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4723 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 4724 | // event should be treated as being in the logical display space. |
| 4725 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 4726 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4727 | |
| 4728 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4729 | ui::Transform injectedEventTransform; |
| 4730 | injectedEventTransform.set(matrix); |
| 4731 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 4732 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 4733 | |
| 4734 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4735 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4736 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4737 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4738 | .x(untransformedPoint.x) |
| 4739 | .y(untransformedPoint.y)) |
| 4740 | .build(); |
| 4741 | event.transform(matrix); |
| 4742 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4743 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4744 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4745 | |
| 4746 | firstWindow->consumeMotionDown(); |
| 4747 | secondWindow->assertNoEvents(); |
| 4748 | } |
| 4749 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4750 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4751 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4752 | |
| 4753 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4754 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4755 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4756 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4757 | |
| 4758 | firstWindow->assertNoEvents(); |
| 4759 | const MotionEvent* event = secondWindow->consumeMotion(); |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 4760 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4761 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 4762 | |
| 4763 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 4764 | EXPECT_EQ(300, event->getRawX(0)); |
| 4765 | EXPECT_EQ(880, event->getRawY(0)); |
| 4766 | |
| 4767 | // Ensure that the x and y values are in the window's coordinate space. |
| 4768 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4769 | // the logical display space. This will be the origin of the window space. |
| 4770 | EXPECT_EQ(100, event->getX(0)); |
| 4771 | EXPECT_EQ(80, event->getY(0)); |
| 4772 | } |
| 4773 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4774 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 4775 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4776 | // The monitor will always receive events in the logical display's coordinate space, because |
| 4777 | // it does not have a window. |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 4778 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4779 | |
| 4780 | // Send down to the first window. |
| 4781 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4782 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 4783 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4784 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4785 | |
| 4786 | // Second pointer goes down on second window. |
| 4787 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4788 | ADISPLAY_ID_DEFAULT, |
| 4789 | {PointF{50, 100}, PointF{150, 220}})); |
| 4790 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 4791 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 4792 | {1, PointF{300, 880}}}; |
| 4793 | monitor.consumeMotionEvent( |
| 4794 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 4795 | |
| 4796 | mDispatcher->cancelCurrentTouch(); |
| 4797 | |
| 4798 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 4799 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 4800 | monitor.consumeMotionEvent( |
| 4801 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 4802 | } |
| 4803 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4804 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 4805 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4806 | |
| 4807 | // Send down to the first window. |
| 4808 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4809 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 4810 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4811 | |
| 4812 | // The pointer is transferred to the second window, and the second window receives it in the |
| 4813 | // correct coordinate space. |
| 4814 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4815 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 4816 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 4817 | } |
| 4818 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 4819 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 4820 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4821 | |
| 4822 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 4823 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 4824 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4825 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4826 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4827 | |
| 4828 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 4829 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 4830 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4831 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4832 | WithRawCoords(300, 880))); |
| 4833 | secondWindow->consumeMotionEvent( |
| 4834 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4835 | secondWindow->assertNoEvents(); |
| 4836 | firstWindow->assertNoEvents(); |
| 4837 | } |
| 4838 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 4839 | // Same as above, but while the window is being mirrored. |
| 4840 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 4841 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 4842 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4843 | |
| 4844 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4845 | ui::Transform secondDisplayTransform; |
| 4846 | secondDisplayTransform.set(matrix); |
| 4847 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 4848 | |
| 4849 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 4850 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 4851 | addWindow(secondWindowClone); |
| 4852 | |
| 4853 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 4854 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 4855 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4856 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4857 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4858 | |
| 4859 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 4860 | // display. |
| 4861 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 4862 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4863 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4864 | WithRawCoords(300, 880))); |
| 4865 | secondWindow->consumeMotionEvent( |
| 4866 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4867 | secondWindow->assertNoEvents(); |
| 4868 | firstWindow->assertNoEvents(); |
| 4869 | } |
| 4870 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 4871 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 4872 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4873 | |
| 4874 | // Send hover enter to second window |
| 4875 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 4876 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4877 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4878 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4879 | |
| 4880 | mDispatcher->cancelCurrentTouch(); |
| 4881 | |
| 4882 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4883 | WithRawCoords(300, 880))); |
| 4884 | secondWindow->assertNoEvents(); |
| 4885 | firstWindow->assertNoEvents(); |
| 4886 | } |
| 4887 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 4888 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4889 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 4890 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 4891 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4892 | |
| 4893 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4894 | ui::Transform secondDisplayTransform; |
| 4895 | secondDisplayTransform.set(matrix); |
| 4896 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 4897 | |
| 4898 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 4899 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 4900 | addWindow(secondWindowClone); |
| 4901 | |
| 4902 | // Send hover enter to second window |
| 4903 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 4904 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4905 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4906 | WithCoords(100, 80), WithRawCoords(300, 880), |
| 4907 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4908 | |
| 4909 | mDispatcher->cancelCurrentTouch(); |
| 4910 | |
| 4911 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 4912 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4913 | WithRawCoords(300, 880), |
| 4914 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4915 | secondWindow->assertNoEvents(); |
| 4916 | firstWindow->assertNoEvents(); |
| 4917 | } |
| 4918 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4919 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4920 | class InputDispatcherDisplayOrientationFixture |
| 4921 | : public InputDispatcherDisplayProjectionTest, |
| 4922 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 4923 | |
| 4924 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 4925 | // in different locations on the display, specifically points close to the four corners of a |
| 4926 | // window. |
| 4927 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 4928 | constexpr static int32_t displayWidth = 400; |
| 4929 | constexpr static int32_t displayHeight = 800; |
| 4930 | |
| 4931 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4932 | |
| 4933 | const auto rotation = GetParam(); |
| 4934 | |
| 4935 | // Set up the display with the specified rotation. |
| 4936 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4937 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4938 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4939 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4940 | logicalDisplayWidth, logicalDisplayHeight); |
| 4941 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4942 | |
| 4943 | // Create a window with its bounds determined in the logical display. |
| 4944 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 4945 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4946 | sp<FakeWindowHandle> window = |
| 4947 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4948 | window->setFrame(frameInDisplay, displayTransform); |
| 4949 | addWindow(window); |
| 4950 | |
| 4951 | // The following points in logical display space should be inside the window. |
| 4952 | static const std::array<vec2, 4> insidePoints{ |
| 4953 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4954 | for (const auto pointInsideWindow : insidePoints) { |
| 4955 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4956 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4957 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4958 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4959 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4960 | window->consumeMotionDown(); |
| 4961 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4962 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4963 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4964 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4965 | window->consumeMotionUp(); |
| 4966 | } |
| 4967 | |
| 4968 | // The following points in logical display space should be outside the window. |
| 4969 | static const std::array<vec2, 5> outsidePoints{ |
| 4970 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4971 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4972 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4973 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4974 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4975 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4976 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4977 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4978 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4979 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4980 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4981 | } |
| 4982 | window->assertNoEvents(); |
| 4983 | } |
| 4984 | |
| 4985 | // Run the precision tests for all rotations. |
| 4986 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4987 | InputDispatcherDisplayOrientationFixture, |
| 4988 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4989 | ui::ROTATION_270), |
| 4990 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4991 | return ftl::enum_string(testParamInfo.param); |
| 4992 | }); |
| 4993 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4994 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4995 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4996 | |
| 4997 | class TransferTouchFixture : public InputDispatcherTest, |
| 4998 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4999 | |
| 5000 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5001 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5002 | |
| 5003 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5004 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5005 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5006 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5007 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5008 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5009 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5010 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5011 | sp<FakeWindowHandle> wallpaper = |
| 5012 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 5013 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5014 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5015 | mDispatcher->onWindowInfosChanged( |
| 5016 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5017 | |
| 5018 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5019 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5020 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5021 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5022 | // Only the first window should get the down event |
| 5023 | firstWindow->consumeMotionDown(); |
| 5024 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5025 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5026 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5027 | // Transfer touch to the second window |
| 5028 | TransferFunction f = GetParam(); |
| 5029 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5030 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5031 | // The first window gets cancel and the second gets down |
| 5032 | firstWindow->consumeMotionCancel(); |
| 5033 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5034 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5035 | |
| 5036 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5037 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5038 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5039 | // The first window gets no events and the second gets up |
| 5040 | firstWindow->assertNoEvents(); |
| 5041 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5042 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5043 | } |
| 5044 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5045 | /** |
| 5046 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 5047 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 5048 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 5049 | * natural to the user. |
| 5050 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 5051 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 5052 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 5053 | * the first window to the second. |
| 5054 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 5055 | * the other API, as well. |
| 5056 | */ |
| 5057 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 5058 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5059 | |
| 5060 | // Create a couple of windows + a spy window |
| 5061 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5062 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5063 | spyWindow->setTrustedOverlay(true); |
| 5064 | spyWindow->setSpy(true); |
| 5065 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5066 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5067 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5068 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5069 | |
| 5070 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5071 | mDispatcher->onWindowInfosChanged( |
| 5072 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5073 | |
| 5074 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5075 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5076 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5077 | // Only the first window and spy should get the down event |
| 5078 | spyWindow->consumeMotionDown(); |
| 5079 | firstWindow->consumeMotionDown(); |
| 5080 | |
| 5081 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 5082 | // if f === 'transferTouch'. |
| 5083 | TransferFunction f = GetParam(); |
| 5084 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5085 | ASSERT_TRUE(success); |
| 5086 | // The first window gets cancel and the second gets down |
| 5087 | firstWindow->consumeMotionCancel(); |
| 5088 | secondWindow->consumeMotionDown(); |
| 5089 | |
| 5090 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5091 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5092 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5093 | // The first window gets no events and the second+spy get up |
| 5094 | firstWindow->assertNoEvents(); |
| 5095 | spyWindow->consumeMotionUp(); |
| 5096 | secondWindow->consumeMotionUp(); |
| 5097 | } |
| 5098 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5099 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5100 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5101 | |
| 5102 | PointF touchPoint = {10, 10}; |
| 5103 | |
| 5104 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5105 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5106 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5107 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5108 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5109 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5110 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5111 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5112 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5113 | |
| 5114 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5115 | mDispatcher->onWindowInfosChanged( |
| 5116 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5117 | |
| 5118 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5119 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5120 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5121 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5122 | // Only the first window should get the down event |
| 5123 | firstWindow->consumeMotionDown(); |
| 5124 | secondWindow->assertNoEvents(); |
| 5125 | |
| 5126 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5127 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5128 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5129 | // Only the first window should get the pointer down event |
| 5130 | firstWindow->consumeMotionPointerDown(1); |
| 5131 | secondWindow->assertNoEvents(); |
| 5132 | |
| 5133 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5134 | TransferFunction f = GetParam(); |
| 5135 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5136 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5137 | // The first window gets cancel and the second gets down and pointer down |
| 5138 | firstWindow->consumeMotionCancel(); |
| 5139 | secondWindow->consumeMotionDown(); |
| 5140 | secondWindow->consumeMotionPointerDown(1); |
| 5141 | |
| 5142 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5143 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5144 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5145 | // The first window gets nothing and the second gets pointer up |
| 5146 | firstWindow->assertNoEvents(); |
| 5147 | secondWindow->consumeMotionPointerUp(1); |
| 5148 | |
| 5149 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5150 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5151 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5152 | // The first window gets nothing and the second gets up |
| 5153 | firstWindow->assertNoEvents(); |
| 5154 | secondWindow->consumeMotionUp(); |
| 5155 | } |
| 5156 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5157 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 5158 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5159 | |
| 5160 | // Create a couple of windows |
| 5161 | sp<FakeWindowHandle> firstWindow = |
| 5162 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5163 | ADISPLAY_ID_DEFAULT); |
| 5164 | firstWindow->setDupTouchToWallpaper(true); |
| 5165 | sp<FakeWindowHandle> secondWindow = |
| 5166 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5167 | ADISPLAY_ID_DEFAULT); |
| 5168 | secondWindow->setDupTouchToWallpaper(true); |
| 5169 | |
| 5170 | sp<FakeWindowHandle> wallpaper1 = |
| 5171 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 5172 | wallpaper1->setIsWallpaper(true); |
| 5173 | |
| 5174 | sp<FakeWindowHandle> wallpaper2 = |
| 5175 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 5176 | wallpaper2->setIsWallpaper(true); |
| 5177 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5178 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 5179 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 5180 | {}, |
| 5181 | 0, |
| 5182 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5183 | |
| 5184 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5185 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5186 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5187 | |
| 5188 | // Only the first window should get the down event |
| 5189 | firstWindow->consumeMotionDown(); |
| 5190 | secondWindow->assertNoEvents(); |
| 5191 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5192 | wallpaper2->assertNoEvents(); |
| 5193 | |
| 5194 | // Transfer touch focus to the second window |
| 5195 | TransferFunction f = GetParam(); |
| 5196 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5197 | ASSERT_TRUE(success); |
| 5198 | |
| 5199 | // The first window gets cancel and the second gets down |
| 5200 | firstWindow->consumeMotionCancel(); |
| 5201 | secondWindow->consumeMotionDown(); |
| 5202 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5203 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5204 | |
| 5205 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5206 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5207 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5208 | // The first window gets no events and the second gets up |
| 5209 | firstWindow->assertNoEvents(); |
| 5210 | secondWindow->consumeMotionUp(); |
| 5211 | wallpaper1->assertNoEvents(); |
| 5212 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5213 | } |
| 5214 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5215 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 5216 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 5217 | // for the case where there are multiple pointers split across several windows. |
| 5218 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 5219 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5220 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5221 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5222 | return dispatcher->transferTouch(destChannelToken, |
| 5223 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5224 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5225 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5226 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5227 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5228 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5229 | })); |
| 5230 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5231 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5232 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5233 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5234 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5235 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5236 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5237 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5238 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5239 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5240 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5241 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5242 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5243 | |
| 5244 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5245 | mDispatcher->onWindowInfosChanged( |
| 5246 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5247 | |
| 5248 | PointF pointInFirst = {300, 200}; |
| 5249 | PointF pointInSecond = {300, 600}; |
| 5250 | |
| 5251 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5252 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5253 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5254 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5255 | // Only the first window should get the down event |
| 5256 | firstWindow->consumeMotionDown(); |
| 5257 | secondWindow->assertNoEvents(); |
| 5258 | |
| 5259 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5260 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5261 | ADISPLAY_ID_DEFAULT, |
| 5262 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5263 | // The first window gets a move and the second a down |
| 5264 | firstWindow->consumeMotionMove(); |
| 5265 | secondWindow->consumeMotionDown(); |
| 5266 | |
| 5267 | // Transfer touch focus to the second window |
| 5268 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5269 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 5270 | firstWindow->consumeMotionCancel(); |
| 5271 | secondWindow->consumeMotionPointerDown(1); |
| 5272 | |
| 5273 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5274 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5275 | ADISPLAY_ID_DEFAULT, |
| 5276 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5277 | // The first window gets nothing and the second gets pointer up |
| 5278 | firstWindow->assertNoEvents(); |
| 5279 | secondWindow->consumeMotionPointerUp(1); |
| 5280 | |
| 5281 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5282 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5283 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5284 | // The first window gets nothing and the second gets up |
| 5285 | firstWindow->assertNoEvents(); |
| 5286 | secondWindow->consumeMotionUp(); |
| 5287 | } |
| 5288 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5289 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 5290 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 5291 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 5292 | // window should get nothing. |
| 5293 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 5294 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5295 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5296 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5297 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5298 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5299 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5300 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5301 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5302 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5303 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5304 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5305 | |
| 5306 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5307 | mDispatcher->onWindowInfosChanged( |
| 5308 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5309 | |
| 5310 | PointF pointInFirst = {300, 200}; |
| 5311 | PointF pointInSecond = {300, 600}; |
| 5312 | |
| 5313 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5314 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5315 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5316 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5317 | // Only the first window should get the down event |
| 5318 | firstWindow->consumeMotionDown(); |
| 5319 | secondWindow->assertNoEvents(); |
| 5320 | |
| 5321 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5322 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5323 | ADISPLAY_ID_DEFAULT, |
| 5324 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5325 | // The first window gets a move and the second a down |
| 5326 | firstWindow->consumeMotionMove(); |
| 5327 | secondWindow->consumeMotionDown(); |
| 5328 | |
| 5329 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5330 | const bool transferred = |
| 5331 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5332 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 5333 | ASSERT_FALSE(transferred); |
| 5334 | firstWindow->assertNoEvents(); |
| 5335 | secondWindow->assertNoEvents(); |
| 5336 | |
| 5337 | // The rest of the dispatch should proceed as normal |
| 5338 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5339 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5340 | ADISPLAY_ID_DEFAULT, |
| 5341 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5342 | // The first window gets MOVE and the second gets pointer up |
| 5343 | firstWindow->consumeMotionMove(); |
| 5344 | secondWindow->consumeMotionUp(); |
| 5345 | |
| 5346 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5347 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5348 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5349 | // The first window gets nothing and the second gets up |
| 5350 | firstWindow->consumeMotionUp(); |
| 5351 | secondWindow->assertNoEvents(); |
| 5352 | } |
| 5353 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5354 | // This case will create two windows and one mirrored window on the default display and mirror |
| 5355 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 5356 | // the windows info of second display before default display. |
| 5357 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 5358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5359 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5360 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5361 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5362 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5363 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5364 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5365 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5366 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5367 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5368 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5369 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5370 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5371 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5372 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5373 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5374 | |
| 5375 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5376 | mDispatcher->onWindowInfosChanged( |
| 5377 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5378 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5379 | *secondWindowInPrimary->getInfo()}, |
| 5380 | {}, |
| 5381 | 0, |
| 5382 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5383 | |
| 5384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5385 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5386 | {50, 50})) |
| 5387 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5388 | |
| 5389 | // Window should receive motion event. |
| 5390 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5391 | |
| 5392 | // Transfer touch focus |
| 5393 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 5394 | secondWindowInPrimary->getToken())); |
| 5395 | // The first window gets cancel. |
| 5396 | firstWindowInPrimary->consumeMotionCancel(); |
| 5397 | secondWindowInPrimary->consumeMotionDown(); |
| 5398 | |
| 5399 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5400 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5401 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5402 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5403 | firstWindowInPrimary->assertNoEvents(); |
| 5404 | secondWindowInPrimary->consumeMotionMove(); |
| 5405 | |
| 5406 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5407 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5408 | {150, 50})) |
| 5409 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5410 | firstWindowInPrimary->assertNoEvents(); |
| 5411 | secondWindowInPrimary->consumeMotionUp(); |
| 5412 | } |
| 5413 | |
| 5414 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 5415 | // 'transferTouch' api. |
| 5416 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 5417 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5418 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5419 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5420 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5421 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5422 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5423 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5424 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5425 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5426 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5427 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5428 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5429 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5430 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5431 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5432 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5433 | |
| 5434 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5435 | mDispatcher->onWindowInfosChanged( |
| 5436 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5437 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5438 | *secondWindowInPrimary->getInfo()}, |
| 5439 | {}, |
| 5440 | 0, |
| 5441 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5442 | |
| 5443 | // Touch on second display. |
| 5444 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5445 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5446 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5447 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5448 | |
| 5449 | // Window should receive motion event. |
| 5450 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5451 | |
| 5452 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5453 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5454 | |
| 5455 | // The first window gets cancel. |
| 5456 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5457 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5458 | |
| 5459 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5460 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5461 | SECOND_DISPLAY_ID, {150, 50})) |
| 5462 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5463 | firstWindowInPrimary->assertNoEvents(); |
| 5464 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 5465 | |
| 5466 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5467 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5468 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5469 | firstWindowInPrimary->assertNoEvents(); |
| 5470 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5471 | } |
| 5472 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5473 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5474 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5475 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5476 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5477 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5478 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5479 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5480 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5481 | |
| 5482 | window->consumeFocusEvent(true); |
| 5483 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5484 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5485 | |
| 5486 | // Window should receive key down event. |
| 5487 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5488 | |
| 5489 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5490 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5491 | mFakePolicy->assertUserActivityPoked(); |
| 5492 | } |
| 5493 | |
| 5494 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 5495 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5496 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5497 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5498 | |
| 5499 | window->setDisableUserActivity(true); |
| 5500 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5501 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5502 | setFocusedWindow(window); |
| 5503 | |
| 5504 | window->consumeFocusEvent(true); |
| 5505 | |
| 5506 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5507 | |
| 5508 | // Window should receive key down event. |
| 5509 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5510 | |
| 5511 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5512 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5513 | mFakePolicy->assertUserActivityNotPoked(); |
| 5514 | } |
| 5515 | |
| 5516 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 5517 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5518 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5519 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5520 | |
| 5521 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5522 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5523 | setFocusedWindow(window); |
| 5524 | |
| 5525 | window->consumeFocusEvent(true); |
| 5526 | |
| 5527 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5528 | mDispatcher->waitForIdle(); |
| 5529 | |
| 5530 | // System key is not passed down |
| 5531 | window->assertNoEvents(); |
| 5532 | |
| 5533 | // Should have poked user activity |
| 5534 | mFakePolicy->assertUserActivityPoked(); |
| 5535 | } |
| 5536 | |
| 5537 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 5538 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5539 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5540 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5541 | |
| 5542 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5543 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5544 | setFocusedWindow(window); |
| 5545 | |
| 5546 | window->consumeFocusEvent(true); |
| 5547 | |
| 5548 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5549 | mDispatcher->waitForIdle(); |
| 5550 | |
| 5551 | // System key is not passed down |
| 5552 | window->assertNoEvents(); |
| 5553 | |
| 5554 | // Should have poked user activity |
| 5555 | mFakePolicy->assertUserActivityPoked(); |
| 5556 | } |
| 5557 | |
| 5558 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 5559 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5560 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5561 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5562 | |
| 5563 | window->setDisableUserActivity(true); |
| 5564 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5565 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5566 | setFocusedWindow(window); |
| 5567 | |
| 5568 | window->consumeFocusEvent(true); |
| 5569 | |
| 5570 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5571 | mDispatcher->waitForIdle(); |
| 5572 | |
| 5573 | // System key is not passed down |
| 5574 | window->assertNoEvents(); |
| 5575 | |
| 5576 | // Should have poked user activity |
| 5577 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5578 | } |
| 5579 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5580 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 5581 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5582 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5583 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5584 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5585 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5586 | |
| 5587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5588 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5589 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 5590 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5591 | |
| 5592 | window->consumeMotionEvent( |
| 5593 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5594 | |
| 5595 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5596 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5597 | mFakePolicy->assertUserActivityPoked(); |
| 5598 | } |
| 5599 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5600 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5601 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5602 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5603 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5604 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5605 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5606 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5607 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5608 | mDispatcher->waitForIdle(); |
| 5609 | |
| 5610 | window->assertNoEvents(); |
| 5611 | } |
| 5612 | |
| 5613 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 5614 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5615 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5616 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5617 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5618 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5619 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5620 | |
| 5621 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5622 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5623 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5624 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5625 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5626 | |
| 5627 | // Window should receive only the motion event |
| 5628 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5629 | window->assertNoEvents(); // Key event or focus event will not be received |
| 5630 | } |
| 5631 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5632 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 5633 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5634 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5635 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5636 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5637 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5638 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5639 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5640 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5641 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5642 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5643 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5644 | |
| 5645 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5646 | mDispatcher->onWindowInfosChanged( |
| 5647 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5648 | |
| 5649 | PointF pointInFirst = {300, 200}; |
| 5650 | PointF pointInSecond = {300, 600}; |
| 5651 | |
| 5652 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5653 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5654 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5655 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5656 | // Only the first window should get the down event |
| 5657 | firstWindow->consumeMotionDown(); |
| 5658 | secondWindow->assertNoEvents(); |
| 5659 | |
| 5660 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5661 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5662 | ADISPLAY_ID_DEFAULT, |
| 5663 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5664 | // The first window gets a move and the second a down |
| 5665 | firstWindow->consumeMotionMove(); |
| 5666 | secondWindow->consumeMotionDown(); |
| 5667 | |
| 5668 | // Send pointer cancel to the second window |
| 5669 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5670 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5671 | {pointInFirst, pointInSecond}); |
| 5672 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5673 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5674 | // The first window gets move and the second gets cancel. |
| 5675 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5676 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5677 | |
| 5678 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5679 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5680 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5681 | // The first window gets up and the second gets nothing. |
| 5682 | firstWindow->consumeMotionUp(); |
| 5683 | secondWindow->assertNoEvents(); |
| 5684 | } |
| 5685 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5686 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 5687 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5688 | |
| 5689 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5690 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5691 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5692 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 5693 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 5694 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 5695 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5696 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5697 | window->assertNoEvents(); |
| 5698 | mDispatcher->waitForIdle(); |
| 5699 | } |
| 5700 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5701 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 5702 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5703 | /** |
| 5704 | * Two entities that receive touch: A window, and a global monitor. |
| 5705 | * The touch goes to the window, and then the window disappears. |
| 5706 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 5707 | * for the monitor, as well. |
| 5708 | * 1. foregroundWindow |
| 5709 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 5710 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5711 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5712 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5713 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5714 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5715 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5716 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5717 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5718 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5720 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5721 | {100, 200})) |
| 5722 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5723 | |
| 5724 | // Both the foreground window and the global monitor should receive the touch down |
| 5725 | window->consumeMotionDown(); |
| 5726 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5727 | |
| 5728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5729 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5730 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5731 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5732 | |
| 5733 | window->consumeMotionMove(); |
| 5734 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5735 | |
| 5736 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5737 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5738 | window->consumeMotionCancel(); |
| 5739 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 5740 | |
| 5741 | // If more events come in, there will be no more foreground window to send them to. This will |
| 5742 | // cause a cancel for the monitor, as well. |
| 5743 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5744 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5745 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 5746 | << "Injection should fail because the window was removed"; |
| 5747 | window->assertNoEvents(); |
| 5748 | // Global monitor now gets the cancel |
| 5749 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5750 | } |
| 5751 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5752 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5753 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5754 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5755 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5756 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5757 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5758 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5759 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5761 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5762 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5763 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5764 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5767 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5768 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5769 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5770 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5771 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5772 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5773 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5774 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5775 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5776 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5777 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5778 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5779 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5780 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5781 | // Pilfer pointers from the monitor. |
| 5782 | // This should not do anything and the window should continue to receive events. |
| 5783 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5784 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5785 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5786 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5787 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5788 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5789 | |
| 5790 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5791 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5794 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5795 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5796 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5797 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5798 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5799 | window->setWindowOffset(20, 40); |
| 5800 | window->setWindowTransform(0, 1, -1, 0); |
| 5801 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5802 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5803 | |
| 5804 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5805 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5806 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5807 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5808 | MotionEvent* event = monitor.consumeMotion(); |
| 5809 | // Even though window has transform, gesture monitor must not. |
| 5810 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 5811 | } |
| 5812 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5813 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5814 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5815 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5816 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5817 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5818 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5819 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 5820 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5821 | } |
| 5822 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5823 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5824 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5825 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5826 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5827 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5828 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5829 | |
| 5830 | NotifyMotionArgs motionArgs = |
| 5831 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5832 | ADISPLAY_ID_DEFAULT); |
| 5833 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5834 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5835 | // Window should receive motion down event. |
| 5836 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5837 | |
| 5838 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5839 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5840 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5841 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5842 | motionArgs.pointerCoords[0].getX() - 10); |
| 5843 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5844 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5845 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5846 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5847 | } |
| 5848 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5849 | /** |
| 5850 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 5851 | * the device default right away. In the test scenario, we check both the default value, |
| 5852 | * and the action of enabling / disabling. |
| 5853 | */ |
| 5854 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5855 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5856 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5857 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5858 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5859 | |
| 5860 | // Set focused application. |
| 5861 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5862 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5863 | |
| 5864 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5865 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5866 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5867 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5868 | |
| 5869 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5870 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5871 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5872 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5873 | |
| 5874 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5875 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5876 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5877 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5878 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5879 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5880 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5881 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5882 | |
| 5883 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5884 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5885 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5886 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5887 | |
| 5888 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5889 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5890 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5891 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5892 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5893 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5894 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5895 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5896 | |
| 5897 | window->assertNoEvents(); |
| 5898 | } |
| 5899 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5900 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5901 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5902 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5903 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5904 | |
| 5905 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5906 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5907 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5908 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5909 | setFocusedWindow(window); |
| 5910 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5911 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5912 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5913 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5914 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5915 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5916 | KeyEvent* event = window->consumeKey(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5917 | ASSERT_NE(event, nullptr); |
| 5918 | |
| 5919 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5920 | ASSERT_NE(verified, nullptr); |
| 5921 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 5922 | |
| 5923 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 5924 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 5925 | ASSERT_EQ(keyArgs.source, verified->source); |
| 5926 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 5927 | |
| 5928 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 5929 | |
| 5930 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5931 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5932 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5933 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 5934 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 5935 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 5936 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 5937 | } |
| 5938 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5939 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5940 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5941 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5942 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5943 | |
| 5944 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5945 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5946 | ui::Transform transform; |
| 5947 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5948 | |
| 5949 | gui::DisplayInfo displayInfo; |
| 5950 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5951 | displayInfo.transform = transform; |
| 5952 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5953 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5954 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5955 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5956 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5957 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5958 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5959 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5960 | MotionEvent* event = window->consumeMotion(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5961 | ASSERT_NE(event, nullptr); |
| 5962 | |
| 5963 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5964 | ASSERT_NE(verified, nullptr); |
| 5965 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 5966 | |
| 5967 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 5968 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 5969 | EXPECT_EQ(motionArgs.source, verified->source); |
| 5970 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 5971 | |
| 5972 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 5973 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5974 | const vec2 rawXY = |
| 5975 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 5976 | motionArgs.pointerCoords[0].getXYValue()); |
| 5977 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 5978 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5979 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5980 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5981 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5982 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 5983 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 5984 | } |
| 5985 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5986 | /** |
| 5987 | * Ensure that separate calls to sign the same data are generating the same key. |
| 5988 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 5989 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 5990 | * tests. |
| 5991 | */ |
| 5992 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 5993 | KeyEvent event = getTestKeyEvent(); |
| 5994 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5995 | |
| 5996 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 5997 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5998 | ASSERT_EQ(hmac1, hmac2); |
| 5999 | } |
| 6000 | |
| 6001 | /** |
| 6002 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 6003 | */ |
| 6004 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 6005 | KeyEvent event = getTestKeyEvent(); |
| 6006 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6007 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 6008 | |
| 6009 | verifiedEvent.deviceId += 1; |
| 6010 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6011 | |
| 6012 | verifiedEvent.source += 1; |
| 6013 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6014 | |
| 6015 | verifiedEvent.eventTimeNanos += 1; |
| 6016 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6017 | |
| 6018 | verifiedEvent.displayId += 1; |
| 6019 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6020 | |
| 6021 | verifiedEvent.action += 1; |
| 6022 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6023 | |
| 6024 | verifiedEvent.downTimeNanos += 1; |
| 6025 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6026 | |
| 6027 | verifiedEvent.flags += 1; |
| 6028 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6029 | |
| 6030 | verifiedEvent.keyCode += 1; |
| 6031 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6032 | |
| 6033 | verifiedEvent.scanCode += 1; |
| 6034 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6035 | |
| 6036 | verifiedEvent.metaState += 1; |
| 6037 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6038 | |
| 6039 | verifiedEvent.repeatCount += 1; |
| 6040 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6041 | } |
| 6042 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6043 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 6044 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6045 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6046 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6047 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6048 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6049 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6050 | |
| 6051 | // Top window is also focusable but is not granted focus. |
| 6052 | windowTop->setFocusable(true); |
| 6053 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6054 | mDispatcher->onWindowInfosChanged( |
| 6055 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6056 | setFocusedWindow(windowSecond); |
| 6057 | |
| 6058 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6059 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6060 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6061 | |
| 6062 | // Focused window should receive event. |
| 6063 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6064 | windowTop->assertNoEvents(); |
| 6065 | } |
| 6066 | |
| 6067 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 6068 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6069 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6070 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6071 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6072 | |
| 6073 | window->setFocusable(true); |
| 6074 | // Release channel for window is no longer valid. |
| 6075 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6076 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6077 | setFocusedWindow(window); |
| 6078 | |
| 6079 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6080 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6081 | |
| 6082 | // window channel is invalid, so it should not receive any input event. |
| 6083 | window->assertNoEvents(); |
| 6084 | } |
| 6085 | |
| 6086 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 6087 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6088 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6089 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6090 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6091 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6092 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6093 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6094 | setFocusedWindow(window); |
| 6095 | |
| 6096 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6097 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6098 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6099 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6100 | window->assertNoEvents(); |
| 6101 | } |
| 6102 | |
| 6103 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 6104 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6105 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6106 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6107 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6108 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6109 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6110 | |
| 6111 | windowTop->setFocusable(true); |
| 6112 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6113 | mDispatcher->onWindowInfosChanged( |
| 6114 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6115 | setFocusedWindow(windowTop); |
| 6116 | windowTop->consumeFocusEvent(true); |
| 6117 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6118 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6119 | mDispatcher->onWindowInfosChanged( |
| 6120 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6121 | windowSecond->consumeFocusEvent(true); |
| 6122 | windowTop->consumeFocusEvent(false); |
| 6123 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6124 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6125 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6126 | |
| 6127 | // Focused window should receive event. |
| 6128 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6129 | } |
| 6130 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6131 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6132 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6133 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6134 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6135 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6136 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6137 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6138 | |
| 6139 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6140 | windowSecond->setFocusable(false); |
| 6141 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6142 | mDispatcher->onWindowInfosChanged( |
| 6143 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6144 | setFocusedWindow(windowTop); |
| 6145 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6146 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6147 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6148 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6149 | |
| 6150 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6151 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6152 | windowSecond->assertNoEvents(); |
| 6153 | } |
| 6154 | |
| 6155 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 6156 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6157 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6158 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6159 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6160 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 6161 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6162 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6163 | |
| 6164 | window->setFocusable(true); |
| 6165 | previousFocusedWindow->setFocusable(true); |
| 6166 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6167 | mDispatcher->onWindowInfosChanged( |
| 6168 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6169 | setFocusedWindow(previousFocusedWindow); |
| 6170 | previousFocusedWindow->consumeFocusEvent(true); |
| 6171 | |
| 6172 | // Requesting focus on invisible window takes focus from currently focused window. |
| 6173 | setFocusedWindow(window); |
| 6174 | previousFocusedWindow->consumeFocusEvent(false); |
| 6175 | |
| 6176 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6177 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6178 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 6179 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6180 | |
| 6181 | // Window does not get focus event or key down. |
| 6182 | window->assertNoEvents(); |
| 6183 | |
| 6184 | // Window becomes visible. |
| 6185 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6186 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6187 | |
| 6188 | // Window receives focus event. |
| 6189 | window->consumeFocusEvent(true); |
| 6190 | // Focused window receives key down. |
| 6191 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6192 | } |
| 6193 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6194 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 6195 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6196 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6197 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6198 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6199 | |
| 6200 | // window is granted focus. |
| 6201 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6202 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6203 | setFocusedWindow(window); |
| 6204 | window->consumeFocusEvent(true); |
| 6205 | |
| 6206 | // When a display is removed window loses focus. |
| 6207 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 6208 | window->consumeFocusEvent(false); |
| 6209 | } |
| 6210 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6211 | /** |
| 6212 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 6213 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 6214 | * of the 'slipperyEnterWindow'. |
| 6215 | * |
| 6216 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 6217 | * a way so that the touched location is no longer covered by the top window. |
| 6218 | * |
| 6219 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 6220 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 6221 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 6222 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 6223 | * with ACTION_DOWN). |
| 6224 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 6225 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 6226 | * |
| 6227 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 6228 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 6229 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 6230 | * |
| 6231 | * In this test, we ensure that the event received by the bottom window has |
| 6232 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 6233 | */ |
| 6234 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6235 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6236 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6237 | |
| 6238 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6239 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6240 | |
| 6241 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6242 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6243 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6244 | // Make sure this one overlaps the bottom window |
| 6245 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 6246 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 6247 | // one. Windows with the same owner are not considered to be occluding each other. |
| 6248 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 6249 | |
| 6250 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6251 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6252 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6253 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6254 | mDispatcher->onWindowInfosChanged( |
| 6255 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6256 | |
| 6257 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6258 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6259 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6260 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6261 | slipperyExitWindow->consumeMotionDown(); |
| 6262 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6263 | mDispatcher->onWindowInfosChanged( |
| 6264 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6265 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6266 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 6267 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6268 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6269 | |
| 6270 | slipperyExitWindow->consumeMotionCancel(); |
| 6271 | |
| 6272 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 6273 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6274 | } |
| 6275 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6276 | /** |
| 6277 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 6278 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 6279 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 6280 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 6281 | * reproduces a crash. |
| 6282 | */ |
| 6283 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 6284 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6285 | |
| 6286 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 6287 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6288 | leftSlipperyWindow->setSlippery(true); |
| 6289 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6290 | |
| 6291 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 6292 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6293 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6294 | rightDropTouchesWindow->setDropInput(true); |
| 6295 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6296 | mDispatcher->onWindowInfosChanged( |
| 6297 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6298 | |
| 6299 | // Start touch in the left window |
| 6300 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6301 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6302 | .build()); |
| 6303 | leftSlipperyWindow->consumeMotionDown(); |
| 6304 | |
| 6305 | // And move it into the right window |
| 6306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6307 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6308 | .build()); |
| 6309 | |
| 6310 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 6311 | // The left window continues to receive the gesture. |
| 6312 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6313 | rightDropTouchesWindow->assertNoEvents(); |
| 6314 | } |
| 6315 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 6316 | /** |
| 6317 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 6318 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 6319 | */ |
| 6320 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 6321 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6322 | sp<FakeWindowHandle> originalWindow = |
| 6323 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT); |
| 6324 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6325 | originalWindow->setSlippery(true); |
| 6326 | |
| 6327 | sp<FakeWindowHandle> appearingWindow = |
| 6328 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT); |
| 6329 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6330 | |
| 6331 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 6332 | |
| 6333 | // Touch down on the original window |
| 6334 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6335 | injectMotionEvent(*mDispatcher, |
| 6336 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6337 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 6338 | .build())); |
| 6339 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6340 | |
| 6341 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 6342 | // after user starts to drag down on the launcher window. |
| 6343 | mDispatcher->onWindowInfosChanged( |
| 6344 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 6345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6346 | injectMotionEvent(*mDispatcher, |
| 6347 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6348 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 6349 | .build())); |
| 6350 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 6351 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6352 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6353 | injectMotionEvent(*mDispatcher, |
| 6354 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6355 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6356 | .build())); |
| 6357 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6358 | |
| 6359 | originalWindow->assertNoEvents(); |
| 6360 | appearingWindow->assertNoEvents(); |
| 6361 | } |
| 6362 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6363 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6364 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6365 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6366 | |
| 6367 | sp<FakeWindowHandle> leftWindow = |
| 6368 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6369 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6370 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6371 | |
| 6372 | sp<FakeWindowHandle> rightSpy = |
| 6373 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 6374 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6375 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6376 | rightSpy->setSpy(true); |
| 6377 | rightSpy->setTrustedOverlay(true); |
| 6378 | |
| 6379 | sp<FakeWindowHandle> rightWindow = |
| 6380 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6381 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6382 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6383 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6384 | mDispatcher->onWindowInfosChanged( |
| 6385 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6386 | |
| 6387 | // Touch in the left window |
| 6388 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6389 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6390 | .build()); |
| 6391 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 6392 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6393 | ASSERT_NO_FATAL_FAILURE( |
| 6394 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6395 | |
| 6396 | // Touch another finger over the right windows |
| 6397 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6398 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6399 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6400 | .build()); |
| 6401 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 6402 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 6403 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 6404 | mDispatcher->waitForIdle(); |
| 6405 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6406 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 6407 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6408 | |
| 6409 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 6410 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 6411 | // is part of the UP action, we do not treat this as device interaction. |
| 6412 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6413 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6414 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6415 | .build()); |
| 6416 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 6417 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6418 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6419 | mDispatcher->waitForIdle(); |
| 6420 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6421 | |
| 6422 | // Move remaining finger |
| 6423 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6424 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6425 | .build()); |
| 6426 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6427 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6428 | mDispatcher->waitForIdle(); |
| 6429 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6430 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6431 | |
| 6432 | // Release all fingers |
| 6433 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6434 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6435 | .build()); |
| 6436 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 6437 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 6438 | mDispatcher->waitForIdle(); |
| 6439 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6440 | } |
| 6441 | |
| 6442 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 6443 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6444 | |
| 6445 | sp<FakeWindowHandle> window = |
| 6446 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 6447 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6448 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6449 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6450 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6451 | setFocusedWindow(window); |
| 6452 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 6453 | |
| 6454 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 6455 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 6456 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6457 | ASSERT_NO_FATAL_FAILURE( |
| 6458 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6459 | |
| 6460 | // The UP actions are not treated as device interaction. |
| 6461 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 6462 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 6463 | mDispatcher->waitForIdle(); |
| 6464 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6465 | } |
| 6466 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6467 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 6468 | protected: |
| 6469 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 6470 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 6471 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6472 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6473 | sp<FakeWindowHandle> mWindow; |
| 6474 | |
| 6475 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6476 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6477 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6478 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 6479 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6480 | ASSERT_EQ(OK, mDispatcher->start()); |
| 6481 | |
| 6482 | setUpWindow(); |
| 6483 | } |
| 6484 | |
| 6485 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6486 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6487 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6488 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6489 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6490 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6491 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6492 | mWindow->consumeFocusEvent(true); |
| 6493 | } |
| 6494 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6495 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6496 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6497 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6498 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6499 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6500 | |
| 6501 | // Window should receive key down event. |
| 6502 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6503 | } |
| 6504 | |
| 6505 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 6506 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6507 | mWindow->consumeKeyEvent( |
| 6508 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6509 | } |
| 6510 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6511 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6512 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6513 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6514 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6515 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6516 | |
| 6517 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6518 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6519 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6520 | } |
| 6521 | }; |
| 6522 | |
| 6523 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6524 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6525 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 6526 | expectKeyRepeatOnce(repeatCount); |
| 6527 | } |
| 6528 | } |
| 6529 | |
| 6530 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6531 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6532 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 6533 | expectKeyRepeatOnce(repeatCount); |
| 6534 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6535 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6536 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6537 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 6538 | expectKeyRepeatOnce(repeatCount); |
| 6539 | } |
| 6540 | } |
| 6541 | |
| 6542 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6543 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 6544 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 6545 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6546 | mWindow->assertNoEvents(); |
| 6547 | } |
| 6548 | |
| 6549 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6550 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 6551 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 6552 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 6553 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6554 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6555 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6556 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6557 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 6558 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6559 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6560 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6561 | mWindow->assertNoEvents(); |
| 6562 | } |
| 6563 | |
| 6564 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6565 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 6566 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 6567 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 6568 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6569 | // 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] | 6570 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6571 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6572 | mWindow->assertNoEvents(); |
| 6573 | } |
| 6574 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 6575 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 6576 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6577 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6578 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 6579 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 6580 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 6581 | mWindow->assertNoEvents(); |
| 6582 | } |
| 6583 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6584 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 6585 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6586 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6587 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6588 | KeyEvent* repeatEvent = mWindow->consumeKey(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6589 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 6590 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 6591 | IdGenerator::getSource(repeatEvent->getId())); |
| 6592 | } |
| 6593 | } |
| 6594 | |
| 6595 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 6596 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6597 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6598 | |
| 6599 | std::unordered_set<int32_t> idSet; |
| 6600 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6601 | KeyEvent* repeatEvent = mWindow->consumeKey(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6602 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 6603 | int32_t id = repeatEvent->getId(); |
| 6604 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 6605 | idSet.insert(id); |
| 6606 | } |
| 6607 | } |
| 6608 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6609 | /* Test InputDispatcher for MultiDisplay */ |
| 6610 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 6611 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6612 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6613 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6614 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6615 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6616 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6617 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6618 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6619 | // Set focus window for primary display, but focused display would be second one. |
| 6620 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6621 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6622 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 6623 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6624 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6625 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6626 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6627 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6628 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6629 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6630 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6631 | // Set focus display to second one. |
| 6632 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 6633 | // Set focus window for second display. |
| 6634 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6635 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6636 | mDispatcher->onWindowInfosChanged( |
| 6637 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6638 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6639 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6640 | } |
| 6641 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6642 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6643 | InputDispatcherTest::TearDown(); |
| 6644 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6645 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6646 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6647 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6648 | windowInSecondary.clear(); |
| 6649 | } |
| 6650 | |
| 6651 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6652 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6653 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6654 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6655 | sp<FakeWindowHandle> windowInSecondary; |
| 6656 | }; |
| 6657 | |
| 6658 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 6659 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6661 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6662 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6663 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6664 | windowInSecondary->assertNoEvents(); |
| 6665 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6666 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6668 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6670 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6671 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6672 | } |
| 6673 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6674 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 6675 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6677 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6678 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6679 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 6680 | windowInSecondary->assertNoEvents(); |
| 6681 | |
| 6682 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6684 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6685 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6686 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6687 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6688 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6689 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6690 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6691 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6692 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6693 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6694 | |
| 6695 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6696 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6697 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6698 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6699 | windowInSecondary->assertNoEvents(); |
| 6700 | } |
| 6701 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6702 | // Test per-display input monitors for motion event. |
| 6703 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6704 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6705 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6706 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6707 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6708 | |
| 6709 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6710 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6711 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6712 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6713 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6714 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6715 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6716 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6717 | |
| 6718 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6720 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6721 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6722 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6723 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6724 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6725 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6726 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 6727 | // Lift up the touch from the second display |
| 6728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6729 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 6730 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6731 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 6732 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 6733 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6734 | // Test inject a non-pointer motion event. |
| 6735 | // If specific a display, it will dispatch to the focused window of particular display, |
| 6736 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6737 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6738 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6739 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6740 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6741 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6742 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6743 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6744 | } |
| 6745 | |
| 6746 | // Test per-display input monitors for key event. |
| 6747 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6748 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6749 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6750 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6751 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6752 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6753 | |
| 6754 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6755 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6756 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6757 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6758 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6759 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6760 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6761 | } |
| 6762 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6763 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 6764 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6765 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6766 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6767 | mDispatcher->onWindowInfosChanged( |
| 6768 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 6769 | *windowInSecondary->getInfo()}, |
| 6770 | {}, |
| 6771 | 0, |
| 6772 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6773 | setFocusedWindow(secondWindowInPrimary); |
| 6774 | windowInPrimary->consumeFocusEvent(false); |
| 6775 | secondWindowInPrimary->consumeFocusEvent(true); |
| 6776 | |
| 6777 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6779 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6780 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6781 | windowInPrimary->assertNoEvents(); |
| 6782 | windowInSecondary->assertNoEvents(); |
| 6783 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6784 | } |
| 6785 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6786 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 6787 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6788 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6789 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6790 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6791 | |
| 6792 | // Test touch down on primary display. |
| 6793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6794 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6795 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6796 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6797 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6798 | |
| 6799 | // Test touch down on second display. |
| 6800 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6801 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6802 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6803 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 6804 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 6805 | |
| 6806 | // Trigger cancel touch. |
| 6807 | mDispatcher->cancelCurrentTouch(); |
| 6808 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6809 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6810 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6811 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6812 | |
| 6813 | // Test inject a move motion event, no window/monitor should receive the event. |
| 6814 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6815 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6816 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6817 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 6818 | windowInPrimary->assertNoEvents(); |
| 6819 | monitorInPrimary.assertNoEvents(); |
| 6820 | |
| 6821 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6822 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6823 | SECOND_DISPLAY_ID, {110, 200})) |
| 6824 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 6825 | windowInSecondary->assertNoEvents(); |
| 6826 | monitorInSecondary.assertNoEvents(); |
| 6827 | } |
| 6828 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6829 | class InputFilterTest : public InputDispatcherTest { |
| 6830 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6831 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 6832 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6833 | NotifyMotionArgs motionArgs; |
| 6834 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6835 | motionArgs = |
| 6836 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6837 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6838 | motionArgs = |
| 6839 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6840 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6841 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6842 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 6843 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6844 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6845 | } else { |
| 6846 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 6847 | } |
| 6848 | } |
| 6849 | |
| 6850 | void testNotifyKey(bool expectToBeFiltered) { |
| 6851 | NotifyKeyArgs keyArgs; |
| 6852 | |
| 6853 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6854 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6855 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6856 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6857 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6858 | |
| 6859 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 6860 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6861 | } else { |
| 6862 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 6863 | } |
| 6864 | } |
| 6865 | }; |
| 6866 | |
| 6867 | // Test InputFilter for MotionEvent |
| 6868 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 6869 | // 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] | 6870 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 6871 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6872 | |
| 6873 | // Enable InputFilter |
| 6874 | mDispatcher->setInputFilterEnabled(true); |
| 6875 | // 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] | 6876 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 6877 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6878 | |
| 6879 | // Disable InputFilter |
| 6880 | mDispatcher->setInputFilterEnabled(false); |
| 6881 | // 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] | 6882 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 6883 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6884 | } |
| 6885 | |
| 6886 | // Test InputFilter for KeyEvent |
| 6887 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 6888 | // 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] | 6889 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6890 | |
| 6891 | // Enable InputFilter |
| 6892 | mDispatcher->setInputFilterEnabled(true); |
| 6893 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6894 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6895 | |
| 6896 | // Disable InputFilter |
| 6897 | mDispatcher->setInputFilterEnabled(false); |
| 6898 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6899 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6900 | } |
| 6901 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6902 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 6903 | // logical display coordinate space. |
| 6904 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 6905 | ui::Transform firstDisplayTransform; |
| 6906 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6907 | ui::Transform secondDisplayTransform; |
| 6908 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 6909 | |
| 6910 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 6911 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 6912 | displayInfos[0].transform = firstDisplayTransform; |
| 6913 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 6914 | displayInfos[1].transform = secondDisplayTransform; |
| 6915 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6916 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6917 | |
| 6918 | // Enable InputFilter |
| 6919 | mDispatcher->setInputFilterEnabled(true); |
| 6920 | |
| 6921 | // Ensure the correct transforms are used for the displays. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6922 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 6923 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6924 | } |
| 6925 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6926 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 6927 | protected: |
| 6928 | virtual void SetUp() override { |
| 6929 | InputDispatcherTest::SetUp(); |
| 6930 | |
| 6931 | /** |
| 6932 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 6933 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 6934 | * on. |
| 6935 | */ |
| 6936 | mDispatcher->setInputFilterEnabled(true); |
| 6937 | |
| 6938 | std::shared_ptr<InputApplicationHandle> application = |
| 6939 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6940 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 6941 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6942 | |
| 6943 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6944 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6945 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6946 | setFocusedWindow(mWindow); |
| 6947 | mWindow->consumeFocusEvent(true); |
| 6948 | } |
| 6949 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6950 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 6951 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6952 | KeyEvent event; |
| 6953 | |
| 6954 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6955 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 6956 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6957 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6958 | const int32_t additionalPolicyFlags = |
| 6959 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 6960 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6961 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 6962 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6963 | policyFlags | additionalPolicyFlags)); |
| 6964 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6965 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6966 | } |
| 6967 | |
| 6968 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 6969 | int32_t flags) { |
| 6970 | MotionEvent event; |
| 6971 | PointerProperties pointerProperties[1]; |
| 6972 | PointerCoords pointerCoords[1]; |
| 6973 | pointerProperties[0].clear(); |
| 6974 | pointerProperties[0].id = 0; |
| 6975 | pointerCoords[0].clear(); |
| 6976 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 6977 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 6978 | |
| 6979 | ui::Transform identityTransform; |
| 6980 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6981 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 6982 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 6983 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 6984 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 6985 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 6986 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6987 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6988 | |
| 6989 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 6990 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6991 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 6992 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6993 | policyFlags | additionalPolicyFlags)); |
| 6994 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6995 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6996 | } |
| 6997 | |
| 6998 | private: |
| 6999 | sp<FakeWindowHandle> mWindow; |
| 7000 | }; |
| 7001 | |
| 7002 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7003 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 7004 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 7005 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7006 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 7007 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7008 | } |
| 7009 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7010 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7011 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7012 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7013 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 7014 | } |
| 7015 | |
| 7016 | TEST_F(InputFilterInjectionPolicyTest, |
| 7017 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 7018 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7019 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7020 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7024 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 7025 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7026 | } |
| 7027 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7028 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7029 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7030 | InputDispatcherTest::SetUp(); |
| 7031 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7032 | std::shared_ptr<FakeApplicationHandle> application = |
| 7033 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7034 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7035 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7036 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7037 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7038 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7039 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7040 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7041 | |
| 7042 | // Set focused application. |
| 7043 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7044 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7045 | |
| 7046 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7047 | mDispatcher->onWindowInfosChanged( |
| 7048 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7049 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7050 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7051 | } |
| 7052 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7053 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7054 | InputDispatcherTest::TearDown(); |
| 7055 | |
| 7056 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7057 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7058 | } |
| 7059 | |
| 7060 | protected: |
| 7061 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7062 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7063 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7064 | }; |
| 7065 | |
| 7066 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 7067 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 7068 | // the onPointerDownOutsideFocus callback. |
| 7069 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7070 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7071 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7072 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7073 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7074 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7075 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7076 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7077 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 7078 | } |
| 7079 | |
| 7080 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 7081 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 7082 | // onPointerDownOutsideFocus callback. |
| 7083 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7084 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7085 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, |
| 7086 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7087 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7088 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7089 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7090 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7091 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7092 | } |
| 7093 | |
| 7094 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 7095 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 7096 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7097 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7098 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7099 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7100 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7101 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7102 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7103 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7104 | } |
| 7105 | |
| 7106 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 7107 | // DOWN on the window that already has focus. Ensure no window received the |
| 7108 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7109 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7110 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7111 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7112 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7113 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7114 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7115 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7116 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7117 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7118 | } |
| 7119 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7120 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 7121 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 7122 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 7123 | const MotionEvent event = |
| 7124 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7125 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7126 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7127 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 7128 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7130 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7131 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 7132 | |
| 7133 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7134 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 7135 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 7136 | mUnfocusedWindow->assertNoEvents(); |
| 7137 | } |
| 7138 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7139 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 7140 | // windows that point to the same client token. |
| 7141 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 7142 | virtual void SetUp() override { |
| 7143 | InputDispatcherTest::SetUp(); |
| 7144 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7145 | std::shared_ptr<FakeApplicationHandle> application = |
| 7146 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7147 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 7148 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7149 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 7150 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7151 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 7152 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7153 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 7154 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7155 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7156 | } |
| 7157 | |
| 7158 | protected: |
| 7159 | sp<FakeWindowHandle> mWindow1; |
| 7160 | sp<FakeWindowHandle> mWindow2; |
| 7161 | |
| 7162 | // 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] | 7163 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 7164 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 7165 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 7169 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7170 | const std::string name = window->getName(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7171 | MotionEvent* motionEvent = window->consumeMotion(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7172 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7173 | ASSERT_NE(nullptr, motionEvent) |
| 7174 | << name.c_str() << ": consumer should have returned non-NULL event."; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7175 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 7176 | ASSERT_THAT(*motionEvent, WithMotionAction(expectedAction)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7177 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7178 | |
| 7179 | for (size_t i = 0; i < points.size(); i++) { |
| 7180 | float expectedX = points[i].x; |
| 7181 | float expectedY = points[i].y; |
| 7182 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7183 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7184 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7185 | << ", got " << motionEvent->getX(i); |
| 7186 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7187 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7188 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7189 | } |
| 7190 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7191 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7192 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7193 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7194 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 7195 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7196 | |
| 7197 | // Always consume from window1 since it's the window that has the InputReceiver |
| 7198 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 7199 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7200 | }; |
| 7201 | |
| 7202 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 7203 | // Touch Window 1 |
| 7204 | PointF touchedPoint = {10, 10}; |
| 7205 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7206 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7207 | |
| 7208 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7209 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7210 | |
| 7211 | // Touch Window 2 |
| 7212 | touchedPoint = {150, 150}; |
| 7213 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7214 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7215 | } |
| 7216 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7217 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 7218 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7219 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7220 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7221 | |
| 7222 | // Touch Window 1 |
| 7223 | PointF touchedPoint = {10, 10}; |
| 7224 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7225 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7226 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7227 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7228 | |
| 7229 | // Touch Window 2 |
| 7230 | touchedPoint = {150, 150}; |
| 7231 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7232 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 7233 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7234 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7235 | // Update the transform so rotation is set |
| 7236 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7237 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7238 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 7239 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7240 | } |
| 7241 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7242 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7243 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7244 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7245 | |
| 7246 | // Touch Window 1 |
| 7247 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7248 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7249 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7250 | |
| 7251 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7252 | touchedPoints.push_back(PointF{150, 150}); |
| 7253 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7254 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7255 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7256 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7257 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7258 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7259 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7260 | // Update the transform so rotation is set for Window 2 |
| 7261 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7262 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7263 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7264 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7265 | } |
| 7266 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7267 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7268 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7269 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7270 | |
| 7271 | // Touch Window 1 |
| 7272 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7273 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7274 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7275 | |
| 7276 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7277 | touchedPoints.push_back(PointF{150, 150}); |
| 7278 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7279 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7280 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7281 | |
| 7282 | // Move both windows |
| 7283 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7284 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7285 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7286 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7287 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7288 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7289 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7290 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7291 | expectedPoints.pop_back(); |
| 7292 | |
| 7293 | // Touch Window 2 |
| 7294 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7295 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7296 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7297 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7298 | |
| 7299 | // Move both windows |
| 7300 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7301 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7302 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7303 | |
| 7304 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
| 7307 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 7308 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7309 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7310 | |
| 7311 | // Touch Window 1 |
| 7312 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7313 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7314 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7315 | |
| 7316 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7317 | touchedPoints.push_back(PointF{150, 150}); |
| 7318 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7319 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7320 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7321 | |
| 7322 | // Move both windows |
| 7323 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7324 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7325 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7326 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7327 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7328 | } |
| 7329 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 7330 | /** |
| 7331 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 7332 | * same input channel. |
| 7333 | */ |
| 7334 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 7335 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7336 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 7337 | |
| 7338 | // Touch down in window 1 |
| 7339 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7340 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 7341 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 7342 | |
| 7343 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 7344 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 7345 | // getting generated. |
| 7346 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7347 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 7348 | |
| 7349 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 7350 | } |
| 7351 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7352 | /** |
| 7353 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 7354 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 7355 | * that the pointer is hovering over may have a different transform. |
| 7356 | */ |
| 7357 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7358 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7359 | |
| 7360 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7361 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 7362 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7363 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7364 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 7365 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7366 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7367 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7368 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 7369 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7370 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 7371 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 7372 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 7373 | } |
| 7374 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7375 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 7376 | virtual void SetUp() override { |
| 7377 | InputDispatcherTest::SetUp(); |
| 7378 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7379 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7380 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7381 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 7382 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7383 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7384 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7385 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7386 | |
| 7387 | // Set focused application. |
| 7388 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 7389 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7390 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7391 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7392 | mWindow->consumeFocusEvent(true); |
| 7393 | } |
| 7394 | |
| 7395 | virtual void TearDown() override { |
| 7396 | InputDispatcherTest::TearDown(); |
| 7397 | mWindow.clear(); |
| 7398 | } |
| 7399 | |
| 7400 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7401 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7402 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7403 | sp<FakeWindowHandle> mWindow; |
| 7404 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 7405 | |
| 7406 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7407 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7408 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7409 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7410 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7411 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7412 | WINDOW_LOCATION)); |
| 7413 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7414 | |
| 7415 | sp<FakeWindowHandle> addSpyWindow() { |
| 7416 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7417 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7418 | spy->setTrustedOverlay(true); |
| 7419 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7420 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7421 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7422 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7423 | return spy; |
| 7424 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7425 | }; |
| 7426 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7427 | // Send a tap and respond, which should not cause an ANR. |
| 7428 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 7429 | tapOnWindow(); |
| 7430 | mWindow->consumeMotionDown(); |
| 7431 | mWindow->consumeMotionUp(); |
| 7432 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7433 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7434 | } |
| 7435 | |
| 7436 | // Send a regular key and respond, which should not cause an ANR. |
| 7437 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7438 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7439 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7440 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7441 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7442 | } |
| 7443 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7444 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 7445 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7446 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7447 | mWindow->consumeFocusEvent(false); |
| 7448 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7449 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7450 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7451 | InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7452 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7453 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7454 | // Key will not go to window because we have no focused window. |
| 7455 | // The 'no focused window' ANR timer should start instead. |
| 7456 | |
| 7457 | // Now, the focused application goes away. |
| 7458 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 7459 | // The key should get dropped and there should be no ANR. |
| 7460 | |
| 7461 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7462 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7463 | } |
| 7464 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7465 | // 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] | 7466 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 7467 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7468 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7469 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7470 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7471 | WINDOW_LOCATION)); |
| 7472 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7473 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 7474 | ASSERT_TRUE(sequenceNum); |
| 7475 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7476 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7477 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7478 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7479 | mWindow->consumeMotionEvent( |
| 7480 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7481 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7482 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7483 | } |
| 7484 | |
| 7485 | // Send a key to the app and have the app not respond right away. |
| 7486 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 7487 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7488 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7489 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 7490 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7491 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7492 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7493 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7494 | } |
| 7495 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7496 | // We have a focused application, but no focused window |
| 7497 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7498 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7499 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7500 | mWindow->consumeFocusEvent(false); |
| 7501 | |
| 7502 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7503 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7504 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7505 | WINDOW_LOCATION)); |
| 7506 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 7507 | mDispatcher->waitForIdle(); |
| 7508 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7509 | |
| 7510 | // Once a focused event arrives, we get an ANR for this application |
| 7511 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 7512 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7513 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7514 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7515 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7516 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7517 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 7518 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7519 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7520 | } |
| 7521 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7522 | /** |
| 7523 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 7524 | * there will not be an ANR. |
| 7525 | */ |
| 7526 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 7527 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7528 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7529 | mWindow->consumeFocusEvent(false); |
| 7530 | |
| 7531 | KeyEvent event; |
| 7532 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 7533 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 7534 | |
| 7535 | // Define a valid key down event that is stale (too old). |
| 7536 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7537 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7538 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7539 | |
| 7540 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 7541 | |
| 7542 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7543 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7544 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 7545 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 7546 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 7547 | << "Injection should fail because the event is stale"; |
| 7548 | |
| 7549 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7550 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7551 | mWindow->assertNoEvents(); |
| 7552 | } |
| 7553 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7554 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7555 | // Make sure that we don't notify policy twice about the same ANR. |
| 7556 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 7557 | const std::chrono::duration appTimeout = 400ms; |
| 7558 | mApplication->setDispatchingTimeout(appTimeout); |
| 7559 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 7560 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7561 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7562 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7563 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7564 | |
| 7565 | // Once a focused event arrives, we get an ANR for this application |
| 7566 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 7567 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 7568 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 7569 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7570 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7571 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 7572 | InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout, |
| 7573 | /*allowKeyRepeat=*/false); |
| 7574 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 7575 | << "result=" << ftl::enum_string(result); |
| 7576 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 7577 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 7578 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 7579 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7580 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 7581 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7582 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 7583 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7584 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7585 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 7586 | // '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] | 7587 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7588 | } |
| 7589 | |
| 7590 | // We have a focused application, but no focused window |
| 7591 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7592 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7593 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7594 | mWindow->consumeFocusEvent(false); |
| 7595 | |
| 7596 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7597 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7598 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 7599 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 7600 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7601 | |
| 7602 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7603 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7604 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7605 | mWindow->assertNoEvents(); |
| 7606 | } |
| 7607 | |
| 7608 | /** |
| 7609 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 7610 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 7611 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 7612 | * the ANR mechanism should still work. |
| 7613 | * |
| 7614 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 7615 | * DOWN event, while not responding on the second one. |
| 7616 | */ |
| 7617 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 7618 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7619 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7620 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 7621 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 7622 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7623 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7624 | |
| 7625 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7626 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7627 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 7628 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 7629 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7630 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7631 | |
| 7632 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 7633 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7634 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7635 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7636 | } |
| 7637 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7638 | // A spy window can receive an ANR |
| 7639 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 7640 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 7641 | |
| 7642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7643 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7644 | WINDOW_LOCATION)); |
| 7645 | mWindow->consumeMotionDown(); |
| 7646 | |
| 7647 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 7648 | ASSERT_TRUE(sequenceNum); |
| 7649 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7650 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7651 | |
| 7652 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7653 | spy->consumeMotionEvent( |
| 7654 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7655 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7656 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7657 | } |
| 7658 | |
| 7659 | // 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] | 7660 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7661 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 7662 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7663 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7664 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7665 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7666 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7668 | |
| 7669 | // Stuck on the ACTION_UP |
| 7670 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7671 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7672 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7673 | // 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] | 7674 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7675 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7676 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7677 | |
| 7678 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 7679 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7680 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7681 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7682 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7683 | } |
| 7684 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7685 | // 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] | 7686 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7687 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 7688 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7689 | |
| 7690 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7691 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7692 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7693 | |
| 7694 | mWindow->consumeMotionDown(); |
| 7695 | // Stuck on the ACTION_UP |
| 7696 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7697 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7698 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7699 | // 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] | 7700 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7701 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7702 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7703 | |
| 7704 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 7705 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7706 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7707 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7708 | spy->assertNoEvents(); |
| 7709 | } |
| 7710 | |
| 7711 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7712 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7713 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7714 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7715 | |
| 7716 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7717 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7718 | WINDOW_LOCATION)); |
| 7719 | |
| 7720 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7721 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 7722 | ASSERT_TRUE(consumeSeq); |
| 7723 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7724 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 7725 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7726 | |
| 7727 | monitor.finishEvent(*consumeSeq); |
| 7728 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7729 | |
| 7730 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7731 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7732 | } |
| 7733 | |
| 7734 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 7735 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 7736 | // get an ANR again. |
| 7737 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 7738 | // 2. consume all pending events (= queue becomes healthy again) |
| 7739 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 7740 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 7741 | tapOnWindow(); |
| 7742 | |
| 7743 | mWindow->consumeMotionDown(); |
| 7744 | // Block on ACTION_UP |
| 7745 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7746 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7747 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 7748 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7749 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7750 | mWindow->assertNoEvents(); |
| 7751 | |
| 7752 | tapOnWindow(); |
| 7753 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7754 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7755 | mWindow->consumeMotionUp(); |
| 7756 | |
| 7757 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7758 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7759 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7760 | mWindow->assertNoEvents(); |
| 7761 | } |
| 7762 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7763 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 7764 | // it. |
| 7765 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7766 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7767 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7768 | WINDOW_LOCATION)); |
| 7769 | |
| 7770 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7771 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7772 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7773 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 7774 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7775 | // 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] | 7776 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7777 | mWindow->consumeMotionEvent( |
| 7778 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7779 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7780 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7781 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7782 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | /** |
| 7786 | * 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] | 7787 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7788 | * |
| 7789 | * Warning!!! |
| 7790 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7791 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7792 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7793 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7794 | * |
| 7795 | * If that value changes, this test should also change. |
| 7796 | */ |
| 7797 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 7798 | 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] | 7799 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7800 | |
| 7801 | tapOnWindow(); |
| 7802 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 7803 | ASSERT_TRUE(downSequenceNum); |
| 7804 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 7805 | ASSERT_TRUE(upSequenceNum); |
| 7806 | // Don't finish the events yet, and send a key |
| 7807 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 7808 | // window even if motions are still being processed. But because the injection timeout is short, |
| 7809 | // we will receive INJECTION_TIMED_OUT as the result. |
| 7810 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7811 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7812 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7813 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7814 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7815 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7816 | // 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] | 7817 | // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR. |
| 7818 | // Rely here on the fact that it uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 7819 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 7820 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7821 | |
| 7822 | std::this_thread::sleep_for(500ms); |
| 7823 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 7824 | // to the focused window, even though we have not yet finished the motion event |
| 7825 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7826 | mWindow->finishEvent(*downSequenceNum); |
| 7827 | mWindow->finishEvent(*upSequenceNum); |
| 7828 | } |
| 7829 | |
| 7830 | /** |
| 7831 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7832 | * not go to the focused window until the motion is processed. |
| 7833 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 7834 | * focused window right away. |
| 7835 | */ |
| 7836 | TEST_F(InputDispatcherSingleWindowAnr, |
| 7837 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 7838 | 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] | 7839 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7840 | |
| 7841 | tapOnWindow(); |
| 7842 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 7843 | ASSERT_TRUE(downSequenceNum); |
| 7844 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 7845 | ASSERT_TRUE(upSequenceNum); |
| 7846 | // Don't finish the events yet, and send a key |
| 7847 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7849 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 7850 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7851 | // 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] | 7852 | // Make sure the `assertNoEvents` check doesn't take too long. It uses |
| 7853 | // CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 7854 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 7855 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7856 | |
| 7857 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 7858 | tapOnWindow(); |
| 7859 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 7860 | // the other events yet. We can finish events in any order. |
| 7861 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 7862 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 7863 | mWindow->consumeMotionDown(); |
| 7864 | mWindow->consumeMotionUp(); |
| 7865 | mWindow->assertNoEvents(); |
| 7866 | } |
| 7867 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 7868 | /** |
| 7869 | * Send an event to the app and have the app not respond right away. |
| 7870 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 7871 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 7872 | * At some point, the window becomes responsive again. |
| 7873 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 7874 | */ |
| 7875 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 7876 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7877 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 7878 | .build()); |
| 7879 | |
| 7880 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 7881 | ASSERT_TRUE(sequenceNum); |
| 7882 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 7883 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 7884 | |
| 7885 | mWindow->finishEvent(*sequenceNum); |
| 7886 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 7887 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7888 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 7889 | |
| 7890 | // Now that the window is responsive, let's continue the gesture. |
| 7891 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7892 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7893 | .build()); |
| 7894 | |
| 7895 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7896 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7897 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 7898 | .build()); |
| 7899 | |
| 7900 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7901 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7902 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 7903 | .build()); |
| 7904 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7905 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7906 | .build()); |
| 7907 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 7908 | mWindow->assertNoEvents(); |
| 7909 | |
| 7910 | // Start another one. |
| 7911 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7912 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 7913 | .build()); |
| 7914 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7915 | } |
| 7916 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7917 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 7918 | virtual void SetUp() override { |
| 7919 | InputDispatcherTest::SetUp(); |
| 7920 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7921 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7922 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7923 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 7924 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7925 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7926 | // 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] | 7927 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7928 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7929 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 7930 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7931 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7932 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7933 | |
| 7934 | // Set focused application. |
| 7935 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7936 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7937 | |
| 7938 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7939 | mDispatcher->onWindowInfosChanged( |
| 7940 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7941 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7942 | mFocusedWindow->consumeFocusEvent(true); |
| 7943 | } |
| 7944 | |
| 7945 | virtual void TearDown() override { |
| 7946 | InputDispatcherTest::TearDown(); |
| 7947 | |
| 7948 | mUnfocusedWindow.clear(); |
| 7949 | mFocusedWindow.clear(); |
| 7950 | } |
| 7951 | |
| 7952 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7953 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7954 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 7955 | sp<FakeWindowHandle> mFocusedWindow; |
| 7956 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 7957 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 7958 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 7959 | |
| 7960 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 7961 | |
| 7962 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 7963 | |
| 7964 | private: |
| 7965 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7966 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7967 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7968 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7970 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7971 | location)); |
| 7972 | } |
| 7973 | }; |
| 7974 | |
| 7975 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 7976 | // should be ANR'd first. |
| 7977 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7978 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 7979 | injectMotionEvent(*mDispatcher, |
| 7980 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7981 | AINPUT_SOURCE_TOUCHSCREEN) |
| 7982 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 7983 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 7984 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 7985 | .build())); |
| 7986 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7987 | injectMotionEvent(*mDispatcher, |
| 7988 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 7989 | AINPUT_SOURCE_TOUCHSCREEN) |
| 7990 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 7991 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 7992 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 7993 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7994 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 7995 | mFocusedWindow->consumeMotionUp(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7996 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7997 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7998 | // We consumed all events, so no ANR |
| 7999 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8000 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8001 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8002 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 8003 | injectMotionEvent(*mDispatcher, |
| 8004 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8005 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8006 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8007 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8008 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8009 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8010 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8011 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8012 | |
| 8013 | const std::chrono::duration timeout = |
| 8014 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8015 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 8016 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8017 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8018 | mFocusedWindow->consumeMotionDown(); |
| 8019 | // This cancel is generated because the connection was unresponsive |
| 8020 | mFocusedWindow->consumeMotionCancel(); |
| 8021 | mFocusedWindow->assertNoEvents(); |
| 8022 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8023 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8024 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8025 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8026 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8027 | } |
| 8028 | |
| 8029 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 8030 | // it doesn't matter which order they should have ANR. |
| 8031 | // But we should receive ANR for both. |
| 8032 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 8033 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8034 | mUnfocusedWindow->setDispatchingTimeout( |
| 8035 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8036 | mDispatcher->onWindowInfosChanged( |
| 8037 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8038 | |
| 8039 | tapOnFocusedWindow(); |
| 8040 | // 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] | 8041 | // 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] | 8042 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 8043 | mFocusedWindow->getDispatchingTimeout( |
| 8044 | DISPATCHING_TIMEOUT)), |
| 8045 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 8046 | |
| 8047 | ASSERT_THAT(anrConnectionTokens, |
| 8048 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 8049 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8050 | |
| 8051 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8052 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8053 | |
| 8054 | mFocusedWindow->consumeMotionDown(); |
| 8055 | mFocusedWindow->consumeMotionUp(); |
| 8056 | mUnfocusedWindow->consumeMotionOutside(); |
| 8057 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8058 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 8059 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8060 | |
| 8061 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8062 | ASSERT_THAT(responsiveTokens, |
| 8063 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 8064 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8065 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8066 | } |
| 8067 | |
| 8068 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 8069 | // We should also log an error to account for the dropped event (not tested here). |
| 8070 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 8071 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 8072 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8073 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8074 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8075 | // Receive the events, but don't respond |
| 8076 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 8077 | ASSERT_TRUE(downEventSequenceNum); |
| 8078 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 8079 | ASSERT_TRUE(upEventSequenceNum); |
| 8080 | const std::chrono::duration timeout = |
| 8081 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8082 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8083 | |
| 8084 | // Tap once again |
| 8085 | // 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] | 8086 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8087 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8088 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8089 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8090 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8091 | FOCUSED_WINDOW_LOCATION)); |
| 8092 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 8093 | // valid touch target |
| 8094 | mUnfocusedWindow->assertNoEvents(); |
| 8095 | |
| 8096 | // Consume the first tap |
| 8097 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 8098 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 8099 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8100 | // The second tap did not go to the focused window |
| 8101 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8102 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8103 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8104 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8105 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8106 | } |
| 8107 | |
| 8108 | // If you tap outside of all windows, there will not be ANR |
| 8109 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8110 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8111 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8112 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 8113 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8114 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8115 | } |
| 8116 | |
| 8117 | // Since the focused window is paused, tapping on it should not produce any events |
| 8118 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 8119 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8120 | mDispatcher->onWindowInfosChanged( |
| 8121 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8122 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8123 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8124 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8125 | FOCUSED_WINDOW_LOCATION)); |
| 8126 | |
| 8127 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 8128 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8129 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 8130 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8131 | |
| 8132 | mFocusedWindow->assertNoEvents(); |
| 8133 | mUnfocusedWindow->assertNoEvents(); |
| 8134 | } |
| 8135 | |
| 8136 | /** |
| 8137 | * 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] | 8138 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8139 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 8140 | * |
| 8141 | * Warning!!! |
| 8142 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 8143 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8144 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8145 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 8146 | * |
| 8147 | * If that value changes, this test should also change. |
| 8148 | */ |
| 8149 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 8150 | // Set a long ANR timeout to prevent it from triggering |
| 8151 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8152 | mDispatcher->onWindowInfosChanged( |
| 8153 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8154 | |
| 8155 | tapOnUnfocusedWindow(); |
| 8156 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8157 | ASSERT_TRUE(downSequenceNum); |
| 8158 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8159 | ASSERT_TRUE(upSequenceNum); |
| 8160 | // Don't finish the events yet, and send a key |
| 8161 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 8162 | // window even if motions are still being processed. |
| 8163 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8164 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8165 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8166 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8167 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8168 | // 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] | 8169 | // and the key remains pending, waiting for the touch events to be processed. |
| 8170 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 8171 | // under the hood. |
| 8172 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8173 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8174 | |
| 8175 | // 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] | 8176 | mFocusedWindow->setFocusable(false); |
| 8177 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8178 | mDispatcher->onWindowInfosChanged( |
| 8179 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8180 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8181 | |
| 8182 | // Focus events should precede the key events |
| 8183 | mUnfocusedWindow->consumeFocusEvent(true); |
| 8184 | mFocusedWindow->consumeFocusEvent(false); |
| 8185 | |
| 8186 | // Finish the tap events, which should unblock dispatcher |
| 8187 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 8188 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 8189 | |
| 8190 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 8191 | // can finally go to the newly focused "mUnfocusedWindow". |
| 8192 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8193 | mFocusedWindow->assertNoEvents(); |
| 8194 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8195 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8196 | } |
| 8197 | |
| 8198 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 8199 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 8200 | // The other window should not be affected by that. |
| 8201 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 8202 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8203 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8204 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8205 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8206 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8207 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8208 | |
| 8209 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8210 | mDispatcher->notifyMotion( |
| 8211 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8212 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8213 | |
| 8214 | const std::chrono::duration timeout = |
| 8215 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8216 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8217 | |
| 8218 | mUnfocusedWindow->consumeMotionDown(); |
| 8219 | mFocusedWindow->consumeMotionDown(); |
| 8220 | // Focused window may or may not receive ACTION_MOVE |
| 8221 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 8222 | InputEvent* event; |
| 8223 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 8224 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 8225 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 8226 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8227 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8228 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 8229 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 8230 | mFocusedWindow->consumeMotionCancel(); |
| 8231 | } else { |
| 8232 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 8233 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8234 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8235 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8236 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8237 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8238 | mUnfocusedWindow->assertNoEvents(); |
| 8239 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8240 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8241 | } |
| 8242 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8243 | /** |
| 8244 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 8245 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 8246 | * |
| 8247 | * If the user touches another application during this time, the key should be dropped. |
| 8248 | * Next, if a new focused window comes in, without toggling the focused application, |
| 8249 | * then no ANR should occur. |
| 8250 | * |
| 8251 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 8252 | * but in some cases the policy may not update the focused application. |
| 8253 | */ |
| 8254 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 8255 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 8256 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 8257 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8258 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 8259 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 8260 | mFocusedWindow->setFocusable(false); |
| 8261 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8262 | mDispatcher->onWindowInfosChanged( |
| 8263 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8264 | mFocusedWindow->consumeFocusEvent(false); |
| 8265 | |
| 8266 | // Send a key. The ANR timer should start because there is no focused window. |
| 8267 | // 'focusedApplication' will get blamed if this timer completes. |
| 8268 | // 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] | 8269 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8270 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8271 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8272 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8273 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8274 | |
| 8275 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 8276 | // then the injected touches won't cause the focused event to get dropped. |
| 8277 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 8278 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 8279 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 8280 | // For this test, it means that the key would get delivered to the window once it becomes |
| 8281 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8282 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8283 | |
| 8284 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8285 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8286 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8287 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8288 | |
| 8289 | // We do not consume the motion right away, because that would require dispatcher to first |
| 8290 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 8291 | // Set the focused window first. |
| 8292 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8293 | mDispatcher->onWindowInfosChanged( |
| 8294 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8295 | setFocusedWindow(mFocusedWindow); |
| 8296 | mFocusedWindow->consumeFocusEvent(true); |
| 8297 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 8298 | // to another application. This could be a bug / behaviour in the policy. |
| 8299 | |
| 8300 | mUnfocusedWindow->consumeMotionDown(); |
| 8301 | |
| 8302 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8303 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 8304 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 8305 | } |
| 8306 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8307 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 8308 | // that has feature NO_INPUT_CHANNEL. |
| 8309 | // Layout: |
| 8310 | // Top (closest to user) |
| 8311 | // mNoInputWindow (above all windows) |
| 8312 | // mBottomWindow |
| 8313 | // Bottom (furthest from user) |
| 8314 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 8315 | virtual void SetUp() override { |
| 8316 | InputDispatcherTest::SetUp(); |
| 8317 | |
| 8318 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8319 | mNoInputWindow = |
| 8320 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 8321 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8322 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8323 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8324 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8325 | // It's perfectly valid for this window to not have an associated input channel |
| 8326 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8327 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 8328 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8329 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8330 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8331 | mDispatcher->onWindowInfosChanged( |
| 8332 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8333 | } |
| 8334 | |
| 8335 | protected: |
| 8336 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 8337 | sp<FakeWindowHandle> mNoInputWindow; |
| 8338 | sp<FakeWindowHandle> mBottomWindow; |
| 8339 | }; |
| 8340 | |
| 8341 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 8342 | PointF touchedPoint = {10, 10}; |
| 8343 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8344 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8345 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8346 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8347 | |
| 8348 | mNoInputWindow->assertNoEvents(); |
| 8349 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 8350 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 8351 | // and therefore should prevent mBottomWindow from receiving touches |
| 8352 | mBottomWindow->assertNoEvents(); |
| 8353 | } |
| 8354 | |
| 8355 | /** |
| 8356 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 8357 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 8358 | */ |
| 8359 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 8360 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8361 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 8362 | "Window with input channel and NO_INPUT_CHANNEL", |
| 8363 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8364 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8365 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8366 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8367 | mDispatcher->onWindowInfosChanged( |
| 8368 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8369 | |
| 8370 | PointF touchedPoint = {10, 10}; |
| 8371 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8372 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8373 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8374 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8375 | |
| 8376 | mNoInputWindow->assertNoEvents(); |
| 8377 | mBottomWindow->assertNoEvents(); |
| 8378 | } |
| 8379 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8380 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 8381 | protected: |
| 8382 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8383 | sp<FakeWindowHandle> mWindow; |
| 8384 | sp<FakeWindowHandle> mMirror; |
| 8385 | |
| 8386 | virtual void SetUp() override { |
| 8387 | InputDispatcherTest::SetUp(); |
| 8388 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8389 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 8390 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 8391 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8392 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 8393 | mWindow->setFocusable(true); |
| 8394 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8395 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8396 | } |
| 8397 | }; |
| 8398 | |
| 8399 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 8400 | // Request focus on a mirrored window |
| 8401 | setFocusedWindow(mMirror); |
| 8402 | |
| 8403 | // window gets focused |
| 8404 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8405 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8406 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8407 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8408 | } |
| 8409 | |
| 8410 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 8411 | // focusable. |
| 8412 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 8413 | setFocusedWindow(mMirror); |
| 8414 | |
| 8415 | // window gets focused |
| 8416 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8417 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8418 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8419 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8420 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8421 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8422 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8423 | |
| 8424 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8425 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8426 | |
| 8427 | // window loses focus since one of the windows associated with the token in not focusable |
| 8428 | mWindow->consumeFocusEvent(false); |
| 8429 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8430 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8431 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8432 | mWindow->assertNoEvents(); |
| 8433 | } |
| 8434 | |
| 8435 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 8436 | // invisible. |
| 8437 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 8438 | setFocusedWindow(mMirror); |
| 8439 | |
| 8440 | // window gets focused |
| 8441 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8442 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8443 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8444 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8445 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8446 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8447 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8448 | |
| 8449 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8450 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8451 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8453 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8454 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8455 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8456 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8457 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8458 | |
| 8459 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8460 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8461 | |
| 8462 | // window loses focus only after all windows associated with the token become invisible. |
| 8463 | mWindow->consumeFocusEvent(false); |
| 8464 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8465 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8466 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8467 | mWindow->assertNoEvents(); |
| 8468 | } |
| 8469 | |
| 8470 | // A focused & mirrored window remains focused until both windows are removed. |
| 8471 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 8472 | setFocusedWindow(mMirror); |
| 8473 | |
| 8474 | // window gets focused |
| 8475 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8477 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8478 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8479 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8480 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8481 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8482 | |
| 8483 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8484 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8485 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8486 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8487 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8488 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8489 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8490 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8491 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8492 | |
| 8493 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8494 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8495 | mWindow->consumeFocusEvent(false); |
| 8496 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8497 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8498 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8499 | mWindow->assertNoEvents(); |
| 8500 | } |
| 8501 | |
| 8502 | // Focus request can be pending until one window becomes visible. |
| 8503 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 8504 | // Request focus on an invisible mirror. |
| 8505 | mWindow->setVisible(false); |
| 8506 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8507 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8508 | setFocusedWindow(mMirror); |
| 8509 | |
| 8510 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8512 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 8513 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8514 | |
| 8515 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8516 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8517 | |
| 8518 | // window gets focused |
| 8519 | mWindow->consumeFocusEvent(true); |
| 8520 | // window gets the pending key event |
| 8521 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8522 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8523 | |
| 8524 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 8525 | protected: |
| 8526 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8527 | sp<FakeWindowHandle> mWindow; |
| 8528 | sp<FakeWindowHandle> mSecondWindow; |
| 8529 | |
| 8530 | void SetUp() override { |
| 8531 | InputDispatcherTest::SetUp(); |
| 8532 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8533 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8534 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8535 | mSecondWindow = |
| 8536 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8537 | mSecondWindow->setFocusable(true); |
| 8538 | |
| 8539 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8540 | mDispatcher->onWindowInfosChanged( |
| 8541 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8542 | |
| 8543 | setFocusedWindow(mWindow); |
| 8544 | mWindow->consumeFocusEvent(true); |
| 8545 | } |
| 8546 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8547 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8548 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8549 | } |
| 8550 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8551 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 8552 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8553 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8554 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 8555 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8556 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8557 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8558 | } |
| 8559 | }; |
| 8560 | |
| 8561 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 8562 | // Ensure that capture cannot be obtained for unfocused windows. |
| 8563 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 8564 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 8565 | mSecondWindow->assertNoEvents(); |
| 8566 | |
| 8567 | // Ensure that capture can be enabled from the focus window. |
| 8568 | requestAndVerifyPointerCapture(mWindow, true); |
| 8569 | |
| 8570 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 8571 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 8572 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 8573 | |
| 8574 | // Ensure that capture can be disabled from the window with capture. |
| 8575 | requestAndVerifyPointerCapture(mWindow, false); |
| 8576 | } |
| 8577 | |
| 8578 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8579 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8580 | |
| 8581 | setFocusedWindow(mSecondWindow); |
| 8582 | |
| 8583 | // Ensure that the capture disabled event was sent first. |
| 8584 | mWindow->consumeCaptureEvent(false); |
| 8585 | mWindow->consumeFocusEvent(false); |
| 8586 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8587 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8588 | |
| 8589 | // 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] | 8590 | notifyPointerCaptureChanged({}); |
| 8591 | notifyPointerCaptureChanged(request); |
| 8592 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8593 | mWindow->assertNoEvents(); |
| 8594 | mSecondWindow->assertNoEvents(); |
| 8595 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 8596 | } |
| 8597 | |
| 8598 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8599 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8600 | |
| 8601 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8602 | notifyPointerCaptureChanged({}); |
| 8603 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8604 | |
| 8605 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8606 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8607 | mWindow->consumeCaptureEvent(false); |
| 8608 | mWindow->assertNoEvents(); |
| 8609 | } |
| 8610 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8611 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 8612 | requestAndVerifyPointerCapture(mWindow, true); |
| 8613 | |
| 8614 | // The first window loses focus. |
| 8615 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8616 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8617 | mWindow->consumeCaptureEvent(false); |
| 8618 | |
| 8619 | // Request Pointer Capture from the second window before the notification from InputReader |
| 8620 | // arrives. |
| 8621 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8622 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8623 | |
| 8624 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8625 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8626 | |
| 8627 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8628 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8629 | |
| 8630 | mSecondWindow->consumeFocusEvent(true); |
| 8631 | mSecondWindow->consumeCaptureEvent(true); |
| 8632 | } |
| 8633 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8634 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 8635 | // App repeatedly enables and disables capture. |
| 8636 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 8637 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 8638 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 8639 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 8640 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 8641 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 8642 | |
| 8643 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 8644 | // first request is now stale, this should do nothing. |
| 8645 | notifyPointerCaptureChanged(firstRequest); |
| 8646 | mWindow->assertNoEvents(); |
| 8647 | |
| 8648 | // InputReader notifies that the second request was enabled. |
| 8649 | notifyPointerCaptureChanged(secondRequest); |
| 8650 | mWindow->consumeCaptureEvent(true); |
| 8651 | } |
| 8652 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 8653 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 8654 | requestAndVerifyPointerCapture(mWindow, true); |
| 8655 | |
| 8656 | // App toggles pointer capture off and on. |
| 8657 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 8658 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 8659 | |
| 8660 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 8661 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 8662 | |
| 8663 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 8664 | // preceding "disable" request. |
| 8665 | notifyPointerCaptureChanged(enableRequest); |
| 8666 | |
| 8667 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 8668 | // any notifications. |
| 8669 | mWindow->assertNoEvents(); |
| 8670 | } |
| 8671 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8672 | /** |
| 8673 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 8674 | * mouse movements don't affect the previous mouse hovering state. |
| 8675 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 8676 | * HOVER_MOVE events). |
| 8677 | */ |
| 8678 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 8679 | // Mouse hover on the window |
| 8680 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 8681 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 8682 | .build()); |
| 8683 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 8684 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 8685 | .build()); |
| 8686 | |
| 8687 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 8688 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 8689 | |
| 8690 | // Start pointer capture |
| 8691 | requestAndVerifyPointerCapture(mWindow, true); |
| 8692 | |
| 8693 | // Send some relative mouse movements and receive them in the window. |
| 8694 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 8695 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 8696 | .build()); |
| 8697 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 8698 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 8699 | |
| 8700 | // Stop pointer capture |
| 8701 | requestAndVerifyPointerCapture(mWindow, false); |
| 8702 | |
| 8703 | // Continue hovering on the window |
| 8704 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 8705 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 8706 | .build()); |
| 8707 | mWindow->consumeMotionEvent( |
| 8708 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 8709 | |
| 8710 | mWindow->assertNoEvents(); |
| 8711 | } |
| 8712 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8713 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 8714 | protected: |
| 8715 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8716 | |
| 8717 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 8718 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 8719 | |
| 8720 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 8721 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 8722 | |
| 8723 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 8724 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 8725 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 8726 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 8727 | MAXIMUM_OBSCURING_OPACITY); |
| 8728 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8729 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 8730 | static constexpr gui::Uid APP_B_UID{10002}; |
| 8731 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8732 | |
| 8733 | sp<FakeWindowHandle> mTouchWindow; |
| 8734 | |
| 8735 | virtual void SetUp() override { |
| 8736 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8737 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8738 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 8739 | } |
| 8740 | |
| 8741 | virtual void TearDown() override { |
| 8742 | InputDispatcherTest::TearDown(); |
| 8743 | mTouchWindow.clear(); |
| 8744 | } |
| 8745 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8746 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 8747 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8748 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8749 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8750 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8751 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8752 | return window; |
| 8753 | } |
| 8754 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8755 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8756 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 8757 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8758 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8759 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8760 | 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] | 8761 | return window; |
| 8762 | } |
| 8763 | |
| 8764 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8765 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8766 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8767 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8768 | } |
| 8769 | }; |
| 8770 | |
| 8771 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8772 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8773 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8774 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8775 | |
| 8776 | touch(); |
| 8777 | |
| 8778 | mTouchWindow->assertNoEvents(); |
| 8779 | } |
| 8780 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8781 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8782 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 8783 | const sp<FakeWindowHandle>& w = |
| 8784 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8785 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8786 | |
| 8787 | touch(); |
| 8788 | |
| 8789 | mTouchWindow->assertNoEvents(); |
| 8790 | } |
| 8791 | |
| 8792 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8793 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 8794 | const sp<FakeWindowHandle>& w = |
| 8795 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8796 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8797 | |
| 8798 | touch(); |
| 8799 | |
| 8800 | w->assertNoEvents(); |
| 8801 | } |
| 8802 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8803 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8804 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8805 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8806 | |
| 8807 | touch(); |
| 8808 | |
| 8809 | mTouchWindow->consumeAnyMotionDown(); |
| 8810 | } |
| 8811 | |
| 8812 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8813 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8814 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8815 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8816 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8817 | |
| 8818 | touch({PointF{100, 100}}); |
| 8819 | |
| 8820 | mTouchWindow->consumeAnyMotionDown(); |
| 8821 | } |
| 8822 | |
| 8823 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8824 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8825 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8826 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8827 | |
| 8828 | touch(); |
| 8829 | |
| 8830 | mTouchWindow->consumeAnyMotionDown(); |
| 8831 | } |
| 8832 | |
| 8833 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 8834 | const sp<FakeWindowHandle>& w = |
| 8835 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8836 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8837 | |
| 8838 | touch(); |
| 8839 | |
| 8840 | mTouchWindow->consumeAnyMotionDown(); |
| 8841 | } |
| 8842 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8843 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 8844 | const sp<FakeWindowHandle>& w = |
| 8845 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8846 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8847 | |
| 8848 | touch(); |
| 8849 | |
| 8850 | w->assertNoEvents(); |
| 8851 | } |
| 8852 | |
| 8853 | /** |
| 8854 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 8855 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 8856 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 8857 | */ |
| 8858 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8859 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 8860 | const sp<FakeWindowHandle>& w = |
| 8861 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8862 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8863 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8864 | |
| 8865 | touch(); |
| 8866 | |
| 8867 | w->consumeMotionOutside(); |
| 8868 | } |
| 8869 | |
| 8870 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 8871 | const sp<FakeWindowHandle>& w = |
| 8872 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8873 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8874 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8875 | |
| 8876 | touch(); |
| 8877 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8878 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8879 | } |
| 8880 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8881 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8882 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8883 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8884 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8885 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8886 | |
| 8887 | touch(); |
| 8888 | |
| 8889 | mTouchWindow->consumeAnyMotionDown(); |
| 8890 | } |
| 8891 | |
| 8892 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 8893 | const sp<FakeWindowHandle>& w = |
| 8894 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8895 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8896 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8897 | |
| 8898 | touch(); |
| 8899 | |
| 8900 | mTouchWindow->consumeAnyMotionDown(); |
| 8901 | } |
| 8902 | |
| 8903 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8904 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8905 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8906 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8907 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8908 | |
| 8909 | touch(); |
| 8910 | |
| 8911 | mTouchWindow->assertNoEvents(); |
| 8912 | } |
| 8913 | |
| 8914 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 8915 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 8916 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8917 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8918 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8919 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8920 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8921 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8922 | mDispatcher->onWindowInfosChanged( |
| 8923 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8924 | |
| 8925 | touch(); |
| 8926 | |
| 8927 | mTouchWindow->assertNoEvents(); |
| 8928 | } |
| 8929 | |
| 8930 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 8931 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 8932 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8933 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8934 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8935 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8936 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8937 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8938 | mDispatcher->onWindowInfosChanged( |
| 8939 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8940 | |
| 8941 | touch(); |
| 8942 | |
| 8943 | mTouchWindow->consumeAnyMotionDown(); |
| 8944 | } |
| 8945 | |
| 8946 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8947 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 8948 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8949 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8950 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8951 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8952 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8953 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8954 | mDispatcher->onWindowInfosChanged( |
| 8955 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8956 | |
| 8957 | touch(); |
| 8958 | |
| 8959 | mTouchWindow->consumeAnyMotionDown(); |
| 8960 | } |
| 8961 | |
| 8962 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 8963 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8964 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8965 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8966 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8967 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8968 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8969 | mDispatcher->onWindowInfosChanged( |
| 8970 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8971 | |
| 8972 | touch(); |
| 8973 | |
| 8974 | mTouchWindow->assertNoEvents(); |
| 8975 | } |
| 8976 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8977 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8978 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 8979 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8980 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8981 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8982 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8983 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8984 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8985 | mDispatcher->onWindowInfosChanged( |
| 8986 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8987 | |
| 8988 | touch(); |
| 8989 | |
| 8990 | mTouchWindow->assertNoEvents(); |
| 8991 | } |
| 8992 | |
| 8993 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8994 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 8995 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8996 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8997 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8998 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8999 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9000 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9001 | mDispatcher->onWindowInfosChanged( |
| 9002 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9003 | |
| 9004 | touch(); |
| 9005 | |
| 9006 | mTouchWindow->consumeAnyMotionDown(); |
| 9007 | } |
| 9008 | |
| 9009 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 9010 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9011 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9012 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9013 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9014 | |
| 9015 | touch(); |
| 9016 | |
| 9017 | mTouchWindow->consumeAnyMotionDown(); |
| 9018 | } |
| 9019 | |
| 9020 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 9021 | const sp<FakeWindowHandle>& w = |
| 9022 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9023 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9024 | |
| 9025 | touch(); |
| 9026 | |
| 9027 | mTouchWindow->consumeAnyMotionDown(); |
| 9028 | } |
| 9029 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9030 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9031 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 9032 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 9033 | const sp<FakeWindowHandle>& w = |
| 9034 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9035 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9036 | |
| 9037 | touch(); |
| 9038 | |
| 9039 | mTouchWindow->assertNoEvents(); |
| 9040 | } |
| 9041 | |
| 9042 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 9043 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 9044 | const sp<FakeWindowHandle>& w = |
| 9045 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9046 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9047 | |
| 9048 | touch(); |
| 9049 | |
| 9050 | mTouchWindow->consumeAnyMotionDown(); |
| 9051 | } |
| 9052 | |
| 9053 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9054 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 9055 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 9056 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9057 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9058 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9059 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9060 | |
| 9061 | touch(); |
| 9062 | |
| 9063 | mTouchWindow->consumeAnyMotionDown(); |
| 9064 | } |
| 9065 | |
| 9066 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9067 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 9068 | const sp<FakeWindowHandle>& w1 = |
| 9069 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 9070 | OPACITY_BELOW_THRESHOLD); |
| 9071 | const sp<FakeWindowHandle>& w2 = |
| 9072 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9073 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9074 | mDispatcher->onWindowInfosChanged( |
| 9075 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9076 | |
| 9077 | touch(); |
| 9078 | |
| 9079 | mTouchWindow->assertNoEvents(); |
| 9080 | } |
| 9081 | |
| 9082 | /** |
| 9083 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 9084 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 9085 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 9086 | */ |
| 9087 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9088 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 9089 | const sp<FakeWindowHandle>& wB = |
| 9090 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 9091 | OPACITY_BELOW_THRESHOLD); |
| 9092 | const sp<FakeWindowHandle>& wC = |
| 9093 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9094 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9095 | mDispatcher->onWindowInfosChanged( |
| 9096 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9097 | |
| 9098 | touch(); |
| 9099 | |
| 9100 | mTouchWindow->assertNoEvents(); |
| 9101 | } |
| 9102 | |
| 9103 | /** |
| 9104 | * This test is testing that a window from a different UID but with same application token doesn't |
| 9105 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 9106 | */ |
| 9107 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9108 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 9109 | const sp<FakeWindowHandle>& w = |
| 9110 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 9111 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9112 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9113 | |
| 9114 | touch(); |
| 9115 | |
| 9116 | mTouchWindow->consumeAnyMotionDown(); |
| 9117 | } |
| 9118 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9119 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 9120 | protected: |
| 9121 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9122 | sp<FakeWindowHandle> mWindow; |
| 9123 | sp<FakeWindowHandle> mSecondWindow; |
| 9124 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9125 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9126 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 9127 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9128 | |
| 9129 | void SetUp() override { |
| 9130 | InputDispatcherTest::SetUp(); |
| 9131 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9132 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9133 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9134 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9135 | mSecondWindow = |
| 9136 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9137 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9138 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9139 | mSpyWindow = |
| 9140 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9141 | mSpyWindow->setSpy(true); |
| 9142 | mSpyWindow->setTrustedOverlay(true); |
| 9143 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 9144 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9145 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9146 | mDispatcher->onWindowInfosChanged( |
| 9147 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 9148 | {}, |
| 9149 | 0, |
| 9150 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9151 | } |
| 9152 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9153 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 9154 | switch (fromSource) { |
| 9155 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 9156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9157 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9158 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9159 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9160 | break; |
| 9161 | case AINPUT_SOURCE_STYLUS: |
| 9162 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9163 | injectMotionEvent(*mDispatcher, |
| 9164 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9165 | AINPUT_SOURCE_STYLUS) |
| 9166 | .buttonState( |
| 9167 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 9168 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 9169 | .x(50) |
| 9170 | .y(50)) |
| 9171 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9172 | break; |
| 9173 | case AINPUT_SOURCE_MOUSE: |
| 9174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9175 | injectMotionEvent(*mDispatcher, |
| 9176 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9177 | AINPUT_SOURCE_MOUSE) |
| 9178 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 9179 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 9180 | ToolType::MOUSE) |
| 9181 | .x(50) |
| 9182 | .y(50)) |
| 9183 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9184 | break; |
| 9185 | default: |
| 9186 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 9187 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9188 | |
| 9189 | // Window should receive motion event. |
| 9190 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9191 | // Spy window should also receive motion event |
| 9192 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9193 | } |
| 9194 | |
| 9195 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 9196 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 9197 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9198 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9199 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9200 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9201 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9202 | |
| 9203 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9204 | mDragWindow = |
| 9205 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9206 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9207 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 9208 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 9209 | {}, |
| 9210 | 0, |
| 9211 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9212 | |
| 9213 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9214 | bool transferred = |
| 9215 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9216 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9217 | if (transferred) { |
| 9218 | mWindow->consumeMotionCancel(); |
| 9219 | mDragWindow->consumeMotionDown(); |
| 9220 | } |
| 9221 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9222 | } |
| 9223 | }; |
| 9224 | |
| 9225 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9226 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9227 | |
| 9228 | // Move on window. |
| 9229 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9230 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9231 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9232 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9233 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9234 | mWindow->consumeDragEvent(false, 50, 50); |
| 9235 | mSecondWindow->assertNoEvents(); |
| 9236 | |
| 9237 | // Move to another window. |
| 9238 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9239 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9240 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9241 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9242 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9243 | mWindow->consumeDragEvent(true, 150, 50); |
| 9244 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9245 | |
| 9246 | // Move back to original window. |
| 9247 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9248 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9249 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9250 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9251 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9252 | mWindow->consumeDragEvent(false, 50, 50); |
| 9253 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 9254 | |
| 9255 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9256 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9257 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9258 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9259 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9260 | mWindow->assertNoEvents(); |
| 9261 | mSecondWindow->assertNoEvents(); |
| 9262 | } |
| 9263 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9264 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9265 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9266 | |
| 9267 | // No cancel event after drag start |
| 9268 | mSpyWindow->assertNoEvents(); |
| 9269 | |
| 9270 | const MotionEvent secondFingerDownEvent = |
| 9271 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9272 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9273 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9274 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9275 | .build(); |
| 9276 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9277 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9278 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9279 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9280 | |
| 9281 | // Receives cancel for first pointer after next pointer down |
| 9282 | mSpyWindow->consumeMotionCancel(); |
| 9283 | mSpyWindow->consumeMotionDown(); |
| 9284 | |
| 9285 | mSpyWindow->assertNoEvents(); |
| 9286 | } |
| 9287 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9288 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9289 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9290 | |
| 9291 | // Move on window. |
| 9292 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9293 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9294 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9295 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9296 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9297 | mWindow->consumeDragEvent(false, 50, 50); |
| 9298 | mSecondWindow->assertNoEvents(); |
| 9299 | |
| 9300 | // Move to another window. |
| 9301 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9302 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9303 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9304 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9305 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9306 | mWindow->consumeDragEvent(true, 150, 50); |
| 9307 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9308 | |
| 9309 | // drop to another window. |
| 9310 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9311 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9312 | {150, 50})) |
| 9313 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9314 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9315 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9316 | mWindow->assertNoEvents(); |
| 9317 | mSecondWindow->assertNoEvents(); |
| 9318 | } |
| 9319 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9320 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9321 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9322 | |
| 9323 | // Move on window and keep button pressed. |
| 9324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9325 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9326 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 9327 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9328 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9329 | .build())) |
| 9330 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9331 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9332 | mWindow->consumeDragEvent(false, 50, 50); |
| 9333 | mSecondWindow->assertNoEvents(); |
| 9334 | |
| 9335 | // Move to another window and release button, expect to drop item. |
| 9336 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9337 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9338 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 9339 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9340 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9341 | .build())) |
| 9342 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9343 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9344 | mWindow->assertNoEvents(); |
| 9345 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9346 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9347 | |
| 9348 | // nothing to the window. |
| 9349 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9350 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9351 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 9352 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9353 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9354 | .build())) |
| 9355 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9356 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9357 | mWindow->assertNoEvents(); |
| 9358 | mSecondWindow->assertNoEvents(); |
| 9359 | } |
| 9360 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9361 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9362 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9363 | |
| 9364 | // Set second window invisible. |
| 9365 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9366 | mDispatcher->onWindowInfosChanged( |
| 9367 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9368 | |
| 9369 | // Move on window. |
| 9370 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9371 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9372 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9373 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9374 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9375 | mWindow->consumeDragEvent(false, 50, 50); |
| 9376 | mSecondWindow->assertNoEvents(); |
| 9377 | |
| 9378 | // Move to another window. |
| 9379 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9380 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9381 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9382 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9383 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9384 | mWindow->consumeDragEvent(true, 150, 50); |
| 9385 | mSecondWindow->assertNoEvents(); |
| 9386 | |
| 9387 | // drop to another window. |
| 9388 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9389 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9390 | {150, 50})) |
| 9391 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9392 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9393 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9394 | mWindow->assertNoEvents(); |
| 9395 | mSecondWindow->assertNoEvents(); |
| 9396 | } |
| 9397 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9398 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9399 | // Ensure window could track pointerIds if it didn't support split touch. |
| 9400 | mWindow->setPreventSplitting(true); |
| 9401 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9402 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9403 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9404 | {50, 50})) |
| 9405 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9406 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9407 | |
| 9408 | const MotionEvent secondFingerDownEvent = |
| 9409 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9410 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9411 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9412 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9413 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9414 | .build(); |
| 9415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9416 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9417 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9418 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9419 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9420 | |
| 9421 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9422 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9423 | } |
| 9424 | |
| 9425 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 9426 | // First down on second window. |
| 9427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9428 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9429 | {150, 50})) |
| 9430 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9431 | |
| 9432 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9433 | |
| 9434 | // Second down on first window. |
| 9435 | const MotionEvent secondFingerDownEvent = |
| 9436 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9437 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9438 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9439 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9440 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9441 | .build(); |
| 9442 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9443 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9444 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9445 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9446 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9447 | |
| 9448 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9449 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9450 | |
| 9451 | // Move on window. |
| 9452 | const MotionEvent secondFingerMoveEvent = |
| 9453 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 9454 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9455 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9456 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9457 | .build(); |
| 9458 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9459 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9460 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 9461 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9462 | mWindow->consumeDragEvent(false, 50, 50); |
| 9463 | mSecondWindow->consumeMotionMove(); |
| 9464 | |
| 9465 | // Release the drag pointer should perform drop. |
| 9466 | const MotionEvent secondFingerUpEvent = |
| 9467 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9468 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9469 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9470 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9471 | .build(); |
| 9472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9473 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9474 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 9475 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9476 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9477 | mWindow->assertNoEvents(); |
| 9478 | mSecondWindow->consumeMotionMove(); |
| 9479 | } |
| 9480 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9481 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9482 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9483 | |
| 9484 | // Update window of second display. |
| 9485 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9486 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9487 | mDispatcher->onWindowInfosChanged( |
| 9488 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 9489 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 9490 | {}, |
| 9491 | 0, |
| 9492 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9493 | |
| 9494 | // Let second display has a touch state. |
| 9495 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9496 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9497 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9498 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9499 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9500 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9501 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 9502 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9503 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9504 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9505 | mDispatcher->onWindowInfosChanged( |
| 9506 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 9507 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 9508 | {}, |
| 9509 | 0, |
| 9510 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9511 | |
| 9512 | // Move on window. |
| 9513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9514 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9515 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9516 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9517 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9518 | mWindow->consumeDragEvent(false, 50, 50); |
| 9519 | mSecondWindow->assertNoEvents(); |
| 9520 | |
| 9521 | // Move to another window. |
| 9522 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9523 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9524 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9525 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9526 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9527 | mWindow->consumeDragEvent(true, 150, 50); |
| 9528 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9529 | |
| 9530 | // drop to another window. |
| 9531 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9532 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9533 | {150, 50})) |
| 9534 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9535 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9536 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9537 | mWindow->assertNoEvents(); |
| 9538 | mSecondWindow->assertNoEvents(); |
| 9539 | } |
| 9540 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9541 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 9542 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 9543 | // Move on window. |
| 9544 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9545 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9546 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 9547 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9548 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9549 | .x(50) |
| 9550 | .y(50)) |
| 9551 | .build())) |
| 9552 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9553 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9554 | mWindow->consumeDragEvent(false, 50, 50); |
| 9555 | mSecondWindow->assertNoEvents(); |
| 9556 | |
| 9557 | // Move to another window. |
| 9558 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9559 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9560 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 9561 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9562 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9563 | .x(150) |
| 9564 | .y(50)) |
| 9565 | .build())) |
| 9566 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9567 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9568 | mWindow->consumeDragEvent(true, 150, 50); |
| 9569 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9570 | |
| 9571 | // drop to another window. |
| 9572 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9573 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9574 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 9575 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9576 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9577 | .x(150) |
| 9578 | .y(50)) |
| 9579 | .build())) |
| 9580 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9581 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9582 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9583 | mWindow->assertNoEvents(); |
| 9584 | mSecondWindow->assertNoEvents(); |
| 9585 | } |
| 9586 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 9587 | /** |
| 9588 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 9589 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 9590 | */ |
| 9591 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 9592 | // Down on second window |
| 9593 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9594 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9595 | {150, 50})) |
| 9596 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9597 | |
| 9598 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 9599 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 9600 | |
| 9601 | // Down on first window |
| 9602 | const MotionEvent secondFingerDownEvent = |
| 9603 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9604 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9605 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9606 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 9607 | .build(); |
| 9608 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9609 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9610 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9611 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9612 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 9613 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 9614 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 9615 | |
| 9616 | // Start drag on first window |
| 9617 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 9618 | |
| 9619 | // Trigger cancel |
| 9620 | mDispatcher->cancelCurrentTouch(); |
| 9621 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
| 9622 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel()); |
| 9623 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 9624 | |
| 9625 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9626 | // The D&D finished with nullptr |
| 9627 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 9628 | |
| 9629 | // Remove drag window |
| 9630 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 9631 | |
| 9632 | // Inject a simple gesture, ensure dispatcher not crashed |
| 9633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9634 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9635 | PointF{50, 50})) |
| 9636 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9637 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 9638 | |
| 9639 | const MotionEvent moveEvent = |
| 9640 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 9641 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9642 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9643 | .build(); |
| 9644 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9645 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 9646 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9647 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9648 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 9649 | |
| 9650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9651 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9652 | {50, 50})) |
| 9653 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9654 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 9655 | } |
| 9656 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9657 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 9658 | |
| 9659 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 9660 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9661 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 9662 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9663 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9664 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9665 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9666 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9667 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9668 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9669 | |
| 9670 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9671 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9672 | window->assertNoEvents(); |
| 9673 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9674 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9675 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 9676 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 9677 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9678 | window->assertNoEvents(); |
| 9679 | |
| 9680 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9681 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9682 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9683 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9684 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9685 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 9686 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9687 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9688 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9689 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9690 | window->assertNoEvents(); |
| 9691 | } |
| 9692 | |
| 9693 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 9694 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 9695 | std::make_shared<FakeApplicationHandle>(); |
| 9696 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9697 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 9698 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9699 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9700 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9701 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9702 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9703 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 9704 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9705 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9706 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9707 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9708 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9709 | mDispatcher->onWindowInfosChanged( |
| 9710 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9711 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9712 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9713 | |
| 9714 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9715 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9716 | window->assertNoEvents(); |
| 9717 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9718 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9719 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 9720 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 9721 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9722 | window->assertNoEvents(); |
| 9723 | |
| 9724 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9725 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9726 | mDispatcher->onWindowInfosChanged( |
| 9727 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9728 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9729 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9730 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 9731 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9732 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9733 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9734 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 9735 | window->assertNoEvents(); |
| 9736 | } |
| 9737 | |
| 9738 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 9739 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 9740 | std::make_shared<FakeApplicationHandle>(); |
| 9741 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9742 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 9743 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9744 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9745 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9746 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9747 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9748 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 9749 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9750 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9751 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9752 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9753 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9754 | mDispatcher->onWindowInfosChanged( |
| 9755 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9756 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9757 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9758 | |
| 9759 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9760 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9761 | window->assertNoEvents(); |
| 9762 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9763 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9764 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 9765 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 9766 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9767 | window->assertNoEvents(); |
| 9768 | |
| 9769 | // 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] | 9770 | mDispatcher->onWindowInfosChanged( |
| 9771 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9772 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9773 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9774 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 9775 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9776 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9777 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9778 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9779 | window->assertNoEvents(); |
| 9780 | } |
| 9781 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9782 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 9783 | protected: |
| 9784 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9785 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9786 | sp<FakeWindowHandle> mWindow; |
| 9787 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9788 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9789 | |
| 9790 | void SetUp() override { |
| 9791 | InputDispatcherTest::SetUp(); |
| 9792 | |
| 9793 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9794 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9795 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9796 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9797 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9798 | mSecondWindow = |
| 9799 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9800 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9801 | mThirdWindow = |
| 9802 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 9803 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 9804 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9805 | |
| 9806 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9807 | mDispatcher->onWindowInfosChanged( |
| 9808 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 9809 | {}, |
| 9810 | 0, |
| 9811 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9812 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9813 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9814 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9815 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9816 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9817 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 9818 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 9819 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9820 | mThirdWindow->assertNoEvents(); |
| 9821 | } |
| 9822 | |
| 9823 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 9824 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9825 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9826 | SECOND_DISPLAY_ID)) { |
| 9827 | mWindow->assertNoEvents(); |
| 9828 | mSecondWindow->assertNoEvents(); |
| 9829 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 9830 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9831 | } |
| 9832 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9833 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9834 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 9835 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 9836 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9837 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 9838 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9839 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9840 | } |
| 9841 | }; |
| 9842 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9843 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 9844 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9845 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 9846 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9847 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9848 | } |
| 9849 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9850 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 9851 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9852 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9853 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9854 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9855 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9856 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 9857 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9858 | mWindow->assertNoEvents(); |
| 9859 | mSecondWindow->assertNoEvents(); |
| 9860 | } |
| 9861 | |
| 9862 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 9863 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9864 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9865 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9866 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9867 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9868 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9869 | } |
| 9870 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9871 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 9872 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9873 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 9874 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9875 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9876 | mWindow->assertNoEvents(); |
| 9877 | mSecondWindow->assertNoEvents(); |
| 9878 | } |
| 9879 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9880 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 9881 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 9882 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 9883 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9884 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9885 | mWindow->assertNoEvents(); |
| 9886 | mSecondWindow->assertNoEvents(); |
| 9887 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 9888 | } |
| 9889 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 9890 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 9891 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9892 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9893 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 9894 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9895 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9896 | |
| 9897 | // Then remove focus. |
| 9898 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9899 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 9900 | |
| 9901 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 9902 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 9903 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 9904 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9905 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 9906 | } |
| 9907 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9908 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 9909 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9910 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9911 | std::shared_ptr<FakeApplicationHandle> application = |
| 9912 | std::make_shared<FakeApplicationHandle>(); |
| 9913 | std::string name = "Fake Spy "; |
| 9914 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9915 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 9916 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9917 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9918 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9919 | return spy; |
| 9920 | } |
| 9921 | |
| 9922 | sp<FakeWindowHandle> createForeground() { |
| 9923 | std::shared_ptr<FakeApplicationHandle> application = |
| 9924 | std::make_shared<FakeApplicationHandle>(); |
| 9925 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9926 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 9927 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9928 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9929 | return window; |
| 9930 | } |
| 9931 | |
| 9932 | private: |
| 9933 | int mSpyCount{0}; |
| 9934 | }; |
| 9935 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9936 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9937 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9938 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 9939 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9940 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 9941 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9942 | ScopedSilentDeath _silentDeath; |
| 9943 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9944 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9945 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9946 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9947 | ".* not a trusted overlay"); |
| 9948 | } |
| 9949 | |
| 9950 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9951 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 9952 | */ |
| 9953 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9954 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9955 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9956 | |
| 9957 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9958 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9959 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9960 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9961 | } |
| 9962 | |
| 9963 | /** |
| 9964 | * Verify the order in which different input windows receive events. The touched foreground window |
| 9965 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 9966 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 9967 | * receive events before ones belows it. |
| 9968 | * |
| 9969 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 9970 | * spy1, spy2, window, spy3. |
| 9971 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 9972 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 9973 | * window. |
| 9974 | */ |
| 9975 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 9976 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9977 | auto spy1 = createSpy(); |
| 9978 | auto spy2 = createSpy(); |
| 9979 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9980 | mDispatcher->onWindowInfosChanged( |
| 9981 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9982 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 9983 | const size_t numChannels = channels.size(); |
| 9984 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 9985 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9986 | if (!epollFd.ok()) { |
| 9987 | FAIL() << "Failed to create epoll fd"; |
| 9988 | } |
| 9989 | |
| 9990 | for (size_t i = 0; i < numChannels; i++) { |
| 9991 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 9992 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 9993 | FAIL() << "Failed to add fd to epoll"; |
| 9994 | } |
| 9995 | } |
| 9996 | |
| 9997 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9998 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9999 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10000 | |
| 10001 | std::vector<size_t> eventOrder; |
| 10002 | std::vector<struct epoll_event> events(numChannels); |
| 10003 | for (;;) { |
| 10004 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 10005 | (100ms).count()); |
| 10006 | if (nFds < 0) { |
| 10007 | FAIL() << "Failed to call epoll_wait"; |
| 10008 | } |
| 10009 | if (nFds == 0) { |
| 10010 | break; // epoll_wait timed out |
| 10011 | } |
| 10012 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 10013 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 10014 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10015 | channels[i]->consumeMotionDown(); |
| 10016 | } |
| 10017 | } |
| 10018 | |
| 10019 | // Verify the order in which the events were received. |
| 10020 | EXPECT_EQ(3u, eventOrder.size()); |
| 10021 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 10022 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 10023 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 10024 | } |
| 10025 | |
| 10026 | /** |
| 10027 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 10028 | */ |
| 10029 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 10030 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10031 | auto spy = createSpy(); |
| 10032 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10033 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10034 | |
| 10035 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10036 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10037 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10038 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10039 | spy->assertNoEvents(); |
| 10040 | } |
| 10041 | |
| 10042 | /** |
| 10043 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 10044 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 10045 | * to the window. |
| 10046 | */ |
| 10047 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 10048 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10049 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10050 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10051 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10052 | |
| 10053 | // Inject an event outside the spy window's touchable region. |
| 10054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10055 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10056 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10057 | window->consumeMotionDown(); |
| 10058 | spy->assertNoEvents(); |
| 10059 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10060 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10061 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10062 | window->consumeMotionUp(); |
| 10063 | spy->assertNoEvents(); |
| 10064 | |
| 10065 | // Inject an event inside the spy window's touchable region. |
| 10066 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10067 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10068 | {5, 10})) |
| 10069 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10070 | window->consumeMotionDown(); |
| 10071 | spy->consumeMotionDown(); |
| 10072 | } |
| 10073 | |
| 10074 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10075 | * 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] | 10076 | * 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] | 10077 | */ |
| 10078 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 10079 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10080 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10081 | auto spy = createSpy(); |
| 10082 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10083 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10084 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10085 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10086 | |
| 10087 | // Inject an event outside the spy window's frame and touchable region. |
| 10088 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10089 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10090 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10091 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10092 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10093 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10094 | } |
| 10095 | |
| 10096 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10097 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 10098 | * pointers that are down within its bounds. |
| 10099 | */ |
| 10100 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 10101 | auto windowLeft = createForeground(); |
| 10102 | windowLeft->setFrame({0, 0, 100, 200}); |
| 10103 | auto windowRight = createForeground(); |
| 10104 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10105 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10106 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10107 | mDispatcher->onWindowInfosChanged( |
| 10108 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10109 | |
| 10110 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10111 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10112 | {50, 50})) |
| 10113 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10114 | windowLeft->consumeMotionDown(); |
| 10115 | spy->consumeMotionDown(); |
| 10116 | |
| 10117 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10118 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10119 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10120 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10121 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10122 | .build(); |
| 10123 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10124 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10125 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10126 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10127 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10128 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10129 | } |
| 10130 | |
| 10131 | /** |
| 10132 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 10133 | * the spy should receive the second pointer with ACTION_DOWN. |
| 10134 | */ |
| 10135 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 10136 | auto window = createForeground(); |
| 10137 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10138 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10139 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10140 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10141 | |
| 10142 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10143 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10144 | {50, 50})) |
| 10145 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10146 | window->consumeMotionDown(); |
| 10147 | spyRight->assertNoEvents(); |
| 10148 | |
| 10149 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10150 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10151 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10152 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10153 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10154 | .build(); |
| 10155 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10156 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10157 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10158 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10159 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10160 | spyRight->consumeMotionDown(); |
| 10161 | } |
| 10162 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10163 | /** |
| 10164 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 10165 | * windows should be allowed to control split touch. |
| 10166 | */ |
| 10167 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 10168 | // 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] | 10169 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10170 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 10171 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10172 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10173 | auto window = createForeground(); |
| 10174 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10175 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10176 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10177 | |
| 10178 | // First finger down, no window touched. |
| 10179 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10180 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10181 | {100, 200})) |
| 10182 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10183 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10184 | window->assertNoEvents(); |
| 10185 | |
| 10186 | // Second finger down on window, the window should receive touch down. |
| 10187 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10188 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10189 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10190 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10191 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10192 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10193 | .build(); |
| 10194 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10195 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10196 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10197 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10198 | |
| 10199 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10200 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10201 | } |
| 10202 | |
| 10203 | /** |
| 10204 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 10205 | * do not receive key events. |
| 10206 | */ |
| 10207 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10208 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10209 | spy->setFocusable(false); |
| 10210 | |
| 10211 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10212 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10213 | setFocusedWindow(window); |
| 10214 | window->consumeFocusEvent(true); |
| 10215 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10217 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10218 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 10219 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10220 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10221 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10222 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 10223 | |
| 10224 | spy->assertNoEvents(); |
| 10225 | } |
| 10226 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10227 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 10228 | |
| 10229 | /** |
| 10230 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 10231 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 10232 | */ |
| 10233 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 10234 | auto window = createForeground(); |
| 10235 | auto spy1 = createSpy(); |
| 10236 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10237 | mDispatcher->onWindowInfosChanged( |
| 10238 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10239 | |
| 10240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10241 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10242 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10243 | window->consumeMotionDown(); |
| 10244 | spy1->consumeMotionDown(); |
| 10245 | spy2->consumeMotionDown(); |
| 10246 | |
| 10247 | // Pilfer pointers from the second spy window. |
| 10248 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 10249 | spy2->assertNoEvents(); |
| 10250 | spy1->consumeMotionCancel(); |
| 10251 | window->consumeMotionCancel(); |
| 10252 | |
| 10253 | // The rest of the gesture should only be sent to the second spy window. |
| 10254 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10255 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10256 | ADISPLAY_ID_DEFAULT)) |
| 10257 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10258 | spy2->consumeMotionMove(); |
| 10259 | spy1->assertNoEvents(); |
| 10260 | window->assertNoEvents(); |
| 10261 | } |
| 10262 | |
| 10263 | /** |
| 10264 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 10265 | * in the middle of the gesture. |
| 10266 | */ |
| 10267 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 10268 | auto window = createForeground(); |
| 10269 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10270 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10271 | |
| 10272 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10273 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10274 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10275 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10276 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10277 | |
| 10278 | window->releaseChannel(); |
| 10279 | |
| 10280 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10281 | |
| 10282 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10283 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10284 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10285 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10286 | } |
| 10287 | |
| 10288 | /** |
| 10289 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 10290 | * the spy, but not to any other windows. |
| 10291 | */ |
| 10292 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 10293 | auto spy = createSpy(); |
| 10294 | auto window = createForeground(); |
| 10295 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10296 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10297 | |
| 10298 | // First finger down on the window and the spy. |
| 10299 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10300 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10301 | {100, 200})) |
| 10302 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10303 | spy->consumeMotionDown(); |
| 10304 | window->consumeMotionDown(); |
| 10305 | |
| 10306 | // Spy window pilfers the pointers. |
| 10307 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10308 | window->consumeMotionCancel(); |
| 10309 | |
| 10310 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 10311 | const MotionEvent secondFingerDownEvent = |
| 10312 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10313 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10314 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10315 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10316 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10317 | .build(); |
| 10318 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10319 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10320 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10321 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10322 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10323 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10324 | |
| 10325 | // Third finger goes down outside all windows, so injection should fail. |
| 10326 | const MotionEvent thirdFingerDownEvent = |
| 10327 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10328 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10329 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10330 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10331 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 10332 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10333 | .build(); |
| 10334 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10335 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10336 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 10337 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10338 | |
| 10339 | spy->assertNoEvents(); |
| 10340 | window->assertNoEvents(); |
| 10341 | } |
| 10342 | |
| 10343 | /** |
| 10344 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 10345 | */ |
| 10346 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 10347 | auto spy = createSpy(); |
| 10348 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 10349 | auto window = createForeground(); |
| 10350 | window->setFrame(Rect(0, 0, 200, 200)); |
| 10351 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10352 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10353 | |
| 10354 | // First finger down on the window only |
| 10355 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10356 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10357 | {150, 150})) |
| 10358 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10359 | window->consumeMotionDown(); |
| 10360 | |
| 10361 | // Second finger down on the spy and window |
| 10362 | const MotionEvent secondFingerDownEvent = |
| 10363 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10364 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10365 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10366 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 10367 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10368 | .build(); |
| 10369 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10370 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10371 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10372 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10373 | spy->consumeMotionDown(); |
| 10374 | window->consumeMotionPointerDown(1); |
| 10375 | |
| 10376 | // Third finger down on the spy and window |
| 10377 | const MotionEvent thirdFingerDownEvent = |
| 10378 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10379 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10380 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10381 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 10382 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 10383 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10384 | .build(); |
| 10385 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10386 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10387 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10388 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10389 | spy->consumeMotionPointerDown(1); |
| 10390 | window->consumeMotionPointerDown(2); |
| 10391 | |
| 10392 | // Spy window pilfers the pointers. |
| 10393 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 10394 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 10395 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10396 | |
| 10397 | spy->assertNoEvents(); |
| 10398 | window->assertNoEvents(); |
| 10399 | } |
| 10400 | |
| 10401 | /** |
| 10402 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 10403 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 10404 | * window, then that window should receive ACTION_CANCEL. |
| 10405 | */ |
| 10406 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 10407 | auto spy = createSpy(); |
| 10408 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 10409 | auto window = createForeground(); |
| 10410 | window->setFrame(Rect(0, 0, 200, 200)); |
| 10411 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10412 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10413 | |
| 10414 | // First finger down on both spy and window |
| 10415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10416 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10417 | {10, 10})) |
| 10418 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10419 | window->consumeMotionDown(); |
| 10420 | spy->consumeMotionDown(); |
| 10421 | |
| 10422 | // Second finger down on the spy and window |
| 10423 | const MotionEvent secondFingerDownEvent = |
| 10424 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10425 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10426 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10427 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 10428 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10429 | .build(); |
| 10430 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10431 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10432 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10433 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10434 | spy->consumeMotionPointerDown(1); |
| 10435 | window->consumeMotionPointerDown(1); |
| 10436 | |
| 10437 | // Spy window pilfers the pointers. |
| 10438 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10439 | window->consumeMotionCancel(); |
| 10440 | |
| 10441 | spy->assertNoEvents(); |
| 10442 | window->assertNoEvents(); |
| 10443 | } |
| 10444 | |
| 10445 | /** |
| 10446 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 10447 | * be sent to other windows |
| 10448 | */ |
| 10449 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 10450 | auto spy = createSpy(); |
| 10451 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 10452 | auto window = createForeground(); |
| 10453 | window->setFrame(Rect(0, 0, 200, 200)); |
| 10454 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10455 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10456 | |
| 10457 | // First finger down on both window and spy |
| 10458 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10459 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10460 | {10, 10})) |
| 10461 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10462 | window->consumeMotionDown(); |
| 10463 | spy->consumeMotionDown(); |
| 10464 | |
| 10465 | // Spy window pilfers the pointers. |
| 10466 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10467 | window->consumeMotionCancel(); |
| 10468 | |
| 10469 | // Second finger down on the window only |
| 10470 | const MotionEvent secondFingerDownEvent = |
| 10471 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10472 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10473 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10474 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 10475 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10476 | .build(); |
| 10477 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10478 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10479 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10480 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10481 | window->consumeMotionDown(); |
| 10482 | window->assertNoEvents(); |
| 10483 | |
| 10484 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 10485 | spy->consumeMotionMove(); |
| 10486 | spy->assertNoEvents(); |
| 10487 | } |
| 10488 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10489 | /** |
| 10490 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 10491 | * windows, and spanning both left and right windows. |
| 10492 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 10493 | * to the right window. |
| 10494 | * Pilfer from spy window. |
| 10495 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 10496 | */ |
| 10497 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 10498 | sp<FakeWindowHandle> spy = createSpy(); |
| 10499 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 10500 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 10501 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 10502 | |
| 10503 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 10504 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 10505 | |
| 10506 | constexpr int32_t stylusDeviceId = 1; |
| 10507 | constexpr int32_t touchDeviceId = 2; |
| 10508 | |
| 10509 | mDispatcher->onWindowInfosChanged( |
| 10510 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 10511 | |
| 10512 | // Stylus down on left window and spy |
| 10513 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 10514 | .deviceId(stylusDeviceId) |
| 10515 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 10516 | .build()); |
| 10517 | leftWindow->consumeMotionEvent( |
| 10518 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 10519 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 10520 | |
| 10521 | // Finger down on right window and spy - but spy already has stylus |
| 10522 | mDispatcher->notifyMotion( |
| 10523 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10524 | .deviceId(touchDeviceId) |
| 10525 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 10526 | .build()); |
| 10527 | rightWindow->consumeMotionEvent( |
| 10528 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 10529 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10530 | |
| 10531 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 10532 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 10533 | leftWindow->consumeMotionEvent( |
| 10534 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10535 | rightWindow->consumeMotionEvent( |
| 10536 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 10537 | |
| 10538 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 10539 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10540 | .deviceId(stylusDeviceId) |
| 10541 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 10542 | .build()); |
| 10543 | mDispatcher->notifyMotion( |
| 10544 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10545 | .deviceId(touchDeviceId) |
| 10546 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 10547 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame^] | 10548 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10549 | |
| 10550 | spy->assertNoEvents(); |
| 10551 | leftWindow->assertNoEvents(); |
| 10552 | rightWindow->assertNoEvents(); |
| 10553 | } |
| 10554 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10555 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 10556 | public: |
| 10557 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 10558 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 10559 | std::make_shared<FakeApplicationHandle>(); |
| 10560 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10561 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 10562 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10563 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10564 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10565 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10566 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10567 | overlay->setTrustedOverlay(true); |
| 10568 | |
| 10569 | std::shared_ptr<FakeApplicationHandle> application = |
| 10570 | std::make_shared<FakeApplicationHandle>(); |
| 10571 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10572 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 10573 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10574 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10575 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10576 | |
| 10577 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10578 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10579 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10580 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10581 | return {std::move(overlay), std::move(window)}; |
| 10582 | } |
| 10583 | |
| 10584 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10585 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10586 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10587 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10588 | } |
| 10589 | |
| 10590 | void sendStylusEvent(int32_t action) { |
| 10591 | NotifyMotionArgs motionArgs = |
| 10592 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 10593 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10594 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10595 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10596 | } |
| 10597 | }; |
| 10598 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10599 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 10600 | |
| 10601 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 10602 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10603 | ScopedSilentDeath _silentDeath; |
| 10604 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10605 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 10606 | overlay->setTrustedOverlay(false); |
| 10607 | // 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] | 10608 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 10609 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10610 | ".* not a trusted overlay"); |
| 10611 | } |
| 10612 | |
| 10613 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 10614 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10615 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10616 | |
| 10617 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10618 | overlay->consumeMotionDown(); |
| 10619 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 10620 | overlay->consumeMotionUp(); |
| 10621 | |
| 10622 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10623 | window->consumeMotionDown(); |
| 10624 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 10625 | window->consumeMotionUp(); |
| 10626 | |
| 10627 | overlay->assertNoEvents(); |
| 10628 | window->assertNoEvents(); |
| 10629 | } |
| 10630 | |
| 10631 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 10632 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10633 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10634 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10635 | |
| 10636 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10637 | overlay->consumeMotionDown(); |
| 10638 | window->consumeMotionDown(); |
| 10639 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 10640 | overlay->consumeMotionUp(); |
| 10641 | window->consumeMotionUp(); |
| 10642 | |
| 10643 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10644 | window->consumeMotionDown(); |
| 10645 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 10646 | window->consumeMotionUp(); |
| 10647 | |
| 10648 | overlay->assertNoEvents(); |
| 10649 | window->assertNoEvents(); |
| 10650 | } |
| 10651 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 10652 | /** |
| 10653 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 10654 | * The scenario is as follows: |
| 10655 | * - The stylus interceptor overlay is configured as a spy window. |
| 10656 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 10657 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 10658 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 10659 | */ |
| 10660 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 10661 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 10662 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10663 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 10664 | |
| 10665 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10666 | overlay->consumeMotionDown(); |
| 10667 | window->consumeMotionDown(); |
| 10668 | |
| 10669 | // The interceptor pilfers the pointers. |
| 10670 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 10671 | window->consumeMotionCancel(); |
| 10672 | |
| 10673 | // The interceptor configures itself so that it is no longer a spy. |
| 10674 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10675 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 10676 | |
| 10677 | // It continues to receive the rest of the stylus gesture. |
| 10678 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 10679 | overlay->consumeMotionMove(); |
| 10680 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 10681 | overlay->consumeMotionUp(); |
| 10682 | |
| 10683 | window->assertNoEvents(); |
| 10684 | } |
| 10685 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10686 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10687 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10688 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10689 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 10690 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 10691 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10692 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10693 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 10694 | |
| 10695 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10696 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10697 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 10698 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 10699 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 10700 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 10701 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 10702 | } |
| 10703 | |
| 10704 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10705 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10706 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10707 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10708 | mPolicyFlags); |
| 10709 | } |
| 10710 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10711 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10712 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 10713 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10714 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 10715 | name, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10716 | window->setOwnerInfo(mPid, mUid); |
| 10717 | return window; |
| 10718 | } |
| 10719 | }; |
| 10720 | |
| 10721 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 10722 | |
| 10723 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10724 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10725 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10726 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10727 | |
| 10728 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10729 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 10730 | window->consumeMotionDown(); |
| 10731 | |
| 10732 | setFocusedWindow(window); |
| 10733 | window->consumeFocusEvent(true); |
| 10734 | |
| 10735 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10736 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 10737 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 10738 | } |
| 10739 | |
| 10740 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10741 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10742 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10743 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10744 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10745 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10746 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 10747 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 10748 | |
| 10749 | setFocusedWindow(window); |
| 10750 | window->consumeFocusEvent(true); |
| 10751 | |
| 10752 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 10753 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 10754 | window->assertNoEvents(); |
| 10755 | } |
| 10756 | |
| 10757 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10758 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10759 | auto window = owner.createWindow("Owned window"); |
| 10760 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10761 | spy->setSpy(true); |
| 10762 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10763 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10764 | |
| 10765 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10766 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 10767 | spy->consumeMotionDown(); |
| 10768 | window->consumeMotionDown(); |
| 10769 | } |
| 10770 | |
| 10771 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10772 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10773 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10774 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10775 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10776 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10777 | randosSpy->setSpy(true); |
| 10778 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10779 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10780 | |
| 10781 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 10782 | // not receive the event. |
| 10783 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10784 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 10785 | randosSpy->assertNoEvents(); |
| 10786 | window->consumeMotionDown(); |
| 10787 | } |
| 10788 | |
| 10789 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10790 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10791 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10792 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10793 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10794 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10795 | randosSpy->setSpy(true); |
| 10796 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10797 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10798 | |
| 10799 | // A user that has injection permission can inject into any window. |
| 10800 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10801 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10802 | ADISPLAY_ID_DEFAULT)); |
| 10803 | randosSpy->consumeMotionDown(); |
| 10804 | window->consumeMotionDown(); |
| 10805 | |
| 10806 | setFocusedWindow(randosSpy); |
| 10807 | randosSpy->consumeFocusEvent(true); |
| 10808 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10809 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10810 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 10811 | window->assertNoEvents(); |
| 10812 | } |
| 10813 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 10814 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10815 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10816 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10817 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10818 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10819 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10820 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 10821 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10822 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10823 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 10824 | // 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] | 10825 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10826 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 10827 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 10828 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10829 | } |
| 10830 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 10831 | } // namespace android::inputdispatcher |