Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 18 | #include "../BlockingQueue.h" |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 19 | #include "FakeApplicationHandle.h" |
Prabir Pradhan | e3b28dd | 2023-10-06 04:19:29 +0000 | [diff] [blame] | 20 | #include "TestEventMatchers.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 22 | #include <NotifyArgsBuilders.h> |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 24 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 26 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 27 | #include <binder/Binder.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 28 | #include <com_android_input_flags.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 30 | #include <flag_macros.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 31 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 32 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 33 | #include <input/Input.h> |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 34 | #include <input/PrintTools.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 36 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 37 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 38 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 39 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 40 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 41 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 42 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 43 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 44 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 45 | using android::gui::FocusRequest; |
| 46 | using android::gui::TouchOcclusionMode; |
| 47 | using android::gui::WindowInfo; |
| 48 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 49 | using android::os::InputEventInjectionResult; |
| 50 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 51 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 52 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 53 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 54 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 55 | using testing::AllOf; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 56 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 57 | namespace { |
| 58 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 59 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 60 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 61 | |
| 62 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 63 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 64 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 65 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 66 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 67 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 68 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 69 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 70 | // Ensure common actions are interchangeable between keys and motions for convenience. |
| 71 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); |
| 72 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 73 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 74 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 75 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 76 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 77 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 78 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 79 | static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 80 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 81 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 82 | /** |
| 83 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 84 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 85 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 86 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 87 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 88 | * pointer id=0 leaves but the pointer id=1 remains. |
| 89 | */ |
| 90 | static constexpr int32_t POINTER_0_DOWN = |
| 91 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 92 | static constexpr int32_t POINTER_1_DOWN = |
| 93 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 94 | static constexpr int32_t POINTER_2_DOWN = |
| 95 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 96 | static constexpr int32_t POINTER_3_DOWN = |
| 97 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 98 | static constexpr int32_t POINTER_0_UP = |
| 99 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 100 | static constexpr int32_t POINTER_1_UP = |
| 101 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 102 | static constexpr int32_t POINTER_2_UP = |
| 103 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 104 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 105 | // 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] | 106 | static constexpr gui::Pid WINDOW_PID{999}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 107 | static constexpr gui::Uid WINDOW_UID{1001}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 108 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 109 | // 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] | 110 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 111 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 112 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 113 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 114 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 115 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 116 | /** |
| 117 | * If we expect to receive the event, the timeout can be made very long. When the test are running |
| 118 | * correctly, we will actually never wait until the end of the timeout because the wait will end |
| 119 | * when the event comes in. Still, this value shouldn't be infinite. During development, a local |
| 120 | * change may cause the test to fail. This timeout should be short enough to not annoy so that the |
| 121 | * developer can see the failure quickly (on human scale). |
| 122 | */ |
| 123 | static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms; |
| 124 | /** |
| 125 | * When no event is expected, we can have a very short timeout. A large value here would slow down |
| 126 | * the tests. In the unlikely event of system being too slow, the event may still be present but the |
| 127 | * timeout would complete before it is consumed. This would result in test flakiness. If this |
| 128 | * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this |
| 129 | * would get noticed and addressed quickly. |
| 130 | */ |
| 131 | static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms; |
| 132 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 133 | static constexpr int expectedWallpaperFlags = |
| 134 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 135 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 136 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 137 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 138 | /** |
| 139 | * Return a DOWN key event with KEYCODE_A. |
| 140 | */ |
| 141 | static KeyEvent getTestKeyEvent() { |
| 142 | KeyEvent event; |
| 143 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 144 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 145 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 146 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 147 | return event; |
| 148 | } |
| 149 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | // --- FakeInputDispatcherPolicy --- |
| 151 | |
| 152 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 153 | struct AnrResult { |
| 154 | sp<IBinder> token{}; |
| 155 | gui::Pid pid{gui::Pid::INVALID}; |
| 156 | }; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 157 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 158 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 159 | FakeInputDispatcherPolicy() = default; |
| 160 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 161 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 162 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 163 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 164 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 165 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 166 | |
| 167 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 168 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 169 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 170 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 171 | } |
| 172 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 173 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 174 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 175 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 176 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 177 | |
| 178 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 179 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 180 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 181 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 182 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 183 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 184 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 185 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 186 | } |
| 187 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 188 | void assertFilterInputEventWasNotCalled() { |
| 189 | std::scoped_lock lock(mLock); |
| 190 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 191 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 192 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 193 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 194 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 195 | ASSERT_TRUE(mConfigurationChangedTime) |
| 196 | << "Timed out waiting for configuration changed call"; |
| 197 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 198 | mConfigurationChangedTime = std::nullopt; |
| 199 | } |
| 200 | |
| 201 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 202 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 203 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 204 | // 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] | 205 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 206 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 207 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 208 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 209 | mLastNotifySwitch = std::nullopt; |
| 210 | } |
| 211 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 212 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 213 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 214 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 215 | mOnPointerDownToken.clear(); |
| 216 | } |
| 217 | |
| 218 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 219 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 220 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 221 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 224 | // This function must be called soon after the expected ANR timer starts, |
| 225 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 226 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 227 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 228 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 229 | std::unique_lock lock(mLock); |
| 230 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 231 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 232 | ASSERT_NO_FATAL_FAILURE( |
| 233 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 234 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 237 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 238 | const sp<WindowInfoHandle>& window) { |
| 239 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 240 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 241 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 242 | } |
| 243 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 244 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 245 | const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 246 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 247 | std::unique_lock lock(mLock); |
| 248 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 249 | AnrResult result; |
| 250 | ASSERT_NO_FATAL_FAILURE(result = |
| 251 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 252 | ASSERT_EQ(expectedToken, result.token); |
| 253 | ASSERT_EQ(expectedPid, result.pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 254 | } |
| 255 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 256 | /** 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] | 257 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 258 | std::unique_lock lock(mLock); |
| 259 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 260 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 261 | const auto& [token, _] = result; |
| 262 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 265 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 266 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 267 | std::unique_lock lock(mLock); |
| 268 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 269 | AnrResult result; |
| 270 | ASSERT_NO_FATAL_FAILURE( |
| 271 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 272 | ASSERT_EQ(expectedToken, result.token); |
| 273 | ASSERT_EQ(expectedPid, result.pid); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** 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] | 277 | sp<IBinder> getResponsiveWindowToken() { |
| 278 | std::unique_lock lock(mLock); |
| 279 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 280 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 281 | const auto& [token, _] = result; |
| 282 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void assertNotifyAnrWasNotCalled() { |
| 286 | std::scoped_lock lock(mLock); |
| 287 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 288 | ASSERT_TRUE(mAnrWindows.empty()); |
| 289 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 290 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 291 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 294 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 295 | std::unique_lock lock(mLock); |
| 296 | base::ScopedLockAssertion assumeLocked(mLock); |
| 297 | |
| 298 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 299 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 300 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 301 | enabled; |
| 302 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 303 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 304 | << ") to be called."; |
| 305 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 306 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 307 | auto request = *mPointerCaptureRequest; |
| 308 | mPointerCaptureRequest.reset(); |
| 309 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void assertSetPointerCaptureNotCalled() { |
| 313 | std::unique_lock lock(mLock); |
| 314 | base::ScopedLockAssertion assumeLocked(mLock); |
| 315 | |
| 316 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 317 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 318 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 319 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 320 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 321 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 324 | void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, |
| 325 | const sp<IBinder>& targetToken) { |
| 326 | dispatcher.waitForIdle(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 327 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 328 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 329 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 330 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 331 | } |
| 332 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 333 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 334 | std::unique_lock lock(mLock); |
| 335 | base::ScopedLockAssertion assumeLocked(mLock); |
| 336 | std::optional<sp<IBinder>> receivedToken = |
| 337 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 338 | mNotifyInputChannelBroken); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 339 | ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token"; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 340 | ASSERT_EQ(token, *receivedToken); |
| 341 | } |
| 342 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 343 | /** |
| 344 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 345 | */ |
| 346 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 347 | mInterceptKeyTimeout = timeout; |
| 348 | } |
| 349 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 350 | void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; } |
| 351 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 352 | void assertUserActivityPoked() { |
| 353 | std::scoped_lock lock(mLock); |
| 354 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; |
| 355 | } |
| 356 | |
| 357 | void assertUserActivityNotPoked() { |
| 358 | std::scoped_lock lock(mLock); |
| 359 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; |
| 360 | } |
| 361 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 362 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 363 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 364 | } |
| 365 | |
| 366 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 367 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 368 | } |
| 369 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 370 | void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) { |
| 371 | std::scoped_lock lock(mLock); |
| 372 | mUnhandledKeyHandler = handler; |
| 373 | } |
| 374 | |
| 375 | void assertUnhandledKeyReported(int32_t keycode) { |
| 376 | std::unique_lock lock(mLock); |
| 377 | base::ScopedLockAssertion assumeLocked(mLock); |
| 378 | std::optional<int32_t> unhandledKeycode = |
| 379 | getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock, |
| 380 | mNotifyUnhandledKey); |
| 381 | ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported"; |
| 382 | ASSERT_EQ(unhandledKeycode, keycode); |
| 383 | } |
| 384 | |
| 385 | void assertUnhandledKeyNotReported() { |
| 386 | std::unique_lock lock(mLock); |
| 387 | base::ScopedLockAssertion assumeLocked(mLock); |
| 388 | std::optional<int32_t> unhandledKeycode = |
| 389 | getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock, |
| 390 | mNotifyUnhandledKey); |
| 391 | ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported"; |
| 392 | } |
| 393 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 394 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 395 | std::mutex mLock; |
| 396 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 397 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 398 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 399 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 400 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 401 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 402 | |
| 403 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 404 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 405 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 406 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 407 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 408 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 409 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 410 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 411 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 412 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 413 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 414 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 415 | bool mPokedUserActivity GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 416 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 417 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 418 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 419 | std::chrono::nanoseconds mStaleEventTimeout = 1000ms; |
| 420 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 421 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 422 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 423 | std::condition_variable mNotifyUnhandledKey; |
| 424 | std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock); |
| 425 | std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock); |
| 426 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 427 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 428 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 429 | // first entry from the container and erase it. |
| 430 | template <class T> |
| 431 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 432 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 433 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 434 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 435 | // before checking if ANR was called. |
| 436 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 437 | // to provide it some time to act. 100ms seems reasonable. |
| 438 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 439 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 440 | std::optional<T> token = |
| 441 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 442 | if (!token.has_value()) { |
| 443 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 444 | return {}; |
| 445 | } |
| 446 | |
| 447 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 448 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 449 | // the dispatcher started counting before this function was called |
| 450 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 451 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 452 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 453 | << "ms, but waited " |
| 454 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 455 | << "ms instead"; |
| 456 | } |
| 457 | return *token; |
| 458 | } |
| 459 | |
| 460 | template <class T> |
| 461 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 462 | std::queue<T>& storage, |
| 463 | std::unique_lock<std::mutex>& lock, |
| 464 | std::condition_variable& condition) |
| 465 | REQUIRES(mLock) { |
| 466 | condition.wait_for(lock, timeout, |
| 467 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 468 | if (storage.empty()) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 469 | return std::nullopt; |
| 470 | } |
| 471 | T item = storage.front(); |
| 472 | storage.pop(); |
| 473 | return std::make_optional(item); |
| 474 | } |
| 475 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 476 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 477 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 478 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 481 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 482 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 483 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 484 | ASSERT_TRUE(pid.has_value()); |
| 485 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 486 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 487 | } |
| 488 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 489 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 490 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 491 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 492 | ASSERT_TRUE(pid.has_value()); |
| 493 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 494 | mNotifyAnr.notify_all(); |
| 495 | } |
| 496 | |
| 497 | void notifyNoFocusedWindowAnr( |
| 498 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 499 | std::scoped_lock lock(mLock); |
| 500 | mAnrApplications.push(applicationHandle); |
| 501 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 504 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 505 | std::scoped_lock lock(mLock); |
| 506 | mBrokenInputChannels.push(connectionToken); |
| 507 | mNotifyInputChannelBroken.notify_all(); |
| 508 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 509 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 510 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 511 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 512 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 513 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 514 | const std::vector<float>& values) override {} |
| 515 | |
| 516 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 517 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 518 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 519 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 520 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 521 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 522 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 523 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 524 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 525 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 526 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 527 | break; |
| 528 | } |
| 529 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 530 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 531 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 532 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 533 | break; |
| 534 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 535 | default: { |
| 536 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 537 | break; |
| 538 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 539 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 543 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 544 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 545 | // Clear intercept state when we handled the event. |
| 546 | mInterceptKeyTimeout = 0ms; |
| 547 | } |
| 548 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 549 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 550 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 552 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 553 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 554 | // Clear intercept state so we could dispatch the event in next wake. |
| 555 | mInterceptKeyTimeout = 0ms; |
| 556 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 559 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event, |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 560 | uint32_t) override { |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 561 | std::scoped_lock lock(mLock); |
| 562 | mReportedUnhandledKeycodes.emplace(event.getKeyCode()); |
| 563 | mNotifyUnhandledKey.notify_all(); |
| 564 | return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 567 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 568 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 569 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 570 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 571 | * essentially a passthrough for notifySwitch. |
| 572 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 573 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 576 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { |
| 577 | std::scoped_lock lock(mLock); |
| 578 | mPokedUserActivity = true; |
| 579 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 581 | bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override { |
| 582 | return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout; |
| 583 | } |
| 584 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 585 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 586 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 587 | mOnPointerDownToken = newToken; |
| 588 | } |
| 589 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 590 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 591 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 592 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 593 | mPointerCaptureChangedCondition.notify_all(); |
| 594 | } |
| 595 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 596 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 597 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 598 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 599 | mDropTargetWindowToken = token; |
| 600 | } |
| 601 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 602 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 603 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 604 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 605 | } |
| 606 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 607 | void assertFilterInputEventWasCalledInternal( |
| 608 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 609 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 610 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 611 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 612 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 613 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 614 | }; |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 615 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | // --- InputDispatcherTest --- |
| 618 | |
| 619 | class InputDispatcherTest : public testing::Test { |
| 620 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 621 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 622 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 624 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 625 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 626 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
| 627 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 628 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 629 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 630 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 633 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 634 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 635 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 636 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 637 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 638 | |
| 639 | /** |
| 640 | * Used for debugging when writing the test |
| 641 | */ |
| 642 | void dumpDispatcherState() { |
| 643 | std::string dump; |
| 644 | mDispatcher->dump(dump); |
| 645 | std::stringstream ss(dump); |
| 646 | std::string to; |
| 647 | |
| 648 | while (std::getline(ss, to, '\n')) { |
| 649 | ALOGE("%s", to.c_str()); |
| 650 | } |
| 651 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 652 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 653 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 654 | FocusRequest request; |
| 655 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 656 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 657 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 658 | request.displayId = window->getInfo()->displayId; |
| 659 | mDispatcher->setFocusedWindow(request); |
| 660 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 661 | }; |
| 662 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 663 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 664 | KeyEvent event; |
| 665 | |
| 666 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 667 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 668 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 669 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 670 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 671 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 672 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 673 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 674 | << "Should reject key events with undefined action."; |
| 675 | |
| 676 | // 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] | 677 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 678 | 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] | 679 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 680 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 681 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 682 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 683 | << "Should reject key events with ACTION_MULTIPLE."; |
| 684 | } |
| 685 | |
| 686 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 687 | MotionEvent event; |
| 688 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 689 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 690 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 691 | pointerProperties[i].clear(); |
| 692 | pointerProperties[i].id = i; |
| 693 | pointerCoords[i].clear(); |
| 694 | } |
| 695 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 696 | // Some constants commonly used below |
| 697 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 698 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 699 | constexpr int32_t metaState = AMETA_NONE; |
| 700 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 701 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 702 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 703 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 704 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 705 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 706 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 707 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 708 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 709 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 710 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 711 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 712 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 713 | << "Should reject motion events with undefined action."; |
| 714 | |
| 715 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 716 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 717 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 718 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 719 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 720 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 721 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 722 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 723 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 724 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 725 | << "Should reject motion events with pointer down index too large."; |
| 726 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 727 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 728 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 729 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 730 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 731 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 732 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 733 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 734 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 735 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 736 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 737 | << "Should reject motion events with pointer down index too small."; |
| 738 | |
| 739 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 740 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 741 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 742 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 743 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 744 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 745 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 746 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 747 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 748 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 749 | << "Should reject motion events with pointer up index too large."; |
| 750 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 751 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 752 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 753 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 754 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 755 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 756 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 757 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 758 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 759 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 760 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 761 | << "Should reject motion events with pointer up index too small."; |
| 762 | |
| 763 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 764 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 765 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 766 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 767 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 768 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 769 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 770 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 771 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 772 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | << "Should reject motion events with 0 pointers."; |
| 774 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 775 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 776 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 777 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 778 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 779 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 780 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 781 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 782 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 783 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 784 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 785 | |
| 786 | // Rejects motion events with invalid pointer ids. |
| 787 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 788 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 789 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 790 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 791 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 792 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 793 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 794 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 795 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 796 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 797 | << "Should reject motion events with pointer ids less than 0."; |
| 798 | |
| 799 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 800 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 801 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 802 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 803 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 804 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 805 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 806 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 807 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 808 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 809 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 810 | |
| 811 | // Rejects motion events with duplicate pointer ids. |
| 812 | pointerProperties[0].id = 1; |
| 813 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 814 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 815 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 816 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 817 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 818 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 819 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 820 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 821 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 822 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 823 | << "Should reject motion events with duplicate pointer ids."; |
| 824 | } |
| 825 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 826 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 827 | |
| 828 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 829 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 830 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 831 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 832 | |
| 833 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 834 | } |
| 835 | |
| 836 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 837 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 838 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 839 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 840 | |
| 841 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 842 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 843 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 844 | } |
| 845 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 846 | namespace { |
| 847 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 848 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 849 | // Default input dispatching timeout if there is no focused application or paused window |
| 850 | // from which to determine an appropriate dispatching timeout. |
| 851 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 852 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 853 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 854 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 855 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 856 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 857 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 858 | : mConsumer(std::move(clientChannel)), mName(name) {} |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 859 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 860 | InputEvent* consume(std::chrono::milliseconds timeout, bool handled = false) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 861 | InputEvent* event; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 862 | std::optional<uint32_t> consumeSeq = receiveEvent(timeout, &event); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 863 | if (!consumeSeq) { |
| 864 | return nullptr; |
| 865 | } |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 866 | finishEvent(*consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 867 | return event; |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * Receive an event without acknowledging it. |
| 872 | * Return the sequence number that could later be used to send finished signal. |
| 873 | */ |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 874 | std::optional<uint32_t> receiveEvent(std::chrono::milliseconds timeout, |
| 875 | InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 876 | uint32_t consumeSeq; |
| 877 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 878 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 879 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 880 | status_t status = WOULD_BLOCK; |
| 881 | while (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 882 | status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
| 883 | &event); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 884 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 885 | if (elapsed > timeout) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 886 | break; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | if (status == WOULD_BLOCK) { |
| 891 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 892 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | if (status != OK) { |
| 896 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 897 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 898 | } |
| 899 | if (event == nullptr) { |
| 900 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 901 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 902 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 903 | if (outEvent != nullptr) { |
| 904 | *outEvent = event; |
| 905 | } |
| 906 | return consumeSeq; |
| 907 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 908 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 909 | /** |
| 910 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 911 | */ |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 912 | void finishEvent(uint32_t consumeSeq, bool handled = true) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 913 | const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 914 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 917 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 918 | const status_t status = mConsumer.sendTimeline(inputEventId, timeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 919 | ASSERT_EQ(OK, status); |
| 920 | } |
| 921 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 922 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 923 | std::optional<int32_t> expectedDisplayId, |
| 924 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 925 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 926 | |
| 927 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 928 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 929 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 930 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 931 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 932 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 933 | if (expectedDisplayId.has_value()) { |
| 934 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 935 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 936 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 937 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 938 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 939 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 940 | ASSERT_THAT(keyEvent, WithKeyAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 941 | if (expectedFlags.has_value()) { |
| 942 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 943 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 944 | break; |
| 945 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 946 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 947 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 948 | ASSERT_THAT(motionEvent, WithMotionAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 949 | if (expectedFlags.has_value()) { |
| 950 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 951 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 952 | break; |
| 953 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 954 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 955 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 956 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 957 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 958 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 959 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 960 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 961 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 962 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 963 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 964 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 965 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 966 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 967 | } |
| 968 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 969 | MotionEvent* consumeMotion() { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 970 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 971 | |
| 972 | if (event == nullptr) { |
| 973 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 974 | return nullptr; |
| 975 | } |
| 976 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 977 | if (event->getType() != InputEventType::MOTION) { |
| 978 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 979 | return nullptr; |
| 980 | } |
| 981 | return static_cast<MotionEvent*>(event); |
| 982 | } |
| 983 | |
| 984 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 985 | MotionEvent* motionEvent = consumeMotion(); |
| 986 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 987 | ASSERT_THAT(*motionEvent, matcher); |
| 988 | } |
| 989 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 990 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 991 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 992 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 993 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 994 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 995 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 996 | |
| 997 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 998 | << mName.c_str() << ": event displayId should always be NONE."; |
| 999 | |
| 1000 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1001 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1004 | void consumeCaptureEvent(bool hasCapture) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1005 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1006 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1007 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1008 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1009 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1010 | |
| 1011 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1012 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1013 | |
| 1014 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1015 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1016 | } |
| 1017 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1018 | void consumeDragEvent(bool isExiting, float x, float y) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1019 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1020 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1021 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1022 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1023 | |
| 1024 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1025 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1026 | |
| 1027 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1028 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1029 | EXPECT_EQ(x, dragEvent.getX()); |
| 1030 | EXPECT_EQ(y, dragEvent.getY()); |
| 1031 | } |
| 1032 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1033 | void consumeTouchModeEvent(bool inTouchMode) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1034 | const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1035 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1036 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1037 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1038 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1039 | |
| 1040 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1041 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1042 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1043 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1044 | } |
| 1045 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1046 | void assertNoEvents() { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1047 | InputEvent* event = consume(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1048 | if (event == nullptr) { |
| 1049 | return; |
| 1050 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1051 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1052 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1053 | ADD_FAILURE() << "Received key event " |
| 1054 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1055 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1056 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1057 | ADD_FAILURE() << "Received motion event " |
| 1058 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1059 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1060 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1061 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1062 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1063 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1064 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1065 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1066 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1067 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1068 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1069 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1070 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1071 | } |
| 1072 | FAIL() << mName.c_str() |
| 1073 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1076 | sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1077 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1078 | int getChannelFd() { return mConsumer.getChannel()->getFd().get(); } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1079 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1080 | private: |
| 1081 | InputConsumer mConsumer; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1082 | PreallocatedInputEventFactory mEventFactory; |
| 1083 | |
| 1084 | std::string mName; |
| 1085 | }; |
| 1086 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1087 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1088 | public: |
| 1089 | static const int32_t WIDTH = 600; |
| 1090 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1091 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1092 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1093 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1094 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1095 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1096 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1097 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1098 | dispatcher->createInputChannel(name); |
| 1099 | token = (*channel)->getConnectionToken(); |
| 1100 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | inputApplicationHandle->updateInfo(); |
| 1104 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1105 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1106 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1107 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1108 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1109 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1110 | mInfo.alpha = 1.0; |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1111 | mInfo.frame = Rect(0, 0, WIDTH, HEIGHT); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1112 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1113 | mInfo.globalScaleFactor = 1.0; |
| 1114 | mInfo.touchableRegion.clear(); |
| 1115 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1116 | mInfo.ownerPid = WINDOW_PID; |
| 1117 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1118 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1119 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1122 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1123 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1124 | handle->mInfo = mInfo; |
| 1125 | handle->mInfo.displayId = displayId; |
| 1126 | handle->mInfo.id = sId++; |
| 1127 | handle->mInputReceiver = mInputReceiver; |
| 1128 | return handle; |
| 1129 | } |
| 1130 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1131 | void setTouchable(bool touchable) { |
| 1132 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1133 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1134 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1135 | void setFocusable(bool focusable) { |
| 1136 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1137 | } |
| 1138 | |
| 1139 | void setVisible(bool visible) { |
| 1140 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1141 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1142 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1143 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1144 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1147 | void setPaused(bool paused) { |
| 1148 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1149 | } |
| 1150 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1151 | void setPreventSplitting(bool preventSplitting) { |
| 1152 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | void setSlippery(bool slippery) { |
| 1156 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1157 | } |
| 1158 | |
| 1159 | void setWatchOutsideTouch(bool watchOutside) { |
| 1160 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1161 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1162 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1163 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1164 | |
| 1165 | void setInterceptsStylus(bool interceptsStylus) { |
| 1166 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1167 | } |
| 1168 | |
| 1169 | void setDropInput(bool dropInput) { |
| 1170 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1171 | } |
| 1172 | |
| 1173 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1174 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1175 | } |
| 1176 | |
| 1177 | void setNoInputChannel(bool noInputChannel) { |
| 1178 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1179 | } |
| 1180 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1181 | void setDisableUserActivity(bool disableUserActivity) { |
| 1182 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1183 | } |
| 1184 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1185 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1186 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1187 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1188 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1189 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1190 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1191 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1192 | mInfo.frame = frame; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1193 | mInfo.touchableRegion.clear(); |
| 1194 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1195 | |
| 1196 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1197 | ui::Transform translate; |
| 1198 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1199 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1202 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1203 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1204 | void setIsWallpaper(bool isWallpaper) { |
| 1205 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1206 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1207 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1208 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1209 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1210 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1211 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1212 | void setTrustedOverlay(bool trustedOverlay) { |
| 1213 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1214 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1215 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1216 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1217 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1218 | } |
| 1219 | |
| 1220 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1221 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1222 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1223 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 1224 | KeyEvent* consumeKey(bool handled = true) { |
| 1225 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1226 | if (event == nullptr) { |
| 1227 | ADD_FAILURE() << "Consume failed : no event"; |
| 1228 | return nullptr; |
| 1229 | } |
| 1230 | if (event->getType() != InputEventType::KEY) { |
| 1231 | ADD_FAILURE() << "Instead of key event, got " << *event; |
| 1232 | return nullptr; |
| 1233 | } |
| 1234 | return static_cast<KeyEvent*>(event); |
| 1235 | } |
| 1236 | |
| 1237 | void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
| 1238 | KeyEvent* keyEvent = consumeKey(); |
| 1239 | ASSERT_NE(nullptr, keyEvent) << "Did not get a key event, but expected " << matcher; |
| 1240 | ASSERT_THAT(*keyEvent, matcher); |
| 1241 | } |
| 1242 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1243 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1244 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1247 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1248 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1251 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1252 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1253 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1254 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1258 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1259 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1260 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1264 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1265 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1266 | } |
| 1267 | |
| 1268 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1269 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1270 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1271 | expectedFlags); |
| 1272 | } |
| 1273 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1274 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1275 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1276 | int32_t expectedFlags = 0) { |
| 1277 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1278 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1279 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1283 | int32_t expectedFlags = 0) { |
| 1284 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1285 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1286 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1290 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1291 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1292 | expectedFlags); |
| 1293 | } |
| 1294 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1295 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1296 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1297 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1298 | expectedFlags); |
| 1299 | } |
| 1300 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1301 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1302 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1303 | MotionEvent* motionEvent = consumeMotion(); |
| 1304 | ASSERT_NE(nullptr, motionEvent); |
| 1305 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent->getActionMasked()); |
| 1306 | EXPECT_EQ(0.f, motionEvent->getRawPointerCoords(0)->getX()); |
| 1307 | EXPECT_EQ(0.f, motionEvent->getRawPointerCoords(0)->getY()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1310 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1311 | ASSERT_NE(mInputReceiver, nullptr) |
| 1312 | << "Cannot consume events from a window with no receiver"; |
| 1313 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1314 | } |
| 1315 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1316 | void consumeCaptureEvent(bool hasCapture) { |
| 1317 | ASSERT_NE(mInputReceiver, nullptr) |
| 1318 | << "Cannot consume events from a window with no receiver"; |
| 1319 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1320 | } |
| 1321 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1322 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1323 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1324 | 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] | 1325 | ASSERT_THAT(*motionEvent, matcher); |
| 1326 | } |
| 1327 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1328 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1329 | std::optional<int32_t> expectedDisplayId, |
| 1330 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1331 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1332 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1333 | expectedFlags); |
| 1334 | } |
| 1335 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1336 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1337 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1338 | } |
| 1339 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1340 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1341 | ASSERT_NE(mInputReceiver, nullptr) |
| 1342 | << "Cannot consume events from a window with no receiver"; |
| 1343 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1344 | } |
| 1345 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1346 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1347 | if (mInputReceiver == nullptr) { |
| 1348 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1349 | return std::nullopt; |
| 1350 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1351 | return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED, outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | void finishEvent(uint32_t sequenceNum) { |
| 1355 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1356 | mInputReceiver->finishEvent(sequenceNum); |
| 1357 | } |
| 1358 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1359 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1360 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1361 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1362 | } |
| 1363 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 1364 | InputEvent* consume(std::chrono::milliseconds timeout, bool handled = true) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1365 | if (mInputReceiver == nullptr) { |
| 1366 | return nullptr; |
| 1367 | } |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 1368 | return mInputReceiver->consume(timeout, handled); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1371 | MotionEvent* consumeMotion() { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1372 | InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1373 | if (event == nullptr) { |
| 1374 | ADD_FAILURE() << "Consume failed : no event"; |
| 1375 | return nullptr; |
| 1376 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1377 | if (event->getType() != InputEventType::MOTION) { |
| 1378 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1379 | return nullptr; |
| 1380 | } |
| 1381 | return static_cast<MotionEvent*>(event); |
| 1382 | } |
| 1383 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1384 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1385 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1386 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1387 | return; // Can't receive events if the window does not have input channel |
| 1388 | } |
| 1389 | ASSERT_NE(nullptr, mInputReceiver) |
| 1390 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1391 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1392 | } |
| 1393 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1394 | sp<IBinder> getToken() { return mInfo.token; } |
| 1395 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1396 | const std::string& getName() { return mName; } |
| 1397 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1398 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1399 | mInfo.ownerPid = ownerPid; |
| 1400 | mInfo.ownerUid = ownerUid; |
| 1401 | } |
| 1402 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1403 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1404 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1405 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1406 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1407 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1408 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1409 | private: |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1410 | FakeWindowHandle(std::string name) : mName(name){}; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1411 | const std::string mName; |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1412 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1413 | 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] | 1414 | friend class sp<FakeWindowHandle>; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1415 | }; |
| 1416 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1417 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1418 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1419 | class FakeMonitorReceiver { |
| 1420 | public: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1421 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId) |
| 1422 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1423 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1424 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1425 | |
| 1426 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1427 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1428 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1432 | return mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1435 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1436 | |
| 1437 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1438 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1439 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1443 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 1444 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1448 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 1449 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1453 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1454 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 1455 | WithDisplayId(expectedDisplayId), |
| 1456 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 1457 | } |
| 1458 | |
| 1459 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 1460 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1461 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1462 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
| 1463 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1467 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1470 | MotionEvent* consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1471 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1472 | void assertNoEvents() { mInputReceiver.assertNoEvents(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1473 | |
| 1474 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1475 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1476 | }; |
| 1477 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1478 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1479 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1480 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1481 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1482 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1483 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1484 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1485 | KeyEvent event; |
| 1486 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1487 | |
| 1488 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1489 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1490 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1491 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1492 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1493 | if (!allowKeyRepeat) { |
| 1494 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1495 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1496 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1497 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1498 | } |
| 1499 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1500 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 1501 | InputEventInjectionResult result = |
| 1502 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
| 1503 | InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
| 1504 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 1505 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1510 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1511 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1514 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1515 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1516 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1517 | static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher, |
| 1518 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1519 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1520 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1521 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1522 | } |
| 1523 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1524 | static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1525 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1526 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1529 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1530 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1531 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1532 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1533 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1534 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1535 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1538 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1539 | InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1540 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1541 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1542 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1543 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1544 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1545 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1546 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1547 | MotionEventBuilder motionBuilder = |
| 1548 | MotionEventBuilder(action, source) |
| 1549 | .displayId(displayId) |
| 1550 | .eventTime(eventTime) |
| 1551 | .rawXCursorPosition(cursorPosition.x) |
| 1552 | .rawYCursorPosition(cursorPosition.y) |
| 1553 | .pointer( |
| 1554 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1555 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1556 | motionBuilder.downTime(eventTime); |
| 1557 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1558 | |
| 1559 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1560 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1561 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1562 | } |
| 1563 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1564 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
| 1565 | int32_t displayId, |
| 1566 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1567 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1570 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
| 1571 | int32_t displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1572 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1573 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1576 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1577 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1578 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1579 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1580 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*flags=*/0, AKEYCODE_A, KEY_A, |
| 1581 | AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1582 | |
| 1583 | return args; |
| 1584 | } |
| 1585 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1586 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1587 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1588 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1589 | // Define a valid key event. |
| 1590 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1591 | 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] | 1592 | currentTime); |
| 1593 | |
| 1594 | return args; |
| 1595 | } |
| 1596 | |
| 1597 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1598 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1599 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1600 | // Define a valid key event. |
| 1601 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1602 | displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, KEY_ASSISTANT, |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1603 | AMETA_NONE, currentTime); |
| 1604 | |
| 1605 | return args; |
| 1606 | } |
| 1607 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1608 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1609 | int32_t displayId, |
| 1610 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1611 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1612 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1613 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1614 | } |
| 1615 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1616 | PointerProperties pointerProperties[pointerCount]; |
| 1617 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1618 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1619 | for (size_t i = 0; i < pointerCount; i++) { |
| 1620 | pointerProperties[i].clear(); |
| 1621 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1622 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1623 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1624 | pointerCoords[i].clear(); |
| 1625 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1626 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1627 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1628 | |
| 1629 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1630 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1631 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1632 | POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, /*flags=*/0, |
| 1633 | AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1634 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1635 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1636 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1637 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1638 | |
| 1639 | return args; |
| 1640 | } |
| 1641 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1642 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1643 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1644 | } |
| 1645 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1646 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1647 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1648 | } |
| 1649 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1650 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1651 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1652 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1655 | } // namespace |
| 1656 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1657 | /** |
| 1658 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1659 | * broken channel. |
| 1660 | */ |
| 1661 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1662 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1663 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1664 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1665 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1666 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1667 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1668 | |
| 1669 | // Window closes its channel, but the window remains. |
| 1670 | window->destroyReceiver(); |
| 1671 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1672 | } |
| 1673 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1674 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1675 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1676 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1677 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1678 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1679 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1680 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1681 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1682 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1683 | |
| 1684 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1685 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1686 | } |
| 1687 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1688 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1689 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1690 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1691 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1692 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1693 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1694 | // Inject a MotionEvent to an unknown display. |
| 1695 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1696 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1697 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1698 | |
| 1699 | // Window should receive motion event. |
| 1700 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1701 | } |
| 1702 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1703 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1704 | * Calling onWindowInfosChanged once should not cause any issues. |
| 1705 | * 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] | 1706 | * called twice. |
| 1707 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1708 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1709 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1710 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1711 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1712 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1713 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1714 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1715 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1716 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1717 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1718 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1719 | |
| 1720 | // Window should receive motion event. |
| 1721 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1722 | } |
| 1723 | |
| 1724 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1725 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1726 | */ |
| 1727 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1728 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1729 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1730 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1731 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1732 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1733 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1734 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1735 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1736 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1737 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1738 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1739 | |
| 1740 | // Window should receive motion event. |
| 1741 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1742 | } |
| 1743 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1744 | // The foreground window should receive the first touch down event. |
| 1745 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1746 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1747 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1748 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1749 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1750 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1751 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1752 | mDispatcher->onWindowInfosChanged( |
| 1753 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1754 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1755 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1756 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1757 | |
| 1758 | // 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] | 1759 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1760 | windowSecond->assertNoEvents(); |
| 1761 | } |
| 1762 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1763 | /** |
| 1764 | * Two windows: A top window, and a wallpaper behind the window. |
| 1765 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1766 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1767 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1768 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1769 | */ |
| 1770 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1771 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1772 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1773 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1774 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1775 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1776 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1777 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1778 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1779 | mDispatcher->onWindowInfosChanged( |
| 1780 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1781 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1782 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1783 | {100, 200})) |
| 1784 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1785 | |
| 1786 | // Both foreground window and its wallpaper should receive the touch down |
| 1787 | foregroundWindow->consumeMotionDown(); |
| 1788 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1789 | |
| 1790 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1791 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1792 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1793 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1794 | |
| 1795 | foregroundWindow->consumeMotionMove(); |
| 1796 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1797 | |
| 1798 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1799 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1800 | foregroundWindow->consumeMotionCancel(); |
| 1801 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1802 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1803 | } |
| 1804 | |
| 1805 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1806 | * Two fingers down on the window, and lift off the first finger. |
| 1807 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1808 | * contains a single pointer. |
| 1809 | */ |
| 1810 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1811 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1812 | sp<FakeWindowHandle> window = |
| 1813 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1814 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1815 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1816 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1817 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1818 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1819 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1820 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1821 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1822 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1823 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1824 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1825 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1826 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1827 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1828 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1829 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1830 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1831 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1832 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1833 | |
| 1834 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1835 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1836 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1837 | window->consumeMotionEvent( |
| 1838 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1839 | } |
| 1840 | |
| 1841 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1842 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1843 | * with the following differences: |
| 1844 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1845 | * clean up the connection. |
| 1846 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1847 | * Ensure that there's no crash in the dispatcher. |
| 1848 | */ |
| 1849 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1850 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1851 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1852 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1853 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1854 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1855 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1856 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1857 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1858 | mDispatcher->onWindowInfosChanged( |
| 1859 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1860 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1861 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1862 | {100, 200})) |
| 1863 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1864 | |
| 1865 | // Both foreground window and its wallpaper should receive the touch down |
| 1866 | foregroundWindow->consumeMotionDown(); |
| 1867 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1868 | |
| 1869 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1870 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1871 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1872 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1873 | |
| 1874 | foregroundWindow->consumeMotionMove(); |
| 1875 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1876 | |
| 1877 | // Wallpaper closes its channel, but the window remains. |
| 1878 | wallpaperWindow->destroyReceiver(); |
| 1879 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1880 | |
| 1881 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1882 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1883 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1884 | foregroundWindow->consumeMotionCancel(); |
| 1885 | } |
| 1886 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1887 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1888 | public ::testing::WithParamInterface<bool> {}; |
| 1889 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1890 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1891 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1892 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1893 | * The top window gets a multitouch gesture. |
| 1894 | * Ensure that wallpaper gets the same gesture. |
| 1895 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1896 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1897 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1898 | sp<FakeWindowHandle> foregroundWindow = |
| 1899 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1900 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1901 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1902 | |
| 1903 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1904 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1905 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1906 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1907 | mDispatcher->onWindowInfosChanged( |
| 1908 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1909 | |
| 1910 | // Touch down on top window |
| 1911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1912 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1913 | {100, 100})) |
| 1914 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1915 | |
| 1916 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1917 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1918 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1919 | |
| 1920 | // Second finger down on the top window |
| 1921 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1922 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 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)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +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, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1929 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1930 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1931 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1932 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1933 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1934 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1935 | |
| 1936 | const MotionEvent secondFingerUpEvent = |
| 1937 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1938 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1939 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1940 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1941 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1942 | .build(); |
| 1943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1944 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1945 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1946 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1947 | foregroundWindow->consumeMotionPointerUp(0); |
| 1948 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1949 | |
| 1950 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1951 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1952 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1953 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1954 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1955 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1956 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1957 | .x(100) |
| 1958 | .y(100)) |
| 1959 | .build(), |
| 1960 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1961 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1962 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1963 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | /** |
| 1967 | * Two windows: a window on the left and window on the right. |
| 1968 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1969 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1970 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1971 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1972 | * ACTION_POINTER_DOWN(1). |
| 1973 | */ |
| 1974 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1975 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1976 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1977 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1978 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1979 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1980 | |
| 1981 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1982 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1983 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1984 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1985 | |
| 1986 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1987 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1988 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1989 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1990 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1991 | mDispatcher->onWindowInfosChanged( |
| 1992 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1993 | {}, |
| 1994 | 0, |
| 1995 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1996 | |
| 1997 | // Touch down on left window |
| 1998 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1999 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2000 | {100, 100})) |
| 2001 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2002 | |
| 2003 | // Both foreground window and its wallpaper should receive the touch down |
| 2004 | leftWindow->consumeMotionDown(); |
| 2005 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2006 | |
| 2007 | // Second finger down on the right window |
| 2008 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2009 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2010 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2011 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2012 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2013 | .build(); |
| 2014 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2015 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2016 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2017 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2018 | |
| 2019 | leftWindow->consumeMotionMove(); |
| 2020 | // Since the touch is split, right window gets ACTION_DOWN |
| 2021 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2022 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2023 | expectedWallpaperFlags); |
| 2024 | |
| 2025 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2026 | mDispatcher->onWindowInfosChanged( |
| 2027 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2028 | leftWindow->consumeMotionCancel(); |
| 2029 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2030 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2031 | |
| 2032 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2033 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2034 | const MotionEvent secondFingerMoveEvent = |
| 2035 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2036 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2037 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2038 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2039 | .build(); |
| 2040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2041 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2042 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2043 | rightWindow->consumeMotionMove(); |
| 2044 | |
| 2045 | leftWindow->assertNoEvents(); |
| 2046 | rightWindow->assertNoEvents(); |
| 2047 | wallpaperWindow->assertNoEvents(); |
| 2048 | } |
| 2049 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2050 | /** |
| 2051 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2052 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2053 | * The right window should receive ACTION_DOWN. |
| 2054 | */ |
| 2055 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2056 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2057 | sp<FakeWindowHandle> leftWindow = |
| 2058 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2059 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2060 | leftWindow->setDupTouchToWallpaper(true); |
| 2061 | leftWindow->setSlippery(true); |
| 2062 | |
| 2063 | sp<FakeWindowHandle> rightWindow = |
| 2064 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2065 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2066 | |
| 2067 | sp<FakeWindowHandle> wallpaperWindow = |
| 2068 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2069 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2070 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2071 | mDispatcher->onWindowInfosChanged( |
| 2072 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2073 | {}, |
| 2074 | 0, |
| 2075 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2076 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2077 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2078 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2079 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2080 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2081 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2082 | |
| 2083 | // Both foreground window and its wallpaper should receive the touch down |
| 2084 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2085 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2086 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2087 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2088 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2089 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2090 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2091 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2092 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2093 | leftWindow->consumeMotionCancel(); |
| 2094 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2095 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2098 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2099 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2100 | * interactive, it might stop sending this flag. |
| 2101 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2102 | * to have a consistent input stream. |
| 2103 | * |
| 2104 | * Test procedure: |
| 2105 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2106 | * DOWN (new gesture). |
| 2107 | * |
| 2108 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2109 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2110 | * |
| 2111 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2112 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2113 | */ |
| 2114 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2115 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2116 | sp<FakeWindowHandle> spyWindow = |
| 2117 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2118 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2119 | spyWindow->setTrustedOverlay(true); |
| 2120 | spyWindow->setSpy(true); |
| 2121 | |
| 2122 | sp<FakeWindowHandle> window = |
| 2123 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2124 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2125 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2126 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2127 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2128 | |
| 2129 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2130 | mDispatcher->notifyMotion( |
| 2131 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2132 | .deviceId(touchDeviceId) |
| 2133 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2134 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2135 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2136 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2137 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2138 | .deviceId(touchDeviceId) |
| 2139 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2140 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2141 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2142 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2143 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2144 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2145 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2146 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2147 | |
| 2148 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2149 | mDispatcher->notifyMotion( |
| 2150 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2151 | .deviceId(touchDeviceId) |
| 2152 | .policyFlags(0) |
| 2153 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2154 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2155 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2156 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2157 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2158 | |
| 2159 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2160 | // 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] | 2161 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2162 | |
| 2163 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2164 | mDispatcher->notifyMotion( |
| 2165 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2166 | .deviceId(touchDeviceId) |
| 2167 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2168 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2169 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2170 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2171 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2172 | |
| 2173 | // No more events |
| 2174 | spyWindow->assertNoEvents(); |
| 2175 | window->assertNoEvents(); |
| 2176 | } |
| 2177 | |
| 2178 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 2179 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 2180 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2181 | * interactive, it might stop sending this flag. |
| 2182 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 2183 | * the consistency of the hover event in this case. |
| 2184 | * |
| 2185 | * Test procedure: |
| 2186 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 2187 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 2188 | * |
| 2189 | * We expect to receive two full streams of hover events. |
| 2190 | */ |
| 2191 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 2192 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2193 | |
| 2194 | sp<FakeWindowHandle> window = |
| 2195 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2196 | window->setFrame(Rect(0, 0, 300, 300)); |
| 2197 | |
| 2198 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2199 | |
| 2200 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2201 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2202 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2203 | .build()); |
| 2204 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2205 | |
| 2206 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2207 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2208 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2209 | .build()); |
| 2210 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2211 | |
| 2212 | // Send hover exit without the default policy flags. |
| 2213 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2214 | .policyFlags(0) |
| 2215 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2216 | .build()); |
| 2217 | |
| 2218 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2219 | |
| 2220 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 2221 | // right event. |
| 2222 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2223 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2224 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 2225 | .build()); |
| 2226 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2227 | |
| 2228 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2229 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2230 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2231 | .build()); |
| 2232 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2233 | |
| 2234 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2235 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2236 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2237 | .build()); |
| 2238 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2239 | } |
| 2240 | |
| 2241 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2242 | * Two windows: a window on the left and a window on the right. |
| 2243 | * Mouse is hovered from the right window into the left window. |
| 2244 | * Next, we tap on the left window, where the cursor was last seen. |
| 2245 | * The second tap is done onto the right window. |
| 2246 | * The mouse and tap are from two different devices. |
| 2247 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2248 | * explicitly helps during debugging. |
| 2249 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2250 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2251 | */ |
| 2252 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2253 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2254 | sp<FakeWindowHandle> leftWindow = |
| 2255 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2256 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2257 | |
| 2258 | sp<FakeWindowHandle> rightWindow = |
| 2259 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2260 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2261 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2262 | mDispatcher->onWindowInfosChanged( |
| 2263 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2264 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2265 | // stale. |
| 2266 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2267 | const int32_t mouseDeviceId = 6; |
| 2268 | const int32_t touchDeviceId = 4; |
| 2269 | // Move the cursor from right |
| 2270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2271 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2272 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2273 | AINPUT_SOURCE_MOUSE) |
| 2274 | .deviceId(mouseDeviceId) |
| 2275 | .downTime(baseTime + 10) |
| 2276 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2277 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2278 | .build())); |
| 2279 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2280 | |
| 2281 | // .. to the left window |
| 2282 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2283 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2284 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2285 | AINPUT_SOURCE_MOUSE) |
| 2286 | .deviceId(mouseDeviceId) |
| 2287 | .downTime(baseTime + 10) |
| 2288 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2289 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2290 | .build())); |
| 2291 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2292 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2293 | // Now tap the left window |
| 2294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2295 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2296 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2297 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2298 | .deviceId(touchDeviceId) |
| 2299 | .downTime(baseTime + 40) |
| 2300 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2301 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2302 | .build())); |
| 2303 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2304 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2305 | |
| 2306 | // release tap |
| 2307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2308 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2309 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2310 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2311 | .deviceId(touchDeviceId) |
| 2312 | .downTime(baseTime + 40) |
| 2313 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2314 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2315 | .build())); |
| 2316 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2317 | |
| 2318 | // Tap the window on the right |
| 2319 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2320 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2321 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2322 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2323 | .deviceId(touchDeviceId) |
| 2324 | .downTime(baseTime + 60) |
| 2325 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2326 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2327 | .build())); |
| 2328 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2329 | |
| 2330 | // release tap |
| 2331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2332 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2333 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2334 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2335 | .deviceId(touchDeviceId) |
| 2336 | .downTime(baseTime + 60) |
| 2337 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2338 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2339 | .build())); |
| 2340 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2341 | |
| 2342 | // No more events |
| 2343 | leftWindow->assertNoEvents(); |
| 2344 | rightWindow->assertNoEvents(); |
| 2345 | } |
| 2346 | |
| 2347 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2348 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2349 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2350 | * While the top window is present, the hovering is stopped. |
| 2351 | * Later, hovering gets resumed again. |
| 2352 | * Ensure that new hover gesture is handled correctly. |
| 2353 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2354 | * to the window that's currently being hovered over. |
| 2355 | */ |
| 2356 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2357 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2358 | sp<FakeWindowHandle> window = |
| 2359 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2360 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2361 | |
| 2362 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2363 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2364 | |
| 2365 | // Start hovering in the window |
| 2366 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2367 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2368 | .build()); |
| 2369 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2370 | |
| 2371 | // Now, an obscuring window appears! |
| 2372 | sp<FakeWindowHandle> obscuringWindow = |
| 2373 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2374 | ADISPLAY_ID_DEFAULT, |
| 2375 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2376 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2377 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2378 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2379 | obscuringWindow->setNoInputChannel(true); |
| 2380 | obscuringWindow->setFocusable(false); |
| 2381 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2382 | mDispatcher->onWindowInfosChanged( |
| 2383 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2384 | |
| 2385 | // While this new obscuring window is present, the hovering is stopped |
| 2386 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2387 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2388 | .build()); |
| 2389 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2390 | |
| 2391 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2392 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2393 | |
| 2394 | // And a new hover gesture starts. |
| 2395 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2396 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2397 | .build()); |
| 2398 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2399 | } |
| 2400 | |
| 2401 | /** |
| 2402 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2403 | * the obscuring window. |
| 2404 | */ |
| 2405 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2406 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2407 | sp<FakeWindowHandle> window = |
| 2408 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2409 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2410 | |
| 2411 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2412 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2413 | |
| 2414 | // Start hovering in the window |
| 2415 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2416 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2417 | .build()); |
| 2418 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2419 | |
| 2420 | // Now, an obscuring window appears! |
| 2421 | sp<FakeWindowHandle> obscuringWindow = |
| 2422 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2423 | ADISPLAY_ID_DEFAULT, |
| 2424 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2425 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2426 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2427 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2428 | obscuringWindow->setNoInputChannel(true); |
| 2429 | obscuringWindow->setFocusable(false); |
| 2430 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2431 | mDispatcher->onWindowInfosChanged( |
| 2432 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2433 | |
| 2434 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2435 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2436 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2437 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2438 | .build()); |
| 2439 | obscuringWindow->assertNoEvents(); |
| 2440 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2441 | |
| 2442 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2443 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2444 | |
| 2445 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2446 | // so it should generate a HOVER_ENTER |
| 2447 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2448 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2449 | .build()); |
| 2450 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2451 | |
| 2452 | // Now the MOVE should be getting dispatched normally |
| 2453 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2454 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2455 | .build()); |
| 2456 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2457 | } |
| 2458 | |
| 2459 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2460 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 2461 | * events are delivered to the window. |
| 2462 | */ |
| 2463 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 2464 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2465 | sp<FakeWindowHandle> window = |
| 2466 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2467 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2468 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2469 | |
| 2470 | // Start hovering in the window |
| 2471 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2472 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2473 | .build()); |
| 2474 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2475 | |
| 2476 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2477 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2478 | .build()); |
| 2479 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2480 | |
| 2481 | // Scroll with the mouse |
| 2482 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 2483 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2484 | .build()); |
| 2485 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 2486 | } |
| 2487 | |
| 2488 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2489 | |
| 2490 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2491 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2492 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2493 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2494 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2495 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2496 | sp<FakeWindowHandle> window = |
| 2497 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2498 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2499 | |
| 2500 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2501 | |
| 2502 | constexpr int32_t touchDeviceId = 4; |
| 2503 | constexpr int32_t stylusDeviceId = 2; |
| 2504 | |
| 2505 | // Stylus down |
| 2506 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2507 | .deviceId(stylusDeviceId) |
| 2508 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2509 | .build()); |
| 2510 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2511 | |
| 2512 | // Touch down |
| 2513 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2514 | .deviceId(touchDeviceId) |
| 2515 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2516 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2517 | |
| 2518 | // Touch move |
| 2519 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2520 | .deviceId(touchDeviceId) |
| 2521 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2522 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2523 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2524 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2525 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2526 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2527 | .deviceId(stylusDeviceId) |
| 2528 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2529 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2530 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2531 | WithCoords(101, 111))); |
| 2532 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2533 | window->assertNoEvents(); |
| 2534 | } |
| 2535 | |
| 2536 | /** |
| 2537 | * 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] | 2538 | * 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] | 2539 | * Similar test as above, but with added SPY window. |
| 2540 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2541 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2542 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2543 | sp<FakeWindowHandle> window = |
| 2544 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2545 | sp<FakeWindowHandle> spyWindow = |
| 2546 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2547 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2548 | spyWindow->setTrustedOverlay(true); |
| 2549 | spyWindow->setSpy(true); |
| 2550 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2551 | |
| 2552 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2553 | |
| 2554 | constexpr int32_t touchDeviceId = 4; |
| 2555 | constexpr int32_t stylusDeviceId = 2; |
| 2556 | |
| 2557 | // Stylus down |
| 2558 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2559 | .deviceId(stylusDeviceId) |
| 2560 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2561 | .build()); |
| 2562 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2563 | spyWindow->consumeMotionEvent( |
| 2564 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2565 | |
| 2566 | // Touch down |
| 2567 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2568 | .deviceId(touchDeviceId) |
| 2569 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2570 | .build()); |
| 2571 | |
| 2572 | // Touch move |
| 2573 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2574 | .deviceId(touchDeviceId) |
| 2575 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2576 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2577 | |
| 2578 | // Touch is ignored because stylus is already down |
| 2579 | |
| 2580 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2581 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2582 | .deviceId(stylusDeviceId) |
| 2583 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2584 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2585 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2586 | WithCoords(101, 111))); |
| 2587 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2588 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2589 | |
| 2590 | window->assertNoEvents(); |
| 2591 | spyWindow->assertNoEvents(); |
| 2592 | } |
| 2593 | |
| 2594 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2595 | * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that |
| 2596 | * touch is not dropped, because stylus hover should be ignored. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2597 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2598 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDoesNotBlockTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2599 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2600 | sp<FakeWindowHandle> window = |
| 2601 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2602 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2603 | |
| 2604 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2605 | |
| 2606 | constexpr int32_t touchDeviceId = 4; |
| 2607 | constexpr int32_t stylusDeviceId = 2; |
| 2608 | |
| 2609 | // Stylus down on the window |
| 2610 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2611 | .deviceId(stylusDeviceId) |
| 2612 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2613 | .build()); |
| 2614 | window->consumeMotionEvent( |
| 2615 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2616 | |
| 2617 | // Touch down on window |
| 2618 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2619 | .deviceId(touchDeviceId) |
| 2620 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2621 | .build()); |
| 2622 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2623 | .deviceId(touchDeviceId) |
| 2624 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2625 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2626 | |
| 2627 | // Stylus hover is canceled because touch is down |
| 2628 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), |
| 2629 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2630 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId), |
| 2631 | WithCoords(140, 145))); |
| 2632 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2633 | WithCoords(141, 146))); |
| 2634 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2635 | // Subsequent stylus movements are ignored |
| 2636 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2637 | .deviceId(stylusDeviceId) |
| 2638 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2639 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2640 | |
| 2641 | // but subsequent touches continue to be delivered |
| 2642 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2643 | .deviceId(touchDeviceId) |
| 2644 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2645 | .build()); |
| 2646 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2647 | WithCoords(142, 147))); |
| 2648 | } |
| 2649 | |
| 2650 | /** |
| 2651 | * One window. Touch down on the window. Then, stylus hover on the window from another device. |
| 2652 | * Ensure that touch is not canceled, because stylus hover should be dropped. |
| 2653 | */ |
| 2654 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusHover) { |
| 2655 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2656 | sp<FakeWindowHandle> window = |
| 2657 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2658 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2659 | |
| 2660 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2661 | |
| 2662 | constexpr int32_t touchDeviceId = 4; |
| 2663 | constexpr int32_t stylusDeviceId = 2; |
| 2664 | |
| 2665 | // Touch down on window |
| 2666 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2667 | .deviceId(touchDeviceId) |
| 2668 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2669 | .build()); |
| 2670 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2671 | .deviceId(touchDeviceId) |
| 2672 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2673 | .build()); |
| 2674 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2675 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2676 | |
| 2677 | // Stylus hover on the window |
| 2678 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2679 | .deviceId(stylusDeviceId) |
| 2680 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2681 | .build()); |
| 2682 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2683 | .deviceId(stylusDeviceId) |
| 2684 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2685 | .build()); |
| 2686 | // Stylus hover movement is dropped |
| 2687 | |
| 2688 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2689 | .deviceId(touchDeviceId) |
| 2690 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2691 | .build()); |
| 2692 | // Subsequent touch movements are delivered correctly |
| 2693 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2694 | WithCoords(142, 147))); |
| 2695 | } |
| 2696 | |
| 2697 | /** |
| 2698 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2699 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2700 | * become active. |
| 2701 | */ |
| 2702 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
| 2703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2704 | sp<FakeWindowHandle> window = |
| 2705 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2706 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2707 | |
| 2708 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2709 | |
| 2710 | constexpr int32_t stylusDeviceId1 = 3; |
| 2711 | constexpr int32_t stylusDeviceId2 = 5; |
| 2712 | |
| 2713 | // Touch down on window |
| 2714 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2715 | .deviceId(stylusDeviceId1) |
| 2716 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2717 | .build()); |
| 2718 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2719 | .deviceId(stylusDeviceId1) |
| 2720 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2721 | .build()); |
| 2722 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2723 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2724 | |
| 2725 | // Second stylus down |
| 2726 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2727 | .deviceId(stylusDeviceId2) |
| 2728 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2729 | .build()); |
| 2730 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2731 | .deviceId(stylusDeviceId2) |
| 2732 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2733 | .build()); |
| 2734 | |
| 2735 | // First stylus is canceled, second one takes over. |
| 2736 | window->consumeMotionEvent( |
| 2737 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2738 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2739 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2740 | |
| 2741 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2742 | .deviceId(stylusDeviceId1) |
| 2743 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2744 | .build()); |
| 2745 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2746 | window->assertNoEvents(); |
| 2747 | } |
| 2748 | |
| 2749 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2750 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2751 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2752 | */ |
| 2753 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
| 2754 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2755 | sp<FakeWindowHandle> window = |
| 2756 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2757 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2758 | |
| 2759 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2760 | |
| 2761 | constexpr int32_t touchDeviceId = 4; |
| 2762 | constexpr int32_t stylusDeviceId = 2; |
| 2763 | |
| 2764 | // Touch down on window |
| 2765 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2766 | .deviceId(touchDeviceId) |
| 2767 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2768 | .build()); |
| 2769 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2770 | .deviceId(touchDeviceId) |
| 2771 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2772 | .build()); |
| 2773 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2774 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2775 | |
| 2776 | // Stylus down on the window |
| 2777 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2778 | .deviceId(stylusDeviceId) |
| 2779 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2780 | .build()); |
| 2781 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2782 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2783 | |
| 2784 | // Subsequent stylus movements are delivered correctly |
| 2785 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2786 | .deviceId(stylusDeviceId) |
| 2787 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2788 | .build()); |
| 2789 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2790 | WithCoords(101, 111))); |
| 2791 | } |
| 2792 | |
| 2793 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2794 | * Two windows: a window on the left and a window on the right. |
| 2795 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2796 | * down. Then, on the left window, also place second touch pointer down. |
| 2797 | * This test tries to reproduce a crash. |
| 2798 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2799 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2800 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2801 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2802 | sp<FakeWindowHandle> leftWindow = |
| 2803 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2804 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2805 | |
| 2806 | sp<FakeWindowHandle> rightWindow = |
| 2807 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2808 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2809 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2810 | mDispatcher->onWindowInfosChanged( |
| 2811 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2812 | |
| 2813 | const int32_t touchDeviceId = 4; |
| 2814 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2815 | |
| 2816 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2817 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2818 | .deviceId(mouseDeviceId) |
| 2819 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2820 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2821 | leftWindow->consumeMotionEvent( |
| 2822 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2823 | |
| 2824 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2825 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2826 | .deviceId(mouseDeviceId) |
| 2827 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2828 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2829 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2830 | |
| 2831 | leftWindow->consumeMotionEvent( |
| 2832 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2833 | leftWindow->consumeMotionEvent( |
| 2834 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2835 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2836 | mDispatcher->notifyMotion( |
| 2837 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2838 | .deviceId(mouseDeviceId) |
| 2839 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2840 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2841 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2842 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2843 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2844 | |
| 2845 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2846 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2847 | .deviceId(touchDeviceId) |
| 2848 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2849 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2850 | leftWindow->assertNoEvents(); |
| 2851 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2852 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2853 | |
| 2854 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2855 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2856 | .deviceId(touchDeviceId) |
| 2857 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2858 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2859 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2860 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2861 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2862 | // be canceled. |
| 2863 | leftWindow->consumeMotionEvent( |
| 2864 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2865 | leftWindow->consumeMotionEvent( |
| 2866 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2867 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2868 | // current implementation. |
| 2869 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2870 | rightWindow->consumeMotionEvent( |
| 2871 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2872 | |
| 2873 | leftWindow->assertNoEvents(); |
| 2874 | rightWindow->assertNoEvents(); |
| 2875 | } |
| 2876 | |
| 2877 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2878 | * Two windows: a window on the left and a window on the right. |
| 2879 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2880 | */ |
| 2881 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2882 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2883 | sp<FakeWindowHandle> leftWindow = |
| 2884 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2885 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2886 | |
| 2887 | sp<FakeWindowHandle> rightWindow = |
| 2888 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2889 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2890 | |
| 2891 | mDispatcher->onWindowInfosChanged( |
| 2892 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2893 | |
| 2894 | const int32_t stylusDeviceId = 3; |
| 2895 | const int32_t mouseDeviceId = 6; |
| 2896 | |
| 2897 | // Start hovering over the left window |
| 2898 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2899 | .deviceId(mouseDeviceId) |
| 2900 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2901 | .build()); |
| 2902 | leftWindow->consumeMotionEvent( |
| 2903 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2904 | |
| 2905 | // Stylus hovered on right window |
| 2906 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2907 | .deviceId(stylusDeviceId) |
| 2908 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2909 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2910 | rightWindow->consumeMotionEvent( |
| 2911 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2912 | |
| 2913 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2914 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2915 | .deviceId(mouseDeviceId) |
| 2916 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2917 | .build()); |
| 2918 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2919 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2920 | |
| 2921 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2922 | .deviceId(stylusDeviceId) |
| 2923 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2924 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2925 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2926 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2927 | |
| 2928 | leftWindow->assertNoEvents(); |
| 2929 | rightWindow->assertNoEvents(); |
| 2930 | } |
| 2931 | |
| 2932 | /** |
| 2933 | * Three windows: a window on the left and a window on the right. |
| 2934 | * And a spy window that's positioned above all of them. |
| 2935 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2936 | * Check the stream that's received by the spy. |
| 2937 | */ |
| 2938 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 2939 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2940 | |
| 2941 | sp<FakeWindowHandle> spyWindow = |
| 2942 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2943 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2944 | spyWindow->setTrustedOverlay(true); |
| 2945 | spyWindow->setSpy(true); |
| 2946 | |
| 2947 | sp<FakeWindowHandle> leftWindow = |
| 2948 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2949 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2950 | |
| 2951 | sp<FakeWindowHandle> rightWindow = |
| 2952 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2953 | |
| 2954 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2955 | |
| 2956 | mDispatcher->onWindowInfosChanged( |
| 2957 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2958 | |
| 2959 | const int32_t stylusDeviceId = 1; |
| 2960 | const int32_t touchDeviceId = 2; |
| 2961 | |
| 2962 | // Stylus down on the left window |
| 2963 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2964 | .deviceId(stylusDeviceId) |
| 2965 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2966 | .build()); |
| 2967 | leftWindow->consumeMotionEvent( |
| 2968 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2969 | spyWindow->consumeMotionEvent( |
| 2970 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2971 | |
| 2972 | // Touch down on the right window |
| 2973 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2974 | .deviceId(touchDeviceId) |
| 2975 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2976 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2977 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2978 | rightWindow->consumeMotionEvent( |
| 2979 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2980 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2981 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 2982 | // already has an active stylus gesture. |
| 2983 | |
| 2984 | // 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] | 2985 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2986 | .deviceId(stylusDeviceId) |
| 2987 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2988 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2989 | leftWindow->consumeMotionEvent( |
| 2990 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2991 | spyWindow->consumeMotionEvent( |
| 2992 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2993 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2994 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2995 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2996 | .deviceId(touchDeviceId) |
| 2997 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2998 | .build()); |
| 2999 | rightWindow->consumeMotionEvent( |
| 3000 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3001 | |
| 3002 | spyWindow->assertNoEvents(); |
| 3003 | leftWindow->assertNoEvents(); |
| 3004 | rightWindow->assertNoEvents(); |
| 3005 | } |
| 3006 | |
| 3007 | /** |
| 3008 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3009 | * both. |
| 3010 | * Check hover in left window and touch down in the right window. |
| 3011 | * 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] | 3012 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3013 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3014 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3015 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlockedByTouchWithSpy) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3016 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3017 | |
| 3018 | sp<FakeWindowHandle> spyWindow = |
| 3019 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3020 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3021 | spyWindow->setTrustedOverlay(true); |
| 3022 | spyWindow->setSpy(true); |
| 3023 | |
| 3024 | sp<FakeWindowHandle> leftWindow = |
| 3025 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3026 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3027 | |
| 3028 | sp<FakeWindowHandle> rightWindow = |
| 3029 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3030 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3031 | |
| 3032 | mDispatcher->onWindowInfosChanged( |
| 3033 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3034 | |
| 3035 | const int32_t stylusDeviceId = 1; |
| 3036 | const int32_t touchDeviceId = 2; |
| 3037 | |
| 3038 | // Stylus hover on the left window |
| 3039 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3040 | .deviceId(stylusDeviceId) |
| 3041 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3042 | .build()); |
| 3043 | leftWindow->consumeMotionEvent( |
| 3044 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3045 | spyWindow->consumeMotionEvent( |
| 3046 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3047 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3048 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 3049 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3050 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3051 | .deviceId(touchDeviceId) |
| 3052 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3053 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3054 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3055 | spyWindow->consumeMotionEvent( |
| 3056 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3057 | spyWindow->consumeMotionEvent( |
| 3058 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3059 | rightWindow->consumeMotionEvent( |
| 3060 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3061 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3062 | // Stylus movements continue. They should be delivered to the left window only. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3063 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3064 | .deviceId(stylusDeviceId) |
| 3065 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3066 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3067 | leftWindow->consumeMotionEvent( |
| 3068 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3069 | |
| 3070 | // Touch movements continue. They should be delivered to the right window and to the spy |
| 3071 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3072 | .deviceId(touchDeviceId) |
| 3073 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3074 | .build()); |
| 3075 | spyWindow->consumeMotionEvent( |
| 3076 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3077 | rightWindow->consumeMotionEvent( |
| 3078 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3079 | |
| 3080 | spyWindow->assertNoEvents(); |
| 3081 | leftWindow->assertNoEvents(); |
| 3082 | rightWindow->assertNoEvents(); |
| 3083 | } |
| 3084 | |
| 3085 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3086 | * On a single window, use two different devices: mouse and touch. |
| 3087 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3088 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3089 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3090 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3091 | * represent a new gesture. |
| 3092 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3093 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3094 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3095 | sp<FakeWindowHandle> window = |
| 3096 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3097 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3098 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3099 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3100 | |
| 3101 | const int32_t touchDeviceId = 4; |
| 3102 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3103 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3104 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3105 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3106 | .deviceId(touchDeviceId) |
| 3107 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3108 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3109 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3110 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3111 | .deviceId(touchDeviceId) |
| 3112 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3113 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3114 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3115 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3116 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3117 | .deviceId(touchDeviceId) |
| 3118 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3119 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3120 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3121 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3122 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3123 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3124 | |
| 3125 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3126 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3127 | .deviceId(mouseDeviceId) |
| 3128 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3129 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3130 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3131 | |
| 3132 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3133 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3134 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3135 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3136 | mDispatcher->notifyMotion( |
| 3137 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3138 | .deviceId(mouseDeviceId) |
| 3139 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3140 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3141 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3142 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3143 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3144 | |
| 3145 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3146 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3147 | .deviceId(touchDeviceId) |
| 3148 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3149 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3150 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3151 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3152 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3153 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3154 | // However, mouse movements should continue to work. |
| 3155 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3156 | .deviceId(mouseDeviceId) |
| 3157 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3158 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3159 | .build()); |
| 3160 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3161 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3162 | window->assertNoEvents(); |
| 3163 | } |
| 3164 | |
| 3165 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3166 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3167 | * the injected event. |
| 3168 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3169 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3170 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3171 | sp<FakeWindowHandle> window = |
| 3172 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3173 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3174 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3175 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3176 | |
| 3177 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3178 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3179 | // completion. |
| 3180 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3181 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3182 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3183 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3184 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3185 | .build())); |
| 3186 | window->consumeMotionEvent( |
| 3187 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3188 | |
| 3189 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3190 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3191 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3192 | .deviceId(touchDeviceId) |
| 3193 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3194 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3195 | |
| 3196 | window->consumeMotionEvent( |
| 3197 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3198 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3199 | } |
| 3200 | |
| 3201 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3202 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3203 | * |
| 3204 | * Two windows: a window on the left and a window on the right. |
| 3205 | * Mouse is hovered over the left window. |
| 3206 | * Next, we tap on the left window, where the cursor was last seen. |
| 3207 | * |
| 3208 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3209 | * the left window. |
| 3210 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3211 | * window (first), and then another on the left window (second). |
| 3212 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3213 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3214 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3215 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3216 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3217 | sp<FakeWindowHandle> leftWindow = |
| 3218 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3219 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3220 | |
| 3221 | sp<FakeWindowHandle> rightWindow = |
| 3222 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3223 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3224 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3225 | mDispatcher->onWindowInfosChanged( |
| 3226 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3227 | |
| 3228 | const int32_t mouseDeviceId = 6; |
| 3229 | const int32_t touchDeviceId = 4; |
| 3230 | // Hover over the left window. Keep the cursor there. |
| 3231 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3232 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3233 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3234 | AINPUT_SOURCE_MOUSE) |
| 3235 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3236 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3237 | .build())); |
| 3238 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3239 | |
| 3240 | // Tap on left window |
| 3241 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3242 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3243 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3244 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3245 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3246 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3247 | .build())); |
| 3248 | |
| 3249 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3250 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3251 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3252 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3253 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3254 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3255 | .build())); |
| 3256 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3257 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3258 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3259 | |
| 3260 | // First finger down on right window |
| 3261 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3262 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3263 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3264 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3265 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3266 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3267 | .build())); |
| 3268 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3269 | |
| 3270 | // Second finger down on the left window |
| 3271 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3272 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3273 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3274 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3275 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3276 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3277 | .build())); |
| 3278 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3279 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3280 | |
| 3281 | // No more events |
| 3282 | leftWindow->assertNoEvents(); |
| 3283 | rightWindow->assertNoEvents(); |
| 3284 | } |
| 3285 | |
| 3286 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3287 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3288 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3289 | * touch is gone, stylus hovering should start working again. |
| 3290 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3291 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDroppedWhenTouchTap) { |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3292 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3293 | sp<FakeWindowHandle> window = |
| 3294 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3295 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3296 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3297 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3298 | |
| 3299 | const int32_t stylusDeviceId = 5; |
| 3300 | const int32_t touchDeviceId = 4; |
| 3301 | // Start hovering with stylus |
| 3302 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3303 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3304 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3305 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3306 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3307 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3308 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3309 | |
| 3310 | // Finger down on the window |
| 3311 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3312 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3313 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3314 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3315 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3316 | .build())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3317 | // The touch device should cause hover to stop! |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3318 | window->consumeMotionEvent( |
| 3319 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3320 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3321 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3322 | // Continue hovering with stylus. |
| 3323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3324 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3325 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3326 | AINPUT_SOURCE_STYLUS) |
| 3327 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3328 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3329 | .build())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3330 | // Hovers are now ignored |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3331 | |
| 3332 | // Lift up the finger |
| 3333 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3334 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3335 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3336 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3337 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3338 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3339 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3340 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3341 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3343 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3344 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3345 | AINPUT_SOURCE_STYLUS) |
| 3346 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3347 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3348 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3349 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3350 | WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3351 | window->assertNoEvents(); |
| 3352 | } |
| 3353 | |
| 3354 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3355 | * A spy window above a window with no input channel. |
| 3356 | * Start hovering with a stylus device, and then tap with it. |
| 3357 | * Ensure spy window receives the entire sequence. |
| 3358 | */ |
| 3359 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3360 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3361 | sp<FakeWindowHandle> spyWindow = |
| 3362 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3363 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3364 | spyWindow->setTrustedOverlay(true); |
| 3365 | spyWindow->setSpy(true); |
| 3366 | sp<FakeWindowHandle> window = |
| 3367 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3368 | window->setNoInputChannel(true); |
| 3369 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3370 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3371 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3372 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3373 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3374 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3375 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3376 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3377 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3378 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3379 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3380 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3381 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3382 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3383 | |
| 3384 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3385 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3386 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3387 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3388 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3389 | |
| 3390 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3391 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3392 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3393 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3394 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3395 | |
| 3396 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3397 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3398 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3399 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3400 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3401 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3402 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3403 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3404 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3405 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3406 | |
| 3407 | // No more events |
| 3408 | spyWindow->assertNoEvents(); |
| 3409 | window->assertNoEvents(); |
| 3410 | } |
| 3411 | |
| 3412 | /** |
| 3413 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3414 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3415 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3416 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3417 | */ |
| 3418 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3419 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3420 | sp<FakeWindowHandle> spyWindow = |
| 3421 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3422 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3423 | spyWindow->setTrustedOverlay(true); |
| 3424 | spyWindow->setSpy(true); |
| 3425 | sp<FakeWindowHandle> window = |
| 3426 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3427 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3428 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3429 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3430 | |
| 3431 | const int32_t mouseDeviceId = 7; |
| 3432 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3433 | |
| 3434 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3435 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3436 | .deviceId(mouseDeviceId) |
| 3437 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3438 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3439 | spyWindow->consumeMotionEvent( |
| 3440 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3441 | window->consumeMotionEvent( |
| 3442 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3443 | |
| 3444 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3445 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3446 | .deviceId(touchDeviceId) |
| 3447 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3448 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3449 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3450 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3451 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3452 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3453 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3454 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3455 | .deviceId(touchDeviceId) |
| 3456 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3457 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3458 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3459 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3460 | |
| 3461 | // Pilfer the stream |
| 3462 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3463 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3464 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3465 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3466 | .deviceId(touchDeviceId) |
| 3467 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3468 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3469 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3470 | |
| 3471 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3472 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3473 | .deviceId(mouseDeviceId) |
| 3474 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3475 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3476 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3477 | |
| 3478 | spyWindow->consumeMotionEvent( |
| 3479 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3480 | spyWindow->consumeMotionEvent( |
| 3481 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3482 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3483 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3484 | mDispatcher->notifyMotion( |
| 3485 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3486 | .deviceId(mouseDeviceId) |
| 3487 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3488 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3489 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3490 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3491 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3492 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3493 | |
| 3494 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3495 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3496 | .deviceId(mouseDeviceId) |
| 3497 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3498 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3499 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3500 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3501 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3502 | |
| 3503 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3504 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3505 | .deviceId(touchDeviceId) |
| 3506 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3507 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3508 | |
| 3509 | // No more events |
| 3510 | spyWindow->assertNoEvents(); |
| 3511 | window->assertNoEvents(); |
| 3512 | } |
| 3513 | |
| 3514 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3515 | * On the display, have a single window, and also an area where there's no window. |
| 3516 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3517 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3518 | */ |
| 3519 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3520 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3521 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3522 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3523 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3524 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3525 | |
| 3526 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3527 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3528 | |
| 3529 | mDispatcher->waitForIdle(); |
| 3530 | window->assertNoEvents(); |
| 3531 | |
| 3532 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3533 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3534 | mDispatcher->waitForIdle(); |
| 3535 | window->consumeMotionDown(); |
| 3536 | } |
| 3537 | |
| 3538 | /** |
| 3539 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3540 | * non-touchable window. |
| 3541 | */ |
| 3542 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3543 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3544 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3545 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3546 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3547 | window1->setTouchable(false); |
| 3548 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3549 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3550 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3551 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3552 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3553 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3554 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3555 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3556 | |
| 3557 | mDispatcher->waitForIdle(); |
| 3558 | window1->assertNoEvents(); |
| 3559 | window2->assertNoEvents(); |
| 3560 | |
| 3561 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3562 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3563 | mDispatcher->waitForIdle(); |
| 3564 | window2->consumeMotionDown(); |
| 3565 | } |
| 3566 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3567 | /** |
| 3568 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3569 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3570 | */ |
| 3571 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3572 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3573 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3574 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3575 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3576 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3577 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3578 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3579 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3580 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3581 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3582 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3583 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3584 | |
| 3585 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3586 | |
| 3587 | MotionEvent* motionEvent1 = window1->consumeMotion(); |
| 3588 | ASSERT_NE(motionEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3589 | window2->assertNoEvents(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3590 | nsecs_t downTimeForWindow1 = motionEvent1->getDownTime(); |
| 3591 | ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3592 | |
| 3593 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3594 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3595 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3596 | MotionEvent* motionEvent2 = window2->consumeMotion(); |
| 3597 | ASSERT_NE(motionEvent2, nullptr); |
| 3598 | nsecs_t downTimeForWindow2 = motionEvent2->getDownTime(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3599 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3600 | ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3601 | |
| 3602 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3603 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3604 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3605 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3606 | |
| 3607 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3608 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3609 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3610 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3611 | |
| 3612 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3613 | window1->consumeMotionMove(); |
| 3614 | window1->assertNoEvents(); |
| 3615 | |
| 3616 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3617 | mDispatcher->notifyMotion( |
| 3618 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3619 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3620 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3621 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3622 | mDispatcher->notifyMotion( |
| 3623 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3624 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3625 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3628 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3629 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3630 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3631 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3632 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3633 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3634 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3635 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3636 | |
| 3637 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3638 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3639 | mDispatcher->onWindowInfosChanged( |
| 3640 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3641 | |
| 3642 | // 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] | 3643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3644 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3645 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3646 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3647 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3648 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3649 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3650 | |
| 3651 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3653 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3654 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3655 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3656 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3657 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3658 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3659 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3660 | |
| 3661 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3663 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3664 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3665 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3666 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3667 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3668 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3669 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3670 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3671 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3672 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3673 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3674 | AINPUT_SOURCE_MOUSE) |
| 3675 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3676 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3677 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3678 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3679 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3680 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3682 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3683 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3684 | AINPUT_SOURCE_MOUSE) |
| 3685 | .buttonState(0) |
| 3686 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3687 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3688 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3689 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3690 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3691 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3692 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3693 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3694 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3695 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3696 | .build())); |
| 3697 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3698 | |
| 3699 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3700 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3701 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3702 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3703 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3704 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3705 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3706 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3707 | |
| 3708 | // No more events |
| 3709 | windowLeft->assertNoEvents(); |
| 3710 | windowRight->assertNoEvents(); |
| 3711 | } |
| 3712 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3713 | /** |
| 3714 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3715 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3716 | * currently active gesture should be canceled, and the new one should proceed. |
| 3717 | */ |
| 3718 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3719 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3720 | sp<FakeWindowHandle> window = |
| 3721 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3722 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3723 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3724 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3725 | |
| 3726 | const int32_t touchDeviceId = 4; |
| 3727 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3728 | |
| 3729 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3730 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3731 | .deviceId(touchDeviceId) |
| 3732 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3733 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3734 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3735 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3736 | .deviceId(touchDeviceId) |
| 3737 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3738 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3739 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3740 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3741 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3742 | |
| 3743 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3744 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3745 | .deviceId(mouseDeviceId) |
| 3746 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3747 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3748 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3749 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3750 | WithPointerCount(2u))); |
| 3751 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3752 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3753 | mDispatcher->notifyMotion( |
| 3754 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3755 | .deviceId(mouseDeviceId) |
| 3756 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3757 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3758 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3759 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3760 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3761 | |
| 3762 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3763 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3764 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3765 | .deviceId(touchDeviceId) |
| 3766 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3767 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3768 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3769 | window->assertNoEvents(); |
| 3770 | } |
| 3771 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3772 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3773 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3774 | |
| 3775 | sp<FakeWindowHandle> spyWindow = |
| 3776 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3777 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3778 | spyWindow->setTrustedOverlay(true); |
| 3779 | spyWindow->setSpy(true); |
| 3780 | sp<FakeWindowHandle> window = |
| 3781 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3782 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3783 | |
| 3784 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3785 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3786 | |
| 3787 | // Send mouse cursor to the window |
| 3788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3789 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3790 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3791 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3792 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3793 | .build())); |
| 3794 | |
| 3795 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3796 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3797 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3798 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3799 | |
| 3800 | window->assertNoEvents(); |
| 3801 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3802 | } |
| 3803 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3804 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3805 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3806 | |
| 3807 | sp<FakeWindowHandle> spyWindow = |
| 3808 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3809 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3810 | spyWindow->setTrustedOverlay(true); |
| 3811 | spyWindow->setSpy(true); |
| 3812 | sp<FakeWindowHandle> window = |
| 3813 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3814 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3815 | |
| 3816 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3817 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3818 | |
| 3819 | // Send mouse cursor to the window |
| 3820 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3821 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3822 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3823 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3824 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3825 | .build())); |
| 3826 | |
| 3827 | // Move mouse cursor |
| 3828 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3829 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3830 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3831 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3832 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3833 | .build())); |
| 3834 | |
| 3835 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3836 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3837 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3838 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3839 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3840 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3841 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3842 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3843 | // Touch down on the window |
| 3844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3845 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3846 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3847 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3848 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3849 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3850 | .build())); |
| 3851 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3852 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3853 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3854 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3855 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3856 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3857 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3858 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3859 | |
| 3860 | // pilfer the motion, retaining the gesture on the spy window. |
| 3861 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3862 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3863 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3864 | |
| 3865 | // Touch UP on the window |
| 3866 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3867 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3868 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3869 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3870 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3871 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3872 | .build())); |
| 3873 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3874 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3875 | |
| 3876 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3877 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3878 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3879 | |
| 3880 | // One more tap - DOWN |
| 3881 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3882 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3883 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3884 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3885 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3886 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3887 | .build())); |
| 3888 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3889 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3890 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3891 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3892 | |
| 3893 | // Touch UP on the window |
| 3894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3895 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3896 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3897 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3898 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3899 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3900 | .build())); |
| 3901 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3902 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3903 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3904 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3905 | |
| 3906 | window->assertNoEvents(); |
| 3907 | spyWindow->assertNoEvents(); |
| 3908 | } |
| 3909 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3910 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3911 | // directly in this test. |
| 3912 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3913 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3914 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3915 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3916 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3917 | |
| 3918 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3919 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3920 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3921 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3922 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3923 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3924 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3925 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3926 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3927 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3928 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3929 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3930 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3931 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3932 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3933 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3934 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3935 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3936 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3937 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3938 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3939 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3940 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3941 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3942 | AINPUT_SOURCE_MOUSE) |
| 3943 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3944 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3945 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3946 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3947 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3948 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3949 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3950 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3951 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3952 | AINPUT_SOURCE_MOUSE) |
| 3953 | .buttonState(0) |
| 3954 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3955 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3956 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3957 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3958 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3959 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3960 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3961 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3962 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3963 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3964 | .build())); |
| 3965 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3966 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3967 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 3968 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 3969 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3970 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3971 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3972 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3973 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3974 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3975 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3976 | } |
| 3977 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3978 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3979 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3980 | * is generated. |
| 3981 | */ |
| 3982 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3983 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3984 | sp<FakeWindowHandle> window = |
| 3985 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3986 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3987 | |
| 3988 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3989 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3990 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3991 | |
| 3992 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3993 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3994 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3995 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3996 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3997 | .build())); |
| 3998 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3999 | |
| 4000 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4001 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4002 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4003 | } |
| 4004 | |
| 4005 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4006 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4007 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4008 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4009 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4010 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4011 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4012 | sp<FakeWindowHandle> window = |
| 4013 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4014 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4015 | |
| 4016 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4017 | |
| 4018 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4019 | |
| 4020 | MotionEventBuilder hoverEnterBuilder = |
| 4021 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4022 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4023 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4025 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4026 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4027 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4028 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4029 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4030 | } |
| 4031 | |
| 4032 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4033 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4034 | */ |
| 4035 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4036 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4037 | sp<FakeWindowHandle> window = |
| 4038 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4039 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4040 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4041 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4042 | |
| 4043 | const int32_t mouseDeviceId = 7; |
| 4044 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4045 | |
| 4046 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4047 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4048 | .deviceId(mouseDeviceId) |
| 4049 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4050 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4051 | window->consumeMotionEvent( |
| 4052 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4053 | |
| 4054 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4055 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4056 | .deviceId(touchDeviceId) |
| 4057 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4058 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4059 | |
| 4060 | window->consumeMotionEvent( |
| 4061 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4062 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4063 | } |
| 4064 | |
| 4065 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4066 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4067 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4068 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4069 | */ |
| 4070 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4071 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4072 | sp<FakeWindowHandle> window = |
| 4073 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4074 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4075 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4076 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4077 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4078 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4079 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4080 | ASSERT_NO_FATAL_FAILURE( |
| 4081 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4082 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4083 | |
| 4084 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4085 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4086 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4087 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4088 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4089 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4090 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4091 | |
| 4092 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4093 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4094 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4095 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4096 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4097 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4098 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4099 | ASSERT_NO_FATAL_FAILURE( |
| 4100 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4101 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4102 | } |
| 4103 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4104 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4105 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4106 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4107 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4108 | ADISPLAY_ID_DEFAULT); |
| 4109 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4110 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4111 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4112 | SECOND_DISPLAY_ID); |
| 4113 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4114 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4115 | mDispatcher->onWindowInfosChanged( |
| 4116 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4117 | |
| 4118 | // Set cursor position in window in default display and check that hover enter and move |
| 4119 | // events are generated. |
| 4120 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4121 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4122 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4123 | AINPUT_SOURCE_MOUSE) |
| 4124 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4125 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4126 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4127 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4128 | |
| 4129 | // Remove all windows in secondary display and check that no event happens on window in |
| 4130 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4131 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4132 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4133 | windowDefaultDisplay->assertNoEvents(); |
| 4134 | |
| 4135 | // Move cursor position in window in default display and check that only hover move |
| 4136 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4137 | mDispatcher->onWindowInfosChanged( |
| 4138 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4139 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4140 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4141 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4142 | AINPUT_SOURCE_MOUSE) |
| 4143 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4144 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4145 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4146 | windowDefaultDisplay->consumeMotionEvent( |
| 4147 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4148 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4149 | windowDefaultDisplay->assertNoEvents(); |
| 4150 | } |
| 4151 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4152 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4153 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4154 | |
| 4155 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4156 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4157 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4158 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4159 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4160 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4161 | |
| 4162 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4163 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4164 | mDispatcher->onWindowInfosChanged( |
| 4165 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4166 | |
| 4167 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4168 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4169 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4170 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4171 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4172 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4173 | windowRight->assertNoEvents(); |
| 4174 | } |
| 4175 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4176 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4177 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4178 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4179 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4180 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4181 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4182 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4183 | setFocusedWindow(window); |
| 4184 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4185 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4186 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4187 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4188 | |
| 4189 | // Window should receive key down event. |
| 4190 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4191 | |
| 4192 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4193 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4194 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4195 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4196 | AKEY_EVENT_FLAG_CANCELED); |
| 4197 | } |
| 4198 | |
| 4199 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4200 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4201 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4202 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4203 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4204 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4205 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4206 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4207 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4208 | |
| 4209 | // Window should receive motion down event. |
| 4210 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4211 | |
| 4212 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4213 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4214 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4215 | window->consumeMotionEvent( |
| 4216 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4217 | } |
| 4218 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4219 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4220 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4221 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4222 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4223 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4224 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4225 | |
| 4226 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4227 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 4228 | .build()); |
| 4229 | |
| 4230 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4231 | |
| 4232 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 4233 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 4234 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4235 | |
| 4236 | // After the device has been reset, a new hovering stream can be sent to the window |
| 4237 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4238 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 4239 | .build()); |
| 4240 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4241 | } |
| 4242 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4243 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 4244 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4245 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4246 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4247 | window->setFocusable(true); |
| 4248 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4249 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4250 | setFocusedWindow(window); |
| 4251 | |
| 4252 | window->consumeFocusEvent(true); |
| 4253 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4254 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4255 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 4256 | const nsecs_t injectTime = keyArgs.eventTime; |
| 4257 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4258 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4259 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 4260 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4261 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 4262 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 4263 | } |
| 4264 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4265 | /** |
| 4266 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 4267 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4268 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 4269 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4270 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4271 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4272 | window->setFocusable(true); |
| 4273 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4274 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4275 | setFocusedWindow(window); |
| 4276 | |
| 4277 | window->consumeFocusEvent(true); |
| 4278 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4279 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4280 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4281 | |
| 4282 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 4283 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 4284 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 4285 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 4286 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4287 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4288 | } |
| 4289 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4290 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4291 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 4292 | * WATCH_OUTSIDE_TOUCH. |
| 4293 | * Both windows are owned by the same UID. |
| 4294 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 4295 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 4296 | */ |
| 4297 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 4298 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4299 | sp<FakeWindowHandle> window = |
| 4300 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4301 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4302 | |
| 4303 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4304 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4305 | ADISPLAY_ID_DEFAULT); |
| 4306 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4307 | outsideWindow->setWatchOutsideTouch(true); |
| 4308 | // 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] | 4309 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4310 | |
| 4311 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4312 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4313 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4314 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4315 | window->consumeMotionDown(); |
| 4316 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 4317 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 4318 | outsideWindow->consumeMotionEvent( |
| 4319 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 4320 | } |
| 4321 | |
| 4322 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4323 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 4324 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 4325 | * ACTION_OUTSIDE event is sent per gesture. |
| 4326 | */ |
| 4327 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 4328 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 4329 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4330 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4331 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4332 | window->setWatchOutsideTouch(true); |
| 4333 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4334 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4335 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4336 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4337 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4338 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4339 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 4340 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4341 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4342 | mDispatcher->onWindowInfosChanged( |
| 4343 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4344 | |
| 4345 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4346 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4347 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4348 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4349 | window->assertNoEvents(); |
| 4350 | secondWindow->assertNoEvents(); |
| 4351 | |
| 4352 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 4353 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4354 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4355 | ADISPLAY_ID_DEFAULT, |
| 4356 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4357 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 4358 | window->consumeMotionEvent( |
| 4359 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4360 | secondWindow->consumeMotionDown(); |
| 4361 | thirdWindow->assertNoEvents(); |
| 4362 | |
| 4363 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 4364 | // 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] | 4365 | mDispatcher->notifyMotion( |
| 4366 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4367 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4368 | window->assertNoEvents(); |
| 4369 | secondWindow->consumeMotionMove(); |
| 4370 | thirdWindow->consumeMotionDown(); |
| 4371 | } |
| 4372 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4373 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 4374 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4375 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4376 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4377 | window->setFocusable(true); |
| 4378 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4379 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4380 | setFocusedWindow(window); |
| 4381 | |
| 4382 | window->consumeFocusEvent(true); |
| 4383 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4384 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4385 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 4386 | mDispatcher->notifyKey(keyDown); |
| 4387 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4388 | |
| 4389 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4390 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4391 | |
| 4392 | // 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] | 4393 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4394 | |
| 4395 | window->consumeFocusEvent(false); |
| 4396 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4397 | mDispatcher->notifyKey(keyDown); |
| 4398 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4399 | window->assertNoEvents(); |
| 4400 | } |
| 4401 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4402 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 4403 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4404 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4405 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4406 | // Ensure window is non-split and have some transform. |
| 4407 | window->setPreventSplitting(true); |
| 4408 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4409 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4410 | |
| 4411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4412 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4413 | {50, 50})) |
| 4414 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4415 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4416 | |
| 4417 | const MotionEvent secondFingerDownEvent = |
| 4418 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4419 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4420 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4421 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4422 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4423 | .build(); |
| 4424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4425 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4426 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4427 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4428 | |
| 4429 | const MotionEvent* event = window->consumeMotion(); |
| 4430 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 4431 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 4432 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 4433 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 4434 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 4435 | } |
| 4436 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 4437 | /** |
| 4438 | * Two windows: a splittable and a non-splittable. |
| 4439 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 4440 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 4441 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 4442 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 4443 | * "incomplete" gestures. |
| 4444 | */ |
| 4445 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 4446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4447 | sp<FakeWindowHandle> leftWindow = |
| 4448 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 4449 | ADISPLAY_ID_DEFAULT); |
| 4450 | leftWindow->setPreventSplitting(false); |
| 4451 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4452 | sp<FakeWindowHandle> rightWindow = |
| 4453 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 4454 | ADISPLAY_ID_DEFAULT); |
| 4455 | rightWindow->setPreventSplitting(true); |
| 4456 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4457 | mDispatcher->onWindowInfosChanged( |
| 4458 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 4459 | |
| 4460 | // Touch down on left, splittable window |
| 4461 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4462 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4463 | .build()); |
| 4464 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4465 | |
| 4466 | mDispatcher->notifyMotion( |
| 4467 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4468 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4469 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 4470 | .build()); |
| 4471 | leftWindow->assertNoEvents(); |
| 4472 | rightWindow->assertNoEvents(); |
| 4473 | } |
| 4474 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4475 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4476 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4477 | sp<FakeWindowHandle> window = |
| 4478 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4479 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4480 | sp<FakeWindowHandle> trustedOverlay = |
| 4481 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4482 | ADISPLAY_ID_DEFAULT); |
| 4483 | trustedOverlay->setSpy(true); |
| 4484 | trustedOverlay->setTrustedOverlay(true); |
| 4485 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4486 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4487 | |
| 4488 | // Start a three-finger touchpad swipe |
| 4489 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4490 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4491 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4492 | .build()); |
| 4493 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4494 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4495 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4496 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4497 | .build()); |
| 4498 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4499 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4500 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4501 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4502 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4503 | .build()); |
| 4504 | |
| 4505 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4506 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4507 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4508 | |
| 4509 | // Move the swipe a bit |
| 4510 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4511 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4512 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4513 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4514 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4515 | .build()); |
| 4516 | |
| 4517 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4518 | |
| 4519 | // End the swipe |
| 4520 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4521 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4522 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4523 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4524 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4525 | .build()); |
| 4526 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4527 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4528 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4529 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4530 | .build()); |
| 4531 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4532 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4533 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4534 | .build()); |
| 4535 | |
| 4536 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4537 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4538 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4539 | |
| 4540 | window->assertNoEvents(); |
| 4541 | } |
| 4542 | |
| 4543 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4544 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4545 | sp<FakeWindowHandle> window = |
| 4546 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4547 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4548 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4549 | |
| 4550 | // Start a three-finger touchpad swipe |
| 4551 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4552 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4553 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4554 | .build()); |
| 4555 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4556 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4557 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4558 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4559 | .build()); |
| 4560 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4561 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4562 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4563 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4564 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4565 | .build()); |
| 4566 | |
| 4567 | // Move the swipe a bit |
| 4568 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4569 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4570 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4571 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4572 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4573 | .build()); |
| 4574 | |
| 4575 | // End the swipe |
| 4576 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4577 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4578 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4579 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4580 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4581 | .build()); |
| 4582 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4583 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4584 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4585 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4586 | .build()); |
| 4587 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4588 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4589 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4590 | .build()); |
| 4591 | |
| 4592 | window->assertNoEvents(); |
| 4593 | } |
| 4594 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4595 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4596 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 4597 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4598 | * 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] | 4599 | */ |
| 4600 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 4601 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4602 | sp<FakeWindowHandle> window = |
| 4603 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4604 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4605 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4606 | |
| 4607 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4609 | .downTime(baseTime + 10) |
| 4610 | .eventTime(baseTime + 10) |
| 4611 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4612 | .build()); |
| 4613 | |
| 4614 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4615 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4616 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4617 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4618 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4619 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4620 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4621 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4622 | .downTime(baseTime + 10) |
| 4623 | .eventTime(baseTime + 30) |
| 4624 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4625 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4626 | .build()); |
| 4627 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4628 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4629 | |
| 4630 | // 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] | 4631 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4632 | .downTime(baseTime + 10) |
| 4633 | .eventTime(baseTime + 40) |
| 4634 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4635 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4636 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4637 | |
| 4638 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4639 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4640 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4641 | .downTime(baseTime + 10) |
| 4642 | .eventTime(baseTime + 50) |
| 4643 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4644 | .build()); |
| 4645 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4646 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4647 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4648 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4649 | .downTime(baseTime + 60) |
| 4650 | .eventTime(baseTime + 60) |
| 4651 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4652 | .build()); |
| 4653 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4654 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4658 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 4659 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 4660 | * its coordinates should be converted by the transform of the windows of target screen. |
| 4661 | */ |
| 4662 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 4663 | // This case will create a window and a spy window on the default display and mirror |
| 4664 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 4665 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4666 | |
| 4667 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
| 4668 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4669 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 4670 | spyWindowDefaultDisplay->setSpy(true); |
| 4671 | |
| 4672 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4673 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4674 | ADISPLAY_ID_DEFAULT); |
| 4675 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 4676 | |
| 4677 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 4678 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 4679 | |
| 4680 | // Add the windows to the dispatcher |
| 4681 | mDispatcher->onWindowInfosChanged( |
| 4682 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 4683 | *windowSecondDisplay->getInfo()}, |
| 4684 | {}, |
| 4685 | 0, |
| 4686 | 0}); |
| 4687 | |
| 4688 | // Send down to ADISPLAY_ID_DEFAULT |
| 4689 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4690 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4691 | {100, 100})) |
| 4692 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4693 | |
| 4694 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 4695 | windowDefaultDisplay->consumeMotionDown(); |
| 4696 | |
| 4697 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 4698 | |
| 4699 | // windowDefaultDisplay gets cancel |
| 4700 | MotionEvent* event = windowDefaultDisplay->consumeMotion(); |
| 4701 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
| 4702 | |
| 4703 | // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the |
| 4704 | // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y |
| 4705 | // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of |
| 4706 | // SECOND_DISPLAY_ID, the x and y coordinates are 200 |
| 4707 | EXPECT_EQ(100, event->getX(0)); |
| 4708 | EXPECT_EQ(100, event->getY(0)); |
| 4709 | } |
| 4710 | |
| 4711 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4712 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4713 | * |
| 4714 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4715 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4716 | * space. |
| 4717 | */ |
| 4718 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4719 | public: |
| 4720 | void SetUp() override { |
| 4721 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4722 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4726 | gui::DisplayInfo info; |
| 4727 | info.displayId = displayId; |
| 4728 | info.transform = transform; |
| 4729 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4730 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4731 | } |
| 4732 | |
| 4733 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4734 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4735 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4736 | } |
| 4737 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4738 | void removeAllWindowsAndDisplays() { |
| 4739 | mDisplayInfos.clear(); |
| 4740 | mWindowInfos.clear(); |
| 4741 | } |
| 4742 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4743 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4744 | // on the display. |
| 4745 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4746 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4747 | // respectively. |
| 4748 | ui::Transform displayTransform; |
| 4749 | displayTransform.set(2, 0, 0, 4); |
| 4750 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4751 | |
| 4752 | std::shared_ptr<FakeApplicationHandle> application = |
| 4753 | std::make_shared<FakeApplicationHandle>(); |
| 4754 | |
| 4755 | // Add two windows to the display. Their frames are represented in the display space. |
| 4756 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4757 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4758 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4759 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 4760 | addWindow(firstWindow); |
| 4761 | |
| 4762 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4763 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4764 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4765 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 4766 | addWindow(secondWindow); |
| 4767 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 4768 | } |
| 4769 | |
| 4770 | private: |
| 4771 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 4772 | std::vector<gui::WindowInfo> mWindowInfos; |
| 4773 | }; |
| 4774 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4775 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4776 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4777 | // 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] | 4778 | // selected so that if the hit test was performed with the point and the bounds being in |
| 4779 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4780 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4781 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4782 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4783 | |
| 4784 | firstWindow->consumeMotionDown(); |
| 4785 | secondWindow->assertNoEvents(); |
| 4786 | } |
| 4787 | |
| 4788 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 4789 | // the event should be treated as being in the logical display space. |
| 4790 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 4791 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4792 | // Send down to the first window. The point is represented in the logical display space. The |
| 4793 | // point is selected so that if the hit test was done in logical display space, then it would |
| 4794 | // end up in the incorrect window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4795 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4796 | PointF{75 * 2, 55 * 4}); |
| 4797 | |
| 4798 | firstWindow->consumeMotionDown(); |
| 4799 | secondWindow->assertNoEvents(); |
| 4800 | } |
| 4801 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4802 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 4803 | // event should be treated as being in the logical display space. |
| 4804 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 4805 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4806 | |
| 4807 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4808 | ui::Transform injectedEventTransform; |
| 4809 | injectedEventTransform.set(matrix); |
| 4810 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 4811 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 4812 | |
| 4813 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4814 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4815 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4816 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4817 | .x(untransformedPoint.x) |
| 4818 | .y(untransformedPoint.y)) |
| 4819 | .build(); |
| 4820 | event.transform(matrix); |
| 4821 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4822 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4823 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4824 | |
| 4825 | firstWindow->consumeMotionDown(); |
| 4826 | secondWindow->assertNoEvents(); |
| 4827 | } |
| 4828 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4829 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4830 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4831 | |
| 4832 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4833 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4834 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4835 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4836 | |
| 4837 | firstWindow->assertNoEvents(); |
| 4838 | const MotionEvent* event = secondWindow->consumeMotion(); |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 4839 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4840 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 4841 | |
| 4842 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 4843 | EXPECT_EQ(300, event->getRawX(0)); |
| 4844 | EXPECT_EQ(880, event->getRawY(0)); |
| 4845 | |
| 4846 | // Ensure that the x and y values are in the window's coordinate space. |
| 4847 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4848 | // the logical display space. This will be the origin of the window space. |
| 4849 | EXPECT_EQ(100, event->getX(0)); |
| 4850 | EXPECT_EQ(80, event->getY(0)); |
| 4851 | } |
| 4852 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4853 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 4854 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4855 | // The monitor will always receive events in the logical display's coordinate space, because |
| 4856 | // it does not have a window. |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 4857 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4858 | |
| 4859 | // Send down to the first window. |
| 4860 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4861 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 4862 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4863 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4864 | |
| 4865 | // Second pointer goes down on second window. |
| 4866 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4867 | ADISPLAY_ID_DEFAULT, |
| 4868 | {PointF{50, 100}, PointF{150, 220}})); |
| 4869 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 4870 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 4871 | {1, PointF{300, 880}}}; |
| 4872 | monitor.consumeMotionEvent( |
| 4873 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 4874 | |
| 4875 | mDispatcher->cancelCurrentTouch(); |
| 4876 | |
| 4877 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 4878 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 4879 | monitor.consumeMotionEvent( |
| 4880 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 4881 | } |
| 4882 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4883 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 4884 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4885 | |
| 4886 | // Send down to the first window. |
| 4887 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4888 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 4889 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4890 | |
| 4891 | // The pointer is transferred to the second window, and the second window receives it in the |
| 4892 | // correct coordinate space. |
| 4893 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4894 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 4895 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 4896 | } |
| 4897 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 4898 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 4899 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4900 | |
| 4901 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 4902 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 4903 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4904 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4905 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4906 | |
| 4907 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 4908 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 4909 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4910 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4911 | WithRawCoords(300, 880))); |
| 4912 | secondWindow->consumeMotionEvent( |
| 4913 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4914 | secondWindow->assertNoEvents(); |
| 4915 | firstWindow->assertNoEvents(); |
| 4916 | } |
| 4917 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 4918 | // Same as above, but while the window is being mirrored. |
| 4919 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 4920 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 4921 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4922 | |
| 4923 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4924 | ui::Transform secondDisplayTransform; |
| 4925 | secondDisplayTransform.set(matrix); |
| 4926 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 4927 | |
| 4928 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 4929 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 4930 | addWindow(secondWindowClone); |
| 4931 | |
| 4932 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 4933 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 4934 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4935 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4936 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4937 | |
| 4938 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 4939 | // display. |
| 4940 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 4941 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4942 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4943 | WithRawCoords(300, 880))); |
| 4944 | secondWindow->consumeMotionEvent( |
| 4945 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4946 | secondWindow->assertNoEvents(); |
| 4947 | firstWindow->assertNoEvents(); |
| 4948 | } |
| 4949 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 4950 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 4951 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4952 | |
| 4953 | // Send hover enter to second window |
| 4954 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 4955 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4956 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4957 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4958 | |
| 4959 | mDispatcher->cancelCurrentTouch(); |
| 4960 | |
| 4961 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4962 | WithRawCoords(300, 880))); |
| 4963 | secondWindow->assertNoEvents(); |
| 4964 | firstWindow->assertNoEvents(); |
| 4965 | } |
| 4966 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 4967 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4968 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 4969 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 4970 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4971 | |
| 4972 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4973 | ui::Transform secondDisplayTransform; |
| 4974 | secondDisplayTransform.set(matrix); |
| 4975 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 4976 | |
| 4977 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 4978 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 4979 | addWindow(secondWindowClone); |
| 4980 | |
| 4981 | // Send hover enter to second window |
| 4982 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 4983 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4984 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4985 | WithCoords(100, 80), WithRawCoords(300, 880), |
| 4986 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4987 | |
| 4988 | mDispatcher->cancelCurrentTouch(); |
| 4989 | |
| 4990 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 4991 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4992 | WithRawCoords(300, 880), |
| 4993 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4994 | secondWindow->assertNoEvents(); |
| 4995 | firstWindow->assertNoEvents(); |
| 4996 | } |
| 4997 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4998 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4999 | class InputDispatcherDisplayOrientationFixture |
| 5000 | : public InputDispatcherDisplayProjectionTest, |
| 5001 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 5002 | |
| 5003 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 5004 | // in different locations on the display, specifically points close to the four corners of a |
| 5005 | // window. |
| 5006 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 5007 | constexpr static int32_t displayWidth = 400; |
| 5008 | constexpr static int32_t displayHeight = 800; |
| 5009 | |
| 5010 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5011 | |
| 5012 | const auto rotation = GetParam(); |
| 5013 | |
| 5014 | // Set up the display with the specified rotation. |
| 5015 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5016 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5017 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5018 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5019 | logicalDisplayWidth, logicalDisplayHeight); |
| 5020 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5021 | |
| 5022 | // Create a window with its bounds determined in the logical display. |
| 5023 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 5024 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 5025 | sp<FakeWindowHandle> window = |
| 5026 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5027 | window->setFrame(frameInDisplay, displayTransform); |
| 5028 | addWindow(window); |
| 5029 | |
| 5030 | // The following points in logical display space should be inside the window. |
| 5031 | static const std::array<vec2, 4> insidePoints{ |
| 5032 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5033 | for (const auto pointInsideWindow : insidePoints) { |
| 5034 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 5035 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5036 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5037 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5038 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5039 | window->consumeMotionDown(); |
| 5040 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5041 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5042 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5043 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5044 | window->consumeMotionUp(); |
| 5045 | } |
| 5046 | |
| 5047 | // The following points in logical display space should be outside the window. |
| 5048 | static const std::array<vec2, 5> outsidePoints{ |
| 5049 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5050 | for (const auto pointOutsideWindow : outsidePoints) { |
| 5051 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 5052 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5053 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5054 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5055 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5056 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5057 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5058 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5059 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5060 | } |
| 5061 | window->assertNoEvents(); |
| 5062 | } |
| 5063 | |
| 5064 | // Run the precision tests for all rotations. |
| 5065 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 5066 | InputDispatcherDisplayOrientationFixture, |
| 5067 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 5068 | ui::ROTATION_270), |
| 5069 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 5070 | return ftl::enum_string(testParamInfo.param); |
| 5071 | }); |
| 5072 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5073 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5074 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5075 | |
| 5076 | class TransferTouchFixture : public InputDispatcherTest, |
| 5077 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 5078 | |
| 5079 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5080 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5081 | |
| 5082 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5083 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5084 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5085 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5086 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5087 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5088 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5089 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5090 | sp<FakeWindowHandle> wallpaper = |
| 5091 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 5092 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5093 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5094 | mDispatcher->onWindowInfosChanged( |
| 5095 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5096 | |
| 5097 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5098 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5099 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5100 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5101 | // Only the first window should get the down event |
| 5102 | firstWindow->consumeMotionDown(); |
| 5103 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5104 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5105 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5106 | // Transfer touch to the second window |
| 5107 | TransferFunction f = GetParam(); |
| 5108 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5109 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5110 | // The first window gets cancel and the second gets down |
| 5111 | firstWindow->consumeMotionCancel(); |
| 5112 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5113 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5114 | |
| 5115 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5116 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5117 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5118 | // The first window gets no events and the second gets up |
| 5119 | firstWindow->assertNoEvents(); |
| 5120 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5121 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5122 | } |
| 5123 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5124 | /** |
| 5125 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 5126 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 5127 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 5128 | * natural to the user. |
| 5129 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 5130 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 5131 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 5132 | * the first window to the second. |
| 5133 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 5134 | * the other API, as well. |
| 5135 | */ |
| 5136 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 5137 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5138 | |
| 5139 | // Create a couple of windows + a spy window |
| 5140 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5141 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5142 | spyWindow->setTrustedOverlay(true); |
| 5143 | spyWindow->setSpy(true); |
| 5144 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5145 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5146 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5147 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5148 | |
| 5149 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5150 | mDispatcher->onWindowInfosChanged( |
| 5151 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5152 | |
| 5153 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5154 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5155 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5156 | // Only the first window and spy should get the down event |
| 5157 | spyWindow->consumeMotionDown(); |
| 5158 | firstWindow->consumeMotionDown(); |
| 5159 | |
| 5160 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 5161 | // if f === 'transferTouch'. |
| 5162 | TransferFunction f = GetParam(); |
| 5163 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5164 | ASSERT_TRUE(success); |
| 5165 | // The first window gets cancel and the second gets down |
| 5166 | firstWindow->consumeMotionCancel(); |
| 5167 | secondWindow->consumeMotionDown(); |
| 5168 | |
| 5169 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5170 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5171 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5172 | // The first window gets no events and the second+spy get up |
| 5173 | firstWindow->assertNoEvents(); |
| 5174 | spyWindow->consumeMotionUp(); |
| 5175 | secondWindow->consumeMotionUp(); |
| 5176 | } |
| 5177 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5178 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5179 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5180 | |
| 5181 | PointF touchPoint = {10, 10}; |
| 5182 | |
| 5183 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5184 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5185 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5186 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5187 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5188 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5189 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5190 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5191 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5192 | |
| 5193 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5194 | mDispatcher->onWindowInfosChanged( |
| 5195 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5196 | |
| 5197 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5198 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5199 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5200 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5201 | // Only the first window should get the down event |
| 5202 | firstWindow->consumeMotionDown(); |
| 5203 | secondWindow->assertNoEvents(); |
| 5204 | |
| 5205 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5206 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5207 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5208 | // Only the first window should get the pointer down event |
| 5209 | firstWindow->consumeMotionPointerDown(1); |
| 5210 | secondWindow->assertNoEvents(); |
| 5211 | |
| 5212 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5213 | TransferFunction f = GetParam(); |
| 5214 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5215 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5216 | // The first window gets cancel and the second gets down and pointer down |
| 5217 | firstWindow->consumeMotionCancel(); |
| 5218 | secondWindow->consumeMotionDown(); |
| 5219 | secondWindow->consumeMotionPointerDown(1); |
| 5220 | |
| 5221 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5222 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5223 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5224 | // The first window gets nothing and the second gets pointer up |
| 5225 | firstWindow->assertNoEvents(); |
| 5226 | secondWindow->consumeMotionPointerUp(1); |
| 5227 | |
| 5228 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5229 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5230 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5231 | // The first window gets nothing and the second gets up |
| 5232 | firstWindow->assertNoEvents(); |
| 5233 | secondWindow->consumeMotionUp(); |
| 5234 | } |
| 5235 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5236 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 5237 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5238 | |
| 5239 | // Create a couple of windows |
| 5240 | sp<FakeWindowHandle> firstWindow = |
| 5241 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5242 | ADISPLAY_ID_DEFAULT); |
| 5243 | firstWindow->setDupTouchToWallpaper(true); |
| 5244 | sp<FakeWindowHandle> secondWindow = |
| 5245 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5246 | ADISPLAY_ID_DEFAULT); |
| 5247 | secondWindow->setDupTouchToWallpaper(true); |
| 5248 | |
| 5249 | sp<FakeWindowHandle> wallpaper1 = |
| 5250 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 5251 | wallpaper1->setIsWallpaper(true); |
| 5252 | |
| 5253 | sp<FakeWindowHandle> wallpaper2 = |
| 5254 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 5255 | wallpaper2->setIsWallpaper(true); |
| 5256 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5257 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 5258 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 5259 | {}, |
| 5260 | 0, |
| 5261 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5262 | |
| 5263 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5264 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5265 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5266 | |
| 5267 | // Only the first window should get the down event |
| 5268 | firstWindow->consumeMotionDown(); |
| 5269 | secondWindow->assertNoEvents(); |
| 5270 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5271 | wallpaper2->assertNoEvents(); |
| 5272 | |
| 5273 | // Transfer touch focus to the second window |
| 5274 | TransferFunction f = GetParam(); |
| 5275 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5276 | ASSERT_TRUE(success); |
| 5277 | |
| 5278 | // The first window gets cancel and the second gets down |
| 5279 | firstWindow->consumeMotionCancel(); |
| 5280 | secondWindow->consumeMotionDown(); |
| 5281 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5282 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5283 | |
| 5284 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5285 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5286 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5287 | // The first window gets no events and the second gets up |
| 5288 | firstWindow->assertNoEvents(); |
| 5289 | secondWindow->consumeMotionUp(); |
| 5290 | wallpaper1->assertNoEvents(); |
| 5291 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5292 | } |
| 5293 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5294 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 5295 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 5296 | // for the case where there are multiple pointers split across several windows. |
| 5297 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 5298 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5299 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5300 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5301 | return dispatcher->transferTouch(destChannelToken, |
| 5302 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5303 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5304 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5305 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5306 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5307 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5308 | })); |
| 5309 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5310 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5311 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5312 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5313 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5314 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5315 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5316 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5317 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5318 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5319 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5320 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5321 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5322 | |
| 5323 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5324 | mDispatcher->onWindowInfosChanged( |
| 5325 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5326 | |
| 5327 | PointF pointInFirst = {300, 200}; |
| 5328 | PointF pointInSecond = {300, 600}; |
| 5329 | |
| 5330 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5331 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5332 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5333 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5334 | // Only the first window should get the down event |
| 5335 | firstWindow->consumeMotionDown(); |
| 5336 | secondWindow->assertNoEvents(); |
| 5337 | |
| 5338 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5339 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5340 | ADISPLAY_ID_DEFAULT, |
| 5341 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5342 | // The first window gets a move and the second a down |
| 5343 | firstWindow->consumeMotionMove(); |
| 5344 | secondWindow->consumeMotionDown(); |
| 5345 | |
| 5346 | // Transfer touch focus to the second window |
| 5347 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5348 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 5349 | firstWindow->consumeMotionCancel(); |
| 5350 | secondWindow->consumeMotionPointerDown(1); |
| 5351 | |
| 5352 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5353 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5354 | ADISPLAY_ID_DEFAULT, |
| 5355 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5356 | // The first window gets nothing and the second gets pointer up |
| 5357 | firstWindow->assertNoEvents(); |
| 5358 | secondWindow->consumeMotionPointerUp(1); |
| 5359 | |
| 5360 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5361 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5362 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5363 | // The first window gets nothing and the second gets up |
| 5364 | firstWindow->assertNoEvents(); |
| 5365 | secondWindow->consumeMotionUp(); |
| 5366 | } |
| 5367 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5368 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 5369 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 5370 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 5371 | // window should get nothing. |
| 5372 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 5373 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5374 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5375 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5376 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5377 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5378 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5379 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5380 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5381 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5382 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5383 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5384 | |
| 5385 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5386 | mDispatcher->onWindowInfosChanged( |
| 5387 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5388 | |
| 5389 | PointF pointInFirst = {300, 200}; |
| 5390 | PointF pointInSecond = {300, 600}; |
| 5391 | |
| 5392 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5393 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5394 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5395 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5396 | // Only the first window should get the down event |
| 5397 | firstWindow->consumeMotionDown(); |
| 5398 | secondWindow->assertNoEvents(); |
| 5399 | |
| 5400 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5401 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5402 | ADISPLAY_ID_DEFAULT, |
| 5403 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5404 | // The first window gets a move and the second a down |
| 5405 | firstWindow->consumeMotionMove(); |
| 5406 | secondWindow->consumeMotionDown(); |
| 5407 | |
| 5408 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5409 | const bool transferred = |
| 5410 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5411 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 5412 | ASSERT_FALSE(transferred); |
| 5413 | firstWindow->assertNoEvents(); |
| 5414 | secondWindow->assertNoEvents(); |
| 5415 | |
| 5416 | // The rest of the dispatch should proceed as normal |
| 5417 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5418 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5419 | ADISPLAY_ID_DEFAULT, |
| 5420 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5421 | // The first window gets MOVE and the second gets pointer up |
| 5422 | firstWindow->consumeMotionMove(); |
| 5423 | secondWindow->consumeMotionUp(); |
| 5424 | |
| 5425 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5426 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5427 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5428 | // The first window gets nothing and the second gets up |
| 5429 | firstWindow->consumeMotionUp(); |
| 5430 | secondWindow->assertNoEvents(); |
| 5431 | } |
| 5432 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5433 | // This case will create two windows and one mirrored window on the default display and mirror |
| 5434 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 5435 | // the windows info of second display before default display. |
| 5436 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 5437 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5438 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5439 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5440 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5441 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5442 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5443 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5444 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5445 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5446 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5447 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5448 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5449 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5450 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5451 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5452 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5453 | |
| 5454 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5455 | mDispatcher->onWindowInfosChanged( |
| 5456 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5457 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5458 | *secondWindowInPrimary->getInfo()}, |
| 5459 | {}, |
| 5460 | 0, |
| 5461 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5462 | |
| 5463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5464 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5465 | {50, 50})) |
| 5466 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5467 | |
| 5468 | // Window should receive motion event. |
| 5469 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5470 | |
| 5471 | // Transfer touch focus |
| 5472 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 5473 | secondWindowInPrimary->getToken())); |
| 5474 | // The first window gets cancel. |
| 5475 | firstWindowInPrimary->consumeMotionCancel(); |
| 5476 | secondWindowInPrimary->consumeMotionDown(); |
| 5477 | |
| 5478 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5479 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5480 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5481 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5482 | firstWindowInPrimary->assertNoEvents(); |
| 5483 | secondWindowInPrimary->consumeMotionMove(); |
| 5484 | |
| 5485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5486 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5487 | {150, 50})) |
| 5488 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5489 | firstWindowInPrimary->assertNoEvents(); |
| 5490 | secondWindowInPrimary->consumeMotionUp(); |
| 5491 | } |
| 5492 | |
| 5493 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 5494 | // 'transferTouch' api. |
| 5495 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 5496 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5497 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5498 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5499 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5500 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5501 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5502 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5503 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5504 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5505 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5506 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5507 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5508 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5509 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5510 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5511 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5512 | |
| 5513 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5514 | mDispatcher->onWindowInfosChanged( |
| 5515 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5516 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5517 | *secondWindowInPrimary->getInfo()}, |
| 5518 | {}, |
| 5519 | 0, |
| 5520 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5521 | |
| 5522 | // Touch on second display. |
| 5523 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5524 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5525 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5526 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5527 | |
| 5528 | // Window should receive motion event. |
| 5529 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5530 | |
| 5531 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5532 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5533 | |
| 5534 | // The first window gets cancel. |
| 5535 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5536 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5537 | |
| 5538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5539 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5540 | SECOND_DISPLAY_ID, {150, 50})) |
| 5541 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5542 | firstWindowInPrimary->assertNoEvents(); |
| 5543 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 5544 | |
| 5545 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5546 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5547 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5548 | firstWindowInPrimary->assertNoEvents(); |
| 5549 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5550 | } |
| 5551 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5552 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5553 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5554 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5555 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5556 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5557 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5558 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5559 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5560 | |
| 5561 | window->consumeFocusEvent(true); |
| 5562 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5563 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5564 | |
| 5565 | // Window should receive key down event. |
| 5566 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5567 | |
| 5568 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5569 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5570 | mFakePolicy->assertUserActivityPoked(); |
| 5571 | } |
| 5572 | |
| 5573 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 5574 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5575 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5576 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5577 | |
| 5578 | window->setDisableUserActivity(true); |
| 5579 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5580 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5581 | setFocusedWindow(window); |
| 5582 | |
| 5583 | window->consumeFocusEvent(true); |
| 5584 | |
| 5585 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5586 | |
| 5587 | // Window should receive key down event. |
| 5588 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5589 | |
| 5590 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5591 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5592 | mFakePolicy->assertUserActivityNotPoked(); |
| 5593 | } |
| 5594 | |
| 5595 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 5596 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5597 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5598 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5599 | |
| 5600 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5601 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5602 | setFocusedWindow(window); |
| 5603 | |
| 5604 | window->consumeFocusEvent(true); |
| 5605 | |
| 5606 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5607 | mDispatcher->waitForIdle(); |
| 5608 | |
| 5609 | // System key is not passed down |
| 5610 | window->assertNoEvents(); |
| 5611 | |
| 5612 | // Should have poked user activity |
| 5613 | mFakePolicy->assertUserActivityPoked(); |
| 5614 | } |
| 5615 | |
| 5616 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 5617 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5618 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5619 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5620 | |
| 5621 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5622 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5623 | setFocusedWindow(window); |
| 5624 | |
| 5625 | window->consumeFocusEvent(true); |
| 5626 | |
| 5627 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5628 | mDispatcher->waitForIdle(); |
| 5629 | |
| 5630 | // System key is not passed down |
| 5631 | window->assertNoEvents(); |
| 5632 | |
| 5633 | // Should have poked user activity |
| 5634 | mFakePolicy->assertUserActivityPoked(); |
| 5635 | } |
| 5636 | |
| 5637 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 5638 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5639 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5640 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5641 | |
| 5642 | window->setDisableUserActivity(true); |
| 5643 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5644 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5645 | setFocusedWindow(window); |
| 5646 | |
| 5647 | window->consumeFocusEvent(true); |
| 5648 | |
| 5649 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5650 | mDispatcher->waitForIdle(); |
| 5651 | |
| 5652 | // System key is not passed down |
| 5653 | window->assertNoEvents(); |
| 5654 | |
| 5655 | // Should have poked user activity |
| 5656 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5657 | } |
| 5658 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5659 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 5660 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5661 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5662 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5663 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5664 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5665 | |
| 5666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5667 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5668 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 5669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5670 | |
| 5671 | window->consumeMotionEvent( |
| 5672 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5673 | |
| 5674 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5675 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5676 | mFakePolicy->assertUserActivityPoked(); |
| 5677 | } |
| 5678 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5679 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5680 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5681 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5682 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5683 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5684 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5685 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5686 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5687 | mDispatcher->waitForIdle(); |
| 5688 | |
| 5689 | window->assertNoEvents(); |
| 5690 | } |
| 5691 | |
| 5692 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 5693 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5694 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5695 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5696 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5697 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5698 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5699 | |
| 5700 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5701 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5702 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5703 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5704 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5705 | |
| 5706 | // Window should receive only the motion event |
| 5707 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5708 | window->assertNoEvents(); // Key event or focus event will not be received |
| 5709 | } |
| 5710 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5711 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 5712 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5713 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5714 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5715 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5716 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5717 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5718 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5719 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5720 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5721 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5722 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5723 | |
| 5724 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5725 | mDispatcher->onWindowInfosChanged( |
| 5726 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5727 | |
| 5728 | PointF pointInFirst = {300, 200}; |
| 5729 | PointF pointInSecond = {300, 600}; |
| 5730 | |
| 5731 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5732 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5733 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5734 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5735 | // Only the first window should get the down event |
| 5736 | firstWindow->consumeMotionDown(); |
| 5737 | secondWindow->assertNoEvents(); |
| 5738 | |
| 5739 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5740 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5741 | ADISPLAY_ID_DEFAULT, |
| 5742 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5743 | // The first window gets a move and the second a down |
| 5744 | firstWindow->consumeMotionMove(); |
| 5745 | secondWindow->consumeMotionDown(); |
| 5746 | |
| 5747 | // Send pointer cancel to the second window |
| 5748 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5749 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5750 | {pointInFirst, pointInSecond}); |
| 5751 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5752 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5753 | // The first window gets move and the second gets cancel. |
| 5754 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5755 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5756 | |
| 5757 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5758 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5759 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5760 | // The first window gets up and the second gets nothing. |
| 5761 | firstWindow->consumeMotionUp(); |
| 5762 | secondWindow->assertNoEvents(); |
| 5763 | } |
| 5764 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5765 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 5766 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5767 | |
| 5768 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5769 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5770 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5771 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 5772 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 5773 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 5774 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5775 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5776 | window->assertNoEvents(); |
| 5777 | mDispatcher->waitForIdle(); |
| 5778 | } |
| 5779 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5780 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 5781 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5782 | /** |
| 5783 | * Two entities that receive touch: A window, and a global monitor. |
| 5784 | * The touch goes to the window, and then the window disappears. |
| 5785 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 5786 | * for the monitor, as well. |
| 5787 | * 1. foregroundWindow |
| 5788 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 5789 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5790 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5791 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5792 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5793 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5794 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5795 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5796 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5797 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5798 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5799 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5800 | {100, 200})) |
| 5801 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5802 | |
| 5803 | // Both the foreground window and the global monitor should receive the touch down |
| 5804 | window->consumeMotionDown(); |
| 5805 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5806 | |
| 5807 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5808 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5809 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5810 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5811 | |
| 5812 | window->consumeMotionMove(); |
| 5813 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5814 | |
| 5815 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5816 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5817 | window->consumeMotionCancel(); |
| 5818 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 5819 | |
| 5820 | // If more events come in, there will be no more foreground window to send them to. This will |
| 5821 | // cause a cancel for the monitor, as well. |
| 5822 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5823 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5824 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 5825 | << "Injection should fail because the window was removed"; |
| 5826 | window->assertNoEvents(); |
| 5827 | // Global monitor now gets the cancel |
| 5828 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5829 | } |
| 5830 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5831 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5832 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5833 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5834 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5835 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5836 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5837 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5838 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5840 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5841 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5842 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5843 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5844 | } |
| 5845 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5846 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5847 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5848 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5849 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5850 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5851 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5852 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5853 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5854 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5855 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5856 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5857 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5858 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5859 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5860 | // Pilfer pointers from the monitor. |
| 5861 | // This should not do anything and the window should continue to receive events. |
| 5862 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5863 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5864 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5865 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5866 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5867 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5868 | |
| 5869 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5870 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5871 | } |
| 5872 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5873 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5874 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5875 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5876 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5877 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5878 | window->setWindowOffset(20, 40); |
| 5879 | window->setWindowTransform(0, 1, -1, 0); |
| 5880 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5881 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5882 | |
| 5883 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5884 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5885 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5886 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5887 | MotionEvent* event = monitor.consumeMotion(); |
| 5888 | // Even though window has transform, gesture monitor must not. |
| 5889 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 5890 | } |
| 5891 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5892 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5893 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5894 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5895 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5896 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5897 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5898 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 5899 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5900 | } |
| 5901 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5902 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5903 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5904 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5905 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5906 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5907 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5908 | |
| 5909 | NotifyMotionArgs motionArgs = |
| 5910 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5911 | ADISPLAY_ID_DEFAULT); |
| 5912 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5913 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5914 | // Window should receive motion down event. |
| 5915 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5916 | |
| 5917 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5918 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5919 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5920 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5921 | motionArgs.pointerCoords[0].getX() - 10); |
| 5922 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5923 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5924 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5925 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5926 | } |
| 5927 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5928 | /** |
| 5929 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 5930 | * the device default right away. In the test scenario, we check both the default value, |
| 5931 | * and the action of enabling / disabling. |
| 5932 | */ |
| 5933 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5934 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5935 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5936 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5937 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5938 | |
| 5939 | // Set focused application. |
| 5940 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5941 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5942 | |
| 5943 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5944 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5945 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5946 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5947 | |
| 5948 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5949 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5950 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5951 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5952 | |
| 5953 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5954 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5955 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5956 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5957 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5958 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5959 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5960 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5961 | |
| 5962 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5963 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5964 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5965 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5966 | |
| 5967 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5968 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5969 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5970 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5971 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5972 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5973 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5974 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5975 | |
| 5976 | window->assertNoEvents(); |
| 5977 | } |
| 5978 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5979 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5980 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5981 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5982 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5983 | |
| 5984 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5985 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5986 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5987 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5988 | setFocusedWindow(window); |
| 5989 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5990 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5991 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5992 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5993 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5994 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5995 | KeyEvent* event = window->consumeKey(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5996 | ASSERT_NE(event, nullptr); |
| 5997 | |
| 5998 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5999 | ASSERT_NE(verified, nullptr); |
| 6000 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 6001 | |
| 6002 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 6003 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 6004 | ASSERT_EQ(keyArgs.source, verified->source); |
| 6005 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 6006 | |
| 6007 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 6008 | |
| 6009 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6010 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6011 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6012 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 6013 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 6014 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 6015 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 6016 | } |
| 6017 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6018 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6019 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6020 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6021 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6022 | |
| 6023 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6024 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6025 | ui::Transform transform; |
| 6026 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6027 | |
| 6028 | gui::DisplayInfo displayInfo; |
| 6029 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6030 | displayInfo.transform = transform; |
| 6031 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6032 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6033 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6034 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6035 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6036 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6037 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6038 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6039 | MotionEvent* event = window->consumeMotion(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6040 | ASSERT_NE(event, nullptr); |
| 6041 | |
| 6042 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 6043 | ASSERT_NE(verified, nullptr); |
| 6044 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 6045 | |
| 6046 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 6047 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 6048 | EXPECT_EQ(motionArgs.source, verified->source); |
| 6049 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 6050 | |
| 6051 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 6052 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6053 | const vec2 rawXY = |
| 6054 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 6055 | motionArgs.pointerCoords[0].getXYValue()); |
| 6056 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 6057 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6058 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6059 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6060 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6061 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 6062 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 6063 | } |
| 6064 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 6065 | /** |
| 6066 | * Ensure that separate calls to sign the same data are generating the same key. |
| 6067 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 6068 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 6069 | * tests. |
| 6070 | */ |
| 6071 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 6072 | KeyEvent event = getTestKeyEvent(); |
| 6073 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6074 | |
| 6075 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 6076 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 6077 | ASSERT_EQ(hmac1, hmac2); |
| 6078 | } |
| 6079 | |
| 6080 | /** |
| 6081 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 6082 | */ |
| 6083 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 6084 | KeyEvent event = getTestKeyEvent(); |
| 6085 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6086 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 6087 | |
| 6088 | verifiedEvent.deviceId += 1; |
| 6089 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6090 | |
| 6091 | verifiedEvent.source += 1; |
| 6092 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6093 | |
| 6094 | verifiedEvent.eventTimeNanos += 1; |
| 6095 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6096 | |
| 6097 | verifiedEvent.displayId += 1; |
| 6098 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6099 | |
| 6100 | verifiedEvent.action += 1; |
| 6101 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6102 | |
| 6103 | verifiedEvent.downTimeNanos += 1; |
| 6104 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6105 | |
| 6106 | verifiedEvent.flags += 1; |
| 6107 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6108 | |
| 6109 | verifiedEvent.keyCode += 1; |
| 6110 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6111 | |
| 6112 | verifiedEvent.scanCode += 1; |
| 6113 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6114 | |
| 6115 | verifiedEvent.metaState += 1; |
| 6116 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6117 | |
| 6118 | verifiedEvent.repeatCount += 1; |
| 6119 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6120 | } |
| 6121 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6122 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 6123 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6124 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6125 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6126 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6127 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6128 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6129 | |
| 6130 | // Top window is also focusable but is not granted focus. |
| 6131 | windowTop->setFocusable(true); |
| 6132 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6133 | mDispatcher->onWindowInfosChanged( |
| 6134 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6135 | setFocusedWindow(windowSecond); |
| 6136 | |
| 6137 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6138 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6139 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6140 | |
| 6141 | // Focused window should receive event. |
| 6142 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6143 | windowTop->assertNoEvents(); |
| 6144 | } |
| 6145 | |
| 6146 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 6147 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6148 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6149 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6150 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6151 | |
| 6152 | window->setFocusable(true); |
| 6153 | // Release channel for window is no longer valid. |
| 6154 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6155 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6156 | setFocusedWindow(window); |
| 6157 | |
| 6158 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6159 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6160 | |
| 6161 | // window channel is invalid, so it should not receive any input event. |
| 6162 | window->assertNoEvents(); |
| 6163 | } |
| 6164 | |
| 6165 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 6166 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6167 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6168 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6169 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6170 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6171 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6172 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6173 | setFocusedWindow(window); |
| 6174 | |
| 6175 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6176 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6177 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6178 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6179 | window->assertNoEvents(); |
| 6180 | } |
| 6181 | |
| 6182 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 6183 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6184 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6185 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6186 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6187 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6188 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6189 | |
| 6190 | windowTop->setFocusable(true); |
| 6191 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6192 | mDispatcher->onWindowInfosChanged( |
| 6193 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6194 | setFocusedWindow(windowTop); |
| 6195 | windowTop->consumeFocusEvent(true); |
| 6196 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6197 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6198 | mDispatcher->onWindowInfosChanged( |
| 6199 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6200 | windowSecond->consumeFocusEvent(true); |
| 6201 | windowTop->consumeFocusEvent(false); |
| 6202 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6204 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6205 | |
| 6206 | // Focused window should receive event. |
| 6207 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6208 | } |
| 6209 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6210 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6211 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6212 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6213 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6214 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6215 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6216 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6217 | |
| 6218 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6219 | windowSecond->setFocusable(false); |
| 6220 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6221 | mDispatcher->onWindowInfosChanged( |
| 6222 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6223 | setFocusedWindow(windowTop); |
| 6224 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6225 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6227 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6228 | |
| 6229 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6230 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6231 | windowSecond->assertNoEvents(); |
| 6232 | } |
| 6233 | |
| 6234 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 6235 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6236 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6237 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6238 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6239 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 6240 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6241 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6242 | |
| 6243 | window->setFocusable(true); |
| 6244 | previousFocusedWindow->setFocusable(true); |
| 6245 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6246 | mDispatcher->onWindowInfosChanged( |
| 6247 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6248 | setFocusedWindow(previousFocusedWindow); |
| 6249 | previousFocusedWindow->consumeFocusEvent(true); |
| 6250 | |
| 6251 | // Requesting focus on invisible window takes focus from currently focused window. |
| 6252 | setFocusedWindow(window); |
| 6253 | previousFocusedWindow->consumeFocusEvent(false); |
| 6254 | |
| 6255 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6257 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 6258 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6259 | |
| 6260 | // Window does not get focus event or key down. |
| 6261 | window->assertNoEvents(); |
| 6262 | |
| 6263 | // Window becomes visible. |
| 6264 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6265 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6266 | |
| 6267 | // Window receives focus event. |
| 6268 | window->consumeFocusEvent(true); |
| 6269 | // Focused window receives key down. |
| 6270 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6271 | } |
| 6272 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6273 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 6274 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6275 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6276 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6277 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6278 | |
| 6279 | // window is granted focus. |
| 6280 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6281 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6282 | setFocusedWindow(window); |
| 6283 | window->consumeFocusEvent(true); |
| 6284 | |
| 6285 | // When a display is removed window loses focus. |
| 6286 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 6287 | window->consumeFocusEvent(false); |
| 6288 | } |
| 6289 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6290 | /** |
| 6291 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 6292 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 6293 | * of the 'slipperyEnterWindow'. |
| 6294 | * |
| 6295 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 6296 | * a way so that the touched location is no longer covered by the top window. |
| 6297 | * |
| 6298 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 6299 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 6300 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 6301 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 6302 | * with ACTION_DOWN). |
| 6303 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 6304 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 6305 | * |
| 6306 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 6307 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 6308 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 6309 | * |
| 6310 | * In this test, we ensure that the event received by the bottom window has |
| 6311 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 6312 | */ |
| 6313 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6314 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6315 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6316 | |
| 6317 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6318 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6319 | |
| 6320 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6321 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6322 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6323 | // Make sure this one overlaps the bottom window |
| 6324 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 6325 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 6326 | // one. Windows with the same owner are not considered to be occluding each other. |
| 6327 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 6328 | |
| 6329 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6330 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6331 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6332 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6333 | mDispatcher->onWindowInfosChanged( |
| 6334 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6335 | |
| 6336 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6337 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6338 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6339 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6340 | slipperyExitWindow->consumeMotionDown(); |
| 6341 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6342 | mDispatcher->onWindowInfosChanged( |
| 6343 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6344 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6345 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 6346 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6347 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6348 | |
| 6349 | slipperyExitWindow->consumeMotionCancel(); |
| 6350 | |
| 6351 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 6352 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6353 | } |
| 6354 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6355 | /** |
| 6356 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 6357 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 6358 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 6359 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 6360 | * reproduces a crash. |
| 6361 | */ |
| 6362 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 6363 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6364 | |
| 6365 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 6366 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6367 | leftSlipperyWindow->setSlippery(true); |
| 6368 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6369 | |
| 6370 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 6371 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6372 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6373 | rightDropTouchesWindow->setDropInput(true); |
| 6374 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6375 | mDispatcher->onWindowInfosChanged( |
| 6376 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6377 | |
| 6378 | // Start touch in the left window |
| 6379 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6380 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6381 | .build()); |
| 6382 | leftSlipperyWindow->consumeMotionDown(); |
| 6383 | |
| 6384 | // And move it into the right window |
| 6385 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6386 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6387 | .build()); |
| 6388 | |
| 6389 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 6390 | // The left window continues to receive the gesture. |
| 6391 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6392 | rightDropTouchesWindow->assertNoEvents(); |
| 6393 | } |
| 6394 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 6395 | /** |
| 6396 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 6397 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 6398 | */ |
| 6399 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 6400 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6401 | sp<FakeWindowHandle> originalWindow = |
| 6402 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT); |
| 6403 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6404 | originalWindow->setSlippery(true); |
| 6405 | |
| 6406 | sp<FakeWindowHandle> appearingWindow = |
| 6407 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT); |
| 6408 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6409 | |
| 6410 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 6411 | |
| 6412 | // Touch down on the original window |
| 6413 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6414 | injectMotionEvent(*mDispatcher, |
| 6415 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6416 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 6417 | .build())); |
| 6418 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6419 | |
| 6420 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 6421 | // after user starts to drag down on the launcher window. |
| 6422 | mDispatcher->onWindowInfosChanged( |
| 6423 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 6424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6425 | injectMotionEvent(*mDispatcher, |
| 6426 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6427 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 6428 | .build())); |
| 6429 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 6430 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6431 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6432 | injectMotionEvent(*mDispatcher, |
| 6433 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6434 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6435 | .build())); |
| 6436 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6437 | |
| 6438 | originalWindow->assertNoEvents(); |
| 6439 | appearingWindow->assertNoEvents(); |
| 6440 | } |
| 6441 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6442 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6443 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6444 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6445 | |
| 6446 | sp<FakeWindowHandle> leftWindow = |
| 6447 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6448 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6449 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6450 | |
| 6451 | sp<FakeWindowHandle> rightSpy = |
| 6452 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 6453 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6454 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6455 | rightSpy->setSpy(true); |
| 6456 | rightSpy->setTrustedOverlay(true); |
| 6457 | |
| 6458 | sp<FakeWindowHandle> rightWindow = |
| 6459 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6460 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6461 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6462 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6463 | mDispatcher->onWindowInfosChanged( |
| 6464 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6465 | |
| 6466 | // Touch in the left window |
| 6467 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6468 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6469 | .build()); |
| 6470 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 6471 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6472 | ASSERT_NO_FATAL_FAILURE( |
| 6473 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6474 | |
| 6475 | // Touch another finger over the right windows |
| 6476 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6477 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6478 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6479 | .build()); |
| 6480 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 6481 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 6482 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 6483 | mDispatcher->waitForIdle(); |
| 6484 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6485 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 6486 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6487 | |
| 6488 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 6489 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 6490 | // is part of the UP action, we do not treat this as device interaction. |
| 6491 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6492 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6493 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6494 | .build()); |
| 6495 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 6496 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6497 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6498 | mDispatcher->waitForIdle(); |
| 6499 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6500 | |
| 6501 | // Move remaining finger |
| 6502 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6503 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6504 | .build()); |
| 6505 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6506 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6507 | mDispatcher->waitForIdle(); |
| 6508 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6509 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6510 | |
| 6511 | // Release all fingers |
| 6512 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6513 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6514 | .build()); |
| 6515 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 6516 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 6517 | mDispatcher->waitForIdle(); |
| 6518 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6519 | } |
| 6520 | |
| 6521 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 6522 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6523 | |
| 6524 | sp<FakeWindowHandle> window = |
| 6525 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 6526 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6527 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6528 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6529 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6530 | setFocusedWindow(window); |
| 6531 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 6532 | |
| 6533 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 6534 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 6535 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6536 | ASSERT_NO_FATAL_FAILURE( |
| 6537 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6538 | |
| 6539 | // The UP actions are not treated as device interaction. |
| 6540 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 6541 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 6542 | mDispatcher->waitForIdle(); |
| 6543 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6544 | } |
| 6545 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 6546 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 6547 | protected: |
| 6548 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6549 | sp<FakeWindowHandle> mWindow; |
| 6550 | |
| 6551 | virtual void SetUp() override { |
| 6552 | InputDispatcherTest::SetUp(); |
| 6553 | |
| 6554 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 6555 | |
| 6556 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 6557 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6558 | |
| 6559 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 6560 | setFocusedWindow(mWindow); |
| 6561 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 6562 | } |
| 6563 | |
| 6564 | void setFallback(int32_t keycode) { |
| 6565 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 6566 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 6567 | }); |
| 6568 | } |
| 6569 | |
| 6570 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
| 6571 | KeyEvent* event = mWindow->consumeKey(handled); |
| 6572 | ASSERT_NE(event, nullptr) << "Did not receive key event"; |
| 6573 | ASSERT_THAT(*event, matcher); |
| 6574 | } |
| 6575 | }; |
| 6576 | |
| 6577 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 6578 | mDispatcher->notifyKey( |
| 6579 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6580 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 6581 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6582 | } |
| 6583 | |
| 6584 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 6585 | mDispatcher->notifyKey( |
| 6586 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6587 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 6588 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6589 | } |
| 6590 | |
| 6591 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 6592 | mDispatcher->notifyKey( |
| 6593 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6594 | |
| 6595 | // Do not handle this key event. |
| 6596 | consumeKey(/*handled=*/false, |
| 6597 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6598 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6599 | |
| 6600 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 6601 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6602 | } |
| 6603 | |
| 6604 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 6605 | setFallback(AKEYCODE_B); |
| 6606 | mDispatcher->notifyKey( |
| 6607 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6608 | |
| 6609 | // Do not handle this key event. |
| 6610 | consumeKey(/*handled=*/false, |
| 6611 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6612 | |
| 6613 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 6614 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6615 | consumeKey(/*handled=*/true, |
| 6616 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6617 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6618 | |
| 6619 | // Release the original key, and ensure the fallback key is also released. |
| 6620 | mDispatcher->notifyKey( |
| 6621 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6622 | consumeKey(/*handled=*/false, |
| 6623 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6624 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6625 | consumeKey(/*handled=*/true, |
| 6626 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6627 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6628 | |
| 6629 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6630 | mWindow->assertNoEvents(); |
| 6631 | } |
| 6632 | |
| 6633 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 6634 | setFallback(AKEYCODE_B); |
| 6635 | mDispatcher->notifyKey( |
| 6636 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6637 | |
| 6638 | // Do not handle this key event, but handle the fallback. |
| 6639 | consumeKey(/*handled=*/false, |
| 6640 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6641 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6642 | consumeKey(/*handled=*/true, |
| 6643 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6644 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6645 | |
| 6646 | // Release the original key, and ensure the fallback key is also released. |
| 6647 | mDispatcher->notifyKey( |
| 6648 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6649 | // But this time, the app handles the original key. |
| 6650 | consumeKey(/*handled=*/true, |
| 6651 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6652 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6653 | // Ensure the fallback key is canceled. |
| 6654 | consumeKey(/*handled=*/true, |
| 6655 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6656 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6657 | |
| 6658 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6659 | mWindow->assertNoEvents(); |
| 6660 | } |
| 6661 | |
| 6662 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 6663 | setFallback(AKEYCODE_B); |
| 6664 | mDispatcher->notifyKey( |
| 6665 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6666 | |
| 6667 | // Do not handle this key event. |
| 6668 | consumeKey(/*handled=*/false, |
| 6669 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6670 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6671 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 6672 | setFallback(AKEYCODE_C); |
| 6673 | consumeKey(/*handled=*/false, |
| 6674 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6675 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6676 | |
| 6677 | // Release the original key, and ensure the fallback key is also released. |
| 6678 | setFallback(AKEYCODE_B); |
| 6679 | mDispatcher->notifyKey( |
| 6680 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6681 | consumeKey(/*handled=*/false, |
| 6682 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6683 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6684 | consumeKey(/*handled=*/false, |
| 6685 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6686 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6687 | |
| 6688 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6689 | mWindow->assertNoEvents(); |
| 6690 | } |
| 6691 | |
| 6692 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 6693 | setFallback(AKEYCODE_B); |
| 6694 | mDispatcher->notifyKey( |
| 6695 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6696 | |
| 6697 | // Do not handle this key event, so fallback is generated. |
| 6698 | consumeKey(/*handled=*/false, |
| 6699 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6700 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6701 | consumeKey(/*handled=*/true, |
| 6702 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6703 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6704 | |
| 6705 | // Release the original key, but assume the policy is misbehaving and it |
| 6706 | // generates an inconsistent fallback to the one from the DOWN event. |
| 6707 | setFallback(AKEYCODE_C); |
| 6708 | mDispatcher->notifyKey( |
| 6709 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6710 | consumeKey(/*handled=*/false, |
| 6711 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6712 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6713 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 6714 | consumeKey(/*handled=*/true, |
| 6715 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6716 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6717 | |
| 6718 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6719 | mWindow->assertNoEvents(); |
| 6720 | } |
| 6721 | |
| 6722 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 6723 | setFallback(AKEYCODE_B); |
| 6724 | mDispatcher->notifyKey( |
| 6725 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6726 | |
| 6727 | // Do not handle this key event, so fallback is generated. |
| 6728 | consumeKey(/*handled=*/false, |
| 6729 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6730 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6731 | consumeKey(/*handled=*/true, |
| 6732 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6733 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6734 | |
| 6735 | // The original key is canceled. |
| 6736 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 6737 | .keyCode(AKEYCODE_A) |
| 6738 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 6739 | .build()); |
| 6740 | consumeKey(/*handled=*/false, |
| 6741 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 6742 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 6743 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6744 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 6745 | consumeKey(/*handled=*/true, |
| 6746 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6747 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6748 | |
| 6749 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6750 | mWindow->assertNoEvents(); |
| 6751 | } |
| 6752 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6753 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 6754 | protected: |
| 6755 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 6756 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 6757 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6758 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6759 | sp<FakeWindowHandle> mWindow; |
| 6760 | |
| 6761 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6762 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6763 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6764 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 6765 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6766 | ASSERT_EQ(OK, mDispatcher->start()); |
| 6767 | |
| 6768 | setUpWindow(); |
| 6769 | } |
| 6770 | |
| 6771 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6772 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6773 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6774 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6775 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6776 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6777 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6778 | mWindow->consumeFocusEvent(true); |
| 6779 | } |
| 6780 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6781 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6782 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6783 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6784 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6785 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6786 | |
| 6787 | // Window should receive key down event. |
| 6788 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6789 | } |
| 6790 | |
| 6791 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 6792 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6793 | mWindow->consumeKeyEvent( |
| 6794 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6795 | } |
| 6796 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6797 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6798 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6799 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6800 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6801 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6802 | |
| 6803 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6804 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6805 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6806 | } |
| 6807 | }; |
| 6808 | |
| 6809 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6810 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6811 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 6812 | expectKeyRepeatOnce(repeatCount); |
| 6813 | } |
| 6814 | } |
| 6815 | |
| 6816 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6817 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6818 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 6819 | expectKeyRepeatOnce(repeatCount); |
| 6820 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6821 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6822 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6823 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 6824 | expectKeyRepeatOnce(repeatCount); |
| 6825 | } |
| 6826 | } |
| 6827 | |
| 6828 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6829 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 6830 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 6831 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6832 | mWindow->assertNoEvents(); |
| 6833 | } |
| 6834 | |
| 6835 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6836 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 6837 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 6838 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 6839 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6840 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6841 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6842 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6843 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 6844 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6845 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6846 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6847 | mWindow->assertNoEvents(); |
| 6848 | } |
| 6849 | |
| 6850 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6851 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 6852 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 6853 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 6854 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6855 | // 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] | 6856 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 6857 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6858 | mWindow->assertNoEvents(); |
| 6859 | } |
| 6860 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 6861 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 6862 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6863 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6864 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 6865 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 6866 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 6867 | mWindow->assertNoEvents(); |
| 6868 | } |
| 6869 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6870 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 6871 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6872 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6873 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6874 | KeyEvent* repeatEvent = mWindow->consumeKey(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6875 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 6876 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 6877 | IdGenerator::getSource(repeatEvent->getId())); |
| 6878 | } |
| 6879 | } |
| 6880 | |
| 6881 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 6882 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6883 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6884 | |
| 6885 | std::unordered_set<int32_t> idSet; |
| 6886 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6887 | KeyEvent* repeatEvent = mWindow->consumeKey(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6888 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 6889 | int32_t id = repeatEvent->getId(); |
| 6890 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 6891 | idSet.insert(id); |
| 6892 | } |
| 6893 | } |
| 6894 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6895 | /* Test InputDispatcher for MultiDisplay */ |
| 6896 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 6897 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6898 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6899 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6900 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6901 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6902 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6903 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6904 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6905 | // Set focus window for primary display, but focused display would be second one. |
| 6906 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6907 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6908 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 6909 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6910 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6911 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6912 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6913 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6914 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6915 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6916 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6917 | // Set focus display to second one. |
| 6918 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 6919 | // Set focus window for second display. |
| 6920 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6921 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6922 | mDispatcher->onWindowInfosChanged( |
| 6923 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6924 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6925 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6926 | } |
| 6927 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6928 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6929 | InputDispatcherTest::TearDown(); |
| 6930 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6931 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6932 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6933 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6934 | windowInSecondary.clear(); |
| 6935 | } |
| 6936 | |
| 6937 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6938 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6939 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6940 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6941 | sp<FakeWindowHandle> windowInSecondary; |
| 6942 | }; |
| 6943 | |
| 6944 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 6945 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6946 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6947 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6948 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6949 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6950 | windowInSecondary->assertNoEvents(); |
| 6951 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6952 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6953 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6954 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6955 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6956 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6957 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6958 | } |
| 6959 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6960 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 6961 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6962 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6963 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6964 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6965 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 6966 | windowInSecondary->assertNoEvents(); |
| 6967 | |
| 6968 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6970 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6971 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6972 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6973 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6974 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6975 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6976 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6977 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6978 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6979 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6980 | |
| 6981 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6982 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6983 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6984 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 6985 | windowInSecondary->assertNoEvents(); |
| 6986 | } |
| 6987 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6988 | // Test per-display input monitors for motion event. |
| 6989 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6990 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6991 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6992 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6993 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6994 | |
| 6995 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6996 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6997 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6998 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6999 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7000 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7001 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7002 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7003 | |
| 7004 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7005 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7006 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7007 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7008 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7009 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7010 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7011 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7012 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7013 | // Lift up the touch from the second display |
| 7014 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7015 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7016 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7017 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 7018 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 7019 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7020 | // Test inject a non-pointer motion event. |
| 7021 | // If specific a display, it will dispatch to the focused window of particular display, |
| 7022 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7023 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7024 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7025 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7026 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7027 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7028 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7029 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7030 | } |
| 7031 | |
| 7032 | // Test per-display input monitors for key event. |
| 7033 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7034 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7035 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7036 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7037 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7038 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7039 | |
| 7040 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7042 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7043 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7044 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7045 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7046 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7047 | } |
| 7048 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7049 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 7050 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7051 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7052 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7053 | mDispatcher->onWindowInfosChanged( |
| 7054 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 7055 | *windowInSecondary->getInfo()}, |
| 7056 | {}, |
| 7057 | 0, |
| 7058 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7059 | setFocusedWindow(secondWindowInPrimary); |
| 7060 | windowInPrimary->consumeFocusEvent(false); |
| 7061 | secondWindowInPrimary->consumeFocusEvent(true); |
| 7062 | |
| 7063 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7065 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7066 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7067 | windowInPrimary->assertNoEvents(); |
| 7068 | windowInSecondary->assertNoEvents(); |
| 7069 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7070 | } |
| 7071 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7072 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 7073 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7074 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7075 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7076 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7077 | |
| 7078 | // Test touch down on primary display. |
| 7079 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7080 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7081 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7082 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7083 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7084 | |
| 7085 | // Test touch down on second display. |
| 7086 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7087 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7088 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7089 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7090 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7091 | |
| 7092 | // Trigger cancel touch. |
| 7093 | mDispatcher->cancelCurrentTouch(); |
| 7094 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7095 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7096 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7097 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7098 | |
| 7099 | // Test inject a move motion event, no window/monitor should receive the event. |
| 7100 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7101 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7102 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 7103 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7104 | windowInPrimary->assertNoEvents(); |
| 7105 | monitorInPrimary.assertNoEvents(); |
| 7106 | |
| 7107 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7108 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7109 | SECOND_DISPLAY_ID, {110, 200})) |
| 7110 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7111 | windowInSecondary->assertNoEvents(); |
| 7112 | monitorInSecondary.assertNoEvents(); |
| 7113 | } |
| 7114 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7115 | class InputFilterTest : public InputDispatcherTest { |
| 7116 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7117 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 7118 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7119 | NotifyMotionArgs motionArgs; |
| 7120 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7121 | motionArgs = |
| 7122 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7123 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7124 | motionArgs = |
| 7125 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7126 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7127 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7128 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 7129 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7130 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7131 | } else { |
| 7132 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7133 | } |
| 7134 | } |
| 7135 | |
| 7136 | void testNotifyKey(bool expectToBeFiltered) { |
| 7137 | NotifyKeyArgs keyArgs; |
| 7138 | |
| 7139 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7140 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7141 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7142 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7143 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7144 | |
| 7145 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 7146 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7147 | } else { |
| 7148 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7149 | } |
| 7150 | } |
| 7151 | }; |
| 7152 | |
| 7153 | // Test InputFilter for MotionEvent |
| 7154 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 7155 | // 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] | 7156 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7157 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7158 | |
| 7159 | // Enable InputFilter |
| 7160 | mDispatcher->setInputFilterEnabled(true); |
| 7161 | // 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] | 7162 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 7163 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7164 | |
| 7165 | // Disable InputFilter |
| 7166 | mDispatcher->setInputFilterEnabled(false); |
| 7167 | // 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] | 7168 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7169 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7170 | } |
| 7171 | |
| 7172 | // Test InputFilter for KeyEvent |
| 7173 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 7174 | // 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] | 7175 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7176 | |
| 7177 | // Enable InputFilter |
| 7178 | mDispatcher->setInputFilterEnabled(true); |
| 7179 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7180 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7181 | |
| 7182 | // Disable InputFilter |
| 7183 | mDispatcher->setInputFilterEnabled(false); |
| 7184 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7185 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7186 | } |
| 7187 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7188 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 7189 | // logical display coordinate space. |
| 7190 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 7191 | ui::Transform firstDisplayTransform; |
| 7192 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7193 | ui::Transform secondDisplayTransform; |
| 7194 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 7195 | |
| 7196 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 7197 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 7198 | displayInfos[0].transform = firstDisplayTransform; |
| 7199 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 7200 | displayInfos[1].transform = secondDisplayTransform; |
| 7201 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7202 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7203 | |
| 7204 | // Enable InputFilter |
| 7205 | mDispatcher->setInputFilterEnabled(true); |
| 7206 | |
| 7207 | // Ensure the correct transforms are used for the displays. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7208 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 7209 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7210 | } |
| 7211 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7212 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 7213 | protected: |
| 7214 | virtual void SetUp() override { |
| 7215 | InputDispatcherTest::SetUp(); |
| 7216 | |
| 7217 | /** |
| 7218 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 7219 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 7220 | * on. |
| 7221 | */ |
| 7222 | mDispatcher->setInputFilterEnabled(true); |
| 7223 | |
| 7224 | std::shared_ptr<InputApplicationHandle> application = |
| 7225 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7226 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 7227 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7228 | |
| 7229 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7230 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7231 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7232 | setFocusedWindow(mWindow); |
| 7233 | mWindow->consumeFocusEvent(true); |
| 7234 | } |
| 7235 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7236 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7237 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7238 | KeyEvent event; |
| 7239 | |
| 7240 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7241 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 7242 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7243 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7244 | const int32_t additionalPolicyFlags = |
| 7245 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 7246 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7247 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7248 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7249 | policyFlags | additionalPolicyFlags)); |
| 7250 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7251 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7252 | } |
| 7253 | |
| 7254 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7255 | int32_t flags) { |
| 7256 | MotionEvent event; |
| 7257 | PointerProperties pointerProperties[1]; |
| 7258 | PointerCoords pointerCoords[1]; |
| 7259 | pointerProperties[0].clear(); |
| 7260 | pointerProperties[0].id = 0; |
| 7261 | pointerCoords[0].clear(); |
| 7262 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 7263 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 7264 | |
| 7265 | ui::Transform identityTransform; |
| 7266 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7267 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 7268 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 7269 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 7270 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 7271 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 7272 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7273 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7274 | |
| 7275 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 7276 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7277 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7278 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7279 | policyFlags | additionalPolicyFlags)); |
| 7280 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7281 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7282 | } |
| 7283 | |
| 7284 | private: |
| 7285 | sp<FakeWindowHandle> mWindow; |
| 7286 | }; |
| 7287 | |
| 7288 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7289 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 7290 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 7291 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7292 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 7293 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7296 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7297 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7298 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7299 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 7300 | } |
| 7301 | |
| 7302 | TEST_F(InputFilterInjectionPolicyTest, |
| 7303 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 7304 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7305 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7306 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7307 | } |
| 7308 | |
| 7309 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7310 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 7311 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7312 | } |
| 7313 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7314 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7315 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7316 | InputDispatcherTest::SetUp(); |
| 7317 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7318 | std::shared_ptr<FakeApplicationHandle> application = |
| 7319 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7320 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7321 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7322 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7323 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7324 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7325 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7326 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7327 | |
| 7328 | // Set focused application. |
| 7329 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7330 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7331 | |
| 7332 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7333 | mDispatcher->onWindowInfosChanged( |
| 7334 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7335 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7336 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7337 | } |
| 7338 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7339 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7340 | InputDispatcherTest::TearDown(); |
| 7341 | |
| 7342 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7343 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7344 | } |
| 7345 | |
| 7346 | protected: |
| 7347 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7348 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7349 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7350 | }; |
| 7351 | |
| 7352 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 7353 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 7354 | // the onPointerDownOutsideFocus callback. |
| 7355 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7356 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7357 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7358 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7359 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7360 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7361 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7362 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7363 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 7364 | } |
| 7365 | |
| 7366 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 7367 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 7368 | // onPointerDownOutsideFocus callback. |
| 7369 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7370 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7371 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, |
| 7372 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7373 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7374 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7375 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7376 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7377 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7378 | } |
| 7379 | |
| 7380 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 7381 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 7382 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7383 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7384 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7385 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7386 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7387 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7388 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7389 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7390 | } |
| 7391 | |
| 7392 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 7393 | // DOWN on the window that already has focus. Ensure no window received the |
| 7394 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7395 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7396 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7397 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7398 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7399 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7400 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7401 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7402 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7403 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7404 | } |
| 7405 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7406 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 7407 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 7408 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 7409 | const MotionEvent event = |
| 7410 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7411 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7412 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7413 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 7414 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7416 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7417 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 7418 | |
| 7419 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7420 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 7421 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 7422 | mUnfocusedWindow->assertNoEvents(); |
| 7423 | } |
| 7424 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7425 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 7426 | // windows that point to the same client token. |
| 7427 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 7428 | virtual void SetUp() override { |
| 7429 | InputDispatcherTest::SetUp(); |
| 7430 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7431 | std::shared_ptr<FakeApplicationHandle> application = |
| 7432 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7433 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 7434 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7435 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 7436 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7437 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 7438 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7439 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 7440 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7441 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7442 | } |
| 7443 | |
| 7444 | protected: |
| 7445 | sp<FakeWindowHandle> mWindow1; |
| 7446 | sp<FakeWindowHandle> mWindow2; |
| 7447 | |
| 7448 | // 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] | 7449 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 7450 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 7451 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7452 | } |
| 7453 | |
| 7454 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 7455 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7456 | const std::string name = window->getName(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7457 | MotionEvent* motionEvent = window->consumeMotion(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7458 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7459 | ASSERT_NE(nullptr, motionEvent) |
| 7460 | << name.c_str() << ": consumer should have returned non-NULL event."; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7461 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 7462 | ASSERT_THAT(*motionEvent, WithMotionAction(expectedAction)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7463 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7464 | |
| 7465 | for (size_t i = 0; i < points.size(); i++) { |
| 7466 | float expectedX = points[i].x; |
| 7467 | float expectedY = points[i].y; |
| 7468 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7469 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7470 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7471 | << ", got " << motionEvent->getX(i); |
| 7472 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7473 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7474 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7475 | } |
| 7476 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7477 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7478 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7479 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7480 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 7481 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7482 | |
| 7483 | // Always consume from window1 since it's the window that has the InputReceiver |
| 7484 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 7485 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7486 | }; |
| 7487 | |
| 7488 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 7489 | // Touch Window 1 |
| 7490 | PointF touchedPoint = {10, 10}; |
| 7491 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7492 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7493 | |
| 7494 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7495 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7496 | |
| 7497 | // Touch Window 2 |
| 7498 | touchedPoint = {150, 150}; |
| 7499 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7500 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7501 | } |
| 7502 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7503 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 7504 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7505 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7506 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7507 | |
| 7508 | // Touch Window 1 |
| 7509 | PointF touchedPoint = {10, 10}; |
| 7510 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7511 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7512 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7513 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7514 | |
| 7515 | // Touch Window 2 |
| 7516 | touchedPoint = {150, 150}; |
| 7517 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7518 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 7519 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7520 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7521 | // Update the transform so rotation is set |
| 7522 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7523 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7524 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 7525 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7526 | } |
| 7527 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7528 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7529 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7530 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7531 | |
| 7532 | // Touch Window 1 |
| 7533 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7534 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7535 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7536 | |
| 7537 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7538 | touchedPoints.push_back(PointF{150, 150}); |
| 7539 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7540 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7541 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7542 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7543 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7544 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7545 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7546 | // Update the transform so rotation is set for Window 2 |
| 7547 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7548 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7549 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7550 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7551 | } |
| 7552 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7553 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7554 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7555 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7556 | |
| 7557 | // Touch Window 1 |
| 7558 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7559 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7560 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7561 | |
| 7562 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7563 | touchedPoints.push_back(PointF{150, 150}); |
| 7564 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7565 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7566 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7567 | |
| 7568 | // Move both windows |
| 7569 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7570 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7571 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7572 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7573 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7574 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7575 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7576 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7577 | expectedPoints.pop_back(); |
| 7578 | |
| 7579 | // Touch Window 2 |
| 7580 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7581 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7582 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7583 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7584 | |
| 7585 | // Move both windows |
| 7586 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7587 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7588 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7589 | |
| 7590 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7591 | } |
| 7592 | |
| 7593 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 7594 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7595 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7596 | |
| 7597 | // Touch Window 1 |
| 7598 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 7599 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7600 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7601 | |
| 7602 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7603 | touchedPoints.push_back(PointF{150, 150}); |
| 7604 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7605 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7606 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7607 | |
| 7608 | // Move both windows |
| 7609 | touchedPoints = {{20, 20}, {175, 175}}; |
| 7610 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 7611 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 7612 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7613 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 7614 | } |
| 7615 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 7616 | /** |
| 7617 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 7618 | * same input channel. |
| 7619 | */ |
| 7620 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 7621 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7622 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 7623 | |
| 7624 | // Touch down in window 1 |
| 7625 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7626 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 7627 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 7628 | |
| 7629 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 7630 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 7631 | // getting generated. |
| 7632 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7633 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 7634 | |
| 7635 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 7636 | } |
| 7637 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7638 | /** |
| 7639 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 7640 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 7641 | * that the pointer is hovering over may have a different transform. |
| 7642 | */ |
| 7643 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7644 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7645 | |
| 7646 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7647 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 7648 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7649 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7650 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 7651 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7652 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7653 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7654 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 7655 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 7656 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 7657 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 7658 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 7659 | } |
| 7660 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7661 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 7662 | virtual void SetUp() override { |
| 7663 | InputDispatcherTest::SetUp(); |
| 7664 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7665 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7666 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7667 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 7668 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7669 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7670 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7671 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7672 | |
| 7673 | // Set focused application. |
| 7674 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 7675 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7676 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7677 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7678 | mWindow->consumeFocusEvent(true); |
| 7679 | } |
| 7680 | |
| 7681 | virtual void TearDown() override { |
| 7682 | InputDispatcherTest::TearDown(); |
| 7683 | mWindow.clear(); |
| 7684 | } |
| 7685 | |
| 7686 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7687 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7688 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7689 | sp<FakeWindowHandle> mWindow; |
| 7690 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 7691 | |
| 7692 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7693 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7694 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7695 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7697 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7698 | WINDOW_LOCATION)); |
| 7699 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7700 | |
| 7701 | sp<FakeWindowHandle> addSpyWindow() { |
| 7702 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7703 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7704 | spy->setTrustedOverlay(true); |
| 7705 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7706 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7707 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7708 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7709 | return spy; |
| 7710 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7711 | }; |
| 7712 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7713 | // Send a tap and respond, which should not cause an ANR. |
| 7714 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 7715 | tapOnWindow(); |
| 7716 | mWindow->consumeMotionDown(); |
| 7717 | mWindow->consumeMotionUp(); |
| 7718 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7719 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7720 | } |
| 7721 | |
| 7722 | // Send a regular key and respond, which should not cause an ANR. |
| 7723 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7724 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7725 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7726 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7727 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7728 | } |
| 7729 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7730 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 7731 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7732 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7733 | mWindow->consumeFocusEvent(false); |
| 7734 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7735 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7736 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7737 | InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7738 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7739 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 7740 | // Key will not go to window because we have no focused window. |
| 7741 | // The 'no focused window' ANR timer should start instead. |
| 7742 | |
| 7743 | // Now, the focused application goes away. |
| 7744 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 7745 | // The key should get dropped and there should be no ANR. |
| 7746 | |
| 7747 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7748 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7749 | } |
| 7750 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7751 | // 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] | 7752 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 7753 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7754 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7755 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7756 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7757 | WINDOW_LOCATION)); |
| 7758 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7759 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 7760 | ASSERT_TRUE(sequenceNum); |
| 7761 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7762 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7763 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7764 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7765 | mWindow->consumeMotionEvent( |
| 7766 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7767 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7768 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7769 | } |
| 7770 | |
| 7771 | // Send a key to the app and have the app not respond right away. |
| 7772 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 7773 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7775 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 7776 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7777 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7778 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 7779 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7780 | } |
| 7781 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7782 | // We have a focused application, but no focused window |
| 7783 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7784 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7785 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7786 | mWindow->consumeFocusEvent(false); |
| 7787 | |
| 7788 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7789 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7790 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7791 | WINDOW_LOCATION)); |
| 7792 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 7793 | mDispatcher->waitForIdle(); |
| 7794 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7795 | |
| 7796 | // Once a focused event arrives, we get an ANR for this application |
| 7797 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 7798 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7799 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7800 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7801 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7802 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7803 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 7804 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7805 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7806 | } |
| 7807 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7808 | /** |
| 7809 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 7810 | * there will not be an ANR. |
| 7811 | */ |
| 7812 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 7813 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7814 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7815 | mWindow->consumeFocusEvent(false); |
| 7816 | |
| 7817 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 7818 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 7819 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7820 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 7821 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 7822 | |
| 7823 | // Define a valid key down event that is stale (too old). |
| 7824 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7825 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7826 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7827 | |
| 7828 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 7829 | |
| 7830 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7831 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 7832 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 7833 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 7834 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 7835 | << "Injection should fail because the event is stale"; |
| 7836 | |
| 7837 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7838 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7839 | mWindow->assertNoEvents(); |
| 7840 | } |
| 7841 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7842 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7843 | // Make sure that we don't notify policy twice about the same ANR. |
| 7844 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 7845 | const std::chrono::duration appTimeout = 400ms; |
| 7846 | mApplication->setDispatchingTimeout(appTimeout); |
| 7847 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 7848 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7849 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7850 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7851 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7852 | |
| 7853 | // Once a focused event arrives, we get an ANR for this application |
| 7854 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 7855 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 7856 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 7857 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7858 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7859 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 7860 | InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout, |
| 7861 | /*allowKeyRepeat=*/false); |
| 7862 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 7863 | << "result=" << ftl::enum_string(result); |
| 7864 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 7865 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 7866 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 7867 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7868 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 7869 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7870 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 7871 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7872 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7873 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 7874 | // '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] | 7875 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7876 | } |
| 7877 | |
| 7878 | // We have a focused application, but no focused window |
| 7879 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7880 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7881 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7882 | mWindow->consumeFocusEvent(false); |
| 7883 | |
| 7884 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7885 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7886 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 7887 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 7888 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7889 | |
| 7890 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7891 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7892 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7893 | mWindow->assertNoEvents(); |
| 7894 | } |
| 7895 | |
| 7896 | /** |
| 7897 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 7898 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 7899 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 7900 | * the ANR mechanism should still work. |
| 7901 | * |
| 7902 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 7903 | * DOWN event, while not responding on the second one. |
| 7904 | */ |
| 7905 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 7906 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7907 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7908 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 7909 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 7910 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7911 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7912 | |
| 7913 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7914 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7915 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 7916 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 7917 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7918 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7919 | |
| 7920 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 7921 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7922 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7923 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7924 | } |
| 7925 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7926 | // A spy window can receive an ANR |
| 7927 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 7928 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 7929 | |
| 7930 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7931 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7932 | WINDOW_LOCATION)); |
| 7933 | mWindow->consumeMotionDown(); |
| 7934 | |
| 7935 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 7936 | ASSERT_TRUE(sequenceNum); |
| 7937 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7938 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7939 | |
| 7940 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7941 | spy->consumeMotionEvent( |
| 7942 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7943 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7944 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7945 | } |
| 7946 | |
| 7947 | // 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] | 7948 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7949 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 7950 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7951 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7953 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7954 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7955 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7956 | |
| 7957 | // Stuck on the ACTION_UP |
| 7958 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7959 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7960 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7961 | // 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] | 7962 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7963 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7964 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7965 | |
| 7966 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 7967 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7968 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7969 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7970 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7971 | } |
| 7972 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7973 | // 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] | 7974 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7975 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 7976 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7977 | |
| 7978 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7979 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7980 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7981 | |
| 7982 | mWindow->consumeMotionDown(); |
| 7983 | // Stuck on the ACTION_UP |
| 7984 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7985 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7986 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7987 | // 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] | 7988 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7989 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7990 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7991 | |
| 7992 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 7993 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7994 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7995 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7996 | spy->assertNoEvents(); |
| 7997 | } |
| 7998 | |
| 7999 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8000 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8001 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8002 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8003 | |
| 8004 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8005 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8006 | WINDOW_LOCATION)); |
| 8007 | |
| 8008 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8009 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 8010 | ASSERT_TRUE(consumeSeq); |
| 8011 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8012 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 8013 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8014 | |
| 8015 | monitor.finishEvent(*consumeSeq); |
| 8016 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 8017 | |
| 8018 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8019 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8020 | } |
| 8021 | |
| 8022 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 8023 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 8024 | // get an ANR again. |
| 8025 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 8026 | // 2. consume all pending events (= queue becomes healthy again) |
| 8027 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 8028 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 8029 | tapOnWindow(); |
| 8030 | |
| 8031 | mWindow->consumeMotionDown(); |
| 8032 | // Block on ACTION_UP |
| 8033 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8034 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8035 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 8036 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8037 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8038 | mWindow->assertNoEvents(); |
| 8039 | |
| 8040 | tapOnWindow(); |
| 8041 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8042 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8043 | mWindow->consumeMotionUp(); |
| 8044 | |
| 8045 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8046 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8047 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8048 | mWindow->assertNoEvents(); |
| 8049 | } |
| 8050 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8051 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 8052 | // it. |
| 8053 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8055 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8056 | WINDOW_LOCATION)); |
| 8057 | |
| 8058 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8059 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8060 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8061 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 8062 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8063 | // 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] | 8064 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8065 | mWindow->consumeMotionEvent( |
| 8066 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8067 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8068 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8069 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8070 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8071 | } |
| 8072 | |
| 8073 | /** |
| 8074 | * 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] | 8075 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8076 | * |
| 8077 | * Warning!!! |
| 8078 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 8079 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8080 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8081 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 8082 | * |
| 8083 | * If that value changes, this test should also change. |
| 8084 | */ |
| 8085 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 8086 | 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] | 8087 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8088 | |
| 8089 | tapOnWindow(); |
| 8090 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 8091 | ASSERT_TRUE(downSequenceNum); |
| 8092 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 8093 | ASSERT_TRUE(upSequenceNum); |
| 8094 | // Don't finish the events yet, and send a key |
| 8095 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 8096 | // window even if motions are still being processed. But because the injection timeout is short, |
| 8097 | // we will receive INJECTION_TIMED_OUT as the result. |
| 8098 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8099 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8100 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8101 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8102 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8103 | // Key will not be sent to the window, yet, because the window is still processing events |
| 8104 | // 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] | 8105 | // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR. |
| 8106 | // Rely here on the fact that it uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 8107 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8108 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8109 | |
| 8110 | std::this_thread::sleep_for(500ms); |
| 8111 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 8112 | // to the focused window, even though we have not yet finished the motion event |
| 8113 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8114 | mWindow->finishEvent(*downSequenceNum); |
| 8115 | mWindow->finishEvent(*upSequenceNum); |
| 8116 | } |
| 8117 | |
| 8118 | /** |
| 8119 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 8120 | * not go to the focused window until the motion is processed. |
| 8121 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 8122 | * focused window right away. |
| 8123 | */ |
| 8124 | TEST_F(InputDispatcherSingleWindowAnr, |
| 8125 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 8126 | 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] | 8127 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8128 | |
| 8129 | tapOnWindow(); |
| 8130 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 8131 | ASSERT_TRUE(downSequenceNum); |
| 8132 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 8133 | ASSERT_TRUE(upSequenceNum); |
| 8134 | // Don't finish the events yet, and send a key |
| 8135 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8136 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8137 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 8138 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8139 | // 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] | 8140 | // Make sure the `assertNoEvents` check doesn't take too long. It uses |
| 8141 | // CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 8142 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8143 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8144 | |
| 8145 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 8146 | tapOnWindow(); |
| 8147 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 8148 | // the other events yet. We can finish events in any order. |
| 8149 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 8150 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 8151 | mWindow->consumeMotionDown(); |
| 8152 | mWindow->consumeMotionUp(); |
| 8153 | mWindow->assertNoEvents(); |
| 8154 | } |
| 8155 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8156 | /** |
| 8157 | * Send an event to the app and have the app not respond right away. |
| 8158 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8159 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 8160 | * At some point, the window becomes responsive again. |
| 8161 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 8162 | */ |
| 8163 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 8164 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8165 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 8166 | .build()); |
| 8167 | |
| 8168 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 8169 | ASSERT_TRUE(sequenceNum); |
| 8170 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8171 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 8172 | |
| 8173 | mWindow->finishEvent(*sequenceNum); |
| 8174 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 8175 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8176 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 8177 | |
| 8178 | // Now that the window is responsive, let's continue the gesture. |
| 8179 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8180 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8181 | .build()); |
| 8182 | |
| 8183 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8184 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8185 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8186 | .build()); |
| 8187 | |
| 8188 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8189 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8190 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8191 | .build()); |
| 8192 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8193 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8194 | .build()); |
| 8195 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 8196 | mWindow->assertNoEvents(); |
| 8197 | |
| 8198 | // Start another one. |
| 8199 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8200 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 8201 | .build()); |
| 8202 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8203 | } |
| 8204 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8205 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 8206 | virtual void SetUp() override { |
| 8207 | InputDispatcherTest::SetUp(); |
| 8208 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8209 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8210 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8211 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 8212 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8213 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8214 | // 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] | 8215 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8216 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8217 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 8218 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8219 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8220 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8221 | |
| 8222 | // Set focused application. |
| 8223 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8224 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8225 | |
| 8226 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8227 | mDispatcher->onWindowInfosChanged( |
| 8228 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8229 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8230 | mFocusedWindow->consumeFocusEvent(true); |
| 8231 | } |
| 8232 | |
| 8233 | virtual void TearDown() override { |
| 8234 | InputDispatcherTest::TearDown(); |
| 8235 | |
| 8236 | mUnfocusedWindow.clear(); |
| 8237 | mFocusedWindow.clear(); |
| 8238 | } |
| 8239 | |
| 8240 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8241 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8242 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 8243 | sp<FakeWindowHandle> mFocusedWindow; |
| 8244 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 8245 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 8246 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 8247 | |
| 8248 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 8249 | |
| 8250 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 8251 | |
| 8252 | private: |
| 8253 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8254 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8255 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8256 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8257 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8258 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8259 | location)); |
| 8260 | } |
| 8261 | }; |
| 8262 | |
| 8263 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 8264 | // should be ANR'd first. |
| 8265 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8266 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8267 | injectMotionEvent(*mDispatcher, |
| 8268 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8269 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8270 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8271 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8272 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8273 | .build())); |
| 8274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8275 | injectMotionEvent(*mDispatcher, |
| 8276 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 8277 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8278 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8279 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8280 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8281 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8282 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8283 | mFocusedWindow->consumeMotionUp(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8284 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8285 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8286 | // We consumed all events, so no ANR |
| 8287 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8288 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8289 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8290 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8291 | injectMotionEvent(*mDispatcher, |
| 8292 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8293 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8294 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8295 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8296 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8297 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8298 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8299 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8300 | |
| 8301 | const std::chrono::duration timeout = |
| 8302 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8303 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8304 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8305 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8306 | mFocusedWindow->consumeMotionDown(); |
| 8307 | // This cancel is generated because the connection was unresponsive |
| 8308 | mFocusedWindow->consumeMotionCancel(); |
| 8309 | mFocusedWindow->assertNoEvents(); |
| 8310 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8311 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8312 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8313 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8314 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8315 | } |
| 8316 | |
| 8317 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 8318 | // it doesn't matter which order they should have ANR. |
| 8319 | // But we should receive ANR for both. |
| 8320 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 8321 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8322 | mUnfocusedWindow->setDispatchingTimeout( |
| 8323 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8324 | mDispatcher->onWindowInfosChanged( |
| 8325 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8326 | |
| 8327 | tapOnFocusedWindow(); |
| 8328 | // 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] | 8329 | // 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] | 8330 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 8331 | mFocusedWindow->getDispatchingTimeout( |
| 8332 | DISPATCHING_TIMEOUT)), |
| 8333 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 8334 | |
| 8335 | ASSERT_THAT(anrConnectionTokens, |
| 8336 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 8337 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8338 | |
| 8339 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8340 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8341 | |
| 8342 | mFocusedWindow->consumeMotionDown(); |
| 8343 | mFocusedWindow->consumeMotionUp(); |
| 8344 | mUnfocusedWindow->consumeMotionOutside(); |
| 8345 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8346 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 8347 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8348 | |
| 8349 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8350 | ASSERT_THAT(responsiveTokens, |
| 8351 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 8352 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8353 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8354 | } |
| 8355 | |
| 8356 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 8357 | // We should also log an error to account for the dropped event (not tested here). |
| 8358 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 8359 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 8360 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8361 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8362 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8363 | // Receive the events, but don't respond |
| 8364 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 8365 | ASSERT_TRUE(downEventSequenceNum); |
| 8366 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 8367 | ASSERT_TRUE(upEventSequenceNum); |
| 8368 | const std::chrono::duration timeout = |
| 8369 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8370 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8371 | |
| 8372 | // Tap once again |
| 8373 | // 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] | 8374 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8375 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8376 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8377 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8378 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8379 | FOCUSED_WINDOW_LOCATION)); |
| 8380 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 8381 | // valid touch target |
| 8382 | mUnfocusedWindow->assertNoEvents(); |
| 8383 | |
| 8384 | // Consume the first tap |
| 8385 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 8386 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 8387 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8388 | // The second tap did not go to the focused window |
| 8389 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8390 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8391 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8392 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8393 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8394 | } |
| 8395 | |
| 8396 | // If you tap outside of all windows, there will not be ANR |
| 8397 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8398 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8399 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8400 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 8401 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8402 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8403 | } |
| 8404 | |
| 8405 | // Since the focused window is paused, tapping on it should not produce any events |
| 8406 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 8407 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8408 | mDispatcher->onWindowInfosChanged( |
| 8409 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8410 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8411 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8412 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8413 | FOCUSED_WINDOW_LOCATION)); |
| 8414 | |
| 8415 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 8416 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8417 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 8418 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8419 | |
| 8420 | mFocusedWindow->assertNoEvents(); |
| 8421 | mUnfocusedWindow->assertNoEvents(); |
| 8422 | } |
| 8423 | |
| 8424 | /** |
| 8425 | * 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] | 8426 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8427 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 8428 | * |
| 8429 | * Warning!!! |
| 8430 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 8431 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8432 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8433 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 8434 | * |
| 8435 | * If that value changes, this test should also change. |
| 8436 | */ |
| 8437 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 8438 | // Set a long ANR timeout to prevent it from triggering |
| 8439 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8440 | mDispatcher->onWindowInfosChanged( |
| 8441 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8442 | |
| 8443 | tapOnUnfocusedWindow(); |
| 8444 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8445 | ASSERT_TRUE(downSequenceNum); |
| 8446 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 8447 | ASSERT_TRUE(upSequenceNum); |
| 8448 | // Don't finish the events yet, and send a key |
| 8449 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 8450 | // window even if motions are still being processed. |
| 8451 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8452 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8453 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8454 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8455 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8456 | // 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] | 8457 | // and the key remains pending, waiting for the touch events to be processed. |
| 8458 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 8459 | // under the hood. |
| 8460 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8461 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8462 | |
| 8463 | // 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] | 8464 | mFocusedWindow->setFocusable(false); |
| 8465 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8466 | mDispatcher->onWindowInfosChanged( |
| 8467 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8468 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8469 | |
| 8470 | // Focus events should precede the key events |
| 8471 | mUnfocusedWindow->consumeFocusEvent(true); |
| 8472 | mFocusedWindow->consumeFocusEvent(false); |
| 8473 | |
| 8474 | // Finish the tap events, which should unblock dispatcher |
| 8475 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 8476 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 8477 | |
| 8478 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 8479 | // can finally go to the newly focused "mUnfocusedWindow". |
| 8480 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8481 | mFocusedWindow->assertNoEvents(); |
| 8482 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8483 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8484 | } |
| 8485 | |
| 8486 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 8487 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 8488 | // The other window should not be affected by that. |
| 8489 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 8490 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8491 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8492 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8493 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8494 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8495 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8496 | |
| 8497 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8498 | mDispatcher->notifyMotion( |
| 8499 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8500 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8501 | |
| 8502 | const std::chrono::duration timeout = |
| 8503 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8504 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8505 | |
| 8506 | mUnfocusedWindow->consumeMotionDown(); |
| 8507 | mFocusedWindow->consumeMotionDown(); |
| 8508 | // Focused window may or may not receive ACTION_MOVE |
| 8509 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 8510 | InputEvent* event; |
| 8511 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 8512 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 8513 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 8514 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8515 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8516 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 8517 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 8518 | mFocusedWindow->consumeMotionCancel(); |
| 8519 | } else { |
| 8520 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 8521 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8522 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8523 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8524 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8525 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8526 | mUnfocusedWindow->assertNoEvents(); |
| 8527 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8528 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8529 | } |
| 8530 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8531 | /** |
| 8532 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 8533 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 8534 | * |
| 8535 | * If the user touches another application during this time, the key should be dropped. |
| 8536 | * Next, if a new focused window comes in, without toggling the focused application, |
| 8537 | * then no ANR should occur. |
| 8538 | * |
| 8539 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 8540 | * but in some cases the policy may not update the focused application. |
| 8541 | */ |
| 8542 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 8543 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 8544 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 8545 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8546 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 8547 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 8548 | mFocusedWindow->setFocusable(false); |
| 8549 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8550 | mDispatcher->onWindowInfosChanged( |
| 8551 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8552 | mFocusedWindow->consumeFocusEvent(false); |
| 8553 | |
| 8554 | // Send a key. The ANR timer should start because there is no focused window. |
| 8555 | // 'focusedApplication' will get blamed if this timer completes. |
| 8556 | // 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] | 8557 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8558 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8559 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8560 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8561 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8562 | |
| 8563 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 8564 | // then the injected touches won't cause the focused event to get dropped. |
| 8565 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 8566 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 8567 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 8568 | // For this test, it means that the key would get delivered to the window once it becomes |
| 8569 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8570 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8571 | |
| 8572 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8573 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8574 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8575 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8576 | |
| 8577 | // We do not consume the motion right away, because that would require dispatcher to first |
| 8578 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 8579 | // Set the focused window first. |
| 8580 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8581 | mDispatcher->onWindowInfosChanged( |
| 8582 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 8583 | setFocusedWindow(mFocusedWindow); |
| 8584 | mFocusedWindow->consumeFocusEvent(true); |
| 8585 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 8586 | // to another application. This could be a bug / behaviour in the policy. |
| 8587 | |
| 8588 | mUnfocusedWindow->consumeMotionDown(); |
| 8589 | |
| 8590 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8591 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 8592 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 8593 | } |
| 8594 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8595 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 8596 | // that has feature NO_INPUT_CHANNEL. |
| 8597 | // Layout: |
| 8598 | // Top (closest to user) |
| 8599 | // mNoInputWindow (above all windows) |
| 8600 | // mBottomWindow |
| 8601 | // Bottom (furthest from user) |
| 8602 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 8603 | virtual void SetUp() override { |
| 8604 | InputDispatcherTest::SetUp(); |
| 8605 | |
| 8606 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8607 | mNoInputWindow = |
| 8608 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 8609 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8610 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8611 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8612 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8613 | // It's perfectly valid for this window to not have an associated input channel |
| 8614 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8615 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 8616 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8617 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8618 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8619 | mDispatcher->onWindowInfosChanged( |
| 8620 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8621 | } |
| 8622 | |
| 8623 | protected: |
| 8624 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 8625 | sp<FakeWindowHandle> mNoInputWindow; |
| 8626 | sp<FakeWindowHandle> mBottomWindow; |
| 8627 | }; |
| 8628 | |
| 8629 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 8630 | PointF touchedPoint = {10, 10}; |
| 8631 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8632 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8633 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8634 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8635 | |
| 8636 | mNoInputWindow->assertNoEvents(); |
| 8637 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 8638 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 8639 | // and therefore should prevent mBottomWindow from receiving touches |
| 8640 | mBottomWindow->assertNoEvents(); |
| 8641 | } |
| 8642 | |
| 8643 | /** |
| 8644 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 8645 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 8646 | */ |
| 8647 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 8648 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8649 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 8650 | "Window with input channel and NO_INPUT_CHANNEL", |
| 8651 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8652 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8653 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8654 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8655 | mDispatcher->onWindowInfosChanged( |
| 8656 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8657 | |
| 8658 | PointF touchedPoint = {10, 10}; |
| 8659 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8660 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8661 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8662 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 8663 | |
| 8664 | mNoInputWindow->assertNoEvents(); |
| 8665 | mBottomWindow->assertNoEvents(); |
| 8666 | } |
| 8667 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8668 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 8669 | protected: |
| 8670 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8671 | sp<FakeWindowHandle> mWindow; |
| 8672 | sp<FakeWindowHandle> mMirror; |
| 8673 | |
| 8674 | virtual void SetUp() override { |
| 8675 | InputDispatcherTest::SetUp(); |
| 8676 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8677 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 8678 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 8679 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8680 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 8681 | mWindow->setFocusable(true); |
| 8682 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8683 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8684 | } |
| 8685 | }; |
| 8686 | |
| 8687 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 8688 | // Request focus on a mirrored window |
| 8689 | setFocusedWindow(mMirror); |
| 8690 | |
| 8691 | // window gets focused |
| 8692 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8693 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8694 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8695 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8696 | } |
| 8697 | |
| 8698 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 8699 | // focusable. |
| 8700 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 8701 | setFocusedWindow(mMirror); |
| 8702 | |
| 8703 | // window gets focused |
| 8704 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8705 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8706 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8707 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8709 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8710 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8711 | |
| 8712 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8713 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8714 | |
| 8715 | // window loses focus since one of the windows associated with the token in not focusable |
| 8716 | mWindow->consumeFocusEvent(false); |
| 8717 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8718 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8719 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8720 | mWindow->assertNoEvents(); |
| 8721 | } |
| 8722 | |
| 8723 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 8724 | // invisible. |
| 8725 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 8726 | setFocusedWindow(mMirror); |
| 8727 | |
| 8728 | // window gets focused |
| 8729 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8731 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8732 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8734 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8735 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8736 | |
| 8737 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8738 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8739 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8740 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8741 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8742 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8743 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8744 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8745 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8746 | |
| 8747 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8748 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8749 | |
| 8750 | // window loses focus only after all windows associated with the token become invisible. |
| 8751 | mWindow->consumeFocusEvent(false); |
| 8752 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8753 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8754 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8755 | mWindow->assertNoEvents(); |
| 8756 | } |
| 8757 | |
| 8758 | // A focused & mirrored window remains focused until both windows are removed. |
| 8759 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 8760 | setFocusedWindow(mMirror); |
| 8761 | |
| 8762 | // window gets focused |
| 8763 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8764 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8765 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8766 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8768 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8769 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8770 | |
| 8771 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8772 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8773 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8775 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8776 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8777 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8778 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8779 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8780 | |
| 8781 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8782 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8783 | mWindow->consumeFocusEvent(false); |
| 8784 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8785 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8786 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8787 | mWindow->assertNoEvents(); |
| 8788 | } |
| 8789 | |
| 8790 | // Focus request can be pending until one window becomes visible. |
| 8791 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 8792 | // Request focus on an invisible mirror. |
| 8793 | mWindow->setVisible(false); |
| 8794 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8795 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8796 | setFocusedWindow(mMirror); |
| 8797 | |
| 8798 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8799 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8800 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 8801 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8802 | |
| 8803 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8804 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8805 | |
| 8806 | // window gets focused |
| 8807 | mWindow->consumeFocusEvent(true); |
| 8808 | // window gets the pending key event |
| 8809 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8810 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8811 | |
| 8812 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 8813 | protected: |
| 8814 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8815 | sp<FakeWindowHandle> mWindow; |
| 8816 | sp<FakeWindowHandle> mSecondWindow; |
| 8817 | |
| 8818 | void SetUp() override { |
| 8819 | InputDispatcherTest::SetUp(); |
| 8820 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8821 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8822 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8823 | mSecondWindow = |
| 8824 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8825 | mSecondWindow->setFocusable(true); |
| 8826 | |
| 8827 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8828 | mDispatcher->onWindowInfosChanged( |
| 8829 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8830 | |
| 8831 | setFocusedWindow(mWindow); |
| 8832 | mWindow->consumeFocusEvent(true); |
| 8833 | } |
| 8834 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8835 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8836 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8837 | } |
| 8838 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8839 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 8840 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8841 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8842 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 8843 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8844 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8845 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8846 | } |
| 8847 | }; |
| 8848 | |
| 8849 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 8850 | // Ensure that capture cannot be obtained for unfocused windows. |
| 8851 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 8852 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 8853 | mSecondWindow->assertNoEvents(); |
| 8854 | |
| 8855 | // Ensure that capture can be enabled from the focus window. |
| 8856 | requestAndVerifyPointerCapture(mWindow, true); |
| 8857 | |
| 8858 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 8859 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 8860 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 8861 | |
| 8862 | // Ensure that capture can be disabled from the window with capture. |
| 8863 | requestAndVerifyPointerCapture(mWindow, false); |
| 8864 | } |
| 8865 | |
| 8866 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8867 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8868 | |
| 8869 | setFocusedWindow(mSecondWindow); |
| 8870 | |
| 8871 | // Ensure that the capture disabled event was sent first. |
| 8872 | mWindow->consumeCaptureEvent(false); |
| 8873 | mWindow->consumeFocusEvent(false); |
| 8874 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8875 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8876 | |
| 8877 | // 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] | 8878 | notifyPointerCaptureChanged({}); |
| 8879 | notifyPointerCaptureChanged(request); |
| 8880 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8881 | mWindow->assertNoEvents(); |
| 8882 | mSecondWindow->assertNoEvents(); |
| 8883 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 8884 | } |
| 8885 | |
| 8886 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8887 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8888 | |
| 8889 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8890 | notifyPointerCaptureChanged({}); |
| 8891 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8892 | |
| 8893 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8894 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 8895 | mWindow->consumeCaptureEvent(false); |
| 8896 | mWindow->assertNoEvents(); |
| 8897 | } |
| 8898 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8899 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 8900 | requestAndVerifyPointerCapture(mWindow, true); |
| 8901 | |
| 8902 | // The first window loses focus. |
| 8903 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8904 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8905 | mWindow->consumeCaptureEvent(false); |
| 8906 | |
| 8907 | // Request Pointer Capture from the second window before the notification from InputReader |
| 8908 | // arrives. |
| 8909 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8910 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8911 | |
| 8912 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8913 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8914 | |
| 8915 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8916 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 8917 | |
| 8918 | mSecondWindow->consumeFocusEvent(true); |
| 8919 | mSecondWindow->consumeCaptureEvent(true); |
| 8920 | } |
| 8921 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 8922 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 8923 | // App repeatedly enables and disables capture. |
| 8924 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 8925 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 8926 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 8927 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 8928 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 8929 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 8930 | |
| 8931 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 8932 | // first request is now stale, this should do nothing. |
| 8933 | notifyPointerCaptureChanged(firstRequest); |
| 8934 | mWindow->assertNoEvents(); |
| 8935 | |
| 8936 | // InputReader notifies that the second request was enabled. |
| 8937 | notifyPointerCaptureChanged(secondRequest); |
| 8938 | mWindow->consumeCaptureEvent(true); |
| 8939 | } |
| 8940 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 8941 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 8942 | requestAndVerifyPointerCapture(mWindow, true); |
| 8943 | |
| 8944 | // App toggles pointer capture off and on. |
| 8945 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 8946 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 8947 | |
| 8948 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 8949 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 8950 | |
| 8951 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 8952 | // preceding "disable" request. |
| 8953 | notifyPointerCaptureChanged(enableRequest); |
| 8954 | |
| 8955 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 8956 | // any notifications. |
| 8957 | mWindow->assertNoEvents(); |
| 8958 | } |
| 8959 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8960 | /** |
| 8961 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 8962 | * mouse movements don't affect the previous mouse hovering state. |
| 8963 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 8964 | * HOVER_MOVE events). |
| 8965 | */ |
| 8966 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 8967 | // Mouse hover on the window |
| 8968 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 8969 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 8970 | .build()); |
| 8971 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 8972 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 8973 | .build()); |
| 8974 | |
| 8975 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 8976 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 8977 | |
| 8978 | // Start pointer capture |
| 8979 | requestAndVerifyPointerCapture(mWindow, true); |
| 8980 | |
| 8981 | // Send some relative mouse movements and receive them in the window. |
| 8982 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 8983 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 8984 | .build()); |
| 8985 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 8986 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 8987 | |
| 8988 | // Stop pointer capture |
| 8989 | requestAndVerifyPointerCapture(mWindow, false); |
| 8990 | |
| 8991 | // Continue hovering on the window |
| 8992 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 8993 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 8994 | .build()); |
| 8995 | mWindow->consumeMotionEvent( |
| 8996 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 8997 | |
| 8998 | mWindow->assertNoEvents(); |
| 8999 | } |
| 9000 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9001 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 9002 | protected: |
| 9003 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9004 | |
| 9005 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 9006 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 9007 | |
| 9008 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 9009 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9010 | |
| 9011 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 9012 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 9013 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9014 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 9015 | MAXIMUM_OBSCURING_OPACITY); |
| 9016 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9017 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 9018 | static constexpr gui::Uid APP_B_UID{10002}; |
| 9019 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9020 | |
| 9021 | sp<FakeWindowHandle> mTouchWindow; |
| 9022 | |
| 9023 | virtual void SetUp() override { |
| 9024 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9025 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9026 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 9027 | } |
| 9028 | |
| 9029 | virtual void TearDown() override { |
| 9030 | InputDispatcherTest::TearDown(); |
| 9031 | mTouchWindow.clear(); |
| 9032 | } |
| 9033 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9034 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 9035 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9036 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9037 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9038 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9039 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9040 | return window; |
| 9041 | } |
| 9042 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9043 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9044 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 9045 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9046 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9047 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9048 | 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] | 9049 | return window; |
| 9050 | } |
| 9051 | |
| 9052 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9053 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9054 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9055 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9056 | } |
| 9057 | }; |
| 9058 | |
| 9059 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9060 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9061 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9062 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9063 | |
| 9064 | touch(); |
| 9065 | |
| 9066 | mTouchWindow->assertNoEvents(); |
| 9067 | } |
| 9068 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9069 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9070 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 9071 | const sp<FakeWindowHandle>& w = |
| 9072 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9073 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9074 | |
| 9075 | touch(); |
| 9076 | |
| 9077 | mTouchWindow->assertNoEvents(); |
| 9078 | } |
| 9079 | |
| 9080 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9081 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 9082 | const sp<FakeWindowHandle>& w = |
| 9083 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9084 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9085 | |
| 9086 | touch(); |
| 9087 | |
| 9088 | w->assertNoEvents(); |
| 9089 | } |
| 9090 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9091 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9092 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9093 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9094 | |
| 9095 | touch(); |
| 9096 | |
| 9097 | mTouchWindow->consumeAnyMotionDown(); |
| 9098 | } |
| 9099 | |
| 9100 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9101 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9102 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9103 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9104 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9105 | |
| 9106 | touch({PointF{100, 100}}); |
| 9107 | |
| 9108 | mTouchWindow->consumeAnyMotionDown(); |
| 9109 | } |
| 9110 | |
| 9111 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9112 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9113 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9114 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9115 | |
| 9116 | touch(); |
| 9117 | |
| 9118 | mTouchWindow->consumeAnyMotionDown(); |
| 9119 | } |
| 9120 | |
| 9121 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 9122 | const sp<FakeWindowHandle>& w = |
| 9123 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9124 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9125 | |
| 9126 | touch(); |
| 9127 | |
| 9128 | mTouchWindow->consumeAnyMotionDown(); |
| 9129 | } |
| 9130 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9131 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 9132 | const sp<FakeWindowHandle>& w = |
| 9133 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9134 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9135 | |
| 9136 | touch(); |
| 9137 | |
| 9138 | w->assertNoEvents(); |
| 9139 | } |
| 9140 | |
| 9141 | /** |
| 9142 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 9143 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 9144 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 9145 | */ |
| 9146 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9147 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 9148 | const sp<FakeWindowHandle>& w = |
| 9149 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9150 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9151 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9152 | |
| 9153 | touch(); |
| 9154 | |
| 9155 | w->consumeMotionOutside(); |
| 9156 | } |
| 9157 | |
| 9158 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 9159 | const sp<FakeWindowHandle>& w = |
| 9160 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9161 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9162 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9163 | |
| 9164 | touch(); |
| 9165 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9166 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9167 | } |
| 9168 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9169 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9170 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9171 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9172 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9173 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9174 | |
| 9175 | touch(); |
| 9176 | |
| 9177 | mTouchWindow->consumeAnyMotionDown(); |
| 9178 | } |
| 9179 | |
| 9180 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 9181 | const sp<FakeWindowHandle>& w = |
| 9182 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9183 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9184 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9185 | |
| 9186 | touch(); |
| 9187 | |
| 9188 | mTouchWindow->consumeAnyMotionDown(); |
| 9189 | } |
| 9190 | |
| 9191 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9192 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9193 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9194 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9195 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9196 | |
| 9197 | touch(); |
| 9198 | |
| 9199 | mTouchWindow->assertNoEvents(); |
| 9200 | } |
| 9201 | |
| 9202 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 9203 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 9204 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9205 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 9206 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9207 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9208 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 9209 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9210 | mDispatcher->onWindowInfosChanged( |
| 9211 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9212 | |
| 9213 | touch(); |
| 9214 | |
| 9215 | mTouchWindow->assertNoEvents(); |
| 9216 | } |
| 9217 | |
| 9218 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 9219 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 9220 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9221 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 9222 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9223 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9224 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 9225 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9226 | mDispatcher->onWindowInfosChanged( |
| 9227 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9228 | |
| 9229 | touch(); |
| 9230 | |
| 9231 | mTouchWindow->consumeAnyMotionDown(); |
| 9232 | } |
| 9233 | |
| 9234 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9235 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 9236 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9237 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9238 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9239 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9240 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9241 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9242 | mDispatcher->onWindowInfosChanged( |
| 9243 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9244 | |
| 9245 | touch(); |
| 9246 | |
| 9247 | mTouchWindow->consumeAnyMotionDown(); |
| 9248 | } |
| 9249 | |
| 9250 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 9251 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9252 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9253 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9254 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9255 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9256 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9257 | mDispatcher->onWindowInfosChanged( |
| 9258 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9259 | |
| 9260 | touch(); |
| 9261 | |
| 9262 | mTouchWindow->assertNoEvents(); |
| 9263 | } |
| 9264 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9265 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9266 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 9267 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9268 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9269 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9270 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9271 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9272 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9273 | mDispatcher->onWindowInfosChanged( |
| 9274 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9275 | |
| 9276 | touch(); |
| 9277 | |
| 9278 | mTouchWindow->assertNoEvents(); |
| 9279 | } |
| 9280 | |
| 9281 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9282 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 9283 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9284 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9285 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9286 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9287 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9288 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9289 | mDispatcher->onWindowInfosChanged( |
| 9290 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9291 | |
| 9292 | touch(); |
| 9293 | |
| 9294 | mTouchWindow->consumeAnyMotionDown(); |
| 9295 | } |
| 9296 | |
| 9297 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 9298 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9299 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9300 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9301 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9302 | |
| 9303 | touch(); |
| 9304 | |
| 9305 | mTouchWindow->consumeAnyMotionDown(); |
| 9306 | } |
| 9307 | |
| 9308 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 9309 | const sp<FakeWindowHandle>& w = |
| 9310 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9311 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9312 | |
| 9313 | touch(); |
| 9314 | |
| 9315 | mTouchWindow->consumeAnyMotionDown(); |
| 9316 | } |
| 9317 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9318 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9319 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 9320 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 9321 | const sp<FakeWindowHandle>& w = |
| 9322 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9323 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9324 | |
| 9325 | touch(); |
| 9326 | |
| 9327 | mTouchWindow->assertNoEvents(); |
| 9328 | } |
| 9329 | |
| 9330 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 9331 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 9332 | const sp<FakeWindowHandle>& w = |
| 9333 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9334 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9335 | |
| 9336 | touch(); |
| 9337 | |
| 9338 | mTouchWindow->consumeAnyMotionDown(); |
| 9339 | } |
| 9340 | |
| 9341 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9342 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 9343 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 9344 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9345 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9346 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9347 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9348 | |
| 9349 | touch(); |
| 9350 | |
| 9351 | mTouchWindow->consumeAnyMotionDown(); |
| 9352 | } |
| 9353 | |
| 9354 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9355 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 9356 | const sp<FakeWindowHandle>& w1 = |
| 9357 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 9358 | OPACITY_BELOW_THRESHOLD); |
| 9359 | const sp<FakeWindowHandle>& w2 = |
| 9360 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9361 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9362 | mDispatcher->onWindowInfosChanged( |
| 9363 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9364 | |
| 9365 | touch(); |
| 9366 | |
| 9367 | mTouchWindow->assertNoEvents(); |
| 9368 | } |
| 9369 | |
| 9370 | /** |
| 9371 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 9372 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 9373 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 9374 | */ |
| 9375 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9376 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 9377 | const sp<FakeWindowHandle>& wB = |
| 9378 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 9379 | OPACITY_BELOW_THRESHOLD); |
| 9380 | const sp<FakeWindowHandle>& wC = |
| 9381 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9382 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9383 | mDispatcher->onWindowInfosChanged( |
| 9384 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9385 | |
| 9386 | touch(); |
| 9387 | |
| 9388 | mTouchWindow->assertNoEvents(); |
| 9389 | } |
| 9390 | |
| 9391 | /** |
| 9392 | * This test is testing that a window from a different UID but with same application token doesn't |
| 9393 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 9394 | */ |
| 9395 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9396 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 9397 | const sp<FakeWindowHandle>& w = |
| 9398 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 9399 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9400 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9401 | |
| 9402 | touch(); |
| 9403 | |
| 9404 | mTouchWindow->consumeAnyMotionDown(); |
| 9405 | } |
| 9406 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9407 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 9408 | protected: |
| 9409 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9410 | sp<FakeWindowHandle> mWindow; |
| 9411 | sp<FakeWindowHandle> mSecondWindow; |
| 9412 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9413 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9414 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 9415 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9416 | |
| 9417 | void SetUp() override { |
| 9418 | InputDispatcherTest::SetUp(); |
| 9419 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9420 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9421 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9422 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9423 | mSecondWindow = |
| 9424 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9425 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9426 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9427 | mSpyWindow = |
| 9428 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9429 | mSpyWindow->setSpy(true); |
| 9430 | mSpyWindow->setTrustedOverlay(true); |
| 9431 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 9432 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9433 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9434 | mDispatcher->onWindowInfosChanged( |
| 9435 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 9436 | {}, |
| 9437 | 0, |
| 9438 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9439 | } |
| 9440 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9441 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 9442 | switch (fromSource) { |
| 9443 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 9444 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9445 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9446 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9447 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9448 | break; |
| 9449 | case AINPUT_SOURCE_STYLUS: |
| 9450 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9451 | injectMotionEvent(*mDispatcher, |
| 9452 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9453 | AINPUT_SOURCE_STYLUS) |
| 9454 | .buttonState( |
| 9455 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 9456 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 9457 | .x(50) |
| 9458 | .y(50)) |
| 9459 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9460 | break; |
| 9461 | case AINPUT_SOURCE_MOUSE: |
| 9462 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9463 | injectMotionEvent(*mDispatcher, |
| 9464 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9465 | AINPUT_SOURCE_MOUSE) |
| 9466 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 9467 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 9468 | ToolType::MOUSE) |
| 9469 | .x(50) |
| 9470 | .y(50)) |
| 9471 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9472 | break; |
| 9473 | default: |
| 9474 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 9475 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9476 | |
| 9477 | // Window should receive motion event. |
| 9478 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9479 | // Spy window should also receive motion event |
| 9480 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9481 | } |
| 9482 | |
| 9483 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 9484 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 9485 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9486 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9487 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9488 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9489 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9490 | |
| 9491 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9492 | mDragWindow = |
| 9493 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9494 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9495 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 9496 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 9497 | {}, |
| 9498 | 0, |
| 9499 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9500 | |
| 9501 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9502 | bool transferred = |
| 9503 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9504 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9505 | if (transferred) { |
| 9506 | mWindow->consumeMotionCancel(); |
| 9507 | mDragWindow->consumeMotionDown(); |
| 9508 | } |
| 9509 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9510 | } |
| 9511 | }; |
| 9512 | |
| 9513 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9514 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9515 | |
| 9516 | // Move on window. |
| 9517 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9518 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9519 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9520 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9521 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9522 | mWindow->consumeDragEvent(false, 50, 50); |
| 9523 | mSecondWindow->assertNoEvents(); |
| 9524 | |
| 9525 | // Move to another window. |
| 9526 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9527 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9528 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9529 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9530 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9531 | mWindow->consumeDragEvent(true, 150, 50); |
| 9532 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9533 | |
| 9534 | // Move back to original window. |
| 9535 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9536 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9537 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9538 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9539 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9540 | mWindow->consumeDragEvent(false, 50, 50); |
| 9541 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 9542 | |
| 9543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9544 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9545 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 9546 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9547 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9548 | mWindow->assertNoEvents(); |
| 9549 | mSecondWindow->assertNoEvents(); |
| 9550 | } |
| 9551 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9552 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9553 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9554 | |
| 9555 | // No cancel event after drag start |
| 9556 | mSpyWindow->assertNoEvents(); |
| 9557 | |
| 9558 | const MotionEvent secondFingerDownEvent = |
| 9559 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9560 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9561 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9562 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9563 | .build(); |
| 9564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9565 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 9566 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9567 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9568 | |
| 9569 | // Receives cancel for first pointer after next pointer down |
| 9570 | mSpyWindow->consumeMotionCancel(); |
| 9571 | mSpyWindow->consumeMotionDown(); |
| 9572 | |
| 9573 | mSpyWindow->assertNoEvents(); |
| 9574 | } |
| 9575 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9576 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9577 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9578 | |
| 9579 | // Move on window. |
| 9580 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9581 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9582 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9583 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9584 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9585 | mWindow->consumeDragEvent(false, 50, 50); |
| 9586 | mSecondWindow->assertNoEvents(); |
| 9587 | |
| 9588 | // Move to another window. |
| 9589 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9590 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9591 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9592 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9593 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9594 | mWindow->consumeDragEvent(true, 150, 50); |
| 9595 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9596 | |
| 9597 | // drop to another window. |
| 9598 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9599 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9600 | {150, 50})) |
| 9601 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9602 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9603 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 9604 | mWindow->assertNoEvents(); |
| 9605 | mSecondWindow->assertNoEvents(); |
| 9606 | } |
| 9607 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9608 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9609 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9610 | |
| 9611 | // Move on window and keep button pressed. |
| 9612 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9613 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9614 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 9615 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9616 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9617 | .build())) |
| 9618 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9619 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9620 | mWindow->consumeDragEvent(false, 50, 50); |
| 9621 | mSecondWindow->assertNoEvents(); |
| 9622 | |
| 9623 | // Move to another window and release button, expect to drop item. |
| 9624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9625 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9626 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 9627 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9628 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9629 | .build())) |
| 9630 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9631 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9632 | mWindow->assertNoEvents(); |
| 9633 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9634 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9635 | |
| 9636 | // nothing to the window. |
| 9637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9638 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9639 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 9640 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9641 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 9642 | .build())) |
| 9643 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9644 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9645 | mWindow->assertNoEvents(); |
| 9646 | mSecondWindow->assertNoEvents(); |
| 9647 | } |
| 9648 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9649 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9650 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9651 | |
| 9652 | // Set second window invisible. |
| 9653 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9654 | mDispatcher->onWindowInfosChanged( |
| 9655 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9656 | |
| 9657 | // Move on window. |
| 9658 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9659 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9660 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9661 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9662 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9663 | mWindow->consumeDragEvent(false, 50, 50); |
| 9664 | mSecondWindow->assertNoEvents(); |
| 9665 | |
| 9666 | // Move to another window. |
| 9667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9668 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9669 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9670 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9671 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9672 | mWindow->consumeDragEvent(true, 150, 50); |
| 9673 | mSecondWindow->assertNoEvents(); |
| 9674 | |
| 9675 | // drop to another window. |
| 9676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9677 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9678 | {150, 50})) |
| 9679 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9680 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9681 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 9682 | mWindow->assertNoEvents(); |
| 9683 | mSecondWindow->assertNoEvents(); |
| 9684 | } |
| 9685 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9686 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9687 | // Ensure window could track pointerIds if it didn't support split touch. |
| 9688 | mWindow->setPreventSplitting(true); |
| 9689 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9690 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9691 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9692 | {50, 50})) |
| 9693 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9694 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9695 | |
| 9696 | const MotionEvent secondFingerDownEvent = |
| 9697 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9698 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9699 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9700 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9701 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9702 | .build(); |
| 9703 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9704 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9705 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9706 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9707 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9708 | |
| 9709 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9710 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9711 | } |
| 9712 | |
| 9713 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 9714 | // First down on second window. |
| 9715 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9716 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9717 | {150, 50})) |
| 9718 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9719 | |
| 9720 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9721 | |
| 9722 | // Second down on first window. |
| 9723 | const MotionEvent secondFingerDownEvent = |
| 9724 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9725 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9726 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9727 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9728 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9729 | .build(); |
| 9730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9731 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9732 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9733 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9734 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9735 | |
| 9736 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9737 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9738 | |
| 9739 | // Move on window. |
| 9740 | const MotionEvent secondFingerMoveEvent = |
| 9741 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 9742 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9743 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9744 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9745 | .build(); |
| 9746 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9747 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9748 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 9749 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9750 | mWindow->consumeDragEvent(false, 50, 50); |
| 9751 | mSecondWindow->consumeMotionMove(); |
| 9752 | |
| 9753 | // Release the drag pointer should perform drop. |
| 9754 | const MotionEvent secondFingerUpEvent = |
| 9755 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9756 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9757 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9758 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9759 | .build(); |
| 9760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9761 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9762 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 9763 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9764 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 9765 | mWindow->assertNoEvents(); |
| 9766 | mSecondWindow->consumeMotionMove(); |
| 9767 | } |
| 9768 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9769 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9770 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9771 | |
| 9772 | // Update window of second display. |
| 9773 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9774 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9775 | mDispatcher->onWindowInfosChanged( |
| 9776 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 9777 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 9778 | {}, |
| 9779 | 0, |
| 9780 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9781 | |
| 9782 | // Let second display has a touch state. |
| 9783 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9784 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9785 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9786 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9787 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9788 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9789 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 9790 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9791 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9792 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9793 | mDispatcher->onWindowInfosChanged( |
| 9794 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 9795 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 9796 | {}, |
| 9797 | 0, |
| 9798 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9799 | |
| 9800 | // Move on window. |
| 9801 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9802 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9803 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 9804 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9805 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9806 | mWindow->consumeDragEvent(false, 50, 50); |
| 9807 | mSecondWindow->assertNoEvents(); |
| 9808 | |
| 9809 | // Move to another window. |
| 9810 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9811 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9812 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 9813 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9814 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9815 | mWindow->consumeDragEvent(true, 150, 50); |
| 9816 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9817 | |
| 9818 | // drop to another window. |
| 9819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9820 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9821 | {150, 50})) |
| 9822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9823 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9824 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 9825 | mWindow->assertNoEvents(); |
| 9826 | mSecondWindow->assertNoEvents(); |
| 9827 | } |
| 9828 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9829 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 9830 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 9831 | // Move on window. |
| 9832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9833 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9834 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 9835 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9836 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9837 | .x(50) |
| 9838 | .y(50)) |
| 9839 | .build())) |
| 9840 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9841 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9842 | mWindow->consumeDragEvent(false, 50, 50); |
| 9843 | mSecondWindow->assertNoEvents(); |
| 9844 | |
| 9845 | // Move to another window. |
| 9846 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9847 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9848 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 9849 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9850 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9851 | .x(150) |
| 9852 | .y(50)) |
| 9853 | .build())) |
| 9854 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9855 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 9856 | mWindow->consumeDragEvent(true, 150, 50); |
| 9857 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 9858 | |
| 9859 | // drop to another window. |
| 9860 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9861 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9862 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 9863 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9864 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9865 | .x(150) |
| 9866 | .y(50)) |
| 9867 | .build())) |
| 9868 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9869 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 9870 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 9871 | mWindow->assertNoEvents(); |
| 9872 | mSecondWindow->assertNoEvents(); |
| 9873 | } |
| 9874 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 9875 | /** |
| 9876 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 9877 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 9878 | */ |
| 9879 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 9880 | // Down on second window |
| 9881 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9882 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9883 | {150, 50})) |
| 9884 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9885 | |
| 9886 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 9887 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 9888 | |
| 9889 | // Down on first window |
| 9890 | const MotionEvent secondFingerDownEvent = |
| 9891 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9892 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9893 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 9894 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 9895 | .build(); |
| 9896 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9897 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9898 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9899 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9900 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 9901 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 9902 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 9903 | |
| 9904 | // Start drag on first window |
| 9905 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 9906 | |
| 9907 | // Trigger cancel |
| 9908 | mDispatcher->cancelCurrentTouch(); |
| 9909 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
| 9910 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel()); |
| 9911 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 9912 | |
| 9913 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9914 | // The D&D finished with nullptr |
| 9915 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 9916 | |
| 9917 | // Remove drag window |
| 9918 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 9919 | |
| 9920 | // Inject a simple gesture, ensure dispatcher not crashed |
| 9921 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9922 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9923 | PointF{50, 50})) |
| 9924 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9925 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 9926 | |
| 9927 | const MotionEvent moveEvent = |
| 9928 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 9929 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9930 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9931 | .build(); |
| 9932 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9933 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 9934 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9935 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9936 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 9937 | |
| 9938 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9939 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9940 | {50, 50})) |
| 9941 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9942 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 9943 | } |
| 9944 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9945 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 9946 | |
| 9947 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 9948 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9949 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 9950 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9951 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9952 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9953 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9954 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9955 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9956 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9957 | |
| 9958 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9959 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9960 | window->assertNoEvents(); |
| 9961 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9962 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9963 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 9964 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 9965 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9966 | window->assertNoEvents(); |
| 9967 | |
| 9968 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9969 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9970 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9971 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9972 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9973 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 9974 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9975 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9976 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9977 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9978 | window->assertNoEvents(); |
| 9979 | } |
| 9980 | |
| 9981 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 9982 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 9983 | std::make_shared<FakeApplicationHandle>(); |
| 9984 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9985 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 9986 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9987 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9988 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9989 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9990 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9991 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 9992 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9993 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9994 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9995 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9996 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9997 | mDispatcher->onWindowInfosChanged( |
| 9998 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 9999 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10000 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10001 | |
| 10002 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10003 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10004 | window->assertNoEvents(); |
| 10005 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10006 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10007 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10008 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10009 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10010 | window->assertNoEvents(); |
| 10011 | |
| 10012 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10013 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10014 | mDispatcher->onWindowInfosChanged( |
| 10015 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10016 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10017 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10018 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10019 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10020 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10021 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10022 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 10023 | window->assertNoEvents(); |
| 10024 | } |
| 10025 | |
| 10026 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 10027 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10028 | std::make_shared<FakeApplicationHandle>(); |
| 10029 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10030 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10031 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10032 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10033 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10034 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10035 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10036 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10037 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10038 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10039 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10040 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10041 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10042 | mDispatcher->onWindowInfosChanged( |
| 10043 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10044 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10045 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10046 | |
| 10047 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10048 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10049 | window->assertNoEvents(); |
| 10050 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10051 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10052 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10053 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10054 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10055 | window->assertNoEvents(); |
| 10056 | |
| 10057 | // 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] | 10058 | mDispatcher->onWindowInfosChanged( |
| 10059 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10060 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10061 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10062 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10063 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10064 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10065 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10066 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10067 | window->assertNoEvents(); |
| 10068 | } |
| 10069 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10070 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 10071 | protected: |
| 10072 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10073 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10074 | sp<FakeWindowHandle> mWindow; |
| 10075 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10076 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10077 | |
| 10078 | void SetUp() override { |
| 10079 | InputDispatcherTest::SetUp(); |
| 10080 | |
| 10081 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10082 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10083 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10084 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10085 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10086 | mSecondWindow = |
| 10087 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10088 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10089 | mThirdWindow = |
| 10090 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 10091 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 10092 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10093 | |
| 10094 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10095 | mDispatcher->onWindowInfosChanged( |
| 10096 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 10097 | {}, |
| 10098 | 0, |
| 10099 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10100 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10101 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10102 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10103 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10104 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10105 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10106 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 10107 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10108 | mThirdWindow->assertNoEvents(); |
| 10109 | } |
| 10110 | |
| 10111 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 10112 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10113 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10114 | SECOND_DISPLAY_ID)) { |
| 10115 | mWindow->assertNoEvents(); |
| 10116 | mSecondWindow->assertNoEvents(); |
| 10117 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10118 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10119 | } |
| 10120 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10121 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10122 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 10123 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 10124 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10125 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 10126 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10127 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10128 | } |
| 10129 | }; |
| 10130 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10131 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 10132 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10133 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 10134 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10135 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10136 | } |
| 10137 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10138 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 10139 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10140 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10141 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10142 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10143 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10144 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 10145 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10146 | mWindow->assertNoEvents(); |
| 10147 | mSecondWindow->assertNoEvents(); |
| 10148 | } |
| 10149 | |
| 10150 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 10151 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10152 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10153 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10154 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10155 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10156 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10157 | } |
| 10158 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10159 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 10160 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10161 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 10162 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10163 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10164 | mWindow->assertNoEvents(); |
| 10165 | mSecondWindow->assertNoEvents(); |
| 10166 | } |
| 10167 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10168 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 10169 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 10170 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 10171 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10172 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10173 | mWindow->assertNoEvents(); |
| 10174 | mSecondWindow->assertNoEvents(); |
| 10175 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 10176 | } |
| 10177 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10178 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 10179 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10180 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10181 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10182 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10183 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 10184 | |
| 10185 | // Then remove focus. |
| 10186 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10187 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10188 | |
| 10189 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 10190 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 10191 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 10192 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10193 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10194 | } |
| 10195 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10196 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 10197 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10198 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10199 | std::shared_ptr<FakeApplicationHandle> application = |
| 10200 | std::make_shared<FakeApplicationHandle>(); |
| 10201 | std::string name = "Fake Spy "; |
| 10202 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10203 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10204 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10205 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10206 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10207 | return spy; |
| 10208 | } |
| 10209 | |
| 10210 | sp<FakeWindowHandle> createForeground() { |
| 10211 | std::shared_ptr<FakeApplicationHandle> application = |
| 10212 | std::make_shared<FakeApplicationHandle>(); |
| 10213 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10214 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 10215 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10216 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10217 | return window; |
| 10218 | } |
| 10219 | |
| 10220 | private: |
| 10221 | int mSpyCount{0}; |
| 10222 | }; |
| 10223 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10224 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10225 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10226 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 10227 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10228 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 10229 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10230 | ScopedSilentDeath _silentDeath; |
| 10231 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10232 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10233 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10234 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10235 | ".* not a trusted overlay"); |
| 10236 | } |
| 10237 | |
| 10238 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10239 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 10240 | */ |
| 10241 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10242 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10243 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10244 | |
| 10245 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10246 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10247 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10248 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10249 | } |
| 10250 | |
| 10251 | /** |
| 10252 | * Verify the order in which different input windows receive events. The touched foreground window |
| 10253 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 10254 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 10255 | * receive events before ones belows it. |
| 10256 | * |
| 10257 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 10258 | * spy1, spy2, window, spy3. |
| 10259 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 10260 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 10261 | * window. |
| 10262 | */ |
| 10263 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 10264 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10265 | auto spy1 = createSpy(); |
| 10266 | auto spy2 = createSpy(); |
| 10267 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10268 | mDispatcher->onWindowInfosChanged( |
| 10269 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10270 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 10271 | const size_t numChannels = channels.size(); |
| 10272 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 10273 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10274 | if (!epollFd.ok()) { |
| 10275 | FAIL() << "Failed to create epoll fd"; |
| 10276 | } |
| 10277 | |
| 10278 | for (size_t i = 0; i < numChannels; i++) { |
| 10279 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 10280 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 10281 | FAIL() << "Failed to add fd to epoll"; |
| 10282 | } |
| 10283 | } |
| 10284 | |
| 10285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10286 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10287 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10288 | |
| 10289 | std::vector<size_t> eventOrder; |
| 10290 | std::vector<struct epoll_event> events(numChannels); |
| 10291 | for (;;) { |
| 10292 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 10293 | (100ms).count()); |
| 10294 | if (nFds < 0) { |
| 10295 | FAIL() << "Failed to call epoll_wait"; |
| 10296 | } |
| 10297 | if (nFds == 0) { |
| 10298 | break; // epoll_wait timed out |
| 10299 | } |
| 10300 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 10301 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 10302 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10303 | channels[i]->consumeMotionDown(); |
| 10304 | } |
| 10305 | } |
| 10306 | |
| 10307 | // Verify the order in which the events were received. |
| 10308 | EXPECT_EQ(3u, eventOrder.size()); |
| 10309 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 10310 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 10311 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 10312 | } |
| 10313 | |
| 10314 | /** |
| 10315 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 10316 | */ |
| 10317 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 10318 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10319 | auto spy = createSpy(); |
| 10320 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10321 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10322 | |
| 10323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10324 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10325 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10326 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10327 | spy->assertNoEvents(); |
| 10328 | } |
| 10329 | |
| 10330 | /** |
| 10331 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 10332 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 10333 | * to the window. |
| 10334 | */ |
| 10335 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 10336 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10337 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10338 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10339 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10340 | |
| 10341 | // Inject an event outside the spy window's touchable region. |
| 10342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10343 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10344 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10345 | window->consumeMotionDown(); |
| 10346 | spy->assertNoEvents(); |
| 10347 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10348 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10349 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10350 | window->consumeMotionUp(); |
| 10351 | spy->assertNoEvents(); |
| 10352 | |
| 10353 | // Inject an event inside the spy window's touchable region. |
| 10354 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10355 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10356 | {5, 10})) |
| 10357 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10358 | window->consumeMotionDown(); |
| 10359 | spy->consumeMotionDown(); |
| 10360 | } |
| 10361 | |
| 10362 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10363 | * 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] | 10364 | * 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] | 10365 | */ |
| 10366 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 10367 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10368 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10369 | auto spy = createSpy(); |
| 10370 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10371 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10372 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10373 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10374 | |
| 10375 | // Inject an event outside the spy window's frame and touchable region. |
| 10376 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10377 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10378 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10379 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10380 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10381 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10382 | } |
| 10383 | |
| 10384 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10385 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 10386 | * pointers that are down within its bounds. |
| 10387 | */ |
| 10388 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 10389 | auto windowLeft = createForeground(); |
| 10390 | windowLeft->setFrame({0, 0, 100, 200}); |
| 10391 | auto windowRight = createForeground(); |
| 10392 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10393 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10394 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10395 | mDispatcher->onWindowInfosChanged( |
| 10396 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10397 | |
| 10398 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10399 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10400 | {50, 50})) |
| 10401 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10402 | windowLeft->consumeMotionDown(); |
| 10403 | spy->consumeMotionDown(); |
| 10404 | |
| 10405 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10406 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10407 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10408 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10409 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10410 | .build(); |
| 10411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10412 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10413 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10414 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10415 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10416 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10417 | } |
| 10418 | |
| 10419 | /** |
| 10420 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 10421 | * the spy should receive the second pointer with ACTION_DOWN. |
| 10422 | */ |
| 10423 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 10424 | auto window = createForeground(); |
| 10425 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10426 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10427 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10428 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10429 | |
| 10430 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10431 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10432 | {50, 50})) |
| 10433 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10434 | window->consumeMotionDown(); |
| 10435 | spyRight->assertNoEvents(); |
| 10436 | |
| 10437 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10438 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10439 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10440 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10441 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10442 | .build(); |
| 10443 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10444 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10445 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10446 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10447 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10448 | spyRight->consumeMotionDown(); |
| 10449 | } |
| 10450 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10451 | /** |
| 10452 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 10453 | * windows should be allowed to control split touch. |
| 10454 | */ |
| 10455 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 10456 | // 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] | 10457 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10458 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 10459 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10460 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10461 | auto window = createForeground(); |
| 10462 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10463 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10464 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10465 | |
| 10466 | // First finger down, no window touched. |
| 10467 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10468 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10469 | {100, 200})) |
| 10470 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10471 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10472 | window->assertNoEvents(); |
| 10473 | |
| 10474 | // Second finger down on window, the window should receive touch down. |
| 10475 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 10476 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10477 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10478 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10479 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10480 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10481 | .build(); |
| 10482 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10483 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10484 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10485 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10486 | |
| 10487 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10488 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10489 | } |
| 10490 | |
| 10491 | /** |
| 10492 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 10493 | * do not receive key events. |
| 10494 | */ |
| 10495 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10496 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10497 | spy->setFocusable(false); |
| 10498 | |
| 10499 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10500 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10501 | setFocusedWindow(window); |
| 10502 | window->consumeFocusEvent(true); |
| 10503 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10504 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10505 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10506 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 10507 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10508 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10509 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10510 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 10511 | |
| 10512 | spy->assertNoEvents(); |
| 10513 | } |
| 10514 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10515 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 10516 | |
| 10517 | /** |
| 10518 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 10519 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 10520 | */ |
| 10521 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 10522 | auto window = createForeground(); |
| 10523 | auto spy1 = createSpy(); |
| 10524 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10525 | mDispatcher->onWindowInfosChanged( |
| 10526 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10527 | |
| 10528 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10529 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10530 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10531 | window->consumeMotionDown(); |
| 10532 | spy1->consumeMotionDown(); |
| 10533 | spy2->consumeMotionDown(); |
| 10534 | |
| 10535 | // Pilfer pointers from the second spy window. |
| 10536 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 10537 | spy2->assertNoEvents(); |
| 10538 | spy1->consumeMotionCancel(); |
| 10539 | window->consumeMotionCancel(); |
| 10540 | |
| 10541 | // The rest of the gesture should only be sent to the second spy window. |
| 10542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10543 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10544 | ADISPLAY_ID_DEFAULT)) |
| 10545 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10546 | spy2->consumeMotionMove(); |
| 10547 | spy1->assertNoEvents(); |
| 10548 | window->assertNoEvents(); |
| 10549 | } |
| 10550 | |
| 10551 | /** |
| 10552 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 10553 | * in the middle of the gesture. |
| 10554 | */ |
| 10555 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 10556 | auto window = createForeground(); |
| 10557 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10558 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10559 | |
| 10560 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10561 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10562 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10563 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10564 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10565 | |
| 10566 | window->releaseChannel(); |
| 10567 | |
| 10568 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10569 | |
| 10570 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10571 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10572 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10573 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10574 | } |
| 10575 | |
| 10576 | /** |
| 10577 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 10578 | * the spy, but not to any other windows. |
| 10579 | */ |
| 10580 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 10581 | auto spy = createSpy(); |
| 10582 | auto window = createForeground(); |
| 10583 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10584 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10585 | |
| 10586 | // First finger down on the window and the spy. |
| 10587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10588 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10589 | {100, 200})) |
| 10590 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10591 | spy->consumeMotionDown(); |
| 10592 | window->consumeMotionDown(); |
| 10593 | |
| 10594 | // Spy window pilfers the pointers. |
| 10595 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10596 | window->consumeMotionCancel(); |
| 10597 | |
| 10598 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 10599 | const MotionEvent secondFingerDownEvent = |
| 10600 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10601 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10602 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10603 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10604 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10605 | .build(); |
| 10606 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10607 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10608 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10609 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10610 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10611 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10612 | |
| 10613 | // Third finger goes down outside all windows, so injection should fail. |
| 10614 | const MotionEvent thirdFingerDownEvent = |
| 10615 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10616 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10617 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10618 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 10619 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 10620 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10621 | .build(); |
| 10622 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10623 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10624 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 10625 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10626 | |
| 10627 | spy->assertNoEvents(); |
| 10628 | window->assertNoEvents(); |
| 10629 | } |
| 10630 | |
| 10631 | /** |
| 10632 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 10633 | */ |
| 10634 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 10635 | auto spy = createSpy(); |
| 10636 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 10637 | auto window = createForeground(); |
| 10638 | window->setFrame(Rect(0, 0, 200, 200)); |
| 10639 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10640 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10641 | |
| 10642 | // First finger down on the window only |
| 10643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10644 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10645 | {150, 150})) |
| 10646 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10647 | window->consumeMotionDown(); |
| 10648 | |
| 10649 | // Second finger down on the spy and window |
| 10650 | const MotionEvent secondFingerDownEvent = |
| 10651 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10652 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10653 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10654 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 10655 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10656 | .build(); |
| 10657 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10658 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10659 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10660 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10661 | spy->consumeMotionDown(); |
| 10662 | window->consumeMotionPointerDown(1); |
| 10663 | |
| 10664 | // Third finger down on the spy and window |
| 10665 | const MotionEvent thirdFingerDownEvent = |
| 10666 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10667 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10668 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10669 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 10670 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 10671 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10672 | .build(); |
| 10673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10674 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10675 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10676 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10677 | spy->consumeMotionPointerDown(1); |
| 10678 | window->consumeMotionPointerDown(2); |
| 10679 | |
| 10680 | // Spy window pilfers the pointers. |
| 10681 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 10682 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 10683 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10684 | |
| 10685 | spy->assertNoEvents(); |
| 10686 | window->assertNoEvents(); |
| 10687 | } |
| 10688 | |
| 10689 | /** |
| 10690 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 10691 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 10692 | * window, then that window should receive ACTION_CANCEL. |
| 10693 | */ |
| 10694 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 10695 | auto spy = createSpy(); |
| 10696 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 10697 | auto window = createForeground(); |
| 10698 | window->setFrame(Rect(0, 0, 200, 200)); |
| 10699 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10700 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10701 | |
| 10702 | // First finger down on both spy and window |
| 10703 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10704 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10705 | {10, 10})) |
| 10706 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10707 | window->consumeMotionDown(); |
| 10708 | spy->consumeMotionDown(); |
| 10709 | |
| 10710 | // Second finger down on the spy and window |
| 10711 | const MotionEvent secondFingerDownEvent = |
| 10712 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10713 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10714 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10715 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 10716 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10717 | .build(); |
| 10718 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10719 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10720 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10721 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10722 | spy->consumeMotionPointerDown(1); |
| 10723 | window->consumeMotionPointerDown(1); |
| 10724 | |
| 10725 | // Spy window pilfers the pointers. |
| 10726 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10727 | window->consumeMotionCancel(); |
| 10728 | |
| 10729 | spy->assertNoEvents(); |
| 10730 | window->assertNoEvents(); |
| 10731 | } |
| 10732 | |
| 10733 | /** |
| 10734 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 10735 | * be sent to other windows |
| 10736 | */ |
| 10737 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 10738 | auto spy = createSpy(); |
| 10739 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 10740 | auto window = createForeground(); |
| 10741 | window->setFrame(Rect(0, 0, 200, 200)); |
| 10742 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10743 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10744 | |
| 10745 | // First finger down on both window and spy |
| 10746 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10747 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10748 | {10, 10})) |
| 10749 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10750 | window->consumeMotionDown(); |
| 10751 | spy->consumeMotionDown(); |
| 10752 | |
| 10753 | // Spy window pilfers the pointers. |
| 10754 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 10755 | window->consumeMotionCancel(); |
| 10756 | |
| 10757 | // Second finger down on the window only |
| 10758 | const MotionEvent secondFingerDownEvent = |
| 10759 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10760 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10761 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10762 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 10763 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10764 | .build(); |
| 10765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10766 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 10767 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10768 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10769 | window->consumeMotionDown(); |
| 10770 | window->assertNoEvents(); |
| 10771 | |
| 10772 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 10773 | spy->consumeMotionMove(); |
| 10774 | spy->assertNoEvents(); |
| 10775 | } |
| 10776 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10777 | /** |
| 10778 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 10779 | * windows, and spanning both left and right windows. |
| 10780 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 10781 | * to the right window. |
| 10782 | * Pilfer from spy window. |
| 10783 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 10784 | */ |
| 10785 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 10786 | sp<FakeWindowHandle> spy = createSpy(); |
| 10787 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 10788 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 10789 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 10790 | |
| 10791 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 10792 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 10793 | |
| 10794 | constexpr int32_t stylusDeviceId = 1; |
| 10795 | constexpr int32_t touchDeviceId = 2; |
| 10796 | |
| 10797 | mDispatcher->onWindowInfosChanged( |
| 10798 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 10799 | |
| 10800 | // Stylus down on left window and spy |
| 10801 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 10802 | .deviceId(stylusDeviceId) |
| 10803 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 10804 | .build()); |
| 10805 | leftWindow->consumeMotionEvent( |
| 10806 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 10807 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 10808 | |
| 10809 | // Finger down on right window and spy - but spy already has stylus |
| 10810 | mDispatcher->notifyMotion( |
| 10811 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10812 | .deviceId(touchDeviceId) |
| 10813 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 10814 | .build()); |
| 10815 | rightWindow->consumeMotionEvent( |
| 10816 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10817 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10818 | |
| 10819 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 10820 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10821 | leftWindow->consumeMotionEvent( |
| 10822 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10823 | rightWindow->consumeMotionEvent( |
| 10824 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 10825 | |
| 10826 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 10827 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10828 | .deviceId(stylusDeviceId) |
| 10829 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 10830 | .build()); |
| 10831 | mDispatcher->notifyMotion( |
| 10832 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10833 | .deviceId(touchDeviceId) |
| 10834 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 10835 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10836 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10837 | |
| 10838 | spy->assertNoEvents(); |
| 10839 | leftWindow->assertNoEvents(); |
| 10840 | rightWindow->assertNoEvents(); |
| 10841 | } |
| 10842 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10843 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 10844 | public: |
| 10845 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 10846 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 10847 | std::make_shared<FakeApplicationHandle>(); |
| 10848 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10849 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 10850 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10851 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10852 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10853 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10854 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10855 | overlay->setTrustedOverlay(true); |
| 10856 | |
| 10857 | std::shared_ptr<FakeApplicationHandle> application = |
| 10858 | std::make_shared<FakeApplicationHandle>(); |
| 10859 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10860 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 10861 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10862 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10863 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10864 | |
| 10865 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10866 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10867 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10868 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10869 | return {std::move(overlay), std::move(window)}; |
| 10870 | } |
| 10871 | |
| 10872 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10873 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10874 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10875 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10876 | } |
| 10877 | |
| 10878 | void sendStylusEvent(int32_t action) { |
| 10879 | NotifyMotionArgs motionArgs = |
| 10880 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 10881 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10882 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10883 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10884 | } |
| 10885 | }; |
| 10886 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10887 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 10888 | |
| 10889 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 10890 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10891 | ScopedSilentDeath _silentDeath; |
| 10892 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10893 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 10894 | overlay->setTrustedOverlay(false); |
| 10895 | // 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] | 10896 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 10897 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10898 | ".* not a trusted overlay"); |
| 10899 | } |
| 10900 | |
| 10901 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 10902 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10903 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10904 | |
| 10905 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10906 | overlay->consumeMotionDown(); |
| 10907 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 10908 | overlay->consumeMotionUp(); |
| 10909 | |
| 10910 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10911 | window->consumeMotionDown(); |
| 10912 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 10913 | window->consumeMotionUp(); |
| 10914 | |
| 10915 | overlay->assertNoEvents(); |
| 10916 | window->assertNoEvents(); |
| 10917 | } |
| 10918 | |
| 10919 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 10920 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10921 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10922 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 10923 | |
| 10924 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10925 | overlay->consumeMotionDown(); |
| 10926 | window->consumeMotionDown(); |
| 10927 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 10928 | overlay->consumeMotionUp(); |
| 10929 | window->consumeMotionUp(); |
| 10930 | |
| 10931 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10932 | window->consumeMotionDown(); |
| 10933 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 10934 | window->consumeMotionUp(); |
| 10935 | |
| 10936 | overlay->assertNoEvents(); |
| 10937 | window->assertNoEvents(); |
| 10938 | } |
| 10939 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 10940 | /** |
| 10941 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 10942 | * The scenario is as follows: |
| 10943 | * - The stylus interceptor overlay is configured as a spy window. |
| 10944 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 10945 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 10946 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 10947 | */ |
| 10948 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 10949 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 10950 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10951 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 10952 | |
| 10953 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 10954 | overlay->consumeMotionDown(); |
| 10955 | window->consumeMotionDown(); |
| 10956 | |
| 10957 | // The interceptor pilfers the pointers. |
| 10958 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 10959 | window->consumeMotionCancel(); |
| 10960 | |
| 10961 | // The interceptor configures itself so that it is no longer a spy. |
| 10962 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10963 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 10964 | |
| 10965 | // It continues to receive the rest of the stylus gesture. |
| 10966 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 10967 | overlay->consumeMotionMove(); |
| 10968 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 10969 | overlay->consumeMotionUp(); |
| 10970 | |
| 10971 | window->assertNoEvents(); |
| 10972 | } |
| 10973 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10974 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10975 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10976 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10977 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 10978 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 10979 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10980 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10981 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 10982 | |
| 10983 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10984 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10985 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 10986 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 10987 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 10988 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 10989 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 10990 | } |
| 10991 | |
| 10992 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10993 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10994 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10995 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10996 | mPolicyFlags); |
| 10997 | } |
| 10998 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 10999 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11000 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11001 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11002 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11003 | name, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11004 | window->setOwnerInfo(mPid, mUid); |
| 11005 | return window; |
| 11006 | } |
| 11007 | }; |
| 11008 | |
| 11009 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 11010 | |
| 11011 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11012 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11013 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11014 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11015 | |
| 11016 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11017 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11018 | window->consumeMotionDown(); |
| 11019 | |
| 11020 | setFocusedWindow(window); |
| 11021 | window->consumeFocusEvent(true); |
| 11022 | |
| 11023 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11024 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11025 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11026 | } |
| 11027 | |
| 11028 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11029 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11030 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11031 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11032 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11033 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11034 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11035 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11036 | |
| 11037 | setFocusedWindow(window); |
| 11038 | window->consumeFocusEvent(true); |
| 11039 | |
| 11040 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11041 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11042 | window->assertNoEvents(); |
| 11043 | } |
| 11044 | |
| 11045 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11046 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11047 | auto window = owner.createWindow("Owned window"); |
| 11048 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11049 | spy->setSpy(true); |
| 11050 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11051 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11052 | |
| 11053 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11054 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11055 | spy->consumeMotionDown(); |
| 11056 | window->consumeMotionDown(); |
| 11057 | } |
| 11058 | |
| 11059 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11060 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11061 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11062 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11063 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11064 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11065 | randosSpy->setSpy(true); |
| 11066 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11067 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11068 | |
| 11069 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 11070 | // not receive the event. |
| 11071 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11072 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11073 | randosSpy->assertNoEvents(); |
| 11074 | window->consumeMotionDown(); |
| 11075 | } |
| 11076 | |
| 11077 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11078 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11079 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11080 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11081 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11082 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11083 | randosSpy->setSpy(true); |
| 11084 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11085 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11086 | |
| 11087 | // A user that has injection permission can inject into any window. |
| 11088 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11089 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11090 | ADISPLAY_ID_DEFAULT)); |
| 11091 | randosSpy->consumeMotionDown(); |
| 11092 | window->consumeMotionDown(); |
| 11093 | |
| 11094 | setFocusedWindow(randosSpy); |
| 11095 | randosSpy->consumeFocusEvent(true); |
| 11096 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11097 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11098 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11099 | window->assertNoEvents(); |
| 11100 | } |
| 11101 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11102 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11103 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11104 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11105 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11106 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11107 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11108 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 11109 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11110 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11111 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11112 | // 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] | 11113 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11114 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11115 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11116 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11117 | } |
| 11118 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 11119 | } // namespace android::inputdispatcher |