blob: 4c455f7804802b70e8a7e19c6c1659f93569d45a [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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 Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000018#include "../BlockingQueue.h"
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070019#include "FakeApplicationHandle.h"
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000020#include "FakeInputTracingBackend.h"
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000021#include "TestEventMatchers.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080022
Cody Heiner166a5af2023-07-07 12:25:00 -070023#include <NotifyArgsBuilders.h>
Prabir Pradhan5893d362023-11-17 04:30:40 +000024#include <android-base/logging.h>
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070025#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080026#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080027#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070028#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070029#include <binder/Binder.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000030#include <com_android_input_flags.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000031#include <fcntl.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000032#include <flag_macros.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080033#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100035#include <input/Input.h>
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070036#include <input/PrintTools.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080038#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100039
Garfield Tan1c7bc862020-01-28 13:24:04 -080040#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080041#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070042#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080043#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080044#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Garfield Tan1c7bc862020-01-28 13:24:04 -080046using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050047using android::gui::FocusRequest;
48using android::gui::TouchOcclusionMode;
49using android::gui::WindowInfo;
50using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080051using android::os::InputEventInjectionResult;
52using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080053
Garfield Tane84e6f92019-08-29 17:28:41 -070054namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
Dominik Laskowski2f01d772022-03-23 16:01:29 -070056using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080057using testing::AllOf;
Prabir Pradhan5893d362023-11-17 04:30:40 +000058using testing::Not;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070059
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070060namespace {
61
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000063static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080064
65// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000066static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080067static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080068
Jeff Brownf086ddb2014-02-11 14:28:48 -080069// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000070static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
71static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080072
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000073// Ensure common actions are interchangeable between keys and motions for convenience.
74static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN);
75static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080076static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
77static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
78static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
79static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070080static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080081static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070082static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080083static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080084static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080085/**
86 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
87 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
88 * index 0) is the new pointer going down. The same pointer could have been placed at a different
89 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
90 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
91 * pointer id=0 leaves but the pointer id=1 remains.
92 */
93static constexpr int32_t POINTER_0_DOWN =
94 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080095static constexpr int32_t POINTER_1_DOWN =
96 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000097static constexpr int32_t POINTER_2_DOWN =
98 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000099static constexpr int32_t POINTER_3_DOWN =
100 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +0000101static constexpr int32_t POINTER_0_UP =
102 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800103static constexpr int32_t POINTER_1_UP =
104 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +0000105static constexpr int32_t POINTER_2_UP =
106 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800107
Antonio Kantek15beb512022-06-13 22:35:41 +0000108// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000109static constexpr gui::Pid WINDOW_PID{999};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000110static constexpr gui::Uid WINDOW_UID{1001};
Prabir Pradhan5735a322022-04-11 17:23:34 +0000111
Antonio Kantek15beb512022-06-13 22:35:41 +0000112// The default pid and uid for the windows created on the secondary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000113static constexpr gui::Pid SECONDARY_WINDOW_PID{1010};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000114static constexpr gui::Uid SECONDARY_WINDOW_UID{1012};
Antonio Kantek15beb512022-06-13 22:35:41 +0000115
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000116// An arbitrary pid of the gesture monitor window
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000117static constexpr gui::Pid MONITOR_PID{2001};
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000118
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700119/**
120 * If we expect to receive the event, the timeout can be made very long. When the test are running
121 * correctly, we will actually never wait until the end of the timeout because the wait will end
122 * when the event comes in. Still, this value shouldn't be infinite. During development, a local
123 * change may cause the test to fail. This timeout should be short enough to not annoy so that the
124 * developer can see the failure quickly (on human scale).
125 */
126static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms;
127/**
128 * When no event is expected, we can have a very short timeout. A large value here would slow down
129 * the tests. In the unlikely event of system being too slow, the event may still be present but the
130 * timeout would complete before it is consumed. This would result in test flakiness. If this
131 * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this
132 * would get noticed and addressed quickly.
133 */
134static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms;
135
Arthur Hungc539dbb2022-12-08 07:45:36 +0000136static constexpr int expectedWallpaperFlags =
137 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
138
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800139using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
140
Gang Wang342c9272020-01-13 13:15:04 -0500141/**
142 * Return a DOWN key event with KEYCODE_A.
143 */
144static KeyEvent getTestKeyEvent() {
145 KeyEvent event;
146
Garfield Tanfbe732e2020-01-24 11:26:14 -0800147 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
148 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
149 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500150 return event;
151}
152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153// --- FakeInputDispatcherPolicy ---
154
155class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000156 struct AnrResult {
157 sp<IBinder> token{};
Prabir Pradhanfc364722024-02-08 17:51:20 +0000158 std::optional<gui::Pid> pid{};
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000159 };
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800160 /* Stores data about a user-activity-poke event from the dispatcher. */
161 struct UserActivityPokeEvent {
162 nsecs_t eventTime;
163 int32_t eventType;
164 int32_t displayId;
165
166 bool operator==(const UserActivityPokeEvent& rhs) const = default;
167
168 friend std::ostream& operator<<(std::ostream& os, const UserActivityPokeEvent& ev) {
169 os << "UserActivityPokeEvent[time=" << ev.eventTime << ", eventType=" << ev.eventType
170 << ", displayId=" << ev.displayId << "]";
171 return os;
172 }
173 };
Prabir Pradhanedd96402022-02-15 01:46:16 -0800174
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000176 FakeInputDispatcherPolicy() = default;
177 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800178
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800179 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700180 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700181 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700182 EXPECT_EQ(event.getDisplayId(), args.displayId);
183
184 const auto& keyEvent = static_cast<const KeyEvent&>(event);
185 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
186 EXPECT_EQ(keyEvent.getAction(), args.action);
187 });
Jackal Guof9696682018-10-05 12:23:23 +0800188 }
189
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700190 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
191 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700192 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700193 EXPECT_EQ(event.getDisplayId(), args.displayId);
194
195 const auto& motionEvent = static_cast<const MotionEvent&>(event);
196 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
197 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000198 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
199 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
200 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
201 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700202 });
Jackal Guof9696682018-10-05 12:23:23 +0800203 }
204
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700205 void assertFilterInputEventWasNotCalled() {
206 std::scoped_lock lock(mLock);
207 ASSERT_EQ(nullptr, mFilteredEvent);
208 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800210 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700211 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800212 ASSERT_TRUE(mConfigurationChangedTime)
213 << "Timed out waiting for configuration changed call";
214 ASSERT_EQ(*mConfigurationChangedTime, when);
215 mConfigurationChangedTime = std::nullopt;
216 }
217
218 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700219 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800220 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800221 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800222 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
223 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
224 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
225 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
226 mLastNotifySwitch = std::nullopt;
227 }
228
chaviwfd6d3512019-03-25 13:23:49 -0700229 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700230 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800231 ASSERT_EQ(touchedToken, mOnPointerDownToken);
232 mOnPointerDownToken.clear();
233 }
234
235 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700236 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800237 ASSERT_TRUE(mOnPointerDownToken == nullptr)
238 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700239 }
240
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700241 // This function must be called soon after the expected ANR timer starts,
242 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500243 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700244 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500245 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800246 std::unique_lock lock(mLock);
247 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500248 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800249 ASSERT_NO_FATAL_FAILURE(
250 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500251 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700252 }
253
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000254 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800255 const sp<WindowInfoHandle>& window) {
256 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
257 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
258 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500259 }
260
Prabir Pradhanedd96402022-02-15 01:46:16 -0800261 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
262 const sp<IBinder>& expectedToken,
Prabir Pradhanfc364722024-02-08 17:51:20 +0000263 std::optional<gui::Pid> expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800264 std::unique_lock lock(mLock);
265 android::base::ScopedLockAssertion assumeLocked(mLock);
266 AnrResult result;
267 ASSERT_NO_FATAL_FAILURE(result =
268 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000269 ASSERT_EQ(expectedToken, result.token);
270 ASSERT_EQ(expectedPid, result.pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500271 }
272
Prabir Pradhanedd96402022-02-15 01:46:16 -0800273 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000274 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500275 std::unique_lock lock(mLock);
276 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800277 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
278 const auto& [token, _] = result;
279 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000280 }
281
Prabir Pradhanedd96402022-02-15 01:46:16 -0800282 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
Prabir Pradhanfc364722024-02-08 17:51:20 +0000283 std::optional<gui::Pid> expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800284 std::unique_lock lock(mLock);
285 android::base::ScopedLockAssertion assumeLocked(mLock);
286 AnrResult result;
287 ASSERT_NO_FATAL_FAILURE(
288 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000289 ASSERT_EQ(expectedToken, result.token);
290 ASSERT_EQ(expectedPid, result.pid);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800291 }
292
293 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000294 sp<IBinder> getResponsiveWindowToken() {
295 std::unique_lock lock(mLock);
296 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800297 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
298 const auto& [token, _] = result;
299 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700300 }
301
302 void assertNotifyAnrWasNotCalled() {
303 std::scoped_lock lock(mLock);
304 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800305 ASSERT_TRUE(mAnrWindows.empty());
306 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500307 << "ANR was not called, but please also consume the 'connection is responsive' "
308 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700309 }
310
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000311 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800312 std::unique_lock lock(mLock);
313 base::ScopedLockAssertion assumeLocked(mLock);
314
315 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
316 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000317 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800318 enabled;
319 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000320 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
321 << ") to be called.";
322 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800323 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000324 auto request = *mPointerCaptureRequest;
325 mPointerCaptureRequest.reset();
326 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800327 }
328
329 void assertSetPointerCaptureNotCalled() {
330 std::unique_lock lock(mLock);
331 base::ScopedLockAssertion assumeLocked(mLock);
332
333 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000334 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800335 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000336 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800337 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000338 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800339 }
340
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -0700341 void assertDropTargetEquals(const InputDispatcherInterface& dispatcher,
342 const sp<IBinder>& targetToken) {
343 dispatcher.waitForIdle();
arthurhungf452d0b2021-01-06 00:19:52 +0800344 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800345 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800346 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800347 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800348 }
349
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800350 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
351 std::unique_lock lock(mLock);
352 base::ScopedLockAssertion assumeLocked(mLock);
353 std::optional<sp<IBinder>> receivedToken =
354 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
355 mNotifyInputChannelBroken);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000356 ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token";
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800357 ASSERT_EQ(token, *receivedToken);
358 }
359
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800360 /**
361 * Set policy timeout. A value of zero means next key will not be intercepted.
362 */
363 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
364 mInterceptKeyTimeout = timeout;
365 }
366
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -0800367 std::chrono::nanoseconds getKeyWaitingForEventsTimeout() override { return 500ms; }
368
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700369 void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; }
370
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800371 void assertUserActivityNotPoked() {
372 std::unique_lock lock(mLock);
373 base::ScopedLockAssertion assumeLocked(mLock);
374
375 std::optional<UserActivityPokeEvent> pokeEvent =
376 getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock,
377 mNotifyUserActivity);
378
379 ASSERT_FALSE(pokeEvent) << "Expected user activity not to have been poked";
Josep del Riob3981622023-04-18 15:49:45 +0000380 }
381
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800382 /**
383 * Asserts that a user activity poke has happened. The earliest recorded poke event will be
384 * cleared after this call.
385 *
386 * If an expected UserActivityPokeEvent is provided, asserts that the given event is the
387 * earliest recorded poke event.
388 */
389 void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {}) {
390 std::unique_lock lock(mLock);
391 base::ScopedLockAssertion assumeLocked(mLock);
392
393 std::optional<UserActivityPokeEvent> pokeEvent =
394 getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock,
395 mNotifyUserActivity);
396 ASSERT_TRUE(pokeEvent) << "Expected a user poke event";
397
398 if (expectedPokeEvent) {
399 ASSERT_EQ(expectedPokeEvent, *pokeEvent);
400 }
Josep del Riob3981622023-04-18 15:49:45 +0000401 }
402
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000403 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000404 ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
405 }
406
407 void assertNotifyDeviceInteractionWasNotCalled() {
408 ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
409 }
410
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000411 void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) {
412 std::scoped_lock lock(mLock);
413 mUnhandledKeyHandler = handler;
414 }
415
416 void assertUnhandledKeyReported(int32_t keycode) {
417 std::unique_lock lock(mLock);
418 base::ScopedLockAssertion assumeLocked(mLock);
419 std::optional<int32_t> unhandledKeycode =
420 getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock,
421 mNotifyUnhandledKey);
422 ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported";
423 ASSERT_EQ(unhandledKeycode, keycode);
424 }
425
426 void assertUnhandledKeyNotReported() {
427 std::unique_lock lock(mLock);
428 base::ScopedLockAssertion assumeLocked(mLock);
429 std::optional<int32_t> unhandledKeycode =
430 getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock,
431 mNotifyUnhandledKey);
432 ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported";
433 }
434
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700436 std::mutex mLock;
437 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
438 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
439 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
440 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800441
Prabir Pradhan99987712020-11-10 18:43:05 -0800442 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000443
444 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800445
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700446 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700447 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800448 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
449 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700450 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800451 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
452 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700453
arthurhungf452d0b2021-01-06 00:19:52 +0800454 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800455 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800456
457 std::condition_variable mNotifyUserActivity;
458 std::queue<UserActivityPokeEvent> mUserActivityPokeEvents;
arthurhungf452d0b2021-01-06 00:19:52 +0800459
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800460 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
461
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700462 std::chrono::nanoseconds mStaleEventTimeout = 1000ms;
463
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000464 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000465
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000466 std::condition_variable mNotifyUnhandledKey;
467 std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock);
468 std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock);
469
Prabir Pradhanedd96402022-02-15 01:46:16 -0800470 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
471 // for a specific container to become non-empty. When the container is non-empty, return the
472 // first entry from the container and erase it.
473 template <class T>
474 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
475 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
476 // If there is an ANR, Dispatcher won't be idle because there are still events
477 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
478 // before checking if ANR was called.
479 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
480 // to provide it some time to act. 100ms seems reasonable.
481 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
482 const std::chrono::time_point start = std::chrono::steady_clock::now();
483 std::optional<T> token =
484 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
485 if (!token.has_value()) {
486 ADD_FAILURE() << "Did not receive the ANR callback";
487 return {};
488 }
489
490 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
491 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
492 // the dispatcher started counting before this function was called
493 if (std::chrono::abs(timeout - waited) > 100ms) {
494 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
495 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
496 << "ms, but waited "
497 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
498 << "ms instead";
499 }
500 return *token;
501 }
502
503 template <class T>
504 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
505 std::queue<T>& storage,
506 std::unique_lock<std::mutex>& lock,
507 std::condition_variable& condition)
508 REQUIRES(mLock) {
509 condition.wait_for(lock, timeout,
510 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
511 if (storage.empty()) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800512 return std::nullopt;
513 }
514 T item = storage.front();
515 storage.pop();
516 return std::make_optional(item);
517 }
518
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600519 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700520 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800521 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522 }
523
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000524 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800525 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700526 std::scoped_lock lock(mLock);
Prabir Pradhanfc364722024-02-08 17:51:20 +0000527 mAnrWindows.push({connectionToken, pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700528 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500529 }
530
Prabir Pradhanedd96402022-02-15 01:46:16 -0800531 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000532 std::optional<gui::Pid> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500533 std::scoped_lock lock(mLock);
Prabir Pradhanfc364722024-02-08 17:51:20 +0000534 mResponsiveWindows.push({connectionToken, pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500535 mNotifyAnr.notify_all();
536 }
537
538 void notifyNoFocusedWindowAnr(
539 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
540 std::scoped_lock lock(mLock);
541 mAnrApplications.push(applicationHandle);
542 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543 }
544
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800545 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
546 std::scoped_lock lock(mLock);
547 mBrokenInputChannels.push(connectionToken);
548 mNotifyInputChannelBroken.notify_all();
549 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600551 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700552
Chris Yef59a2f42020-10-16 12:55:26 -0700553 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
554 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
555 const std::vector<float>& values) override {}
556
557 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
558 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000559
Chris Yefb552902021-02-03 17:18:37 -0800560 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
561
Prabir Pradhana41d2442023-04-20 21:30:40 +0000562 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700563 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000564 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700565 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000566 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
567 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800568 break;
569 }
570
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700571 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000572 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
573 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800574 break;
575 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700576 default: {
577 ADD_FAILURE() << "Should only filter keys or motions";
578 break;
579 }
Jackal Guof9696682018-10-05 12:23:23 +0800580 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 return true;
582 }
583
Prabir Pradhana41d2442023-04-20 21:30:40 +0000584 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
585 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800586 // Clear intercept state when we handled the event.
587 mInterceptKeyTimeout = 0ms;
588 }
589 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590
Yeabkal Wubshit88a90412023-12-21 18:23:04 -0800591 void interceptMotionBeforeQueueing(int32_t, uint32_t, int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592
Prabir Pradhana41d2442023-04-20 21:30:40 +0000593 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800594 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
595 // Clear intercept state so we could dispatch the event in next wake.
596 mInterceptKeyTimeout = 0ms;
597 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 }
599
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000600 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event,
Prabir Pradhana41d2442023-04-20 21:30:40 +0000601 uint32_t) override {
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000602 std::scoped_lock lock(mLock);
603 mReportedUnhandledKeycodes.emplace(event.getKeyCode());
604 mNotifyUnhandledKey.notify_all();
605 return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 }
607
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600608 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
609 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700610 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800611 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
612 * essentially a passthrough for notifySwitch.
613 */
Prabir Pradhana8fe7c52023-12-14 22:07:23 +0000614 mLastNotifySwitch =
615 NotifySwitchArgs(InputEvent::nextId(), when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 }
617
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800618 void pokeUserActivity(nsecs_t eventTime, int32_t eventType, int32_t displayId) override {
Josep del Riob3981622023-04-18 15:49:45 +0000619 std::scoped_lock lock(mLock);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800620 mNotifyUserActivity.notify_all();
621 mUserActivityPokeEvents.push({eventTime, eventType, displayId});
Josep del Riob3981622023-04-18 15:49:45 +0000622 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700624 bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override {
625 return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout;
626 }
627
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600628 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700629 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700630 mOnPointerDownToken = newToken;
631 }
632
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000633 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800634 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000635 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800636 mPointerCaptureChangedCondition.notify_all();
637 }
638
arthurhungf452d0b2021-01-06 00:19:52 +0800639 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
640 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800641 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800642 mDropTargetWindowToken = token;
643 }
644
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000645 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000646 const std::set<gui::Uid>& uids) override {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000647 ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids));
648 }
649
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700650 void assertFilterInputEventWasCalledInternal(
651 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700652 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800653 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700654 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800655 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800656 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657};
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700658} // namespace
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660// --- InputDispatcherTest ---
661
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000662// The trace is a global variable for now, to avoid having to pass it into all of the
663// FakeWindowHandles created throughout the tests.
664// TODO(b/210460522): Update the tests to avoid the need to have the trace be a global variable.
665static std::shared_ptr<VerifyingTrace> gVerifyingTrace = std::make_shared<VerifyingTrace>();
666
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667class InputDispatcherTest : public testing::Test {
668protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000669 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700670 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000672 void SetUp() override {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000673 gVerifyingTrace->reset();
Prabir Pradhana41d2442023-04-20 21:30:40 +0000674 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000675 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy,
676 std::make_unique<FakeInputTracingBackend>(
677 gVerifyingTrace));
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700678
Harry Cutts101ee9b2023-07-06 18:04:14 +0000679 mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000680 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700681 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 }
683
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000684 void TearDown() override {
Prabir Pradhan0eaf1402024-02-05 22:43:04 +0000685 ASSERT_NO_FATAL_FAILURE(gVerifyingTrace->verifyExpectedEventsTraced());
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700686 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000687 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700688 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700690
691 /**
692 * Used for debugging when writing the test
693 */
694 void dumpDispatcherState() {
695 std::string dump;
696 mDispatcher->dump(dump);
697 std::stringstream ss(dump);
698 std::string to;
699
700 while (std::getline(ss, to, '\n')) {
701 ALOGE("%s", to.c_str());
702 }
703 }
Vishnu Nair958da932020-08-21 17:12:37 -0700704
Chavi Weingarten847e8512023-03-29 00:26:09 +0000705 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700706 FocusRequest request;
707 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000708 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700709 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
710 request.displayId = window->getInfo()->displayId;
711 mDispatcher->setFocusedWindow(request);
712 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713};
714
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
716 KeyEvent event;
717
718 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800719 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
720 INVALID_HMAC,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000721 /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600722 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800723 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000724 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000725 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800726 << "Should reject key events with undefined action.";
727
728 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800729 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
730 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600731 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800732 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000733 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000734 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 << "Should reject key events with ACTION_MULTIPLE.";
736}
737
738TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
739 MotionEvent event;
740 PointerProperties pointerProperties[MAX_POINTERS + 1];
741 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800742 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 pointerProperties[i].clear();
744 pointerProperties[i].id = i;
745 pointerCoords[i].clear();
746 }
747
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800748 // Some constants commonly used below
749 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
750 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
751 constexpr int32_t metaState = AMETA_NONE;
752 constexpr MotionClassification classification = MotionClassification::NONE;
753
chaviw9eaa22c2020-07-01 16:21:27 -0700754 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800756 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000757 /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700758 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700759 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
760 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000761 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800762 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000763 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000764 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765 << "Should reject motion events with undefined action.";
766
767 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800768 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800769 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
770 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
771 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
772 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000773 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800774 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000775 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000776 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 << "Should reject motion events with pointer down index too large.";
778
Garfield Tanfbe732e2020-01-24 11:26:14 -0800779 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700780 AMOTION_EVENT_ACTION_POINTER_DOWN |
781 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700782 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
783 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700784 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000785 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800786 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000787 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000788 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 << "Should reject motion events with pointer down index too small.";
790
791 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800792 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800793 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
794 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
795 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
796 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000797 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800798 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000799 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000800 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801 << "Should reject motion events with pointer up index too large.";
802
Garfield Tanfbe732e2020-01-24 11:26:14 -0800803 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700804 AMOTION_EVENT_ACTION_POINTER_UP |
805 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700806 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
807 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700808 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000809 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800810 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000811 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000812 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 << "Should reject motion events with pointer up index too small.";
814
815 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800816 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
817 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700818 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700819 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
820 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000821 /*pointerCount=*/0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800822 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000823 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000824 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 << "Should reject motion events with 0 pointers.";
826
Garfield Tanfbe732e2020-01-24 11:26:14 -0800827 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
828 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700829 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700830 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
831 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000832 /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800833 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000834 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000835 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 << "Should reject motion events with more than MAX_POINTERS pointers.";
837
838 // Rejects motion events with invalid pointer ids.
839 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800840 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
841 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700842 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700843 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
844 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000845 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800846 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000847 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000848 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 << "Should reject motion events with pointer ids less than 0.";
850
851 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800852 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
853 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700854 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700855 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
856 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000857 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800858 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000859 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000860 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
862
863 // Rejects motion events with duplicate pointer ids.
864 pointerProperties[0].id = 1;
865 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800866 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
867 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700868 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700869 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
870 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000871 /*pointerCount=*/2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800872 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000873 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000874 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 << "Should reject motion events with duplicate pointer ids.";
876}
877
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800878/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
879
880TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
881 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000882 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800883 ASSERT_TRUE(mDispatcher->waitForIdle());
884
885 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
886}
887
888TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Prabir Pradhana8fe7c52023-12-14 22:07:23 +0000889 NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0,
890 /*switchValues=*/1,
Harry Cutts33476232023-01-30 19:57:29 +0000891 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000892 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800893
894 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
895 args.policyFlags |= POLICY_FLAG_TRUSTED;
896 mFakePolicy->assertNotifySwitchWasCalled(args);
897}
898
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700899namespace {
900
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700901static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700902// Default input dispatching timeout if there is no focused application or paused window
903// from which to determine an appropriate dispatching timeout.
904static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
905 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
906 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800907
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800908class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800909public:
Garfield Tan15601662020-09-22 15:32:38 -0700910 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700911 : mConsumer(std::move(clientChannel)), mName(name) {}
chaviwd1c23182019-12-20 18:44:56 -0800912
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800913 std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false) {
914 auto [consumeSeq, event] = receiveEvent(timeout);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700915 if (!consumeSeq) {
916 return nullptr;
917 }
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000918 finishEvent(*consumeSeq, handled);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800919 return std::move(event);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700920 }
921
922 /**
923 * Receive an event without acknowledging it.
924 * Return the sequence number that could later be used to send finished signal.
925 */
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800926 std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent(
927 std::chrono::milliseconds timeout) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800928 uint32_t consumeSeq;
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800929 std::unique_ptr<InputEvent> event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800930
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800931 std::chrono::time_point start = std::chrono::steady_clock::now();
932 status_t status = WOULD_BLOCK;
933 while (status == WOULD_BLOCK) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800934 InputEvent* rawEventPtr = nullptr;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700935 status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800936 &rawEventPtr);
937 event = std::unique_ptr<InputEvent>(rawEventPtr);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800938 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700939 if (elapsed > timeout) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800940 break;
941 }
942 }
943
944 if (status == WOULD_BLOCK) {
945 // Just means there's no event available.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800946 return std::make_pair(std::nullopt, nullptr);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800947 }
948
949 if (status != OK) {
950 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800951 return std::make_pair(std::nullopt, nullptr);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800952 }
953 if (event == nullptr) {
954 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800955 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800956 return std::make_pair(consumeSeq, std::move(event));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700957 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800958
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700959 /**
960 * To be used together with "receiveEvent" to complete the consumption of an event.
961 */
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000962 void finishEvent(uint32_t consumeSeq, bool handled = true) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700963 const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700964 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800965 }
966
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000967 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700968 const status_t status = mConsumer.sendTimeline(inputEventId, timeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000969 ASSERT_EQ(OK, status);
970 }
971
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700972 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000973 std::optional<int32_t> expectedDisplayId,
974 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800975 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800976
977 ASSERT_NE(nullptr, event) << mName.c_str()
978 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800979 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700980 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
981 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800982
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000983 if (expectedDisplayId.has_value()) {
984 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
985 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800986
Tiger Huang8664f8c2018-10-11 19:14:35 +0800987 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700988 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800989 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700990 ASSERT_THAT(keyEvent, WithKeyAction(expectedAction));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000991 if (expectedFlags.has_value()) {
992 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
993 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800994 break;
995 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700996 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800997 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700998 ASSERT_THAT(motionEvent, WithMotionAction(expectedAction));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000999 if (expectedFlags.has_value()) {
1000 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
1001 }
Tiger Huang8664f8c2018-10-11 19:14:35 +08001002 break;
1003 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001004 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001005 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
1006 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001007 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -08001008 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
1009 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001010 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001011 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
1012 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001013 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +08001014 FAIL() << "Use 'consumeDragEvent' for DRAG events";
1015 }
Tiger Huang8664f8c2018-10-11 19:14:35 +08001016 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001017 }
1018
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001019 std::unique_ptr<MotionEvent> consumeMotion() {
1020 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001021
1022 if (event == nullptr) {
1023 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
1024 return nullptr;
1025 }
1026
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001027 if (event->getType() != InputEventType::MOTION) {
1028 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001029 return nullptr;
1030 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001031 return std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release()));
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001032 }
1033
1034 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001035 std::unique_ptr<MotionEvent> motionEvent = consumeMotion();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001036 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1037 ASSERT_THAT(*motionEvent, matcher);
1038 }
1039
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001040 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001041 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001042 ASSERT_NE(nullptr, event) << mName.c_str()
1043 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001044 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1045 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001046
1047 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1048 << mName.c_str() << ": event displayId should always be NONE.";
1049
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001050 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1051 EXPECT_EQ(hasFocus, focusEvent.getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001052 }
1053
Prabir Pradhan99987712020-11-10 18:43:05 -08001054 void consumeCaptureEvent(bool hasCapture) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001055 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Prabir Pradhan99987712020-11-10 18:43:05 -08001056 ASSERT_NE(nullptr, event) << mName.c_str()
1057 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001058 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1059 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001060
1061 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1062 << mName.c_str() << ": event displayId should always be NONE.";
1063
1064 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1065 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1066 }
1067
arthurhungb89ccb02020-12-30 16:19:01 +08001068 void consumeDragEvent(bool isExiting, float x, float y) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001069 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
arthurhungb89ccb02020-12-30 16:19:01 +08001070 ASSERT_NE(nullptr, event) << mName.c_str()
1071 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001072 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001073
1074 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1075 << mName.c_str() << ": event displayId should always be NONE.";
1076
1077 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1078 EXPECT_EQ(isExiting, dragEvent.isExiting());
1079 EXPECT_EQ(x, dragEvent.getX());
1080 EXPECT_EQ(y, dragEvent.getY());
1081 }
1082
Antonio Kantekf16f2832021-09-28 04:39:20 +00001083 void consumeTouchModeEvent(bool inTouchMode) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001084 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Antonio Kantekf16f2832021-09-28 04:39:20 +00001085 ASSERT_NE(nullptr, event) << mName.c_str()
1086 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001087 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1088 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001089
1090 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1091 << mName.c_str() << ": event displayId should always be NONE.";
1092 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1093 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1094 }
1095
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001096 void assertNoEvents(std::chrono::milliseconds timeout) {
1097 std::unique_ptr<InputEvent> event = consume(timeout);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001098 if (event == nullptr) {
1099 return;
1100 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001101 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001102 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001103 ADD_FAILURE() << "Received key event " << keyEvent;
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001104 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001105 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001106 ADD_FAILURE() << "Received motion event " << motionEvent;
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001107 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001108 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1109 ADD_FAILURE() << "Received focus event, hasFocus = "
1110 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001111 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001112 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1113 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1114 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001115 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001116 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1117 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1118 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001119 }
1120 FAIL() << mName.c_str()
1121 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001122 }
1123
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001124 sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); }
chaviwd1c23182019-12-20 18:44:56 -08001125
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08001126 int getChannelFd() { return mConsumer.getChannel()->getFd(); }
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001127
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001128private:
1129 InputConsumer mConsumer;
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001130 DynamicInputEventFactory mEventFactory;
chaviwd1c23182019-12-20 18:44:56 -08001131
1132 std::string mName;
1133};
1134
chaviw3277faf2021-05-19 16:45:23 -05001135class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001136public:
1137 static const int32_t WIDTH = 600;
1138 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001139
Chris Yea209fde2020-07-22 13:54:51 -07001140 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001141 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00001142 int32_t displayId, bool createInputChannel = true)
chaviwd1c23182019-12-20 18:44:56 -08001143 : mName(name) {
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00001144 sp<IBinder> token;
1145 if (createInputChannel) {
Garfield Tan15601662020-09-22 15:32:38 -07001146 base::Result<std::unique_ptr<InputChannel>> channel =
1147 dispatcher->createInputChannel(name);
1148 token = (*channel)->getConnectionToken();
1149 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001150 }
1151
1152 inputApplicationHandle->updateInfo();
1153 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1154
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00001155 mInfo.token = token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001156 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001157 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001158 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001159 mInfo.alpha = 1.0;
Chavi Weingarten7f019192023-08-08 20:39:01 +00001160 mInfo.frame = Rect(0, 0, WIDTH, HEIGHT);
chaviw1ff3d1e2020-07-01 15:53:47 -07001161 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001162 mInfo.globalScaleFactor = 1.0;
1163 mInfo.touchableRegion.clear();
1164 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001165 mInfo.ownerPid = WINDOW_PID;
1166 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001167 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001168 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001169 }
1170
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001171 sp<FakeWindowHandle> clone(int32_t displayId) {
1172 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)");
1173 handle->mInfo = mInfo;
1174 handle->mInfo.displayId = displayId;
1175 handle->mInfo.id = sId++;
1176 handle->mInputReceiver = mInputReceiver;
1177 return handle;
1178 }
1179
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001180 void setTouchable(bool touchable) {
1181 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1182 }
chaviwd1c23182019-12-20 18:44:56 -08001183
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001184 void setFocusable(bool focusable) {
1185 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1186 }
1187
1188 void setVisible(bool visible) {
1189 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1190 }
Vishnu Nair958da932020-08-21 17:12:37 -07001191
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001192 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001193 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001194 }
1195
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001196 void setPaused(bool paused) {
1197 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1198 }
1199
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001200 void setPreventSplitting(bool preventSplitting) {
1201 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001202 }
1203
1204 void setSlippery(bool slippery) {
1205 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1206 }
1207
1208 void setWatchOutsideTouch(bool watchOutside) {
1209 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1210 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001211
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001212 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1213
1214 void setInterceptsStylus(bool interceptsStylus) {
1215 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1216 }
1217
1218 void setDropInput(bool dropInput) {
1219 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1220 }
1221
1222 void setDropInputIfObscured(bool dropInputIfObscured) {
1223 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1224 }
1225
1226 void setNoInputChannel(bool noInputChannel) {
1227 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1228 }
1229
Josep del Riob3981622023-04-18 15:49:45 +00001230 void setDisableUserActivity(bool disableUserActivity) {
1231 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1232 }
1233
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07001234 void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) {
1235 mInfo.setInputConfig(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH,
1236 shouldGlobalStylusBlockTouch);
1237 }
1238
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001239 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1240
chaviw3277faf2021-05-19 16:45:23 -05001241 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001242
Bernardo Rufino7393d172021-02-26 13:56:11 +00001243 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1244
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001245 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
Chavi Weingarten7f019192023-08-08 20:39:01 +00001246 mInfo.frame = frame;
chaviwd1c23182019-12-20 18:44:56 -08001247 mInfo.touchableRegion.clear();
1248 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001249
1250 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1251 ui::Transform translate;
1252 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1253 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001254 }
1255
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001256 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1257
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001258 void setIsWallpaper(bool isWallpaper) {
1259 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1260 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001261
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001262 void setDupTouchToWallpaper(bool hasWallpaper) {
1263 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1264 }
chaviwd1c23182019-12-20 18:44:56 -08001265
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001266 void setTrustedOverlay(bool trustedOverlay) {
1267 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1268 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001269
chaviw9eaa22c2020-07-01 16:21:27 -07001270 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1271 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1272 }
1273
1274 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001275
yunho.shinf4a80b82020-11-16 21:13:57 +09001276 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1277
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001278 std::unique_ptr<KeyEvent> consumeKey(bool handled = true) {
1279 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled);
1280 if (event == nullptr) {
1281 ADD_FAILURE() << "No event";
1282 return nullptr;
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001283 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001284 if (event->getType() != InputEventType::KEY) {
1285 ADD_FAILURE() << "Instead of key event, got " << event;
1286 return nullptr;
1287 }
1288 return std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event.release()));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001289 }
1290
1291 void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001292 std::unique_ptr<KeyEvent> keyEvent = consumeKey();
1293 ASSERT_NE(nullptr, keyEvent);
1294 ASSERT_THAT(*keyEvent, matcher);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001295 }
1296
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001297 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001298 consumeKeyEvent(AllOf(WithKeyAction(ACTION_DOWN), WithDisplayId(expectedDisplayId),
1299 WithFlags(expectedFlags)));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001300 }
1301
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001302 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001303 consumeKeyEvent(AllOf(WithKeyAction(ACTION_UP), WithDisplayId(expectedDisplayId),
1304 WithFlags(expectedFlags)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001305 }
1306
Svet Ganov5d3bc372020-01-26 23:11:07 -08001307 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001308 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001309 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1310 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001311 }
1312
1313 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001314 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001315 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1316 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001317 }
1318
1319 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001320 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001321 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1322 }
1323
1324 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1325 std::optional<int32_t> expectedFlags = std::nullopt) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001326 consumeMotionEvent(
1327 AllOf(WithMotionAction(ACTION_DOWN),
1328 testing::Conditional(expectedDisplayId.has_value(),
1329 WithDisplayId(*expectedDisplayId), testing::_),
1330 testing::Conditional(expectedFlags.has_value(), WithFlags(*expectedFlags),
1331 testing::_)));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001332 }
1333
Svet Ganov5d3bc372020-01-26 23:11:07 -08001334 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001335 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1336 int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001337 const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001338 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001339 consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId),
1340 WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001341 }
1342
1343 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001344 int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001345 const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001346 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001347 consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId),
1348 WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001349 }
1350
1351 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001352 int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001353 consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDisplayId(expectedDisplayId),
1354 WithFlags(expectedFlags)));
Michael Wright3a240c42019-12-10 20:53:41 +00001355 }
1356
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001357 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1358 int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001359 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE),
1360 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001361 }
1362
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001363 void consumeMotionOutsideWithZeroedCoords() {
1364 consumeMotionEvent(
1365 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), WithRawCoords(0, 0)));
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001366 }
1367
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001368 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1369 ASSERT_NE(mInputReceiver, nullptr)
1370 << "Cannot consume events from a window with no receiver";
1371 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1372 }
1373
Prabir Pradhan99987712020-11-10 18:43:05 -08001374 void consumeCaptureEvent(bool hasCapture) {
1375 ASSERT_NE(mInputReceiver, nullptr)
1376 << "Cannot consume events from a window with no receiver";
1377 mInputReceiver->consumeCaptureEvent(hasCapture);
1378 }
1379
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001380 std::unique_ptr<MotionEvent> consumeMotionEvent(
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001381 const ::testing::Matcher<MotionEvent>& matcher = testing::_) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001382 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
1383 if (event == nullptr) {
1384 ADD_FAILURE() << "No event";
1385 return nullptr;
Prabir Pradhan5893d362023-11-17 04:30:40 +00001386 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001387 if (event->getType() != InputEventType::MOTION) {
1388 ADD_FAILURE() << "Instead of motion event, got " << *event;
1389 return nullptr;
1390 }
1391 std::unique_ptr<MotionEvent> motionEvent =
1392 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release()));
1393 EXPECT_THAT(*motionEvent, matcher);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001394 return motionEvent;
chaviwd1c23182019-12-20 18:44:56 -08001395 }
1396
arthurhungb89ccb02020-12-30 16:19:01 +08001397 void consumeDragEvent(bool isExiting, float x, float y) {
1398 mInputReceiver->consumeDragEvent(isExiting, x, y);
1399 }
1400
Antonio Kantekf16f2832021-09-28 04:39:20 +00001401 void consumeTouchModeEvent(bool inTouchMode) {
1402 ASSERT_NE(mInputReceiver, nullptr)
1403 << "Cannot consume events from a window with no receiver";
1404 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1405 }
1406
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001407 std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001408 return receive();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001409 }
1410
1411 void finishEvent(uint32_t sequenceNum) {
1412 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1413 mInputReceiver->finishEvent(sequenceNum);
1414 }
1415
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001416 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1417 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1418 mInputReceiver->sendTimeline(inputEventId, timeline);
1419 }
1420
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001421 void assertNoEvents(std::chrono::milliseconds timeout = CONSUME_TIMEOUT_NO_EVENT_EXPECTED) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001422 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001423 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001424 return; // Can't receive events if the window does not have input channel
1425 }
1426 ASSERT_NE(nullptr, mInputReceiver)
1427 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001428 mInputReceiver->assertNoEvents(timeout);
Arthur Hungb92218b2018-08-14 12:00:21 +08001429 }
1430
chaviwaf87b3e2019-10-01 16:59:28 -07001431 sp<IBinder> getToken() { return mInfo.token; }
1432
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001433 const std::string& getName() { return mName; }
1434
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00001435 void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001436 mInfo.ownerPid = ownerPid;
1437 mInfo.ownerUid = ownerUid;
1438 }
1439
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00001440 gui::Pid getPid() const { return mInfo.ownerPid; }
Prabir Pradhanedd96402022-02-15 01:46:16 -08001441
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001442 void destroyReceiver() { mInputReceiver = nullptr; }
1443
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001444 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1445
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001446 // FakeWindowHandle uses this consume method to ensure received events are added to the trace.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001447 std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001448 if (mInputReceiver == nullptr) {
1449 LOG(FATAL) << "Cannot consume event from a window with no input event receiver";
1450 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001451 std::unique_ptr<InputEvent> event = mInputReceiver->consume(timeout, handled);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001452 if (event == nullptr) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001453 ADD_FAILURE() << "Consume failed: no event";
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001454 }
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001455 expectReceivedEventTraced(event);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001456 return event;
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001457 }
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001458
1459private:
1460 FakeWindowHandle(std::string name) : mName(name){};
1461 const std::string mName;
1462 std::shared_ptr<FakeInputReceiver> mInputReceiver;
1463 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
1464 friend class sp<FakeWindowHandle>;
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001465
1466 // FakeWindowHandle uses this receive method to ensure received events are added to the trace.
1467 std::pair<std::optional<uint32_t /*seq*/>, std::unique_ptr<InputEvent>> receive() {
1468 if (mInputReceiver == nullptr) {
1469 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1470 return std::make_pair(std::nullopt, nullptr);
1471 }
1472 auto out = mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
1473 const auto& [_, event] = out;
1474 expectReceivedEventTraced(event);
1475 return std::move(out);
1476 }
1477
1478 void expectReceivedEventTraced(const std::unique_ptr<InputEvent>& event) {
1479 if (!event) {
1480 return;
1481 }
1482
1483 switch (event->getType()) {
1484 case InputEventType::KEY: {
Prabir Pradhan4497c862023-12-15 07:13:30 +00001485 gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), mInfo.id);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001486 break;
1487 }
1488 case InputEventType::MOTION: {
Prabir Pradhan4497c862023-12-15 07:13:30 +00001489 gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event),
1490 mInfo.id);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001491 break;
1492 }
1493 default:
1494 break;
1495 }
1496 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001497};
1498
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001499std::atomic<int32_t> FakeWindowHandle::sId{1};
1500
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001501class FakeMonitorReceiver {
1502public:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001503 FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId)
1504 : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {}
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001505
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001506 sp<IBinder> getToken() { return mInputReceiver.getToken(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001507
1508 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001509 mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1510 expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001511 }
1512
1513 std::optional<int32_t> receiveEvent() {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001514 const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
1515 return sequenceNum;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001516 }
1517
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001518 void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001519
1520 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001521 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
1522 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001523 }
1524
1525 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001526 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
1527 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001528 }
1529
1530 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001531 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
1532 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001533 }
1534
1535 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001536 mInputReceiver.consumeMotionEvent(
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001537 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
1538 WithDisplayId(expectedDisplayId),
1539 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
1540 }
1541
1542 void consumeMotionPointerDown(int32_t pointerIdx) {
1543 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1544 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001545 mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
1546 /*expectedFlags=*/0);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001547 }
1548
1549 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001550 mInputReceiver.consumeMotionEvent(matcher);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001551 }
1552
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001553 std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001554
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001555 void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001556
1557private:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001558 FakeInputReceiver mInputReceiver;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001559};
1560
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001561static InputEventInjectionResult injectKey(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001562 InputDispatcher& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001563 int32_t displayId = ADISPLAY_ID_NONE,
1564 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001565 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001566 bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {},
Prabir Pradhan5735a322022-04-11 17:23:34 +00001567 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001568 KeyEvent event;
1569 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1570
1571 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001572 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001573 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1574 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001575
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001576 if (!allowKeyRepeat) {
1577 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1578 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001579 // Inject event until dispatch out.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001580 return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001581}
1582
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001583static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) {
1584 InputEventInjectionResult result =
1585 injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE,
1586 InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
1587 if (result != InputEventInjectionResult::TIMED_OUT) {
1588 FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result);
1589 }
1590}
1591
1592static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001593 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001594 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001595}
1596
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001597// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1598// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1599// has to be woken up to process the repeating key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001600static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher,
1601 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001602 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001603 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001604 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001605}
1606
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001607static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001608 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001609 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001610}
1611
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001612static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001613 InputDispatcher& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001614 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001615 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001616 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001617 return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1618 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001619}
1620
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001621static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001622 InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId,
1623 const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001624 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001625 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1626 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001627 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001628 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001629 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001630 MotionEventBuilder motionBuilder =
1631 MotionEventBuilder(action, source)
1632 .displayId(displayId)
1633 .eventTime(eventTime)
1634 .rawXCursorPosition(cursorPosition.x)
1635 .rawYCursorPosition(cursorPosition.y)
1636 .pointer(
1637 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1638 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1639 motionBuilder.downTime(eventTime);
1640 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001641
1642 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001643 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1644 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001645}
1646
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001647static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source,
1648 int32_t displayId,
1649 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001650 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001651}
1652
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001653static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source,
1654 int32_t displayId,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001655 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001656 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001657}
1658
Jackal Guof9696682018-10-05 12:23:23 +08001659static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1660 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1661 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001662 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1663 AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action,
1664 /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001665
1666 return args;
1667}
1668
Josep del Riob3981622023-04-18 15:49:45 +00001669static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1670 int32_t displayId = ADISPLAY_ID_NONE) {
1671 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1672 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001673 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1674 AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C,
1675 AMETA_META_ON, currentTime);
Josep del Riob3981622023-04-18 15:49:45 +00001676
1677 return args;
1678}
1679
1680static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1681 int32_t displayId = ADISPLAY_ID_NONE) {
1682 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1683 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001684 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1685 AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST,
1686 KEY_ASSISTANT, AMETA_NONE, currentTime);
Josep del Riob3981622023-04-18 15:49:45 +00001687
1688 return args;
1689}
1690
Prabir Pradhan678438e2023-04-13 19:32:51 +00001691[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1692 int32_t displayId,
1693 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001694 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001695 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1696 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1697 }
1698
chaviwd1c23182019-12-20 18:44:56 -08001699 PointerProperties pointerProperties[pointerCount];
1700 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001701
chaviwd1c23182019-12-20 18:44:56 -08001702 for (size_t i = 0; i < pointerCount; i++) {
1703 pointerProperties[i].clear();
1704 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001705 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001706
chaviwd1c23182019-12-20 18:44:56 -08001707 pointerCoords[i].clear();
1708 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1709 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1710 }
Jackal Guof9696682018-10-05 12:23:23 +08001711
1712 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1713 // Define a valid motion event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001714 NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source,
1715 displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0,
1716 /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001717 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001718 pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001719 AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001720 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{});
Jackal Guof9696682018-10-05 12:23:23 +08001721
1722 return args;
1723}
1724
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001725static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1726 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1727}
1728
chaviwd1c23182019-12-20 18:44:56 -08001729static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1730 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1731}
1732
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001733static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1734 const PointerCaptureRequest& request) {
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001735 return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC),
1736 request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001737}
1738
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001739} // namespace
1740
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001741/**
1742 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1743 * broken channel.
1744 */
1745TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1746 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1747 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001748 sp<FakeWindowHandle>::make(application, mDispatcher,
1749 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001750
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001751 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001752
1753 // Window closes its channel, but the window remains.
1754 window->destroyReceiver();
1755 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1756}
1757
Arthur Hungb92218b2018-08-14 12:00:21 +08001758TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001759 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001760 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1761 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001762
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001763 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001765 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001767
1768 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001769 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001770}
1771
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -08001772using InputDispatcherDeathTest = InputDispatcherTest;
1773
1774/**
1775 * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher
1776 * should crash.
1777 */
1778TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) {
1779 testing::GTEST_FLAG(death_test_style) = "threadsafe";
1780 ScopedSilentDeath _silentDeath;
1781
1782 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1783 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1784 "Fake Window", ADISPLAY_ID_DEFAULT);
1785 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
1786 {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}),
1787 "Incorrect WindowInfosUpdate provided");
1788}
1789
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001790TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1791 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001792 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1793 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001794
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001795 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001796 // Inject a MotionEvent to an unknown display.
1797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001798 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001799 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1800
1801 // Window should receive motion event.
1802 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1803}
1804
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001805/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001806 * Calling onWindowInfosChanged once should not cause any issues.
1807 * This test serves as a sanity check for the next test, where onWindowInfosChanged is
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001808 * called twice.
1809 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001810TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001811 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001812 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1813 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001814 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001815
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001816 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001818 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001819 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001820 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001821
1822 // Window should receive motion event.
1823 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1824}
1825
1826/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001827 * Calling onWindowInfosChanged twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001828 */
1829TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001830 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001831 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1832 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001833 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001834
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001835 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1836 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001837 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001838 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001839 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001840 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001841
1842 // Window should receive motion event.
1843 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1844}
1845
Arthur Hungb92218b2018-08-14 12:00:21 +08001846// The foreground window should receive the first touch down event.
1847TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001848 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001849 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001850 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001851 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001852 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001853
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001854 mDispatcher->onWindowInfosChanged(
1855 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001856 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001857 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001858 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001859
1860 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001861 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001862 windowSecond->assertNoEvents();
1863}
1864
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001865/**
1866 * Two windows: A top window, and a wallpaper behind the window.
1867 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1868 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001869 * 1. foregroundWindow <-- dup touch to wallpaper
1870 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001871 */
1872TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1873 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1874 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001875 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001876 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001877 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001878 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001879 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001880
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001881 mDispatcher->onWindowInfosChanged(
1882 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001883 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001884 injectMotionEvent(*mDispatcher,
1885 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1886 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200))
1887 .build()))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001888 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1889
1890 // Both foreground window and its wallpaper should receive the touch down
1891 foregroundWindow->consumeMotionDown();
1892 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1893
1894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001895 injectMotionEvent(*mDispatcher,
1896 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1897 .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200))
1898 .build()))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001899 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1900
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001901 foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001902 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1903
1904 // Now the foreground window goes away, but the wallpaper stays
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001905 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001906 foregroundWindow->consumeMotionCancel();
1907 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1908 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1909}
1910
1911/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001912 * Two fingers down on the window, and lift off the first finger.
1913 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1914 * contains a single pointer.
1915 */
1916TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1917 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1918 sp<FakeWindowHandle> window =
1919 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1920
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001921 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001922 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001923 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1924 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1925 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001926 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001927 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1928 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1929 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1930 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001931 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001932 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1933 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1934 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1935 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001936 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1937 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1938 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1939
1940 // Remove the window. The gesture should be canceled
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001941 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001942 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1943 window->consumeMotionEvent(
1944 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1945}
1946
1947/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001948 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1949 * with the following differences:
1950 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1951 * clean up the connection.
1952 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1953 * Ensure that there's no crash in the dispatcher.
1954 */
1955TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1956 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1957 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001958 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001959 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001960 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001961 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001962 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001963
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001964 mDispatcher->onWindowInfosChanged(
1965 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001967 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001968 {100, 200}))
1969 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1970
1971 // Both foreground window and its wallpaper should receive the touch down
1972 foregroundWindow->consumeMotionDown();
1973 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1974
1975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001976 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001977 ADISPLAY_ID_DEFAULT, {110, 200}))
1978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1979
1980 foregroundWindow->consumeMotionMove();
1981 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1982
1983 // Wallpaper closes its channel, but the window remains.
1984 wallpaperWindow->destroyReceiver();
1985 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1986
1987 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1988 // is no longer valid.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001989 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001990 foregroundWindow->consumeMotionCancel();
1991}
1992
Arthur Hungc539dbb2022-12-08 07:45:36 +00001993class ShouldSplitTouchFixture : public InputDispatcherTest,
1994 public ::testing::WithParamInterface<bool> {};
1995INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1996 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001997/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001998 * A single window that receives touch (on top), and a wallpaper window underneath it.
1999 * The top window gets a multitouch gesture.
2000 * Ensure that wallpaper gets the same gesture.
2001 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002002TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002004 sp<FakeWindowHandle> foregroundWindow =
2005 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2006 foregroundWindow->setDupTouchToWallpaper(true);
2007 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002008
2009 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002010 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002011 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002012
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002013 mDispatcher->onWindowInfosChanged(
2014 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002015
2016 // Touch down on top window
2017 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002018 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002019 {100, 100}))
2020 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2021
2022 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002023 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002024 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2025
2026 // Second finger down on the top window
2027 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002028 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002029 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002030 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2031 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002032 .build();
2033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002034 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002035 InputEventInjectionSync::WAIT_FOR_RESULT))
2036 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2037
Harry Cutts33476232023-01-30 19:57:29 +00002038 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2039 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002040 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002041
2042 const MotionEvent secondFingerUpEvent =
2043 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2044 .displayId(ADISPLAY_ID_DEFAULT)
2045 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002046 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2047 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002048 .build();
2049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002050 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002051 InputEventInjectionSync::WAIT_FOR_RESULT))
2052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2053 foregroundWindow->consumeMotionPointerUp(0);
2054 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2055
2056 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002057 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002058 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2059 AINPUT_SOURCE_TOUCHSCREEN)
2060 .displayId(ADISPLAY_ID_DEFAULT)
2061 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts101ee9b2023-07-06 18:04:14 +00002062 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002063 .x(100)
2064 .y(100))
2065 .build(),
2066 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002067 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2068 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2069 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002070}
2071
2072/**
2073 * Two windows: a window on the left and window on the right.
2074 * A third window, wallpaper, is behind both windows, and spans both top windows.
2075 * The first touch down goes to the left window. A second pointer touches down on the right window.
2076 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2077 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2078 * ACTION_POINTER_DOWN(1).
2079 */
2080TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2081 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2082 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002083 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002084 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002085 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002086
2087 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002088 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002089 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002090 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002091
2092 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002093 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002094 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002095 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002096
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002097 mDispatcher->onWindowInfosChanged(
2098 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
2099 {},
2100 0,
2101 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002102
2103 // Touch down on left window
2104 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002105 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002106 {100, 100}))
2107 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2108
2109 // Both foreground window and its wallpaper should receive the touch down
2110 leftWindow->consumeMotionDown();
2111 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2112
2113 // Second finger down on the right window
2114 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002115 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002116 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002117 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2118 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002119 .build();
2120 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002121 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002122 InputEventInjectionSync::WAIT_FOR_RESULT))
2123 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2124
2125 leftWindow->consumeMotionMove();
2126 // Since the touch is split, right window gets ACTION_DOWN
2127 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002128 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002129 expectedWallpaperFlags);
2130
2131 // Now, leftWindow, which received the first finger, disappears.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002132 mDispatcher->onWindowInfosChanged(
2133 {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002134 leftWindow->consumeMotionCancel();
2135 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2136 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2137
2138 // The pointer that's still down on the right window moves, and goes to the right window only.
2139 // As far as the dispatcher's concerned though, both pointers are still present.
2140 const MotionEvent secondFingerMoveEvent =
2141 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2142 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002143 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2144 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002145 .build();
2146 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002147 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002148 InputEventInjectionSync::WAIT_FOR_RESULT));
2149 rightWindow->consumeMotionMove();
2150
2151 leftWindow->assertNoEvents();
2152 rightWindow->assertNoEvents();
2153 wallpaperWindow->assertNoEvents();
2154}
2155
Arthur Hungc539dbb2022-12-08 07:45:36 +00002156/**
2157 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2158 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2159 * The right window should receive ACTION_DOWN.
2160 */
2161TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002162 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002163 sp<FakeWindowHandle> leftWindow =
2164 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2165 leftWindow->setFrame(Rect(0, 0, 200, 200));
2166 leftWindow->setDupTouchToWallpaper(true);
2167 leftWindow->setSlippery(true);
2168
2169 sp<FakeWindowHandle> rightWindow =
2170 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2171 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002172
2173 sp<FakeWindowHandle> wallpaperWindow =
2174 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2175 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002176
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002177 mDispatcher->onWindowInfosChanged(
2178 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
2179 {},
2180 0,
2181 0});
Arthur Hung74c248d2022-11-23 07:09:59 +00002182
Arthur Hungc539dbb2022-12-08 07:45:36 +00002183 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002184 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002185 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002186 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002188
2189 // Both foreground window and its wallpaper should receive the touch down
2190 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002191 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2192
Arthur Hungc539dbb2022-12-08 07:45:36 +00002193 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002195 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002196 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002197 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2198
Arthur Hungc539dbb2022-12-08 07:45:36 +00002199 leftWindow->consumeMotionCancel();
2200 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2201 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002202}
2203
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002204/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002205 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2206 * interactive, it might stop sending this flag.
2207 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2208 * to have a consistent input stream.
2209 *
2210 * Test procedure:
2211 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2212 * DOWN (new gesture).
2213 *
2214 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2215 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2216 *
2217 * We technically just need a single window here, but we are using two windows (spy on top and a
2218 * regular window below) to emulate the actual situation where it happens on the device.
2219 */
2220TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2221 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2222 sp<FakeWindowHandle> spyWindow =
2223 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2224 spyWindow->setFrame(Rect(0, 0, 200, 200));
2225 spyWindow->setTrustedOverlay(true);
2226 spyWindow->setSpy(true);
2227
2228 sp<FakeWindowHandle> window =
2229 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2230 window->setFrame(Rect(0, 0, 200, 200));
2231
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002232 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002233 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002234
2235 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002236 mDispatcher->notifyMotion(
2237 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2238 .deviceId(touchDeviceId)
2239 .policyFlags(DEFAULT_POLICY_FLAGS)
2240 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2241 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002242
Prabir Pradhan678438e2023-04-13 19:32:51 +00002243 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2244 .deviceId(touchDeviceId)
2245 .policyFlags(DEFAULT_POLICY_FLAGS)
2246 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2247 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2248 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002249 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2250 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2251 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2252 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2253
2254 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002255 mDispatcher->notifyMotion(
2256 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2257 .deviceId(touchDeviceId)
2258 .policyFlags(0)
2259 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2260 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2261 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002262 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2263 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2264
2265 // We don't need to reset the device to reproduce the issue, but the reset event typically
2266 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002267 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002268
2269 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002270 mDispatcher->notifyMotion(
2271 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2272 .deviceId(touchDeviceId)
2273 .policyFlags(DEFAULT_POLICY_FLAGS)
2274 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2275 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002276 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2277 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2278
2279 // No more events
2280 spyWindow->assertNoEvents();
2281 window->assertNoEvents();
2282}
2283
2284/**
Linnan Li907ae732023-09-05 17:14:21 +08002285 * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers.
2286 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2287 * interactive, it might stop sending this flag.
2288 * We've already ensured the consistency of the touch event in this case, and we should also ensure
2289 * the consistency of the hover event in this case.
2290 *
2291 * Test procedure:
2292 * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT
2293 * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT
2294 *
2295 * We expect to receive two full streams of hover events.
2296 */
2297TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) {
2298 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2299
2300 sp<FakeWindowHandle> window =
2301 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2302 window->setFrame(Rect(0, 0, 300, 300));
2303
2304 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2305
2306 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2307 .policyFlags(DEFAULT_POLICY_FLAGS)
2308 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2309 .build());
2310 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2311
2312 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2313 .policyFlags(DEFAULT_POLICY_FLAGS)
2314 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2315 .build());
2316 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2317
2318 // Send hover exit without the default policy flags.
2319 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2320 .policyFlags(0)
2321 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2322 .build());
2323
2324 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2325
2326 // Send a simple hover event stream, ensure dispatcher not crashed and window can receive
2327 // right event.
2328 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2329 .policyFlags(DEFAULT_POLICY_FLAGS)
2330 .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201))
2331 .build());
2332 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2333
2334 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2335 .policyFlags(DEFAULT_POLICY_FLAGS)
2336 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
2337 .build());
2338 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2339
2340 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2341 .policyFlags(DEFAULT_POLICY_FLAGS)
2342 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
2343 .build());
2344 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2345}
2346
2347/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002348 * Two windows: a window on the left and a window on the right.
2349 * Mouse is hovered from the right window into the left window.
2350 * Next, we tap on the left window, where the cursor was last seen.
2351 * The second tap is done onto the right window.
2352 * The mouse and tap are from two different devices.
2353 * We technically don't need to set the downtime / eventtime for these events, but setting these
2354 * explicitly helps during debugging.
2355 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2356 * In the buggy implementation, a tap on the right window would cause a crash.
2357 */
2358TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2359 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2360 sp<FakeWindowHandle> leftWindow =
2361 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2362 leftWindow->setFrame(Rect(0, 0, 200, 200));
2363
2364 sp<FakeWindowHandle> rightWindow =
2365 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2366 rightWindow->setFrame(Rect(200, 0, 400, 200));
2367
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002368 mDispatcher->onWindowInfosChanged(
2369 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002370 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2371 // stale.
2372 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2373 const int32_t mouseDeviceId = 6;
2374 const int32_t touchDeviceId = 4;
2375 // Move the cursor from right
2376 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002377 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002378 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2379 AINPUT_SOURCE_MOUSE)
2380 .deviceId(mouseDeviceId)
2381 .downTime(baseTime + 10)
2382 .eventTime(baseTime + 20)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002383 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002384 .build()));
2385 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2386
2387 // .. to the left window
2388 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002389 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002390 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2391 AINPUT_SOURCE_MOUSE)
2392 .deviceId(mouseDeviceId)
2393 .downTime(baseTime + 10)
2394 .eventTime(baseTime + 30)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002395 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002396 .build()));
2397 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2398 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2399 // Now tap the left window
2400 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002401 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002402 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2403 AINPUT_SOURCE_TOUCHSCREEN)
2404 .deviceId(touchDeviceId)
2405 .downTime(baseTime + 40)
2406 .eventTime(baseTime + 40)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002407 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002408 .build()));
2409 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2410 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2411
2412 // release tap
2413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002414 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002415 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2416 AINPUT_SOURCE_TOUCHSCREEN)
2417 .deviceId(touchDeviceId)
2418 .downTime(baseTime + 40)
2419 .eventTime(baseTime + 50)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002420 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002421 .build()));
2422 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2423
2424 // Tap the window on the right
2425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002426 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002427 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2428 AINPUT_SOURCE_TOUCHSCREEN)
2429 .deviceId(touchDeviceId)
2430 .downTime(baseTime + 60)
2431 .eventTime(baseTime + 60)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002432 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002433 .build()));
2434 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2435
2436 // release tap
2437 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002438 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002439 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2440 AINPUT_SOURCE_TOUCHSCREEN)
2441 .deviceId(touchDeviceId)
2442 .downTime(baseTime + 60)
2443 .eventTime(baseTime + 70)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002444 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002445 .build()));
2446 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2447
2448 // No more events
2449 leftWindow->assertNoEvents();
2450 rightWindow->assertNoEvents();
2451}
2452
2453/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002454 * Start hovering in a window. While this hover is still active, make another window appear on top.
2455 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2456 * While the top window is present, the hovering is stopped.
2457 * Later, hovering gets resumed again.
2458 * Ensure that new hover gesture is handled correctly.
2459 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2460 * to the window that's currently being hovered over.
2461 */
2462TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2463 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2464 sp<FakeWindowHandle> window =
2465 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2466 window->setFrame(Rect(0, 0, 200, 200));
2467
2468 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002469 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002470
2471 // Start hovering in the window
2472 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2473 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2474 .build());
2475 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2476
2477 // Now, an obscuring window appears!
2478 sp<FakeWindowHandle> obscuringWindow =
2479 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2480 ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00002481 /*createInputChannel=*/false);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002482 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2483 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2484 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2485 obscuringWindow->setNoInputChannel(true);
2486 obscuringWindow->setFocusable(false);
2487 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002488 mDispatcher->onWindowInfosChanged(
2489 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002490
2491 // While this new obscuring window is present, the hovering is stopped
2492 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2493 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2494 .build());
2495 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2496
2497 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002498 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002499
2500 // And a new hover gesture starts.
2501 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2502 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2503 .build());
2504 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2505}
2506
2507/**
2508 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2509 * the obscuring window.
2510 */
2511TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2512 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2513 sp<FakeWindowHandle> window =
2514 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2515 window->setFrame(Rect(0, 0, 200, 200));
2516
2517 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002518 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002519
2520 // Start hovering in the window
2521 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2522 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2523 .build());
2524 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2525
2526 // Now, an obscuring window appears!
2527 sp<FakeWindowHandle> obscuringWindow =
2528 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2529 ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00002530 /*createInputChannel=*/false);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002531 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2532 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2533 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2534 obscuringWindow->setNoInputChannel(true);
2535 obscuringWindow->setFocusable(false);
2536 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002537 mDispatcher->onWindowInfosChanged(
2538 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002539
2540 // While this new obscuring window is present, the hovering continues. The event can't go to the
2541 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2542 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2543 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2544 .build());
2545 obscuringWindow->assertNoEvents();
2546 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2547
2548 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002549 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002550
2551 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2552 // so it should generate a HOVER_ENTER
2553 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2554 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2555 .build());
2556 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2557
2558 // Now the MOVE should be getting dispatched normally
2559 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2560 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2561 .build());
2562 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2563}
2564
2565/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002566 * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll
2567 * events are delivered to the window.
2568 */
2569TEST_F(InputDispatcherTest, HoverMoveAndScroll) {
2570 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2571 sp<FakeWindowHandle> window =
2572 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2573 window->setFrame(Rect(0, 0, 200, 200));
2574 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2575
2576 // Start hovering in the window
2577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2578 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
2579 .build());
2580 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2581
2582 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
2583 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2584 .build());
2585 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2586
2587 // Scroll with the mouse
2588 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE)
2589 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2590 .build());
2591 window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL));
2592}
2593
2594using InputDispatcherMultiDeviceTest = InputDispatcherTest;
2595
2596/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002597 * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that
2598 * touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002599 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002600TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002601 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2602 sp<FakeWindowHandle> window =
2603 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2604 window->setFrame(Rect(0, 0, 200, 200));
2605
2606 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2607
2608 constexpr int32_t touchDeviceId = 4;
2609 constexpr int32_t stylusDeviceId = 2;
2610
2611 // Stylus down
2612 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2613 .deviceId(stylusDeviceId)
2614 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2615 .build());
2616 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2617
2618 // Touch down
2619 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2620 .deviceId(touchDeviceId)
2621 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2622 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002623
2624 // Touch move
2625 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2626 .deviceId(touchDeviceId)
2627 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2628 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002629 // Touch is ignored because stylus is already down
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002630
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002631 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002632 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2633 .deviceId(stylusDeviceId)
2634 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2635 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002636 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2637 WithCoords(101, 111)));
2638
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002639 window->assertNoEvents();
2640}
2641
2642/**
2643 * One window and one spy window. Stylus down on the window. Next, touch from another device goes
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002644 * down. Ensure that touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002645 * Similar test as above, but with added SPY window.
2646 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002647TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002648 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2649 sp<FakeWindowHandle> window =
2650 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2651 sp<FakeWindowHandle> spyWindow =
2652 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2653 spyWindow->setFrame(Rect(0, 0, 200, 200));
2654 spyWindow->setTrustedOverlay(true);
2655 spyWindow->setSpy(true);
2656 window->setFrame(Rect(0, 0, 200, 200));
2657
2658 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
2659
2660 constexpr int32_t touchDeviceId = 4;
2661 constexpr int32_t stylusDeviceId = 2;
2662
2663 // Stylus down
2664 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2665 .deviceId(stylusDeviceId)
2666 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2667 .build());
2668 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2669 spyWindow->consumeMotionEvent(
2670 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2671
2672 // Touch down
2673 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2674 .deviceId(touchDeviceId)
2675 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2676 .build());
2677
2678 // Touch move
2679 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2680 .deviceId(touchDeviceId)
2681 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2682 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002683
2684 // Touch is ignored because stylus is already down
2685
2686 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002687 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2688 .deviceId(stylusDeviceId)
2689 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2690 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002691 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2692 WithCoords(101, 111)));
2693 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2694 WithCoords(101, 111)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002695
2696 window->assertNoEvents();
2697 spyWindow->assertNoEvents();
2698}
2699
2700/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002701 * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002702 * touch is dropped, because stylus hover takes precedence.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002703 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002704TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002705 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2706 sp<FakeWindowHandle> window =
2707 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2708 window->setFrame(Rect(0, 0, 200, 200));
2709
2710 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2711
2712 constexpr int32_t touchDeviceId = 4;
2713 constexpr int32_t stylusDeviceId = 2;
2714
2715 // Stylus down on the window
2716 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2717 .deviceId(stylusDeviceId)
2718 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2719 .build());
2720 window->consumeMotionEvent(
2721 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2722
2723 // Touch down on window
2724 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2725 .deviceId(touchDeviceId)
2726 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2727 .build());
2728 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2729 .deviceId(touchDeviceId)
2730 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2731 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002732
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002733 // Touch is ignored because stylus is hovering
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002734
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002735 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002736 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2737 .deviceId(stylusDeviceId)
2738 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2739 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002740 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2741 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002742
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002743 // and subsequent touches continue to be ignored
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002744 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2745 .deviceId(touchDeviceId)
2746 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2747 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002748 window->assertNoEvents();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002749}
2750
2751/**
2752 * One window. Touch down on the window. Then, stylus hover on the window from another device.
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002753 * Ensure that touch is canceled, because stylus hover should take precedence.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002754 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002755TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002756 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2757 sp<FakeWindowHandle> window =
2758 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2759 window->setFrame(Rect(0, 0, 200, 200));
2760
2761 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2762
2763 constexpr int32_t touchDeviceId = 4;
2764 constexpr int32_t stylusDeviceId = 2;
2765
2766 // Touch down on window
2767 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2768 .deviceId(touchDeviceId)
2769 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2770 .build());
2771 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2772 .deviceId(touchDeviceId)
2773 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2774 .build());
2775 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2776 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2777
2778 // Stylus hover on the window
2779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2780 .deviceId(stylusDeviceId)
2781 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2782 .build());
2783 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2784 .deviceId(stylusDeviceId)
2785 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2786 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002787 // Stylus hover movement causes touch to be canceled
2788 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
2789 WithCoords(141, 146)));
2790 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
2791 WithDeviceId(stylusDeviceId), WithCoords(100, 110)));
2792 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2793 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002794
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002795 // Subsequent touch movements are ignored
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002796 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2797 .deviceId(touchDeviceId)
2798 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2799 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002800
2801 window->assertNoEvents();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002802}
2803
2804/**
2805 * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that
2806 * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should
2807 * become active.
2808 */
2809TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) {
2810 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2811 sp<FakeWindowHandle> window =
2812 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2813 window->setFrame(Rect(0, 0, 200, 200));
2814
2815 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2816
2817 constexpr int32_t stylusDeviceId1 = 3;
2818 constexpr int32_t stylusDeviceId2 = 5;
2819
2820 // Touch down on window
2821 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2822 .deviceId(stylusDeviceId1)
2823 .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100))
2824 .build());
2825 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2826 .deviceId(stylusDeviceId1)
2827 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2828 .build());
2829 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1)));
2830 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1)));
2831
2832 // Second stylus down
2833 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2834 .deviceId(stylusDeviceId2)
2835 .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10))
2836 .build());
2837 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2838 .deviceId(stylusDeviceId2)
2839 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11))
2840 .build());
2841
2842 // First stylus is canceled, second one takes over.
2843 window->consumeMotionEvent(
2844 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1)));
2845 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2)));
2846 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2)));
2847
2848 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2849 .deviceId(stylusDeviceId1)
2850 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2851 .build());
2852 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002853 window->assertNoEvents();
2854}
2855
2856/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002857 * One window. Touch down on the window. Then, stylus down on the window from another device.
2858 * Ensure that is canceled, because stylus down should be preferred over touch.
2859 */
2860TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) {
2861 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2862 sp<FakeWindowHandle> window =
2863 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2864 window->setFrame(Rect(0, 0, 200, 200));
2865
2866 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2867
2868 constexpr int32_t touchDeviceId = 4;
2869 constexpr int32_t stylusDeviceId = 2;
2870
2871 // Touch down on window
2872 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2873 .deviceId(touchDeviceId)
2874 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2875 .build());
2876 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2877 .deviceId(touchDeviceId)
2878 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2879 .build());
2880 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2881 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2882
2883 // Stylus down on the window
2884 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2885 .deviceId(stylusDeviceId)
2886 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2887 .build());
2888 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2889 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2890
2891 // Subsequent stylus movements are delivered correctly
2892 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2893 .deviceId(stylusDeviceId)
2894 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2895 .build());
2896 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2897 WithCoords(101, 111)));
2898}
2899
2900/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002901 * Two windows: a window on the left and a window on the right.
2902 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2903 * down. Then, on the left window, also place second touch pointer down.
2904 * This test tries to reproduce a crash.
2905 * In the buggy implementation, second pointer down on the left window would cause a crash.
2906 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002907TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002908 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2909 sp<FakeWindowHandle> leftWindow =
2910 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2911 leftWindow->setFrame(Rect(0, 0, 200, 200));
2912
2913 sp<FakeWindowHandle> rightWindow =
2914 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2915 rightWindow->setFrame(Rect(200, 0, 400, 200));
2916
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002917 mDispatcher->onWindowInfosChanged(
2918 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002919
2920 const int32_t touchDeviceId = 4;
2921 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002922
2923 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002924 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2925 .deviceId(mouseDeviceId)
2926 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2927 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002928 leftWindow->consumeMotionEvent(
2929 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2930
2931 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002932 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2933 .deviceId(mouseDeviceId)
2934 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2935 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2936 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002937
2938 leftWindow->consumeMotionEvent(
2939 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2940 leftWindow->consumeMotionEvent(
2941 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2942
Prabir Pradhan678438e2023-04-13 19:32:51 +00002943 mDispatcher->notifyMotion(
2944 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2945 .deviceId(mouseDeviceId)
2946 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2947 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2948 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2949 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002950 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2951
2952 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002953 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2954 .deviceId(touchDeviceId)
2955 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2956 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002957 leftWindow->assertNoEvents();
2958
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002959 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2960
2961 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002962 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2963 .deviceId(touchDeviceId)
2964 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2965 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2966 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002967 // Since this is now a new splittable pointer going down on the left window, and it's coming
2968 // from a different device, the current gesture in the left window (pointer down) should first
2969 // be canceled.
2970 leftWindow->consumeMotionEvent(
2971 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002972 leftWindow->consumeMotionEvent(
2973 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2974 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2975 // current implementation.
2976 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2977 rightWindow->consumeMotionEvent(
2978 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2979
2980 leftWindow->assertNoEvents();
2981 rightWindow->assertNoEvents();
2982}
2983
2984/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002985 * Two windows: a window on the left and a window on the right.
2986 * Mouse is hovered on the left window and stylus is hovered on the right window.
2987 */
2988TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) {
2989 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2990 sp<FakeWindowHandle> leftWindow =
2991 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2992 leftWindow->setFrame(Rect(0, 0, 200, 200));
2993
2994 sp<FakeWindowHandle> rightWindow =
2995 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2996 rightWindow->setFrame(Rect(200, 0, 400, 200));
2997
2998 mDispatcher->onWindowInfosChanged(
2999 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3000
3001 const int32_t stylusDeviceId = 3;
3002 const int32_t mouseDeviceId = 6;
3003
3004 // Start hovering over the left window
3005 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3006 .deviceId(mouseDeviceId)
3007 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
3008 .build());
3009 leftWindow->consumeMotionEvent(
3010 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3011
3012 // Stylus hovered on right window
3013 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3014 .deviceId(stylusDeviceId)
3015 .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100))
3016 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003017 rightWindow->consumeMotionEvent(
3018 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3019
3020 // Subsequent HOVER_MOVE events are dispatched correctly.
3021 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
3022 .deviceId(mouseDeviceId)
3023 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
3024 .build());
3025 leftWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003026 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003027
3028 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3029 .deviceId(stylusDeviceId)
3030 .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110))
3031 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003032 rightWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003033 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003034
3035 leftWindow->assertNoEvents();
3036 rightWindow->assertNoEvents();
3037}
3038
3039/**
3040 * Three windows: a window on the left and a window on the right.
3041 * And a spy window that's positioned above all of them.
3042 * Stylus down on the left window and remains down. Touch goes down on the right and remains down.
3043 * Check the stream that's received by the spy.
3044 */
3045TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) {
3046 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3047
3048 sp<FakeWindowHandle> spyWindow =
3049 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3050 spyWindow->setFrame(Rect(0, 0, 400, 400));
3051 spyWindow->setTrustedOverlay(true);
3052 spyWindow->setSpy(true);
3053
3054 sp<FakeWindowHandle> leftWindow =
3055 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3056 leftWindow->setFrame(Rect(0, 0, 200, 200));
3057
3058 sp<FakeWindowHandle> rightWindow =
3059 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3060
3061 rightWindow->setFrame(Rect(200, 0, 400, 200));
3062
3063 mDispatcher->onWindowInfosChanged(
3064 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3065
3066 const int32_t stylusDeviceId = 1;
3067 const int32_t touchDeviceId = 2;
3068
3069 // Stylus down on the left window
3070 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3071 .deviceId(stylusDeviceId)
3072 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
3073 .build());
3074 leftWindow->consumeMotionEvent(
3075 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3076 spyWindow->consumeMotionEvent(
3077 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3078
3079 // Touch down on the right window
3080 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3081 .deviceId(touchDeviceId)
3082 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3083 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003084 leftWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003085 rightWindow->consumeMotionEvent(
3086 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003087
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003088 // Spy window does not receive touch events, because stylus events take precedence, and it
3089 // already has an active stylus gesture.
3090
3091 // Stylus movements continue. They should be delivered to the left window and to the spy window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003092 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
3093 .deviceId(stylusDeviceId)
3094 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
3095 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003096 leftWindow->consumeMotionEvent(
3097 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
3098 spyWindow->consumeMotionEvent(
3099 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003100
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003101 // Further MOVE events keep going to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003102 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3103 .deviceId(touchDeviceId)
3104 .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110))
3105 .build());
3106 rightWindow->consumeMotionEvent(
3107 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003108
3109 spyWindow->assertNoEvents();
3110 leftWindow->assertNoEvents();
3111 rightWindow->assertNoEvents();
3112}
3113
3114/**
3115 * Three windows: a window on the left, a window on the right, and a spy window positioned above
3116 * both.
3117 * Check hover in left window and touch down in the right window.
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003118 * At first, spy should receive hover. Spy shouldn't receive touch while stylus is hovering.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003119 * At the same time, left and right should be getting independent streams of hovering and touch,
3120 * respectively.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003121 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003122TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003123 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3124
3125 sp<FakeWindowHandle> spyWindow =
3126 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3127 spyWindow->setFrame(Rect(0, 0, 400, 400));
3128 spyWindow->setTrustedOverlay(true);
3129 spyWindow->setSpy(true);
3130
3131 sp<FakeWindowHandle> leftWindow =
3132 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3133 leftWindow->setFrame(Rect(0, 0, 200, 200));
3134
3135 sp<FakeWindowHandle> rightWindow =
3136 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3137 rightWindow->setFrame(Rect(200, 0, 400, 200));
3138
3139 mDispatcher->onWindowInfosChanged(
3140 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3141
3142 const int32_t stylusDeviceId = 1;
3143 const int32_t touchDeviceId = 2;
3144
3145 // Stylus hover on the left window
3146 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3147 .deviceId(stylusDeviceId)
3148 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
3149 .build());
3150 leftWindow->consumeMotionEvent(
3151 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3152 spyWindow->consumeMotionEvent(
3153 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3154
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003155 // Touch down on the right window. Spy doesn't receive this touch because it already has
3156 // stylus hovering there.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003157 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3158 .deviceId(touchDeviceId)
3159 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3160 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003161 leftWindow->assertNoEvents();
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003162 spyWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003163 rightWindow->consumeMotionEvent(
3164 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3165
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003166 // Stylus movements continue. They should be delivered to the left window and the spy.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003167 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3168 .deviceId(stylusDeviceId)
3169 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
3170 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003171 leftWindow->consumeMotionEvent(
3172 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003173 spyWindow->consumeMotionEvent(
3174 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003175
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003176 // Touch movements continue. They should be delivered to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003177 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3178 .deviceId(touchDeviceId)
3179 .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101))
3180 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003181 rightWindow->consumeMotionEvent(
3182 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
3183
3184 spyWindow->assertNoEvents();
3185 leftWindow->assertNoEvents();
3186 rightWindow->assertNoEvents();
3187}
3188
3189/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003190 * On a single window, use two different devices: mouse and touch.
3191 * Touch happens first, with two pointers going down, and then the first pointer leaving.
3192 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
3193 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
3194 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
3195 * represent a new gesture.
3196 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003197TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003198 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3199 sp<FakeWindowHandle> window =
3200 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3201 window->setFrame(Rect(0, 0, 400, 400));
3202
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003203 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003204
3205 const int32_t touchDeviceId = 4;
3206 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003207
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003208 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003209 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3210 .deviceId(touchDeviceId)
3211 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3212 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003213 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003214 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3215 .deviceId(touchDeviceId)
3216 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3217 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3218 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003219 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003220 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
3221 .deviceId(touchDeviceId)
3222 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3223 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3224 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003225 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3226 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3227 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
3228
3229 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00003230 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3231 .deviceId(mouseDeviceId)
3232 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3233 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3234 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003235
3236 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08003237 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003238 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3239
Prabir Pradhan678438e2023-04-13 19:32:51 +00003240 mDispatcher->notifyMotion(
3241 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3242 .deviceId(mouseDeviceId)
3243 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3244 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3245 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3246 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003247 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3248
3249 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003250 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3251 .deviceId(touchDeviceId)
3252 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3253 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3254 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003255 // Since we already canceled this touch gesture, it will be ignored until a completely new
3256 // gesture is started. This is easier to implement than trying to keep track of the new pointer
3257 // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN.
3258 // However, mouse movements should continue to work.
3259 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3260 .deviceId(mouseDeviceId)
3261 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3262 .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110))
3263 .build());
3264 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId)));
3265
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003266 window->assertNoEvents();
3267}
3268
3269/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003270 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
3271 * the injected event.
3272 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003273TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) {
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003274 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3275 sp<FakeWindowHandle> window =
3276 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3277 window->setFrame(Rect(0, 0, 400, 400));
3278
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003279 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003280
3281 const int32_t touchDeviceId = 4;
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003282 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
3283 // completion.
3284 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003285 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003286 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3287 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003288 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003289 .build()));
3290 window->consumeMotionEvent(
3291 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3292
3293 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
3294 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003295 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3296 .deviceId(touchDeviceId)
3297 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3298 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003299
3300 window->consumeMotionEvent(
3301 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3302 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3303}
3304
3305/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003306 * This test is similar to the test above, but the sequence of injected events is different.
3307 *
3308 * Two windows: a window on the left and a window on the right.
3309 * Mouse is hovered over the left window.
3310 * Next, we tap on the left window, where the cursor was last seen.
3311 *
3312 * After that, we inject one finger down onto the right window, and then a second finger down onto
3313 * the left window.
3314 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
3315 * window (first), and then another on the left window (second).
3316 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
3317 * In the buggy implementation, second finger down on the left window would cause a crash.
3318 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003319TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) {
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003320 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3321 sp<FakeWindowHandle> leftWindow =
3322 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3323 leftWindow->setFrame(Rect(0, 0, 200, 200));
3324
3325 sp<FakeWindowHandle> rightWindow =
3326 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3327 rightWindow->setFrame(Rect(200, 0, 400, 200));
3328
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003329 mDispatcher->onWindowInfosChanged(
3330 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003331
3332 const int32_t mouseDeviceId = 6;
3333 const int32_t touchDeviceId = 4;
3334 // Hover over the left window. Keep the cursor there.
3335 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003336 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003337 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3338 AINPUT_SOURCE_MOUSE)
3339 .deviceId(mouseDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003340 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003341 .build()));
3342 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3343
3344 // Tap on left window
3345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003346 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003347 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3348 AINPUT_SOURCE_TOUCHSCREEN)
3349 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003350 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003351 .build()));
3352
3353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003354 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003355 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3356 AINPUT_SOURCE_TOUCHSCREEN)
3357 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003358 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003359 .build()));
3360 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3361 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3362 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
3363
3364 // First finger down on right window
3365 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003366 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003367 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3368 AINPUT_SOURCE_TOUCHSCREEN)
3369 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003370 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003371 .build()));
3372 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3373
3374 // Second finger down on the left window
3375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003376 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003377 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3378 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003379 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3380 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003381 .build()));
3382 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3383 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
3384
3385 // No more events
3386 leftWindow->assertNoEvents();
3387 rightWindow->assertNoEvents();
3388}
3389
3390/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003391 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
3392 * While the touch is down, new hover events from the stylus device should be ignored. After the
3393 * touch is gone, stylus hovering should start working again.
3394 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003395TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) {
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003396 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3397 sp<FakeWindowHandle> window =
3398 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3399 window->setFrame(Rect(0, 0, 200, 200));
3400
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003401 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003402
3403 const int32_t stylusDeviceId = 5;
3404 const int32_t touchDeviceId = 4;
3405 // Start hovering with stylus
3406 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003407 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003408 MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003409 .deviceId(stylusDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003410 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003411 .build()));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003412 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003413
3414 // Finger down on the window
3415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003416 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003417 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003418 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003419 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003420 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003421 // The touch device should be ignored!
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003422
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003423 // Continue hovering with stylus.
3424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003425 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003426 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3427 AINPUT_SOURCE_STYLUS)
3428 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003429 .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003430 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003431 // Hovers continue to work
3432 window->consumeMotionEvent(
3433 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003434
3435 // Lift up the finger
3436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003437 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003438 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3439 AINPUT_SOURCE_TOUCHSCREEN)
3440 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003441 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003442 .build()));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003443
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003444 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003445 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003446 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3447 AINPUT_SOURCE_STYLUS)
3448 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003449 .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003450 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003451 window->consumeMotionEvent(
3452 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003453 window->assertNoEvents();
3454}
3455
3456/**
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07003457 * If stylus is down anywhere on the screen, then touches should not be delivered to windows that
3458 * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH.
3459 *
3460 * Two windows: one on the left and one on the right.
3461 * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config.
3462 * Stylus down on the left window, and then touch down on the right window.
3463 * Check that the right window doesn't get touches while the stylus is down on the left window.
3464 */
3465TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) {
3466 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3467 sp<FakeWindowHandle> leftWindow =
3468 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
3469 ADISPLAY_ID_DEFAULT);
3470 leftWindow->setFrame(Rect(0, 0, 100, 100));
3471
3472 sp<FakeWindowHandle> sbtRightWindow =
3473 sp<FakeWindowHandle>::make(application, mDispatcher,
3474 "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT);
3475 sbtRightWindow->setFrame(Rect(100, 100, 200, 200));
3476 sbtRightWindow->setGlobalStylusBlocksTouch(true);
3477
3478 mDispatcher->onWindowInfosChanged(
3479 {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0});
3480
3481 const int32_t stylusDeviceId = 5;
3482 const int32_t touchDeviceId = 4;
3483
3484 // Stylus down in the left window
3485 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3486 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52))
3487 .deviceId(stylusDeviceId)
3488 .build());
3489 leftWindow->consumeMotionEvent(
3490 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3491
3492 // Finger tap on the right window
3493 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3494 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3495 .deviceId(touchDeviceId)
3496 .build());
3497 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3498 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3499 .deviceId(touchDeviceId)
3500 .build());
3501
3502 // The touch should be blocked, because stylus is down somewhere else on screen!
3503 sbtRightWindow->assertNoEvents();
3504
3505 // Continue stylus motion, and ensure it's not impacted.
3506 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
3507 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3508 .deviceId(stylusDeviceId)
3509 .build());
3510 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3511 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3512 .deviceId(stylusDeviceId)
3513 .build());
3514 leftWindow->consumeMotionEvent(
3515 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
3516 leftWindow->consumeMotionEvent(
3517 AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId)));
3518
3519 // Now that the stylus gesture is done, touches should be getting delivered correctly.
3520 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3521 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153))
3522 .deviceId(touchDeviceId)
3523 .build());
3524 sbtRightWindow->consumeMotionEvent(
3525 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3526}
3527
3528/**
3529 * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows
3530 * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH.
3531 *
3532 * Two windows: one on the left and one on the right.
3533 * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config.
3534 * Stylus hover on the left window, and then touch down on the right window.
3535 * Check that the right window doesn't get touches while the stylus is hovering on the left window.
3536 */
3537TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) {
3538 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3539 sp<FakeWindowHandle> leftWindow =
3540 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
3541 ADISPLAY_ID_DEFAULT);
3542 leftWindow->setFrame(Rect(0, 0, 100, 100));
3543
3544 sp<FakeWindowHandle> sbtRightWindow =
3545 sp<FakeWindowHandle>::make(application, mDispatcher,
3546 "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT);
3547 sbtRightWindow->setFrame(Rect(100, 100, 200, 200));
3548 sbtRightWindow->setGlobalStylusBlocksTouch(true);
3549
3550 mDispatcher->onWindowInfosChanged(
3551 {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0});
3552
3553 const int32_t stylusDeviceId = 5;
3554 const int32_t touchDeviceId = 4;
3555
3556 // Stylus hover in the left window
3557 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3558 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52))
3559 .deviceId(stylusDeviceId)
3560 .build());
3561 leftWindow->consumeMotionEvent(
3562 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3563
3564 // Finger tap on the right window
3565 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3566 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3567 .deviceId(touchDeviceId)
3568 .build());
3569 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3570 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3571 .deviceId(touchDeviceId)
3572 .build());
3573
3574 // The touch should be blocked, because stylus is hovering somewhere else on screen!
3575 sbtRightWindow->assertNoEvents();
3576
3577 // Continue stylus motion, and ensure it's not impacted.
3578 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3579 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3580 .deviceId(stylusDeviceId)
3581 .build());
3582 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3583 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3584 .deviceId(stylusDeviceId)
3585 .build());
3586 leftWindow->consumeMotionEvent(
3587 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
3588 leftWindow->consumeMotionEvent(
3589 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId)));
3590
3591 // Now that the stylus gesture is done, touches should be getting delivered correctly.
3592 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3593 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153))
3594 .deviceId(touchDeviceId)
3595 .build());
3596 sbtRightWindow->consumeMotionEvent(
3597 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3598}
3599
3600/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003601 * A spy window above a window with no input channel.
3602 * Start hovering with a stylus device, and then tap with it.
3603 * Ensure spy window receives the entire sequence.
3604 */
3605TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
3606 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3607 sp<FakeWindowHandle> spyWindow =
3608 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3609 spyWindow->setFrame(Rect(0, 0, 200, 200));
3610 spyWindow->setTrustedOverlay(true);
3611 spyWindow->setSpy(true);
3612 sp<FakeWindowHandle> window =
3613 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3614 window->setNoInputChannel(true);
3615 window->setFrame(Rect(0, 0, 200, 200));
3616
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003617 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003618
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003619 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00003620 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3621 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3622 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003623 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3624 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003625 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3626 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3627 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003628 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3629
3630 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003631 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3632 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3633 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003634 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3635
3636 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00003637 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3638 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3639 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003640 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
3641
3642 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00003643 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3644 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3645 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003646 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3647 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003648 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3649 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3650 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003651 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3652
3653 // No more events
3654 spyWindow->assertNoEvents();
3655 window->assertNoEvents();
3656}
3657
3658/**
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07003659 * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be
3660 * rejected. But since we already have an ongoing gesture, this event should be processed.
3661 * This prevents inconsistent events being handled inside the dispatcher.
3662 */
3663TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) {
3664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3665
3666 sp<FakeWindowHandle> window =
3667 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3668 window->setFrame(Rect(0, 0, 200, 200));
3669
3670 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
3671
3672 // Start hovering with stylus
3673 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3674 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3675 .build());
3676 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3677
3678 NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3679 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3680 .build();
3681 // Make this 'hoverExit' event stale
3682 mFakePolicy->setStaleEventTimeout(100ms);
3683 std::this_thread::sleep_for(100ms);
3684
3685 // It shouldn't be dropped by the dispatcher, even though it's stale.
3686 mDispatcher->notifyMotion(hoverExit);
3687 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3688
3689 // Stylus starts hovering again! There should be no crash.
3690 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3691 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51))
3692 .build());
3693 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3694}
3695
3696/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003697 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
3698 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3699 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3700 * While the mouse is down, new move events from the touch device should be ignored.
3701 */
3702TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3703 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3704 sp<FakeWindowHandle> spyWindow =
3705 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3706 spyWindow->setFrame(Rect(0, 0, 200, 200));
3707 spyWindow->setTrustedOverlay(true);
3708 spyWindow->setSpy(true);
3709 sp<FakeWindowHandle> window =
3710 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3711 window->setFrame(Rect(0, 0, 200, 200));
3712
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003713 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003714
3715 const int32_t mouseDeviceId = 7;
3716 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003717
3718 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003719 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3720 .deviceId(mouseDeviceId)
3721 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3722 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003723 spyWindow->consumeMotionEvent(
3724 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3725 window->consumeMotionEvent(
3726 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3727
3728 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003729 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3730 .deviceId(touchDeviceId)
3731 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3732 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003733 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3734 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3735 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3736 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3737
Prabir Pradhan678438e2023-04-13 19:32:51 +00003738 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3739 .deviceId(touchDeviceId)
3740 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3741 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003742 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3743 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3744
3745 // Pilfer the stream
3746 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3747 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3748
Prabir Pradhan678438e2023-04-13 19:32:51 +00003749 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3750 .deviceId(touchDeviceId)
3751 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3752 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003753 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3754
3755 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003756 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3757 .deviceId(mouseDeviceId)
3758 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3759 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3760 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003761
3762 spyWindow->consumeMotionEvent(
3763 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3764 spyWindow->consumeMotionEvent(
3765 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3766 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3767
Prabir Pradhan678438e2023-04-13 19:32:51 +00003768 mDispatcher->notifyMotion(
3769 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3770 .deviceId(mouseDeviceId)
3771 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3772 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3773 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3774 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003775 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3776 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3777
3778 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3780 .deviceId(mouseDeviceId)
3781 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3782 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3783 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003784 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3785 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3786
3787 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003788 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3789 .deviceId(touchDeviceId)
3790 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3791 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003792
3793 // No more events
3794 spyWindow->assertNoEvents();
3795 window->assertNoEvents();
3796}
3797
3798/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003799 * On the display, have a single window, and also an area where there's no window.
3800 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
3801 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
3802 */
3803TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3804 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3805 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003806 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003807
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003808 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003809
3810 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00003811 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003812
3813 mDispatcher->waitForIdle();
3814 window->assertNoEvents();
3815
3816 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003817 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003818 mDispatcher->waitForIdle();
3819 window->consumeMotionDown();
3820}
3821
3822/**
3823 * Same test as above, but instead of touching the empty space, the first touch goes to
3824 * non-touchable window.
3825 */
3826TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3827 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3828 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003829 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003830 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3831 window1->setTouchable(false);
3832 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003833 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003834 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3835
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003836 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003837
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003838 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003839 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003840
3841 mDispatcher->waitForIdle();
3842 window1->assertNoEvents();
3843 window2->assertNoEvents();
3844
3845 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003846 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003847 mDispatcher->waitForIdle();
3848 window2->consumeMotionDown();
3849}
3850
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003851/**
3852 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3853 * to the event time of the first ACTION_DOWN sent to the particular window.
3854 */
3855TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3856 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3857 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003858 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003859 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3860 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003861 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003862 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3863
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003864 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003865
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003866 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003867 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003868
3869 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003870
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003871 std::unique_ptr<MotionEvent> motionEvent1 = window1->consumeMotionEvent();
3872 ASSERT_NE(nullptr, motionEvent1);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003873 window2->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003874 nsecs_t downTimeForWindow1 = motionEvent1->getDownTime();
3875 ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003876
3877 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003878 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003879 mDispatcher->waitForIdle();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003880 std::unique_ptr<MotionEvent> motionEvent2 = window2->consumeMotionEvent();
3881 ASSERT_NE(nullptr, motionEvent2);
3882 nsecs_t downTimeForWindow2 = motionEvent2->getDownTime();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003883 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003884 ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003885
3886 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003887 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003888 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003889 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003890
3891 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003892 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003893 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003894 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003895
3896 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3897 window1->consumeMotionMove();
3898 window1->assertNoEvents();
3899
3900 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003901 mDispatcher->notifyMotion(
3902 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003903 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003904 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003905
Prabir Pradhan678438e2023-04-13 19:32:51 +00003906 mDispatcher->notifyMotion(
3907 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003908 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003909 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003910}
3911
Garfield Tandf26e862020-07-01 20:18:19 -07003912TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003913 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003914 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003915 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003916 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003917 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003918 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003919 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003920
3921 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3922
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003923 mDispatcher->onWindowInfosChanged(
3924 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07003925
3926 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003928 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003929 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3930 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003931 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003932 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003933 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003934
3935 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003937 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003938 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3939 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003940 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003941 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003942 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3943 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003944
3945 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003947 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003948 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3949 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003950 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003951 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003952 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3953 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003954
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003955 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003956 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003957 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3958 AINPUT_SOURCE_MOUSE)
3959 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3960 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003961 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003962 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003963 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003964
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003966 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003967 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3968 AINPUT_SOURCE_MOUSE)
3969 .buttonState(0)
3970 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003971 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003972 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003973 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003974
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003976 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003977 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3978 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003979 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003980 .build()));
3981 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3982
3983 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003985 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003986 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3987 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003988 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003989 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003990 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003991
3992 // No more events
3993 windowLeft->assertNoEvents();
3994 windowRight->assertNoEvents();
3995}
3996
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003997/**
3998 * Put two fingers down (and don't release them) and click the mouse button.
3999 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
4000 * currently active gesture should be canceled, and the new one should proceed.
4001 */
4002TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
4003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4004 sp<FakeWindowHandle> window =
4005 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4006 window->setFrame(Rect(0, 0, 600, 800));
4007
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004008 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004009
4010 const int32_t touchDeviceId = 4;
4011 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004012
4013 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004014 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4015 .deviceId(touchDeviceId)
4016 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4017 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004018
Prabir Pradhan678438e2023-04-13 19:32:51 +00004019 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4020 .deviceId(touchDeviceId)
4021 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4022 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
4023 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004024 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4025 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4026
4027 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00004028 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4029 .deviceId(mouseDeviceId)
4030 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4031 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4032 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004033 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
4034 WithPointerCount(2u)));
4035 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4036
Prabir Pradhan678438e2023-04-13 19:32:51 +00004037 mDispatcher->notifyMotion(
4038 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4039 .deviceId(mouseDeviceId)
4040 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4041 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4042 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4043 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004044 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4045
4046 // Try to send more touch events while the mouse is down. Since it's a continuation of an
4047 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004048 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4049 .deviceId(touchDeviceId)
4050 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
4051 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
4052 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004053 window->assertNoEvents();
4054}
4055
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004056TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
4057 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4058
4059 sp<FakeWindowHandle> spyWindow =
4060 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4061 spyWindow->setFrame(Rect(0, 0, 600, 800));
4062 spyWindow->setTrustedOverlay(true);
4063 spyWindow->setSpy(true);
4064 sp<FakeWindowHandle> window =
4065 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4066 window->setFrame(Rect(0, 0, 600, 800));
4067
4068 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004069 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004070
4071 // Send mouse cursor to the window
4072 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004073 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004074 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4075 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004076 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004077 .build()));
4078
4079 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4080 WithSource(AINPUT_SOURCE_MOUSE)));
4081 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4082 WithSource(AINPUT_SOURCE_MOUSE)));
4083
4084 window->assertNoEvents();
4085 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004086}
4087
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004088TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
4089 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4090
4091 sp<FakeWindowHandle> spyWindow =
4092 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4093 spyWindow->setFrame(Rect(0, 0, 600, 800));
4094 spyWindow->setTrustedOverlay(true);
4095 spyWindow->setSpy(true);
4096 sp<FakeWindowHandle> window =
4097 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4098 window->setFrame(Rect(0, 0, 600, 800));
4099
4100 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004101 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004102
4103 // Send mouse cursor to the window
4104 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004105 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004106 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4107 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004108 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004109 .build()));
4110
4111 // Move mouse cursor
4112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004113 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004114 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4115 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004116 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004117 .build()));
4118
4119 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4120 WithSource(AINPUT_SOURCE_MOUSE)));
4121 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4122 WithSource(AINPUT_SOURCE_MOUSE)));
4123 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4124 WithSource(AINPUT_SOURCE_MOUSE)));
4125 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4126 WithSource(AINPUT_SOURCE_MOUSE)));
4127 // Touch down on the window
4128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004129 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004130 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4131 AINPUT_SOURCE_TOUCHSCREEN)
4132 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004133 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004134 .build()));
4135 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4136 WithSource(AINPUT_SOURCE_MOUSE)));
4137 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4138 WithSource(AINPUT_SOURCE_MOUSE)));
4139 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4140 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4141 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4142 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4143
4144 // pilfer the motion, retaining the gesture on the spy window.
4145 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
4146 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4147 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4148
4149 // Touch UP on the window
4150 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004151 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004152 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4153 AINPUT_SOURCE_TOUCHSCREEN)
4154 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004155 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004156 .build()));
4157 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4158 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4159
4160 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
4161 // to send a new gesture. It should again go to both windows (spy and the window below), just
4162 // like the first gesture did, before pilfering. The window configuration has not changed.
4163
4164 // One more tap - DOWN
4165 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004166 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004167 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4168 AINPUT_SOURCE_TOUCHSCREEN)
4169 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004170 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004171 .build()));
4172 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4173 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4174 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4175 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4176
4177 // Touch UP on the window
4178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004179 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004180 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4181 AINPUT_SOURCE_TOUCHSCREEN)
4182 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004183 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004184 .build()));
4185 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4186 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4187 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4188 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4189
4190 window->assertNoEvents();
4191 spyWindow->assertNoEvents();
4192}
4193
Garfield Tandf26e862020-07-01 20:18:19 -07004194// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
4195// directly in this test.
4196TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07004197 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07004198 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004199 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004200 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07004201
4202 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4203
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004204 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07004205
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004206 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004207 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004208 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4209 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004210 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004211 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004212 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004213 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004215 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004216 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4217 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004218 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004219 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004220 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4221 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07004222
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004224 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004225 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
4226 AINPUT_SOURCE_MOUSE)
4227 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4228 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004229 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004230 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004231 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07004232
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004234 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004235 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
4236 AINPUT_SOURCE_MOUSE)
4237 .buttonState(0)
4238 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004239 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004240 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004241 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07004242
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004244 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004245 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
4246 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004247 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004248 .build()));
4249 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4250
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07004251 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
4252 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
4253 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004254 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004255 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
4256 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004257 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004258 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004259 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004260}
4261
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004262/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004263 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
4264 * is generated.
4265 */
4266TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
4267 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4268 sp<FakeWindowHandle> window =
4269 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4270 window->setFrame(Rect(0, 0, 1200, 800));
4271
4272 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4273
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004274 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004275
4276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004277 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004278 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4279 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004280 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004281 .build()));
4282 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4283
4284 // Remove the window, but keep the channel.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004285 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004286 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4287}
4288
4289/**
Daniel Norman7487dfa2023-08-02 16:39:45 -07004290 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
4291 */
Ameer Armalycff4fa52023-10-04 23:45:11 +00004292TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash,
4293 REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags,
4294 a11y_crash_on_inconsistent_event_stream))) {
Daniel Norman7487dfa2023-08-02 16:39:45 -07004295 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4296 sp<FakeWindowHandle> window =
4297 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4298 window->setFrame(Rect(0, 0, 1200, 800));
4299
4300 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4301
4302 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4303
4304 MotionEventBuilder hoverEnterBuilder =
4305 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4306 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4307 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4308 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4309 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4311 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4312 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4313 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4314}
4315
4316/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004317 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
4318 */
4319TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
4320 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4321 sp<FakeWindowHandle> window =
4322 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4323 window->setFrame(Rect(0, 0, 100, 100));
4324
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004325 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004326
4327 const int32_t mouseDeviceId = 7;
4328 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004329
4330 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00004331 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4332 .deviceId(mouseDeviceId)
4333 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4334 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004335 window->consumeMotionEvent(
4336 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4337
4338 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004339 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4340 .deviceId(touchDeviceId)
4341 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4342 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004343
4344 window->consumeMotionEvent(
4345 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4346 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4347}
4348
4349/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004350 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004351 * The tap causes a HOVER_EXIT event to be generated because the current event
4352 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004353 */
4354TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
4355 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4356 sp<FakeWindowHandle> window =
4357 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4358 window->setFrame(Rect(0, 0, 100, 100));
4359
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004360 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004361 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4362 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4363 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004364 ASSERT_NO_FATAL_FAILURE(
4365 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4366 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004367
4368 // Tap on the window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004369 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4370 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4371 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004372 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004373 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4374 WithSource(AINPUT_SOURCE_MOUSE))));
4375
4376 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004377 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4378 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4379
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004380 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4381 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4382 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004383 ASSERT_NO_FATAL_FAILURE(
4384 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4385 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4386}
4387
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004388TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
4389 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4390 sp<FakeWindowHandle> windowDefaultDisplay =
4391 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4392 ADISPLAY_ID_DEFAULT);
4393 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
4394 sp<FakeWindowHandle> windowSecondDisplay =
4395 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
4396 SECOND_DISPLAY_ID);
4397 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
4398
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004399 mDispatcher->onWindowInfosChanged(
4400 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004401
4402 // Set cursor position in window in default display and check that hover enter and move
4403 // events are generated.
4404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004405 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004406 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4407 AINPUT_SOURCE_MOUSE)
4408 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004409 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004410 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004411 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004412
4413 // Remove all windows in secondary display and check that no event happens on window in
4414 // primary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004415 mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0});
4416
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004417 windowDefaultDisplay->assertNoEvents();
4418
4419 // Move cursor position in window in default display and check that only hover move
4420 // event is generated and not hover enter event.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004421 mDispatcher->onWindowInfosChanged(
4422 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004424 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004425 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4426 AINPUT_SOURCE_MOUSE)
4427 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004428 .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004429 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004430 windowDefaultDisplay->consumeMotionEvent(
4431 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4432 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004433 windowDefaultDisplay->assertNoEvents();
4434}
4435
Garfield Tan00f511d2019-06-12 16:55:40 -07004436TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07004437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07004438
4439 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004440 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004441 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004442 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004443 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004444 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004445
4446 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4447
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004448 mDispatcher->onWindowInfosChanged(
4449 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tan00f511d2019-06-12 16:55:40 -07004450
4451 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
4452 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004454 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004455 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004456 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004457 windowRight->assertNoEvents();
4458}
4459
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004460TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004461 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004462 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4463 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07004464 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004465
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004466 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07004467 setFocusedWindow(window);
4468
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004469 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004470
Prabir Pradhan678438e2023-04-13 19:32:51 +00004471 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004472
4473 // Window should receive key down event.
4474 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4475
4476 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
4477 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004478 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00004479 window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004480}
4481
4482TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004483 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004484 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4485 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004486
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004487 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004488
Prabir Pradhan678438e2023-04-13 19:32:51 +00004489 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4490 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004491
4492 // Window should receive motion down event.
4493 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4494
4495 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
4496 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004497 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004498 window->consumeMotionEvent(
4499 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004500}
4501
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004502TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
4503 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4504 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4505 "Fake Window", ADISPLAY_ID_DEFAULT);
4506
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004507 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004508
4509 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4510 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
4511 .build());
4512
4513 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4514
4515 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
4516 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
4517 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4518
4519 // After the device has been reset, a new hovering stream can be sent to the window
4520 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4521 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
4522 .build());
4523 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4524}
4525
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004526TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
4527 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004528 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4529 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004530 window->setFocusable(true);
4531
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004532 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004533 setFocusedWindow(window);
4534
4535 window->consumeFocusEvent(true);
4536
Prabir Pradhan678438e2023-04-13 19:32:51 +00004537 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004538 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
4539 const nsecs_t injectTime = keyArgs.eventTime;
4540 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00004541 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004542 // The dispatching time should be always greater than or equal to intercept key timeout.
4543 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4544 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
4545 std::chrono::nanoseconds(interceptKeyTimeout).count());
4546}
4547
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004548/**
4549 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
4550 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004551TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
4552 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004553 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4554 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004555 window->setFocusable(true);
4556
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004557 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004558 setFocusedWindow(window);
4559
4560 window->consumeFocusEvent(true);
4561
Prabir Pradhan678438e2023-04-13 19:32:51 +00004562 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004563 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004564
4565 // Set a value that's significantly larger than the default consumption timeout. If the
4566 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
4567 mFakePolicy->setInterceptKeyTimeout(600ms);
4568 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
4569 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004570 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4571}
4572
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004573/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004574 * Two windows. First is a regular window. Second does not overlap with the first, and has
4575 * WATCH_OUTSIDE_TOUCH.
4576 * Both windows are owned by the same UID.
4577 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
4578 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
4579 */
4580TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
4581 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004582 sp<FakeWindowHandle> window =
4583 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004584 window->setFrame(Rect{0, 0, 100, 100});
4585
4586 sp<FakeWindowHandle> outsideWindow =
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004587 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004588 ADISPLAY_ID_DEFAULT);
4589 outsideWindow->setFrame(Rect{100, 100, 200, 200});
4590 outsideWindow->setWatchOutsideTouch(true);
4591 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004592 mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004593
4594 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004595 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4596 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4597 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004598 window->consumeMotionDown();
4599 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
4600 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
4601 outsideWindow->consumeMotionEvent(
4602 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
Prabir Pradhan502a7252023-12-01 16:11:24 +00004603
4604 // Ensure outsideWindow doesn't get any more events for the gesture.
4605 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4606 ADISPLAY_ID_DEFAULT, {PointF{51, 51}}));
4607 window->consumeMotionMove();
4608 outsideWindow->assertNoEvents();
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004609}
4610
4611/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004612 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
4613 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
4614 * ACTION_OUTSIDE event is sent per gesture.
4615 */
4616TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
4617 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
4618 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004619 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4620 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004621 window->setWatchOutsideTouch(true);
4622 window->setFrame(Rect{0, 0, 100, 100});
4623 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004624 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4625 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004626 secondWindow->setFrame(Rect{100, 100, 200, 200});
4627 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004628 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
4629 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004630 thirdWindow->setFrame(Rect{200, 200, 300, 300});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004631 mDispatcher->onWindowInfosChanged(
4632 {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0});
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004633
4634 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004635 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4636 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4637 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004638 window->assertNoEvents();
4639 secondWindow->assertNoEvents();
4640
4641 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
4642 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004643 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4644 ADISPLAY_ID_DEFAULT,
4645 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004646 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
4647 window->consumeMotionEvent(
4648 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004649 secondWindow->consumeMotionDown();
4650 thirdWindow->assertNoEvents();
4651
4652 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
4653 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004654 mDispatcher->notifyMotion(
4655 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4656 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004657 window->assertNoEvents();
4658 secondWindow->consumeMotionMove();
4659 thirdWindow->consumeMotionDown();
4660}
4661
Prabir Pradhan814fe082022-07-22 20:22:18 +00004662TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
4663 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004664 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4665 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004666 window->setFocusable(true);
4667
Patrick Williamsd828f302023-04-28 17:52:08 -05004668 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004669 setFocusedWindow(window);
4670
4671 window->consumeFocusEvent(true);
4672
Prabir Pradhan678438e2023-04-13 19:32:51 +00004673 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
4674 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
4675 mDispatcher->notifyKey(keyDown);
4676 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004677
4678 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4679 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4680
4681 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05004682 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004683
4684 window->consumeFocusEvent(false);
4685
Prabir Pradhan678438e2023-04-13 19:32:51 +00004686 mDispatcher->notifyKey(keyDown);
4687 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004688 window->assertNoEvents();
4689}
4690
Arthur Hung96483742022-11-15 03:30:48 +00004691TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
4692 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4693 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4694 "Fake Window", ADISPLAY_ID_DEFAULT);
4695 // Ensure window is non-split and have some transform.
4696 window->setPreventSplitting(true);
4697 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05004698 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00004699
4700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004701 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung96483742022-11-15 03:30:48 +00004702 {50, 50}))
4703 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4704 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4705
4706 const MotionEvent secondFingerDownEvent =
4707 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4708 .displayId(ADISPLAY_ID_DEFAULT)
4709 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004710 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4711 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00004712 .build();
4713 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004714 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung96483742022-11-15 03:30:48 +00004715 InputEventInjectionSync::WAIT_FOR_RESULT))
4716 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4717
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004718 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
4719 ASSERT_NE(nullptr, event);
4720 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4721 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4722 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4723 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4724 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
Arthur Hung96483742022-11-15 03:30:48 +00004725}
4726
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07004727/**
4728 * Two windows: a splittable and a non-splittable.
4729 * The non-splittable window shouldn't receive any "incomplete" gestures.
4730 * Send the first pointer to the splittable window, and then touch the non-splittable window.
4731 * The second pointer should be dropped because the initial window is splittable, so it won't get
4732 * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any
4733 * "incomplete" gestures.
4734 */
4735TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) {
4736 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4737 sp<FakeWindowHandle> leftWindow =
4738 sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window",
4739 ADISPLAY_ID_DEFAULT);
4740 leftWindow->setPreventSplitting(false);
4741 leftWindow->setFrame(Rect(0, 0, 100, 100));
4742 sp<FakeWindowHandle> rightWindow =
4743 sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window",
4744 ADISPLAY_ID_DEFAULT);
4745 rightWindow->setPreventSplitting(true);
4746 rightWindow->setFrame(Rect(100, 100, 200, 200));
4747 mDispatcher->onWindowInfosChanged(
4748 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
4749
4750 // Touch down on left, splittable window
4751 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4752 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4753 .build());
4754 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4755
4756 mDispatcher->notifyMotion(
4757 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4758 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4759 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
4760 .build());
4761 leftWindow->assertNoEvents();
4762 rightWindow->assertNoEvents();
4763}
4764
Harry Cuttsb166c002023-05-09 13:06:05 +00004765TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4766 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4767 sp<FakeWindowHandle> window =
4768 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4769 window->setFrame(Rect(0, 0, 400, 400));
4770 sp<FakeWindowHandle> trustedOverlay =
4771 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4772 ADISPLAY_ID_DEFAULT);
4773 trustedOverlay->setSpy(true);
4774 trustedOverlay->setTrustedOverlay(true);
4775
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004776 mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004777
4778 // Start a three-finger touchpad swipe
4779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4780 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4781 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4782 .build());
4783 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4784 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4785 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4786 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4787 .build());
4788 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4789 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4790 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4791 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4792 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4793 .build());
4794
4795 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4796 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4797 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4798
4799 // Move the swipe a bit
4800 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4801 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4802 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4803 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4804 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4805 .build());
4806
4807 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4808
4809 // End the swipe
4810 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4811 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4812 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4813 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4814 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4815 .build());
4816 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4817 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4818 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4819 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4820 .build());
4821 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4822 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4823 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4824 .build());
4825
4826 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4827 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4828 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4829
4830 window->assertNoEvents();
4831}
4832
4833TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4834 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4835 sp<FakeWindowHandle> window =
4836 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4837 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004838 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004839
4840 // Start a three-finger touchpad swipe
4841 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4842 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4843 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4844 .build());
4845 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4846 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4847 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4848 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4849 .build());
4850 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4851 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4852 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4853 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4854 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4855 .build());
4856
4857 // Move the swipe a bit
4858 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4859 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4860 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4861 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4862 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4863 .build());
4864
4865 // End the swipe
4866 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4867 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4868 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4869 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4870 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4871 .build());
4872 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4873 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4874 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4875 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4876 .build());
4877 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4878 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4879 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4880 .build());
4881
4882 window->assertNoEvents();
4883}
4884
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004885/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004886 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
4887 * the first pointer.
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004888 * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004889 */
4890TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
4891 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4892 sp<FakeWindowHandle> window =
4893 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4894 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004895 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004896
4897 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
4898 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4899 .downTime(baseTime + 10)
4900 .eventTime(baseTime + 10)
4901 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4902 .build());
4903
4904 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4905
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004906 // Change the transform so that the orientation is now different from original.
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004907 window->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004908
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004909 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004910
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004911 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4912 .downTime(baseTime + 10)
4913 .eventTime(baseTime + 30)
4914 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4915 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4916 .build());
4917
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004918 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4919
4920 // Finish the gesture and start a new one. Ensure all events are sent to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004921 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
4922 .downTime(baseTime + 10)
4923 .eventTime(baseTime + 40)
4924 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4925 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4926 .build());
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004927
4928 window->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4929
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004930 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4931 .downTime(baseTime + 10)
4932 .eventTime(baseTime + 50)
4933 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4934 .build());
4935
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004936 window->consumeMotionEvent(WithMotionAction(ACTION_UP));
4937
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004938 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4939 .downTime(baseTime + 60)
4940 .eventTime(baseTime + 60)
4941 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
4942 .build());
4943
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004944 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004945}
4946
4947/**
Hu Guo771a7692023-09-17 20:51:08 +08004948 * When there are multiple screens, such as screen projection to TV or screen recording, if the
4949 * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and
4950 * its coordinates should be converted by the transform of the windows of target screen.
4951 */
4952TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) {
4953 // This case will create a window and a spy window on the default display and mirror
4954 // window on the second display. cancel event is sent through spy window pilferPointers
4955 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4956
4957 sp<FakeWindowHandle> spyWindowDefaultDisplay =
4958 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4959 spyWindowDefaultDisplay->setTrustedOverlay(true);
4960 spyWindowDefaultDisplay->setSpy(true);
4961
4962 sp<FakeWindowHandle> windowDefaultDisplay =
4963 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4964 ADISPLAY_ID_DEFAULT);
4965 windowDefaultDisplay->setWindowTransform(1, 0, 0, 1);
4966
4967 sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID);
4968 windowSecondDisplay->setWindowTransform(2, 0, 0, 2);
4969
4970 // Add the windows to the dispatcher
4971 mDispatcher->onWindowInfosChanged(
4972 {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(),
4973 *windowSecondDisplay->getInfo()},
4974 {},
4975 0,
4976 0});
4977
4978 // Send down to ADISPLAY_ID_DEFAULT
4979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4980 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4981 {100, 100}))
4982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4983
4984 spyWindowDefaultDisplay->consumeMotionDown();
4985 windowDefaultDisplay->consumeMotionDown();
4986
4987 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken()));
4988
4989 // windowDefaultDisplay gets cancel
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004990 std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent();
4991 ASSERT_NE(nullptr, event);
4992 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction());
Hu Guo771a7692023-09-17 20:51:08 +08004993
4994 // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the
4995 // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y
4996 // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of
4997 // SECOND_DISPLAY_ID, the x and y coordinates are 200
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004998 EXPECT_EQ(100, event->getX(0));
4999 EXPECT_EQ(100, event->getY(0));
Hu Guo771a7692023-09-17 20:51:08 +08005000}
5001
5002/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005003 * Ensure the correct coordinate spaces are used by InputDispatcher.
5004 *
5005 * InputDispatcher works in the display space, so its coordinate system is relative to the display
5006 * panel. Windows get events in the window space, and get raw coordinates in the logical display
5007 * space.
5008 */
5009class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
5010public:
5011 void SetUp() override {
5012 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005013 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005014 }
5015
5016 void addDisplayInfo(int displayId, const ui::Transform& transform) {
5017 gui::DisplayInfo info;
5018 info.displayId = displayId;
5019 info.transform = transform;
5020 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05005021 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005022 }
5023
5024 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
5025 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05005026 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005027 }
5028
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005029 void removeAllWindowsAndDisplays() {
5030 mDisplayInfos.clear();
5031 mWindowInfos.clear();
5032 }
5033
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005034 // Set up a test scenario where the display has a scaled projection and there are two windows
5035 // on the display.
5036 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
5037 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
5038 // respectively.
5039 ui::Transform displayTransform;
5040 displayTransform.set(2, 0, 0, 4);
5041 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5042
5043 std::shared_ptr<FakeApplicationHandle> application =
5044 std::make_shared<FakeApplicationHandle>();
5045
5046 // Add two windows to the display. Their frames are represented in the display space.
5047 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005048 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5049 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005050 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
5051 addWindow(firstWindow);
5052
5053 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005054 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5055 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005056 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
5057 addWindow(secondWindow);
5058 return {std::move(firstWindow), std::move(secondWindow)};
5059 }
5060
5061private:
5062 std::vector<gui::DisplayInfo> mDisplayInfos;
5063 std::vector<gui::WindowInfo> mWindowInfos;
5064};
5065
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005066TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005067 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5068 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005069 // selected so that if the hit test was performed with the point and the bounds being in
5070 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005071 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5072 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5073 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005074
5075 firstWindow->consumeMotionDown();
5076 secondWindow->assertNoEvents();
5077}
5078
5079// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
5080// the event should be treated as being in the logical display space.
5081TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
5082 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5083 // Send down to the first window. The point is represented in the logical display space. The
5084 // point is selected so that if the hit test was done in logical display space, then it would
5085 // end up in the incorrect window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005086 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005087 PointF{75 * 2, 55 * 4});
5088
5089 firstWindow->consumeMotionDown();
5090 secondWindow->assertNoEvents();
5091}
5092
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005093// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
5094// event should be treated as being in the logical display space.
5095TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
5096 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5097
5098 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5099 ui::Transform injectedEventTransform;
5100 injectedEventTransform.set(matrix);
5101 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
5102 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
5103
5104 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5105 .displayId(ADISPLAY_ID_DEFAULT)
5106 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005107 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005108 .x(untransformedPoint.x)
5109 .y(untransformedPoint.y))
5110 .build();
5111 event.transform(matrix);
5112
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005113 injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT,
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005114 InputEventInjectionSync::WAIT_FOR_RESULT);
5115
5116 firstWindow->consumeMotionDown();
5117 secondWindow->assertNoEvents();
5118}
5119
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005120TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
5121 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5122
5123 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005124 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5125 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5126 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005127
5128 firstWindow->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005129 std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent();
5130 ASSERT_NE(nullptr, event);
5131 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005132
5133 // Ensure that the events from the "getRaw" API are in logical display coordinates.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005134 EXPECT_EQ(300, event->getRawX(0));
5135 EXPECT_EQ(880, event->getRawY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005136
5137 // Ensure that the x and y values are in the window's coordinate space.
5138 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
5139 // the logical display space. This will be the origin of the window space.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005140 EXPECT_EQ(100, event->getX(0));
5141 EXPECT_EQ(80, event->getY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005142}
5143
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005144TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) {
5145 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5146 // The monitor will always receive events in the logical display's coordinate space, because
5147 // it does not have a window.
Prabir Pradhanfb549072023-10-05 19:17:36 +00005148 FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005149
5150 // Send down to the first window.
5151 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5152 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
5153 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5154 monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5155
5156 // Second pointer goes down on second window.
5157 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5158 ADISPLAY_ID_DEFAULT,
5159 {PointF{50, 100}, PointF{150, 220}}));
5160 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80)));
5161 const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}},
5162 {1, PointF{300, 880}}};
5163 monitor.consumeMotionEvent(
5164 AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers)));
5165
5166 mDispatcher->cancelCurrentTouch();
5167
5168 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5169 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80)));
5170 monitor.consumeMotionEvent(
5171 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers)));
5172}
5173
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005174TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) {
5175 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5176
5177 // Send down to the first window.
5178 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5179 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
5180 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5181
5182 // The pointer is transferred to the second window, and the second window receives it in the
5183 // correct coordinate space.
5184 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
5185 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5186 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400)));
5187}
5188
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005189TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) {
5190 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5191
5192 // Send hover move to the second window, and ensure it shows up as hover enter.
5193 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
5194 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5195 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5196 WithCoords(100, 80), WithRawCoords(300, 880)));
5197
5198 // Touch down at the same location and ensure a hover exit is synthesized.
5199 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
5200 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5201 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5202 WithRawCoords(300, 880)));
5203 secondWindow->consumeMotionEvent(
5204 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5205 secondWindow->assertNoEvents();
5206 firstWindow->assertNoEvents();
5207}
5208
Prabir Pradhan453ae732023-10-13 14:30:14 +00005209// Same as above, but while the window is being mirrored.
5210TEST_F(InputDispatcherDisplayProjectionTest,
5211 SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) {
5212 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5213
5214 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5215 ui::Transform secondDisplayTransform;
5216 secondDisplayTransform.set(matrix);
5217 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5218
5219 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5220 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5221 addWindow(secondWindowClone);
5222
5223 // Send hover move to the second window, and ensure it shows up as hover enter.
5224 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
5225 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5226 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5227 WithCoords(100, 80), WithRawCoords(300, 880)));
5228
5229 // Touch down at the same location and ensure a hover exit is synthesized for the correct
5230 // display.
5231 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
5232 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5233 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5234 WithRawCoords(300, 880)));
5235 secondWindow->consumeMotionEvent(
5236 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5237 secondWindow->assertNoEvents();
5238 firstWindow->assertNoEvents();
5239}
5240
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005241TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) {
5242 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5243
5244 // Send hover enter to second window
5245 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
5246 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5247 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5248 WithCoords(100, 80), WithRawCoords(300, 880)));
5249
5250 mDispatcher->cancelCurrentTouch();
5251
5252 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5253 WithRawCoords(300, 880)));
5254 secondWindow->assertNoEvents();
5255 firstWindow->assertNoEvents();
5256}
5257
Prabir Pradhan453ae732023-10-13 14:30:14 +00005258// Same as above, but while the window is being mirrored.
Prabir Pradhan16463382023-10-12 23:03:19 +00005259TEST_F(InputDispatcherDisplayProjectionTest,
5260 SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) {
5261 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5262
5263 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5264 ui::Transform secondDisplayTransform;
5265 secondDisplayTransform.set(matrix);
5266 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5267
5268 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5269 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5270 addWindow(secondWindowClone);
5271
5272 // Send hover enter to second window
5273 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
5274 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5275 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5276 WithCoords(100, 80), WithRawCoords(300, 880),
5277 WithDisplayId(ADISPLAY_ID_DEFAULT)));
5278
5279 mDispatcher->cancelCurrentTouch();
5280
5281 // Ensure the cancelation happens with the correct displayId and the correct coordinates.
5282 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5283 WithRawCoords(300, 880),
5284 WithDisplayId(ADISPLAY_ID_DEFAULT)));
5285 secondWindow->assertNoEvents();
5286 firstWindow->assertNoEvents();
5287}
5288
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005289/** Ensure consistent behavior of InputDispatcher in all orientations. */
5290class InputDispatcherDisplayOrientationFixture
5291 : public InputDispatcherDisplayProjectionTest,
5292 public ::testing::WithParamInterface<ui::Rotation> {};
5293
5294// This test verifies the touchable region of a window for all rotations of the display by tapping
5295// in different locations on the display, specifically points close to the four corners of a
5296// window.
5297TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
5298 constexpr static int32_t displayWidth = 400;
5299 constexpr static int32_t displayHeight = 800;
5300
5301 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5302
5303 const auto rotation = GetParam();
5304
5305 // Set up the display with the specified rotation.
5306 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
5307 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
5308 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
5309 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
5310 logicalDisplayWidth, logicalDisplayHeight);
5311 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5312
5313 // Create a window with its bounds determined in the logical display.
5314 const Rect frameInLogicalDisplay(100, 100, 200, 300);
5315 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
5316 sp<FakeWindowHandle> window =
5317 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5318 window->setFrame(frameInDisplay, displayTransform);
5319 addWindow(window);
5320
5321 // The following points in logical display space should be inside the window.
5322 static const std::array<vec2, 4> insidePoints{
5323 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
5324 for (const auto pointInsideWindow : insidePoints) {
5325 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
5326 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005327 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5328 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5329 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005330 window->consumeMotionDown();
5331
Prabir Pradhan678438e2023-04-13 19:32:51 +00005332 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5333 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5334 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005335 window->consumeMotionUp();
5336 }
5337
5338 // The following points in logical display space should be outside the window.
5339 static const std::array<vec2, 5> outsidePoints{
5340 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
5341 for (const auto pointOutsideWindow : outsidePoints) {
5342 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
5343 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005344 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5345 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5346 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005347
Prabir Pradhan678438e2023-04-13 19:32:51 +00005348 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5349 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5350 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005351 }
5352 window->assertNoEvents();
5353}
5354
5355// Run the precision tests for all rotations.
5356INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
5357 InputDispatcherDisplayOrientationFixture,
5358 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
5359 ui::ROTATION_270),
5360 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
5361 return ftl::enum_string(testParamInfo.param);
5362 });
5363
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005364using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
5365 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005366
5367class TransferTouchFixture : public InputDispatcherTest,
5368 public ::testing::WithParamInterface<TransferFunction> {};
5369
5370TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07005371 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005372
5373 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005374 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005375 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5376 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005377 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005378 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005379 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5380 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005381 sp<FakeWindowHandle> wallpaper =
5382 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
5383 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005384 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005385 mDispatcher->onWindowInfosChanged(
5386 {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005387
5388 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005389 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5390 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005391
Svet Ganov5d3bc372020-01-26 23:11:07 -08005392 // Only the first window should get the down event
5393 firstWindow->consumeMotionDown();
5394 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005395 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005396
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005397 // Transfer touch to the second window
5398 TransferFunction f = GetParam();
5399 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5400 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005401 // The first window gets cancel and the second gets down
5402 firstWindow->consumeMotionCancel();
5403 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005404 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005405
5406 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005407 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5408 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005409 // The first window gets no events and the second gets up
5410 firstWindow->assertNoEvents();
5411 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005412 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005413}
5414
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005415/**
5416 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
5417 * from. When we have spy windows, there are several windows to choose from: either spy, or the
5418 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
5419 * natural to the user.
5420 * In this test, we are sending a pointer to both spy window and first window. We then try to
5421 * transfer touch to the second window. The dispatcher should identify the first window as the
5422 * one that should lose the gesture, and therefore the action should be to move the gesture from
5423 * the first window to the second.
5424 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
5425 * the other API, as well.
5426 */
5427TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
5428 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5429
5430 // Create a couple of windows + a spy window
5431 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005432 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005433 spyWindow->setTrustedOverlay(true);
5434 spyWindow->setSpy(true);
5435 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005436 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005437 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005438 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005439
5440 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005441 mDispatcher->onWindowInfosChanged(
5442 {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005443
5444 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005445 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5446 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005447 // Only the first window and spy should get the down event
5448 spyWindow->consumeMotionDown();
5449 firstWindow->consumeMotionDown();
5450
5451 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
5452 // if f === 'transferTouch'.
5453 TransferFunction f = GetParam();
5454 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5455 ASSERT_TRUE(success);
5456 // The first window gets cancel and the second gets down
5457 firstWindow->consumeMotionCancel();
5458 secondWindow->consumeMotionDown();
5459
5460 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005461 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5462 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005463 // The first window gets no events and the second+spy get up
5464 firstWindow->assertNoEvents();
5465 spyWindow->consumeMotionUp();
5466 secondWindow->consumeMotionUp();
5467}
5468
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005469TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005470 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005471
5472 PointF touchPoint = {10, 10};
5473
5474 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005475 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005476 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5477 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005478 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005479 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005480 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5481 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005482 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005483
5484 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005485 mDispatcher->onWindowInfosChanged(
5486 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005487
5488 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005489 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5490 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5491 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005492 // Only the first window should get the down event
5493 firstWindow->consumeMotionDown();
5494 secondWindow->assertNoEvents();
5495
5496 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005497 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5498 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005499 // Only the first window should get the pointer down event
5500 firstWindow->consumeMotionPointerDown(1);
5501 secondWindow->assertNoEvents();
5502
5503 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005504 TransferFunction f = GetParam();
5505 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5506 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005507 // The first window gets cancel and the second gets down and pointer down
5508 firstWindow->consumeMotionCancel();
5509 secondWindow->consumeMotionDown();
5510 secondWindow->consumeMotionPointerDown(1);
5511
5512 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005513 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5514 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005515 // The first window gets nothing and the second gets pointer up
5516 firstWindow->assertNoEvents();
5517 secondWindow->consumeMotionPointerUp(1);
5518
5519 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005520 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5521 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005522 // The first window gets nothing and the second gets up
5523 firstWindow->assertNoEvents();
5524 secondWindow->consumeMotionUp();
5525}
5526
Arthur Hungc539dbb2022-12-08 07:45:36 +00005527TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
5528 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5529
5530 // Create a couple of windows
5531 sp<FakeWindowHandle> firstWindow =
5532 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5533 ADISPLAY_ID_DEFAULT);
5534 firstWindow->setDupTouchToWallpaper(true);
5535 sp<FakeWindowHandle> secondWindow =
5536 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5537 ADISPLAY_ID_DEFAULT);
5538 secondWindow->setDupTouchToWallpaper(true);
5539
5540 sp<FakeWindowHandle> wallpaper1 =
5541 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
5542 wallpaper1->setIsWallpaper(true);
5543
5544 sp<FakeWindowHandle> wallpaper2 =
5545 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
5546 wallpaper2->setIsWallpaper(true);
5547 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005548 mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(),
5549 *secondWindow->getInfo(), *wallpaper2->getInfo()},
5550 {},
5551 0,
5552 0});
Arthur Hungc539dbb2022-12-08 07:45:36 +00005553
5554 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005555 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5556 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005557
5558 // Only the first window should get the down event
5559 firstWindow->consumeMotionDown();
5560 secondWindow->assertNoEvents();
5561 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5562 wallpaper2->assertNoEvents();
5563
5564 // Transfer touch focus to the second window
5565 TransferFunction f = GetParam();
5566 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5567 ASSERT_TRUE(success);
5568
5569 // The first window gets cancel and the second gets down
5570 firstWindow->consumeMotionCancel();
5571 secondWindow->consumeMotionDown();
5572 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5573 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5574
5575 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005576 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5577 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005578 // The first window gets no events and the second gets up
5579 firstWindow->assertNoEvents();
5580 secondWindow->consumeMotionUp();
5581 wallpaper1->assertNoEvents();
5582 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5583}
5584
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005585// For the cases of single pointer touch and two pointers non-split touch, the api's
5586// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
5587// for the case where there are multiple pointers split across several windows.
5588INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
5589 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005590 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
5591 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005592 return dispatcher->transferTouch(destChannelToken,
5593 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005594 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005595 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
5596 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005597 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00005598 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005599 }));
5600
Svet Ganov5d3bc372020-01-26 23:11:07 -08005601TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005602 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005603
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005604 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005605 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5606 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005607 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005608
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005609 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005610 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5611 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005612 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005613
5614 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005615 mDispatcher->onWindowInfosChanged(
5616 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005617
5618 PointF pointInFirst = {300, 200};
5619 PointF pointInSecond = {300, 600};
5620
5621 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005622 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5623 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5624 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005625 // Only the first window should get the down event
5626 firstWindow->consumeMotionDown();
5627 secondWindow->assertNoEvents();
5628
5629 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005630 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5631 ADISPLAY_ID_DEFAULT,
5632 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005633 // The first window gets a move and the second a down
5634 firstWindow->consumeMotionMove();
5635 secondWindow->consumeMotionDown();
5636
5637 // Transfer touch focus to the second window
5638 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
5639 // The first window gets cancel and the new gets pointer down (it already saw down)
5640 firstWindow->consumeMotionCancel();
5641 secondWindow->consumeMotionPointerDown(1);
5642
5643 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005644 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5645 ADISPLAY_ID_DEFAULT,
5646 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005647 // The first window gets nothing and the second gets pointer up
5648 firstWindow->assertNoEvents();
5649 secondWindow->consumeMotionPointerUp(1);
5650
5651 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005652 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5653 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005654 // The first window gets nothing and the second gets up
5655 firstWindow->assertNoEvents();
5656 secondWindow->consumeMotionUp();
5657}
5658
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005659// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
5660// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
5661// touch is not supported, so the touch should continue on those windows and the transferred-to
5662// window should get nothing.
5663TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
5664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5665
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005666 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005667 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5668 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005669 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005670
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005671 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005672 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5673 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005674 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005675
5676 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005677 mDispatcher->onWindowInfosChanged(
5678 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005679
5680 PointF pointInFirst = {300, 200};
5681 PointF pointInSecond = {300, 600};
5682
5683 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005684 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5685 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5686 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005687 // Only the first window should get the down event
5688 firstWindow->consumeMotionDown();
5689 secondWindow->assertNoEvents();
5690
5691 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005692 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5693 ADISPLAY_ID_DEFAULT,
5694 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005695 // The first window gets a move and the second a down
5696 firstWindow->consumeMotionMove();
5697 secondWindow->consumeMotionDown();
5698
5699 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005700 const bool transferred =
5701 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005702 // The 'transferTouch' call should not succeed, because there are 2 touched windows
5703 ASSERT_FALSE(transferred);
5704 firstWindow->assertNoEvents();
5705 secondWindow->assertNoEvents();
5706
5707 // The rest of the dispatch should proceed as normal
5708 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005709 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5710 ADISPLAY_ID_DEFAULT,
5711 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005712 // The first window gets MOVE and the second gets pointer up
5713 firstWindow->consumeMotionMove();
5714 secondWindow->consumeMotionUp();
5715
5716 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005717 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5718 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005719 // The first window gets nothing and the second gets up
5720 firstWindow->consumeMotionUp();
5721 secondWindow->assertNoEvents();
5722}
5723
Arthur Hungabbb9d82021-09-01 14:52:30 +00005724// This case will create two windows and one mirrored window on the default display and mirror
5725// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
5726// the windows info of second display before default display.
5727TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
5728 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5729 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005730 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005731 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005732 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005733 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005734 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005735
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005736 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005737 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005738
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005739 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005740 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005741
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005742 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005743 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005744
5745 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005746 mDispatcher->onWindowInfosChanged(
5747 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5748 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5749 *secondWindowInPrimary->getInfo()},
5750 {},
5751 0,
5752 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005753
5754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005755 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005756 {50, 50}))
5757 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5758
5759 // Window should receive motion event.
5760 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5761
5762 // Transfer touch focus
5763 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
5764 secondWindowInPrimary->getToken()));
5765 // The first window gets cancel.
5766 firstWindowInPrimary->consumeMotionCancel();
5767 secondWindowInPrimary->consumeMotionDown();
5768
5769 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005770 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005771 ADISPLAY_ID_DEFAULT, {150, 50}))
5772 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5773 firstWindowInPrimary->assertNoEvents();
5774 secondWindowInPrimary->consumeMotionMove();
5775
5776 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005777 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005778 {150, 50}))
5779 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5780 firstWindowInPrimary->assertNoEvents();
5781 secondWindowInPrimary->consumeMotionUp();
5782}
5783
5784// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
5785// 'transferTouch' api.
5786TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
5787 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5788 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005789 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005790 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005791 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005792 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005793 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005794
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005795 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005796 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005797
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005798 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005799 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005800
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005801 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005802 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005803
5804 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005805 mDispatcher->onWindowInfosChanged(
5806 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5807 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5808 *secondWindowInPrimary->getInfo()},
5809 {},
5810 0,
5811 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005812
5813 // Touch on second display.
5814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005815 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
5816 {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005817 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5818
5819 // Window should receive motion event.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005820 firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005821
5822 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005823 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005824
5825 // The first window gets cancel.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005826 firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5827 secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005828
5829 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005830 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005831 SECOND_DISPLAY_ID, {150, 50}))
5832 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005833 firstWindowInSecondary->assertNoEvents();
5834 secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005835
5836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005837 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005838 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005839 firstWindowInSecondary->assertNoEvents();
5840 secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005841}
5842
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005843TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005845 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5846 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005847
Vishnu Nair47074b82020-08-14 11:54:47 -07005848 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005849 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005850 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005851
5852 window->consumeFocusEvent(true);
5853
Prabir Pradhan678438e2023-04-13 19:32:51 +00005854 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005855
5856 // Window should receive key down event.
5857 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00005858
5859 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005860 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005861 mFakePolicy->assertUserActivityPoked();
5862}
5863
5864TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
5865 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5866 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5867 "Fake Window", ADISPLAY_ID_DEFAULT);
5868
5869 window->setDisableUserActivity(true);
5870 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005871 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005872 setFocusedWindow(window);
5873
5874 window->consumeFocusEvent(true);
5875
5876 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5877
5878 // Window should receive key down event.
5879 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5880
5881 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005882 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005883 mFakePolicy->assertUserActivityNotPoked();
5884}
5885
5886TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
5887 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5888 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5889 "Fake Window", ADISPLAY_ID_DEFAULT);
5890
5891 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005892 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005893 setFocusedWindow(window);
5894
5895 window->consumeFocusEvent(true);
5896
5897 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5898 mDispatcher->waitForIdle();
5899
5900 // System key is not passed down
5901 window->assertNoEvents();
5902
5903 // Should have poked user activity
5904 mFakePolicy->assertUserActivityPoked();
5905}
5906
5907TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
5908 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5909 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5910 "Fake Window", ADISPLAY_ID_DEFAULT);
5911
5912 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005913 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005914 setFocusedWindow(window);
5915
5916 window->consumeFocusEvent(true);
5917
5918 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5919 mDispatcher->waitForIdle();
5920
5921 // System key is not passed down
5922 window->assertNoEvents();
5923
5924 // Should have poked user activity
5925 mFakePolicy->assertUserActivityPoked();
5926}
5927
5928TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
5929 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5930 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5931 "Fake Window", ADISPLAY_ID_DEFAULT);
5932
5933 window->setDisableUserActivity(true);
5934 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005935 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005936 setFocusedWindow(window);
5937
5938 window->consumeFocusEvent(true);
5939
5940 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5941 mDispatcher->waitForIdle();
5942
5943 // System key is not passed down
5944 window->assertNoEvents();
5945
5946 // Should have poked user activity
5947 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005948}
5949
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005950TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
5951 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5952 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5953 "Fake Window", ADISPLAY_ID_DEFAULT);
5954
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005955 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005956
5957 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005958 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005959 ADISPLAY_ID_DEFAULT, {100, 100}))
5960 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5961
5962 window->consumeMotionEvent(
5963 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
5964
5965 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005966 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005967 mFakePolicy->assertUserActivityPoked();
5968}
5969
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005970TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005971 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005972 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5973 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005974
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005975 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005976
Prabir Pradhan678438e2023-04-13 19:32:51 +00005977 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005978 mDispatcher->waitForIdle();
5979
5980 window->assertNoEvents();
5981}
5982
5983// If a window is touchable, but does not have focus, it should receive motion events, but not keys
5984TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07005985 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005986 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5987 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005988
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005989 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005990
5991 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00005992 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005993 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00005994 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5995 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005996
5997 // Window should receive only the motion event
5998 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5999 window->assertNoEvents(); // Key event or focus event will not be received
6000}
6001
arthurhungea3f4fc2020-12-21 23:18:53 +08006002TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
6003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6004
arthurhungea3f4fc2020-12-21 23:18:53 +08006005 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006006 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
6007 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006008 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08006009
arthurhungea3f4fc2020-12-21 23:18:53 +08006010 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006011 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
6012 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006013 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08006014
6015 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006016 mDispatcher->onWindowInfosChanged(
6017 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
arthurhungea3f4fc2020-12-21 23:18:53 +08006018
6019 PointF pointInFirst = {300, 200};
6020 PointF pointInSecond = {300, 600};
6021
6022 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006023 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6024 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6025 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006026 // Only the first window should get the down event
6027 firstWindow->consumeMotionDown();
6028 secondWindow->assertNoEvents();
6029
6030 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006031 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6032 ADISPLAY_ID_DEFAULT,
6033 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006034 // The first window gets a move and the second a down
6035 firstWindow->consumeMotionMove();
6036 secondWindow->consumeMotionDown();
6037
6038 // Send pointer cancel to the second window
6039 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006040 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08006041 {pointInFirst, pointInSecond});
6042 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00006043 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08006044 // The first window gets move and the second gets cancel.
6045 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6046 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6047
6048 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006049 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6050 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08006051 // The first window gets up and the second gets nothing.
6052 firstWindow->consumeMotionUp();
6053 secondWindow->assertNoEvents();
6054}
6055
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006056TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
6057 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6058
6059 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006060 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006061 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006062 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
6063 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
6064 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
6065
Harry Cutts33476232023-01-30 19:57:29 +00006066 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006067 window->assertNoEvents();
6068 mDispatcher->waitForIdle();
6069}
6070
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006071using InputDispatcherMonitorTest = InputDispatcherTest;
6072
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006073/**
6074 * Two entities that receive touch: A window, and a global monitor.
6075 * The touch goes to the window, and then the window disappears.
6076 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
6077 * for the monitor, as well.
6078 * 1. foregroundWindow
6079 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
6080 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006081TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006082 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6083 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006084 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006085
Prabir Pradhanfb549072023-10-05 19:17:36 +00006086 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006087
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006088 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006089 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006090 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006091 {100, 200}))
6092 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6093
6094 // Both the foreground window and the global monitor should receive the touch down
6095 window->consumeMotionDown();
6096 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6097
6098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006099 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006100 ADISPLAY_ID_DEFAULT, {110, 200}))
6101 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6102
6103 window->consumeMotionMove();
6104 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
6105
6106 // Now the foreground window goes away
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006107 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006108 window->consumeMotionCancel();
6109 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
6110
6111 // If more events come in, there will be no more foreground window to send them to. This will
6112 // cause a cancel for the monitor, as well.
6113 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006114 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006115 ADISPLAY_ID_DEFAULT, {120, 200}))
6116 << "Injection should fail because the window was removed";
6117 window->assertNoEvents();
6118 // Global monitor now gets the cancel
6119 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6120}
6121
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006122TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07006123 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006124 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6125 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006126 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006127
Prabir Pradhanfb549072023-10-05 19:17:36 +00006128 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006129
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006130 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006131 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006132 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00006133 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08006134 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006135}
6136
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006137TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
Prabir Pradhanfb549072023-10-05 19:17:36 +00006138 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006139
Chris Yea209fde2020-07-22 13:54:51 -07006140 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006141 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6142 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006143 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006144
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006145 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006146 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006147 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08006148 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006149 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006150
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006151 // Pilfer pointers from the monitor.
6152 // This should not do anything and the window should continue to receive events.
6153 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00006154
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006155 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006156 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006157 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006158 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006159
6160 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
6161 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006162}
6163
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006164TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07006165 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006166 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6167 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006168 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Evan Rosky84f07f02021-04-16 10:42:42 -07006169 window->setWindowOffset(20, 40);
6170 window->setWindowTransform(0, 1, -1, 0);
6171
Prabir Pradhanfb549072023-10-05 19:17:36 +00006172 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07006173
6174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006175 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Evan Rosky84f07f02021-04-16 10:42:42 -07006176 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6177 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006178 std::unique_ptr<MotionEvent> event = monitor.consumeMotion();
6179 ASSERT_NE(nullptr, event);
Evan Rosky84f07f02021-04-16 10:42:42 -07006180 // Even though window has transform, gesture monitor must not.
6181 ASSERT_EQ(ui::Transform(), event->getTransform());
6182}
6183
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006184TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00006185 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhanfb549072023-10-05 19:17:36 +00006186 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00006187
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006188 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006189 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006190 << "Injection should fail if there is a monitor, but no touchable window";
6191 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00006192}
6193
Linnan Lid8150952024-01-26 18:07:17 +00006194/**
6195 * Two displays
6196 * The first monitor has a foreground window, a monitor
6197 * The second window has only one monitor.
6198 * We first inject a Down event into the first display, this injection should succeed and both
6199 * the foreground window and monitor should receive a down event, then inject a Down event into
6200 * the second display as well, this injection should fail, at this point, the first display
6201 * window and monitor should not receive a cancel or any other event.
6202 * Continue to inject Move and UP events to the first display, the events should be received
6203 * normally by the foreground window and monitor.
6204 */
6205TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) {
6206 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6207 sp<FakeWindowHandle> window =
6208 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6209
6210 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6211 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6212
6213 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6215 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6216 {100, 200}))
6217 << "The down event injected into the first display should succeed";
6218
6219 window->consumeMotionDown();
6220 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006221
6222 ASSERT_EQ(InputEventInjectionResult::FAILED,
6223 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6224 {100, 200}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006225 << "The down event injected into the second display should fail since there's no "
6226 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006227
6228 // Continue to inject event to first display.
6229 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6230 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6231 ADISPLAY_ID_DEFAULT, {110, 220}))
6232 << "The move event injected into the first display should succeed";
6233
6234 window->consumeMotionMove();
6235 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006236
6237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6238 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6239 {110, 220}))
6240 << "The up event injected into the first display should succeed";
6241
6242 window->consumeMotionUp();
6243 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006244
6245 window->assertNoEvents();
6246 monitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00006247 secondMonitor.assertNoEvents();
6248}
6249
6250/**
6251 * Two displays
6252 * There is a monitor and foreground window on each display.
6253 * First, we inject down events into each of the two displays, at this point, the foreground windows
6254 * and monitors on both displays should receive down events.
6255 * At this point, the foreground window of the second display goes away, the gone window should
6256 * receive the cancel event, and the other windows and monitors should not receive any events.
6257 * Inject a move event into the second display. At this point, the injection should fail because
6258 * the second display no longer has a foreground window. At this point, the monitor on the second
6259 * display should receive a cancel event, and any windows or monitors on the first display should
6260 * not receive any events, and any subsequent injection of events into the second display should
6261 * also fail.
6262 * Continue to inject events into the first display, and the events should all be injected
6263 * successfully and received normally.
6264 */
6265TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) {
6266 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6267 sp<FakeWindowHandle> window =
6268 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6269 sp<FakeWindowHandle> secondWindow =
6270 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground",
6271 SECOND_DISPLAY_ID);
6272
6273 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6274 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6275
6276 // There is a foreground window on both displays.
6277 mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
6278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6279 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6280 {100, 200}))
6281 << "The down event injected into the first display should succeed";
6282
6283 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6284 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006285
6286 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6287 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6288 {100, 200}))
6289 << "The down event injected into the second display should succeed";
6290
Linnan Lid8150952024-01-26 18:07:17 +00006291 secondWindow->consumeMotionDown(SECOND_DISPLAY_ID);
6292 secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID);
6293
6294 // Now second window is gone away.
6295 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6296
6297 // The gone window should receive a cancel, and the monitor on the second display should not
6298 // receive any events.
Linnan Lid8150952024-01-26 18:07:17 +00006299 secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID);
6300 secondMonitor.assertNoEvents();
6301
6302 ASSERT_EQ(InputEventInjectionResult::FAILED,
6303 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6304 SECOND_DISPLAY_ID, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006305 << "The move event injected into the second display should fail because there's no "
6306 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006307 // Now the monitor on the second display should receive a cancel event.
6308 secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID);
Linnan Lid8150952024-01-26 18:07:17 +00006309
6310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6311 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6312 ADISPLAY_ID_DEFAULT, {110, 200}))
6313 << "The move event injected into the first display should succeed";
6314
6315 window->consumeMotionMove();
6316 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006317
6318 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006319 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6320 {110, 220}))
6321 << "The up event injected into the second display should fail because there's no "
6322 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006323
6324 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6325 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6326 {110, 220}))
6327 << "The up event injected into the first display should succeed";
6328
6329 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6330 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006331
Linnan Lid8150952024-01-26 18:07:17 +00006332 window->assertNoEvents();
6333 monitor.assertNoEvents();
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006334 secondWindow->assertNoEvents();
6335 secondMonitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00006336}
6337
6338/**
6339 * One display with transform
6340 * There is a foreground window and a monitor on the display
6341 * Inject down event and move event sequentially, the foreground window and monitor can receive down
6342 * event and move event, then let the foreground window go away, the foreground window receives
6343 * cancel event, inject move event again, the monitor receives cancel event, all the events received
6344 * by the monitor should be with the same transform as the display
6345 */
6346TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) {
6347 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6348 sp<FakeWindowHandle> window =
6349 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6350 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6351
6352 ui::Transform transform;
6353 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6354
6355 gui::DisplayInfo displayInfo;
6356 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6357 displayInfo.transform = transform;
6358
6359 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
6360
6361 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6362 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6363 {100, 200}))
6364 << "The down event injected should succeed";
6365
6366 window->consumeMotionDown();
6367 std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion();
6368 EXPECT_EQ(transform, downMotionEvent->getTransform());
6369 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction());
6370
6371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6372 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6373 ADISPLAY_ID_DEFAULT, {110, 220}))
6374 << "The move event injected should succeed";
6375
6376 window->consumeMotionMove();
6377 std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion();
6378 EXPECT_EQ(transform, moveMotionEvent->getTransform());
6379 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction());
6380
6381 // Let foreground window gone
6382 mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0});
6383
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006384 // Foreground window should receive a cancel event, but not the monitor.
Linnan Lid8150952024-01-26 18:07:17 +00006385 window->consumeMotionCancel();
Linnan Lid8150952024-01-26 18:07:17 +00006386
6387 ASSERT_EQ(InputEventInjectionResult::FAILED,
6388 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6389 ADISPLAY_ID_DEFAULT, {110, 220}))
6390 << "The move event injected should failed";
6391 // Now foreground should not receive any events, but monitor should receive a cancel event
6392 // with transform that same as display's display.
Linnan Lid8150952024-01-26 18:07:17 +00006393 std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion();
6394 EXPECT_EQ(transform, cancelMotionEvent->getTransform());
6395 EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId());
6396 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction());
6397
6398 // Other event inject to this display should fail.
6399 ASSERT_EQ(InputEventInjectionResult::FAILED,
6400 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6401 ADISPLAY_ID_DEFAULT, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006402 << "The up event injected should fail because the touched window was removed";
Linnan Lid8150952024-01-26 18:07:17 +00006403 window->assertNoEvents();
6404 monitor.assertNoEvents();
6405}
6406
chaviw81e2bb92019-12-18 15:03:51 -08006407TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006408 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006409 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6410 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08006411
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006412 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
chaviw81e2bb92019-12-18 15:03:51 -08006413
6414 NotifyMotionArgs motionArgs =
6415 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6416 ADISPLAY_ID_DEFAULT);
6417
Prabir Pradhan678438e2023-04-13 19:32:51 +00006418 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08006419 // Window should receive motion down event.
6420 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6421
6422 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08006423 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08006424 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6425 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
6426 motionArgs.pointerCoords[0].getX() - 10);
6427
Prabir Pradhan678438e2023-04-13 19:32:51 +00006428 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00006429 window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08006430}
6431
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006432/**
6433 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
6434 * the device default right away. In the test scenario, we check both the default value,
6435 * and the action of enabling / disabling.
6436 */
6437TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07006438 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006439 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6440 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08006441 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006442
6443 // Set focused application.
6444 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006445 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006446
6447 SCOPED_TRACE("Check default value of touch mode");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006448 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006449 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006450 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006451
6452 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07006453 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006454 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00006455 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006456
6457 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08006458 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00006459 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006460 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07006461 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006462 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006463 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006464 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006465
6466 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07006467 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006468 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00006469 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006470
6471 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08006472 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00006473 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006474 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07006475 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006476 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006477 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006478 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006479
6480 window->assertNoEvents();
6481}
6482
Gang Wange9087892020-01-07 12:17:14 -05006483TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006484 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006485 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6486 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05006487
6488 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006489 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05006490
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006491 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006492 setFocusedWindow(window);
6493
Harry Cutts33476232023-01-30 19:57:29 +00006494 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05006495
Prabir Pradhan678438e2023-04-13 19:32:51 +00006496 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
6497 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05006498
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006499 std::unique_ptr<KeyEvent> event = window->consumeKey();
6500 ASSERT_NE(event, nullptr);
6501 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Gang Wange9087892020-01-07 12:17:14 -05006502 ASSERT_NE(verified, nullptr);
6503 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
6504
6505 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
6506 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
6507 ASSERT_EQ(keyArgs.source, verified->source);
6508 ASSERT_EQ(keyArgs.displayId, verified->displayId);
6509
6510 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
6511
6512 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05006513 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006514 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05006515 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
6516 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
6517 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
6518 ASSERT_EQ(0, verifiedKey.repeatCount);
6519}
6520
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006521TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006522 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006523 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6524 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006525
6526 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6527
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006528 ui::Transform transform;
6529 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6530
6531 gui::DisplayInfo displayInfo;
6532 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6533 displayInfo.transform = transform;
6534
Patrick Williamsd828f302023-04-28 17:52:08 -05006535 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006536
Prabir Pradhan678438e2023-04-13 19:32:51 +00006537 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006538 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6539 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006540 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006541
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006542 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
6543 ASSERT_NE(nullptr, event);
6544 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006545 ASSERT_NE(verified, nullptr);
6546 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
6547
6548 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
6549 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
6550 EXPECT_EQ(motionArgs.source, verified->source);
6551 EXPECT_EQ(motionArgs.displayId, verified->displayId);
6552
6553 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
6554
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006555 const vec2 rawXY =
6556 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
6557 motionArgs.pointerCoords[0].getXYValue());
6558 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
6559 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006560 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006561 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006562 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006563 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
6564 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
6565}
6566
chaviw09c8d2d2020-08-24 15:48:26 -07006567/**
6568 * Ensure that separate calls to sign the same data are generating the same key.
6569 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
6570 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
6571 * tests.
6572 */
6573TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
6574 KeyEvent event = getTestKeyEvent();
6575 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6576
6577 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
6578 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
6579 ASSERT_EQ(hmac1, hmac2);
6580}
6581
6582/**
6583 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
6584 */
6585TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
6586 KeyEvent event = getTestKeyEvent();
6587 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6588 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
6589
6590 verifiedEvent.deviceId += 1;
6591 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6592
6593 verifiedEvent.source += 1;
6594 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6595
6596 verifiedEvent.eventTimeNanos += 1;
6597 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6598
6599 verifiedEvent.displayId += 1;
6600 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6601
6602 verifiedEvent.action += 1;
6603 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6604
6605 verifiedEvent.downTimeNanos += 1;
6606 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6607
6608 verifiedEvent.flags += 1;
6609 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6610
6611 verifiedEvent.keyCode += 1;
6612 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6613
6614 verifiedEvent.scanCode += 1;
6615 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6616
6617 verifiedEvent.metaState += 1;
6618 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6619
6620 verifiedEvent.repeatCount += 1;
6621 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6622}
6623
Vishnu Nair958da932020-08-21 17:12:37 -07006624TEST_F(InputDispatcherTest, SetFocusedWindow) {
6625 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6626 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006627 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006628 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006629 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006630 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6631
6632 // Top window is also focusable but is not granted focus.
6633 windowTop->setFocusable(true);
6634 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006635 mDispatcher->onWindowInfosChanged(
6636 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006637 setFocusedWindow(windowSecond);
6638
6639 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006641 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006642
6643 // Focused window should receive event.
6644 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6645 windowTop->assertNoEvents();
6646}
6647
6648TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
6649 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6650 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006651 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006652 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6653
6654 window->setFocusable(true);
6655 // Release channel for window is no longer valid.
6656 window->releaseChannel();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006657 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006658 setFocusedWindow(window);
6659
6660 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006661 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006662
6663 // window channel is invalid, so it should not receive any input event.
6664 window->assertNoEvents();
6665}
6666
6667TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
6668 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6669 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006670 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006671 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07006672 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6673
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006674 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006675 setFocusedWindow(window);
6676
6677 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006678 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006679
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006680 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07006681 window->assertNoEvents();
6682}
6683
6684TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
6685 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6686 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006687 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006688 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006689 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006690 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6691
6692 windowTop->setFocusable(true);
6693 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006694 mDispatcher->onWindowInfosChanged(
6695 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006696 setFocusedWindow(windowTop);
6697 windowTop->consumeFocusEvent(true);
6698
Chavi Weingarten847e8512023-03-29 00:26:09 +00006699 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006700 mDispatcher->onWindowInfosChanged(
6701 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006702 windowSecond->consumeFocusEvent(true);
6703 windowTop->consumeFocusEvent(false);
6704
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006705 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006706 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006707
6708 // Focused window should receive event.
6709 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6710}
6711
Chavi Weingarten847e8512023-03-29 00:26:09 +00006712TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07006713 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6714 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006715 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006716 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006717 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006718 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6719
6720 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00006721 windowSecond->setFocusable(false);
6722 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006723 mDispatcher->onWindowInfosChanged(
6724 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Chavi Weingarten847e8512023-03-29 00:26:09 +00006725 setFocusedWindow(windowTop);
6726 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07006727
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006728 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Chavi Weingarten847e8512023-03-29 00:26:09 +00006729 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006730
6731 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00006732 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07006733 windowSecond->assertNoEvents();
6734}
6735
6736TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
6737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6738 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006739 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006740 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006741 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
6742 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006743 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6744
6745 window->setFocusable(true);
6746 previousFocusedWindow->setFocusable(true);
6747 window->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006748 mDispatcher->onWindowInfosChanged(
6749 {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006750 setFocusedWindow(previousFocusedWindow);
6751 previousFocusedWindow->consumeFocusEvent(true);
6752
6753 // Requesting focus on invisible window takes focus from currently focused window.
6754 setFocusedWindow(window);
6755 previousFocusedWindow->consumeFocusEvent(false);
6756
6757 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006759 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
6760 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006761
6762 // Window does not get focus event or key down.
6763 window->assertNoEvents();
6764
6765 // Window becomes visible.
6766 window->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006767 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006768
6769 // Window receives focus event.
6770 window->consumeFocusEvent(true);
6771 // Focused window receives key down.
6772 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6773}
6774
Vishnu Nair599f1412021-06-21 10:39:58 -07006775TEST_F(InputDispatcherTest, DisplayRemoved) {
6776 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6777 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006778 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07006779 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6780
6781 // window is granted focus.
6782 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006783 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair599f1412021-06-21 10:39:58 -07006784 setFocusedWindow(window);
6785 window->consumeFocusEvent(true);
6786
6787 // When a display is removed window loses focus.
6788 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
6789 window->consumeFocusEvent(false);
6790}
6791
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006792/**
6793 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
6794 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
6795 * of the 'slipperyEnterWindow'.
6796 *
6797 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
6798 * a way so that the touched location is no longer covered by the top window.
6799 *
6800 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
6801 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
6802 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
6803 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
6804 * with ACTION_DOWN).
6805 * Thus, the touch has been transferred from the top window into the bottom window, because the top
6806 * window moved itself away from the touched location and had Flag::SLIPPERY.
6807 *
6808 * Even though the top window moved away from the touched location, it is still obscuring the bottom
6809 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
6810 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
6811 *
6812 * In this test, we ensure that the event received by the bottom window has
6813 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
6814 */
6815TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006816 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006817 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006818
6819 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6820 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6821
6822 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006823 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006824 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006825 // Make sure this one overlaps the bottom window
6826 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
6827 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
6828 // one. Windows with the same owner are not considered to be occluding each other.
6829 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
6830
6831 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006832 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006833 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
6834
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006835 mDispatcher->onWindowInfosChanged(
6836 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006837
6838 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00006839 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6840 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6841 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006842 slipperyExitWindow->consumeMotionDown();
6843 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006844 mDispatcher->onWindowInfosChanged(
6845 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006846
Prabir Pradhan678438e2023-04-13 19:32:51 +00006847 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
6848 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6849 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006850
6851 slipperyExitWindow->consumeMotionCancel();
6852
6853 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
6854 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6855}
6856
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006857/**
6858 * Two windows, one on the left and another on the right. The left window is slippery. The right
6859 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
6860 * touch moves from the left window into the right window, the gesture should continue to go to the
6861 * left window. Touch shouldn't slip because the right window can't receive touches. This test
6862 * reproduces a crash.
6863 */
6864TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
6865 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6866
6867 sp<FakeWindowHandle> leftSlipperyWindow =
6868 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6869 leftSlipperyWindow->setSlippery(true);
6870 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
6871
6872 sp<FakeWindowHandle> rightDropTouchesWindow =
6873 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6874 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
6875 rightDropTouchesWindow->setDropInput(true);
6876
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006877 mDispatcher->onWindowInfosChanged(
6878 {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006879
6880 // Start touch in the left window
6881 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6882 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6883 .build());
6884 leftSlipperyWindow->consumeMotionDown();
6885
6886 // And move it into the right window
6887 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6888 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
6889 .build());
6890
6891 // Since the right window isn't eligible to receive input, touch does not slip.
6892 // The left window continues to receive the gesture.
6893 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6894 rightDropTouchesWindow->assertNoEvents();
6895}
6896
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07006897/**
6898 * A single window is on screen first. Touch is injected into that window. Next, a second window
6899 * appears. Since the first window is slippery, touch will move from the first window to the second.
6900 */
6901TEST_F(InputDispatcherTest, InjectedTouchSlips) {
6902 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6903 sp<FakeWindowHandle> originalWindow =
6904 sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT);
6905 originalWindow->setFrame(Rect(0, 0, 200, 200));
6906 originalWindow->setSlippery(true);
6907
6908 sp<FakeWindowHandle> appearingWindow =
6909 sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT);
6910 appearingWindow->setFrame(Rect(0, 0, 200, 200));
6911
6912 mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0});
6913
6914 // Touch down on the original window
6915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6916 injectMotionEvent(*mDispatcher,
6917 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6918 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
6919 .build()));
6920 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6921
6922 // Now, a new window appears. This could be, for example, a notification shade that appears
6923 // after user starts to drag down on the launcher window.
6924 mDispatcher->onWindowInfosChanged(
6925 {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0});
6926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6927 injectMotionEvent(*mDispatcher,
6928 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6929 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110))
6930 .build()));
6931 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
6932 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6934 injectMotionEvent(*mDispatcher,
6935 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6936 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
6937 .build()));
6938 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6939
6940 originalWindow->assertNoEvents();
6941 appearingWindow->assertNoEvents();
6942}
6943
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006944TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006945 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006946 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6947
6948 sp<FakeWindowHandle> leftWindow =
6949 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6950 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006951 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006952
6953 sp<FakeWindowHandle> rightSpy =
6954 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
6955 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006956 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006957 rightSpy->setSpy(true);
6958 rightSpy->setTrustedOverlay(true);
6959
6960 sp<FakeWindowHandle> rightWindow =
6961 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6962 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006963 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006964
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006965 mDispatcher->onWindowInfosChanged(
6966 {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006967
6968 // Touch in the left window
6969 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6970 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6971 .build());
6972 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
6973 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006974 ASSERT_NO_FATAL_FAILURE(
6975 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006976
6977 // Touch another finger over the right windows
6978 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6979 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6980 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6981 .build());
6982 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
6983 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
6984 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
6985 mDispatcher->waitForIdle();
6986 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006987 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
6988 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006989
6990 // Release finger over left window. The UP actions are not treated as device interaction.
6991 // The windows that did not receive the UP pointer will receive MOVE events, but since this
6992 // is part of the UP action, we do not treat this as device interaction.
6993 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
6994 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6995 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6996 .build());
6997 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
6998 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
6999 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7000 mDispatcher->waitForIdle();
7001 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7002
7003 // Move remaining finger
7004 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7005 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7006 .build());
7007 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
7008 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7009 mDispatcher->waitForIdle();
7010 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007011 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007012
7013 // Release all fingers
7014 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7015 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7016 .build());
7017 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
7018 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
7019 mDispatcher->waitForIdle();
7020 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7021}
7022
7023TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
7024 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7025
7026 sp<FakeWindowHandle> window =
7027 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
7028 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007029 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007030
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007031 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007032 setFocusedWindow(window);
7033 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
7034
7035 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
7036 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
7037 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007038 ASSERT_NO_FATAL_FAILURE(
7039 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007040
7041 // The UP actions are not treated as device interaction.
7042 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
7043 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
7044 mDispatcher->waitForIdle();
7045 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7046}
7047
Prabir Pradhan5893d362023-11-17 04:30:40 +00007048TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) {
7049 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7050
7051 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
7052 ADISPLAY_ID_DEFAULT);
7053 left->setFrame(Rect(0, 0, 100, 100));
7054 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
7055 "Right Window", ADISPLAY_ID_DEFAULT);
7056 right->setFrame(Rect(100, 0, 200, 100));
7057 sp<FakeWindowHandle> spy =
7058 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
7059 spy->setFrame(Rect(0, 0, 200, 100));
7060 spy->setTrustedOverlay(true);
7061 spy->setSpy(true);
7062
7063 mDispatcher->onWindowInfosChanged(
7064 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
7065
7066 // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId.
7067 NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
7068 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
7069 mDispatcher->notifyMotion(notifyArgs);
7070
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007071 std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00007072 AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)),
7073 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007074 ASSERT_NE(nullptr, leftEnter);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007075 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
7076 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007077 Not(WithEventId(leftEnter->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00007078 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
7079
7080 // Send move to the right window, and ensure hover exit and enter are synthesized with new ids.
7081 notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
7082 {PointF{150, 50}});
7083 mDispatcher->notifyMotion(notifyArgs);
7084
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007085 std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00007086 AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)),
7087 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007088 ASSERT_NE(nullptr, leftExit);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007089 right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
7090 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007091 Not(WithEventId(leftExit->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00007092 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
7093
7094 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id)));
7095}
7096
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00007097class InputDispatcherFallbackKeyTest : public InputDispatcherTest {
7098protected:
7099 std::shared_ptr<FakeApplicationHandle> mApp;
7100 sp<FakeWindowHandle> mWindow;
7101
7102 virtual void SetUp() override {
7103 InputDispatcherTest::SetUp();
7104
7105 mApp = std::make_shared<FakeApplicationHandle>();
7106
7107 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
7108 mWindow->setFrame(Rect(0, 0, 100, 100));
7109
7110 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
7111 setFocusedWindow(mWindow);
7112 ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true));
7113 }
7114
7115 void setFallback(int32_t keycode) {
7116 mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) {
7117 return KeyEventBuilder(event).keyCode(keycode).build();
7118 });
7119 }
7120
7121 void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007122 std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled);
7123 ASSERT_NE(nullptr, event);
7124 ASSERT_THAT(*event, matcher);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00007125 }
7126};
7127
7128TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) {
7129 mDispatcher->notifyKey(
7130 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7131 consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
7132 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7133}
7134
7135TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) {
7136 mDispatcher->notifyKey(
7137 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7138 consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
7139 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7140}
7141
7142TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) {
7143 mDispatcher->notifyKey(
7144 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7145
7146 // Do not handle this key event.
7147 consumeKey(/*handled=*/false,
7148 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7149 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7150
7151 // Since the policy did not request any fallback to be generated, ensure there are no events.
7152 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7153}
7154
7155TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) {
7156 setFallback(AKEYCODE_B);
7157 mDispatcher->notifyKey(
7158 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7159
7160 // Do not handle this key event.
7161 consumeKey(/*handled=*/false,
7162 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7163
7164 // Since the key was not handled, ensure the fallback event was dispatched instead.
7165 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7166 consumeKey(/*handled=*/true,
7167 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7168 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7169
7170 // Release the original key, and ensure the fallback key is also released.
7171 mDispatcher->notifyKey(
7172 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7173 consumeKey(/*handled=*/false,
7174 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7175 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7176 consumeKey(/*handled=*/true,
7177 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7178 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7179
7180 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7181 mWindow->assertNoEvents();
7182}
7183
7184TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) {
7185 setFallback(AKEYCODE_B);
7186 mDispatcher->notifyKey(
7187 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7188
7189 // Do not handle this key event, but handle the fallback.
7190 consumeKey(/*handled=*/false,
7191 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7192 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7193 consumeKey(/*handled=*/true,
7194 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7195 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7196
7197 // Release the original key, and ensure the fallback key is also released.
7198 mDispatcher->notifyKey(
7199 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7200 // But this time, the app handles the original key.
7201 consumeKey(/*handled=*/true,
7202 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7203 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7204 // Ensure the fallback key is canceled.
7205 consumeKey(/*handled=*/true,
7206 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7207 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7208
7209 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7210 mWindow->assertNoEvents();
7211}
7212
7213TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) {
7214 setFallback(AKEYCODE_B);
7215 mDispatcher->notifyKey(
7216 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7217
7218 // Do not handle this key event.
7219 consumeKey(/*handled=*/false,
7220 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7221 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7222 // App does not handle the fallback either, so ensure another fallback is not generated.
7223 setFallback(AKEYCODE_C);
7224 consumeKey(/*handled=*/false,
7225 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7226 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7227
7228 // Release the original key, and ensure the fallback key is also released.
7229 setFallback(AKEYCODE_B);
7230 mDispatcher->notifyKey(
7231 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7232 consumeKey(/*handled=*/false,
7233 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7234 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7235 consumeKey(/*handled=*/false,
7236 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7237 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7238
7239 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7240 mWindow->assertNoEvents();
7241}
7242
7243TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) {
7244 setFallback(AKEYCODE_B);
7245 mDispatcher->notifyKey(
7246 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7247
7248 // Do not handle this key event, so fallback is generated.
7249 consumeKey(/*handled=*/false,
7250 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7251 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7252 consumeKey(/*handled=*/true,
7253 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7254 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7255
7256 // Release the original key, but assume the policy is misbehaving and it
7257 // generates an inconsistent fallback to the one from the DOWN event.
7258 setFallback(AKEYCODE_C);
7259 mDispatcher->notifyKey(
7260 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7261 consumeKey(/*handled=*/false,
7262 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7263 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7264 // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency.
7265 consumeKey(/*handled=*/true,
7266 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7267 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7268
7269 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7270 mWindow->assertNoEvents();
7271}
7272
7273TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) {
7274 setFallback(AKEYCODE_B);
7275 mDispatcher->notifyKey(
7276 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7277
7278 // Do not handle this key event, so fallback is generated.
7279 consumeKey(/*handled=*/false,
7280 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7281 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7282 consumeKey(/*handled=*/true,
7283 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7284 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7285
7286 // The original key is canceled.
7287 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD)
7288 .keyCode(AKEYCODE_A)
7289 .addFlag(AKEY_EVENT_FLAG_CANCELED)
7290 .build());
7291 consumeKey(/*handled=*/false,
7292 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
7293 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7294 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7295 // Ensure the fallback key is also canceled due to the original key being canceled.
7296 consumeKey(/*handled=*/true,
7297 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7298 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7299
7300 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7301 mWindow->assertNoEvents();
7302}
7303
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00007304TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) {
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00007305 setFallback(AKEYCODE_B);
7306 mDispatcher->notifyKey(
7307 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7308
7309 // Do not handle this key event.
7310 consumeKey(/*handled=*/false,
7311 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7312 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7313 consumeKey(/*handled=*/true,
7314 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7315 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7316
7317 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
7318 // When the unhandled key is reported to the policy next, remove the input channel.
7319 mDispatcher->removeInputChannel(mWindow->getToken());
7320 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
7321 });
7322 // Release the original key, and let the app now handle the previously unhandled key.
7323 // This should result in the previously generated fallback key to be cancelled.
7324 // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified
7325 // of the UP event for consistency. The Dispatcher calls into the policy from its own thread
7326 // without holding the lock, because it need to synchronously fetch the fallback key. While in
7327 // the policy call, we will now remove the input channel. Once the policy call returns, the
7328 // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does
7329 // not cause any crashes.
7330 mDispatcher->notifyKey(
7331 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7332 consumeKey(/*handled=*/true,
7333 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7334 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7335}
7336
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00007337TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) {
7338 setFallback(AKEYCODE_B);
7339 mDispatcher->notifyKey(
7340 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7341
7342 // Do not handle this key event.
7343 consumeKey(/*handled=*/false,
7344 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7346 consumeKey(/*handled=*/true,
7347 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7348 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7349
7350 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
7351 // When the unhandled key is reported to the policy next, remove the window.
7352 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
7353 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
7354 });
7355 // Release the original key, which the app will not handle. When this unhandled key is reported
7356 // to the policy, the window will be removed.
7357 mDispatcher->notifyKey(
7358 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7359 consumeKey(/*handled=*/false,
7360 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7361 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7362
7363 // Since the window was removed, it loses focus, and the channel state will be reset.
7364 consumeKey(/*handled=*/true,
7365 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7366 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7367 mWindow->consumeFocusEvent(false);
7368 mWindow->assertNoEvents();
7369}
7370
7371TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) {
7372 setFallback(AKEYCODE_B);
7373 mDispatcher->notifyKey(
7374 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7375
7376 // Do not handle this key event.
7377 consumeKey(/*handled=*/false,
7378 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7379 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7380 const auto [seq, event] = mWindow->receiveEvent();
7381 ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event";
7382 ASSERT_EQ(event->getType(), InputEventType::KEY);
7383 ASSERT_THAT(static_cast<const KeyEvent&>(*event),
7384 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7385 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7386
7387 // Remove the window now, which should generate a cancellations and make the window lose focus.
7388 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
7389 consumeKey(/*handled=*/true,
7390 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
7391 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7392 consumeKey(/*handled=*/true,
7393 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7394 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7395 mWindow->consumeFocusEvent(false);
7396
7397 // Finish the event by reporting it as handled.
7398 mWindow->finishEvent(*seq);
7399 mWindow->assertNoEvents();
7400}
7401
Garfield Tan1c7bc862020-01-28 13:24:04 -08007402class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
7403protected:
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007404 static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms;
7405 static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007406
Chris Yea209fde2020-07-22 13:54:51 -07007407 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007408 sp<FakeWindowHandle> mWindow;
7409
7410 virtual void SetUp() override {
Prabir Pradhandae52792023-12-15 07:36:40 +00007411 InputDispatcherTest::SetUp();
Garfield Tan1c7bc862020-01-28 13:24:04 -08007412
Prabir Pradhandae52792023-12-15 07:36:40 +00007413 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007414 setUpWindow();
7415 }
7416
7417 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07007418 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007419 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007420
Vishnu Nair47074b82020-08-14 11:54:47 -07007421 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007422 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007423 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007424 mWindow->consumeFocusEvent(true);
7425 }
7426
Chris Ye2ad95392020-09-01 13:44:44 -07007427 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08007428 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07007429 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007430 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00007431 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007432
7433 // Window should receive key down event.
7434 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7435 }
7436
7437 void expectKeyRepeatOnce(int32_t repeatCount) {
7438 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007439 mWindow->consumeKeyEvent(
7440 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount)));
Garfield Tan1c7bc862020-01-28 13:24:04 -08007441 }
7442
Chris Ye2ad95392020-09-01 13:44:44 -07007443 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08007444 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07007445 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007446 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00007447 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007448
7449 // Window should receive key down event.
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00007450 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007451 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007452 }
7453};
7454
7455TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00007456 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007457 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7458 expectKeyRepeatOnce(repeatCount);
7459 }
7460}
7461
7462TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00007463 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007464 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7465 expectKeyRepeatOnce(repeatCount);
7466 }
Harry Cutts33476232023-01-30 19:57:29 +00007467 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007468 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08007469 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7470 expectKeyRepeatOnce(repeatCount);
7471 }
7472}
7473
7474TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007475 sendAndConsumeKeyDown(/*deviceId=*/1);
7476 expectKeyRepeatOnce(/*repeatCount=*/1);
7477 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007478 mWindow->assertNoEvents();
7479}
7480
7481TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007482 sendAndConsumeKeyDown(/*deviceId=*/1);
7483 expectKeyRepeatOnce(/*repeatCount=*/1);
7484 sendAndConsumeKeyDown(/*deviceId=*/2);
7485 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007486 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00007487 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007488 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00007489 expectKeyRepeatOnce(/*repeatCount=*/2);
7490 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07007491 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00007492 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007493 mWindow->assertNoEvents();
7494}
7495
7496TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007497 sendAndConsumeKeyDown(/*deviceId=*/1);
7498 expectKeyRepeatOnce(/*repeatCount=*/1);
7499 sendAndConsumeKeyDown(/*deviceId=*/2);
7500 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007501 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00007502 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007503 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08007504 mWindow->assertNoEvents();
7505}
7506
liushenxiang42232912021-05-21 20:24:09 +08007507TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
7508 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00007509 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007510 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08007511 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
7512 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
7513 mWindow->assertNoEvents();
7514}
7515
Garfield Tan1c7bc862020-01-28 13:24:04 -08007516TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00007517 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00007518 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007519 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007520 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
7521 ASSERT_NE(nullptr, repeatEvent);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007522 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007523 IdGenerator::getSource(repeatEvent->getId()));
Garfield Tan1c7bc862020-01-28 13:24:04 -08007524 }
7525}
7526
7527TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00007528 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00007529 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007530
7531 std::unordered_set<int32_t> idSet;
7532 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007533 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
7534 ASSERT_NE(nullptr, repeatEvent);
7535 int32_t id = repeatEvent->getId();
Garfield Tan1c7bc862020-01-28 13:24:04 -08007536 EXPECT_EQ(idSet.end(), idSet.find(id));
7537 idSet.insert(id);
7538 }
7539}
7540
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007541/* Test InputDispatcher for MultiDisplay */
7542class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
7543public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007544 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007545 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08007546
Chris Yea209fde2020-07-22 13:54:51 -07007547 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007548 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007549 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007550
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007551 // Set focus window for primary display, but focused display would be second one.
7552 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07007553 windowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007554 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
7555
Vishnu Nair958da932020-08-21 17:12:37 -07007556 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007557 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08007558
Chris Yea209fde2020-07-22 13:54:51 -07007559 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007560 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007561 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007562 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007563 // Set focus display to second one.
7564 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
7565 // Set focus window for second display.
7566 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07007567 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007568 mDispatcher->onWindowInfosChanged(
7569 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007570 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007571 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007572 }
7573
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007574 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007575 InputDispatcherTest::TearDown();
7576
Chris Yea209fde2020-07-22 13:54:51 -07007577 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007578 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07007579 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007580 windowInSecondary.clear();
7581 }
7582
7583protected:
Chris Yea209fde2020-07-22 13:54:51 -07007584 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007585 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07007586 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007587 sp<FakeWindowHandle> windowInSecondary;
7588};
7589
7590TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
7591 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007592 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007593 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007594 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007595 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08007596 windowInSecondary->assertNoEvents();
7597
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007598 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007600 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007601 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08007602 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007603 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08007604}
7605
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007606TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08007607 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08007608 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007609 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007610 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007611 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08007612 windowInSecondary->assertNoEvents();
7613
7614 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007615 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007616 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08007617 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007618 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08007619
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007620 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007621 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08007622
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007623 // Old focus should receive a cancel event.
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00007624 windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08007625
7626 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007627 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08007628 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007629 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08007630 windowInSecondary->assertNoEvents();
7631}
7632
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007633// Test per-display input monitors for motion event.
7634TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08007635 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007636 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007637 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007638 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007639
7640 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007642 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007644 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007645 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007646 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007647 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007648
7649 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007651 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007652 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007653 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007654 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007655 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08007656 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007657
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007658 // Lift up the touch from the second display
7659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007660 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7662 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
7663 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
7664
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007665 // Test inject a non-pointer motion event.
7666 // If specific a display, it will dispatch to the focused window of particular display,
7667 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007669 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007671 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007672 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007673 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007674 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007675}
7676
7677// Test per-display input monitors for key event.
7678TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007679 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08007680 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007681 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007682 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007683 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007684
7685 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007687 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007688 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007689 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007690 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007691 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007692}
7693
Vishnu Nair958da932020-08-21 17:12:37 -07007694TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
7695 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007696 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007697 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007698 mDispatcher->onWindowInfosChanged(
7699 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
7700 *windowInSecondary->getInfo()},
7701 {},
7702 0,
7703 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007704 setFocusedWindow(secondWindowInPrimary);
7705 windowInPrimary->consumeFocusEvent(false);
7706 secondWindowInPrimary->consumeFocusEvent(true);
7707
7708 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7710 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007711 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007712 windowInPrimary->assertNoEvents();
7713 windowInSecondary->assertNoEvents();
7714 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7715}
7716
Arthur Hungdfd528e2021-12-08 13:23:04 +00007717TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
7718 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007719 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007720 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007721 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007722
7723 // Test touch down on primary display.
7724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007725 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007726 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7727 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7728 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
7729
7730 // Test touch down on second display.
7731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007732 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7734 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
7735 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
7736
7737 // Trigger cancel touch.
7738 mDispatcher->cancelCurrentTouch();
7739 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7740 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7741 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
7742 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
7743
7744 // Test inject a move motion event, no window/monitor should receive the event.
7745 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007746 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007747 ADISPLAY_ID_DEFAULT, {110, 200}))
7748 << "Inject motion event should return InputEventInjectionResult::FAILED";
7749 windowInPrimary->assertNoEvents();
7750 monitorInPrimary.assertNoEvents();
7751
7752 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007753 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007754 SECOND_DISPLAY_ID, {110, 200}))
7755 << "Inject motion event should return InputEventInjectionResult::FAILED";
7756 windowInSecondary->assertNoEvents();
7757 monitorInSecondary.assertNoEvents();
7758}
7759
Hu Guocb134f12023-12-23 13:42:44 +00007760/**
7761 * Send a key to the primary display and to the secondary display.
7762 * Then cause the key on the primary display to be canceled by sending in a stale key.
7763 * Ensure that the key on the primary display is canceled, and that the key on the secondary display
7764 * does not get canceled.
7765 */
7766TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) {
7767 // Send a key down on primary display
7768 mDispatcher->notifyKey(
7769 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
7770 .displayId(ADISPLAY_ID_DEFAULT)
7771 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7772 .build());
7773 windowInPrimary->consumeKeyEvent(
7774 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
7775 windowInSecondary->assertNoEvents();
7776
7777 // Send a key down on second display
7778 mDispatcher->notifyKey(
7779 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
7780 .displayId(SECOND_DISPLAY_ID)
7781 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7782 .build());
7783 windowInSecondary->consumeKeyEvent(
7784 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
7785 windowInPrimary->assertNoEvents();
7786
7787 // Send a valid key up event on primary display that will be dropped because it is stale
7788 NotifyKeyArgs staleKeyUp =
7789 KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
7790 .displayId(ADISPLAY_ID_DEFAULT)
7791 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7792 .build();
7793 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
7794 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
7795 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
7796 mDispatcher->notifyKey(staleKeyUp);
7797
7798 // Only the key gesture corresponding to the dropped event should receive the cancel event.
7799 // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not
7800 // receive any events.
7801 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
7802 WithDisplayId(ADISPLAY_ID_DEFAULT),
7803 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7804 windowInSecondary->assertNoEvents();
7805}
7806
7807/**
7808 * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events.
7809 */
7810TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) {
7811 // Send touch down on primary display.
7812 mDispatcher->notifyMotion(
7813 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7814 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7815 .displayId(ADISPLAY_ID_DEFAULT)
7816 .build());
7817 windowInPrimary->consumeMotionEvent(
7818 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
7819 windowInSecondary->assertNoEvents();
7820
7821 // Send touch down on second display.
7822 mDispatcher->notifyMotion(
7823 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7824 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7825 .displayId(SECOND_DISPLAY_ID)
7826 .build());
7827 windowInPrimary->assertNoEvents();
7828 windowInSecondary->consumeMotionEvent(
7829 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
7830
7831 // inject a valid MotionEvent on primary display that will be stale when it arrives.
7832 NotifyMotionArgs staleMotionUp =
7833 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7834 .displayId(ADISPLAY_ID_DEFAULT)
7835 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7836 .build();
7837 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
7838 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
7839 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
7840 mDispatcher->notifyMotion(staleMotionUp);
7841
7842 // For stale motion events, we let the gesture to complete. This behaviour is different from key
7843 // events, where we would cancel the current keys instead.
7844 windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP));
7845 windowInSecondary->assertNoEvents();
7846}
7847
Jackal Guof9696682018-10-05 12:23:23 +08007848class InputFilterTest : public InputDispatcherTest {
7849protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007850 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
7851 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08007852 NotifyMotionArgs motionArgs;
7853
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007854 motionArgs =
7855 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007856 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007857 motionArgs =
7858 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007859 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007860 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007861 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007862 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007863 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08007864 } else {
7865 mFakePolicy->assertFilterInputEventWasNotCalled();
7866 }
7867 }
7868
7869 void testNotifyKey(bool expectToBeFiltered) {
7870 NotifyKeyArgs keyArgs;
7871
7872 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007873 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007874 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007875 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007876 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007877
7878 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08007879 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007880 } else {
7881 mFakePolicy->assertFilterInputEventWasNotCalled();
7882 }
7883 }
7884};
7885
7886// Test InputFilter for MotionEvent
7887TEST_F(InputFilterTest, MotionEvent_InputFilter) {
7888 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007889 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7890 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007891
7892 // Enable InputFilter
7893 mDispatcher->setInputFilterEnabled(true);
7894 // Test touch on both primary and second display, and check if both events are filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007895 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true);
7896 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007897
7898 // Disable InputFilter
7899 mDispatcher->setInputFilterEnabled(false);
7900 // Test touch on both primary and second display, and check if both events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007901 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7902 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007903}
7904
7905// Test InputFilter for KeyEvent
7906TEST_F(InputFilterTest, KeyEvent_InputFilter) {
7907 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007908 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007909
7910 // Enable InputFilter
7911 mDispatcher->setInputFilterEnabled(true);
7912 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007913 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007914
7915 // Disable InputFilter
7916 mDispatcher->setInputFilterEnabled(false);
7917 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007918 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007919}
7920
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007921// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
7922// logical display coordinate space.
7923TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
7924 ui::Transform firstDisplayTransform;
7925 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7926 ui::Transform secondDisplayTransform;
7927 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
7928
7929 std::vector<gui::DisplayInfo> displayInfos(2);
7930 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
7931 displayInfos[0].transform = firstDisplayTransform;
7932 displayInfos[1].displayId = SECOND_DISPLAY_ID;
7933 displayInfos[1].transform = secondDisplayTransform;
7934
Patrick Williamsd828f302023-04-28 17:52:08 -05007935 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007936
7937 // Enable InputFilter
7938 mDispatcher->setInputFilterEnabled(true);
7939
7940 // Ensure the correct transforms are used for the displays.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007941 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform);
7942 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007943}
7944
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007945class InputFilterInjectionPolicyTest : public InputDispatcherTest {
7946protected:
7947 virtual void SetUp() override {
7948 InputDispatcherTest::SetUp();
7949
7950 /**
7951 * We don't need to enable input filter to test the injected event policy, but we enabled it
7952 * here to make the tests more realistic, since this policy only matters when inputfilter is
7953 * on.
7954 */
7955 mDispatcher->setInputFilterEnabled(true);
7956
7957 std::shared_ptr<InputApplicationHandle> application =
7958 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007959 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
7960 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007961
7962 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7963 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007964 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007965 setFocusedWindow(mWindow);
7966 mWindow->consumeFocusEvent(true);
7967 }
7968
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007969 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7970 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007971 KeyEvent event;
7972
7973 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7974 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
7975 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00007976 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007977 const int32_t additionalPolicyFlags =
7978 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
7979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007980 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00007981 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007982 policyFlags | additionalPolicyFlags));
7983
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007984 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007985 }
7986
7987 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7988 int32_t flags) {
7989 MotionEvent event;
7990 PointerProperties pointerProperties[1];
7991 PointerCoords pointerCoords[1];
7992 pointerProperties[0].clear();
7993 pointerProperties[0].id = 0;
7994 pointerCoords[0].clear();
7995 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
7996 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
7997
7998 ui::Transform identityTransform;
7999 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
8000 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
8001 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
8002 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
8003 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07008004 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07008005 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00008006 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008007
8008 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
8009 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00008010 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00008011 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008012 policyFlags | additionalPolicyFlags));
8013
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008014 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008015 }
8016
8017private:
8018 sp<FakeWindowHandle> mWindow;
8019};
8020
8021TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008022 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
8023 // filter. Without it, the event will no different from a regularly injected event, and the
8024 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00008025 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
8026 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008027}
8028
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008029TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008030 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00008031 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008032 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
8033}
8034
8035TEST_F(InputFilterInjectionPolicyTest,
8036 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
8037 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00008038 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008039 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008040}
8041
8042TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00008043 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
8044 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008045}
8046
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08008047class InputDispatcherUserActivityPokeTests : public InputDispatcherTest {
8048protected:
8049 virtual void SetUp() override {
8050 InputDispatcherTest::SetUp();
8051
8052 std::shared_ptr<FakeApplicationHandle> application =
8053 std::make_shared<FakeApplicationHandle>();
8054 application->setDispatchingTimeout(100ms);
8055 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
8056 ADISPLAY_ID_DEFAULT);
Yeabkal Wubshit222d83d2024-01-24 18:00:09 +00008057 mWindow->setFrame(Rect(0, 0, 100, 100));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08008058 mWindow->setDispatchingTimeout(100ms);
8059 mWindow->setFocusable(true);
8060
8061 // Set focused application.
8062 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8063
8064 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
8065 setFocusedWindow(mWindow);
8066 mWindow->consumeFocusEvent(true);
8067 }
8068
8069 void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId,
8070 nsecs_t eventTime) {
8071 mDispatcher->notifyMotion(MotionArgsBuilder(action, source)
8072 .displayId(displayId)
8073 .eventTime(eventTime)
8074 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8075 .build());
8076 mWindow->consumeMotionEvent(WithMotionAction(action));
8077 }
8078
8079private:
8080 sp<FakeWindowHandle> mWindow;
8081};
8082
8083TEST_F_WITH_FLAGS(
8084 InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
8085 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8086 rate_limit_user_activity_poke_in_dispatcher))) {
8087 mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
8088
8089 // First event of type TOUCH. Should poke.
8090 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8091 milliseconds_to_nanoseconds(50));
8092 mFakePolicy->assertUserActivityPoked(
8093 {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8094
8095 // 80ns > 50ns has passed since previous TOUCH event. Should poke.
8096 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8097 milliseconds_to_nanoseconds(130));
8098 mFakePolicy->assertUserActivityPoked(
8099 {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8100
8101 // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
8102 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8103 milliseconds_to_nanoseconds(135));
8104 mFakePolicy->assertUserActivityPoked(
8105 {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}});
8106
8107 // Within 50ns of previous TOUCH event. Should NOT poke.
8108 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8109 milliseconds_to_nanoseconds(140));
8110 mFakePolicy->assertUserActivityNotPoked();
8111
8112 // Within 50ns of previous OTHER event. Should NOT poke.
8113 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8114 milliseconds_to_nanoseconds(150));
8115 mFakePolicy->assertUserActivityNotPoked();
8116
8117 // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
8118 // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
8119 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
8120 milliseconds_to_nanoseconds(160));
8121 mFakePolicy->assertUserActivityNotPoked();
8122
8123 // 65ns > 50ns has passed since previous OTHER event. Should poke.
8124 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8125 milliseconds_to_nanoseconds(200));
8126 mFakePolicy->assertUserActivityPoked(
8127 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}});
8128
8129 // 170ns > 50ns has passed since previous TOUCH event. Should poke.
8130 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
8131 milliseconds_to_nanoseconds(300));
8132 mFakePolicy->assertUserActivityPoked(
8133 {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8134
8135 // Assert that there's no more user activity poke event.
8136 mFakePolicy->assertUserActivityNotPoked();
8137}
8138
8139TEST_F_WITH_FLAGS(
8140 InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
8141 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8142 rate_limit_user_activity_poke_in_dispatcher))) {
8143 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8144 milliseconds_to_nanoseconds(200));
8145 mFakePolicy->assertUserActivityPoked(
8146 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8147
8148 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8149 milliseconds_to_nanoseconds(280));
8150 mFakePolicy->assertUserActivityNotPoked();
8151
8152 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8153 milliseconds_to_nanoseconds(340));
8154 mFakePolicy->assertUserActivityPoked(
8155 {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8156}
8157
8158TEST_F_WITH_FLAGS(
8159 InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
8160 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8161 rate_limit_user_activity_poke_in_dispatcher))) {
8162 mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
8163
8164 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20);
8165 mFakePolicy->assertUserActivityPoked();
8166
8167 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30);
8168 mFakePolicy->assertUserActivityPoked();
8169}
8170
chaviwfd6d3512019-03-25 13:23:49 -07008171class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008172 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07008173 InputDispatcherTest::SetUp();
8174
Chris Yea209fde2020-07-22 13:54:51 -07008175 std::shared_ptr<FakeApplicationHandle> application =
8176 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008177 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008178 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07008179 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07008180
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008181 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008182 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008183 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07008184
8185 // Set focused application.
8186 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07008187 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07008188
8189 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008190 mDispatcher->onWindowInfosChanged(
8191 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008192 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008193 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07008194 }
8195
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008196 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07008197 InputDispatcherTest::TearDown();
8198
8199 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008200 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07008201 }
8202
8203protected:
8204 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008205 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008206 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07008207};
8208
8209// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
8210// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
8211// the onPointerDownOutsideFocus callback.
8212TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008214 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008215 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008216 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008217 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008218
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008219 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07008220 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
8221}
8222
8223// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
8224// DOWN on the window that doesn't have focus. Ensure no window received the
8225// onPointerDownOutsideFocus callback.
8226TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008227 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008228 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT,
8229 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008230 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008231 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008232
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008233 ASSERT_TRUE(mDispatcher->waitForIdle());
8234 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008235}
8236
8237// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
8238// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
8239TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08008240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008241 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008242 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008243 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07008244
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008245 ASSERT_TRUE(mDispatcher->waitForIdle());
8246 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008247}
8248
8249// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
8250// DOWN on the window that already has focus. Ensure no window received the
8251// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008252TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008254 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008255 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008256 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008257 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008258
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008259 ASSERT_TRUE(mDispatcher->waitForIdle());
8260 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008261}
8262
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008263// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
8264// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
8265TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
8266 const MotionEvent event =
8267 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8268 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008269 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008270 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
8271 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008272 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008273 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8274 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
8275
8276 ASSERT_TRUE(mDispatcher->waitForIdle());
8277 mFakePolicy->assertOnPointerDownWasNotCalled();
8278 // Ensure that the unfocused window did not receive any FOCUS events.
8279 mUnfocusedWindow->assertNoEvents();
8280}
8281
chaviwaf87b3e2019-10-01 16:59:28 -07008282// These tests ensures we can send touch events to a single client when there are multiple input
8283// windows that point to the same client token.
8284class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
8285 virtual void SetUp() override {
8286 InputDispatcherTest::SetUp();
8287
Chris Yea209fde2020-07-22 13:54:51 -07008288 std::shared_ptr<FakeApplicationHandle> application =
8289 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008290 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
8291 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07008292 mWindow1->setFrame(Rect(0, 0, 100, 100));
8293
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00008294 mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07008295 mWindow2->setFrame(Rect(100, 100, 200, 200));
8296
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008297 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07008298 }
8299
8300protected:
8301 sp<FakeWindowHandle> mWindow1;
8302 sp<FakeWindowHandle> mWindow2;
8303
8304 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05008305 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07008306 vec2 vals = windowInfo->transform.transform(point.x, point.y);
8307 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07008308 }
8309
8310 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
8311 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008312 const std::string name = window->getName();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008313 std::unique_ptr<MotionEvent> motionEvent =
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00008314 window->consumeMotionEvent(WithMotionAction(expectedAction));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008315 ASSERT_NE(nullptr, motionEvent);
8316 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07008317
8318 for (size_t i = 0; i < points.size(); i++) {
8319 float expectedX = points[i].x;
8320 float expectedY = points[i].y;
8321
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008322 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07008323 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008324 << ", got " << motionEvent->getX(i);
8325 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07008326 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008327 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07008328 }
8329 }
chaviw9eaa22c2020-07-01 16:21:27 -07008330
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008331 void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action,
8332 const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07008333 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00008334 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
8335 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07008336
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008337 consumeMotionEvent(touchedWindow, action, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008338 }
chaviwaf87b3e2019-10-01 16:59:28 -07008339};
8340
8341TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
8342 // Touch Window 1
8343 PointF touchedPoint = {10, 10};
8344 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008345 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008346
8347 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008348 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008349
8350 // Touch Window 2
8351 touchedPoint = {150, 150};
8352 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008353 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008354}
8355
chaviw9eaa22c2020-07-01 16:21:27 -07008356TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
8357 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07008358 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008359 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07008360
8361 // Touch Window 1
8362 PointF touchedPoint = {10, 10};
8363 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008364 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008365 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008366 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008367
8368 // Touch Window 2
8369 touchedPoint = {150, 150};
8370 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008371 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
8372 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008373
chaviw9eaa22c2020-07-01 16:21:27 -07008374 // Update the transform so rotation is set
8375 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008376 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008377 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008378 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008379}
8380
chaviw9eaa22c2020-07-01 16:21:27 -07008381TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008382 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008383 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008384
8385 // Touch Window 1
8386 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8387 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008388 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008389
8390 // Touch Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008391 // Since this is part of the same touch gesture that has already been dispatched to Window 1,
8392 // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream
8393 // will continue to be dispatched through Window 1.
chaviw9eaa22c2020-07-01 16:21:27 -07008394 touchedPoints.push_back(PointF{150, 150});
8395 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008396 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008397
chaviw9eaa22c2020-07-01 16:21:27 -07008398 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008399 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008400 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008401
chaviw9eaa22c2020-07-01 16:21:27 -07008402 // Update the transform so rotation is set for Window 2
8403 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008404 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008405 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008406 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008407}
8408
chaviw9eaa22c2020-07-01 16:21:27 -07008409TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008410 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008411 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008412
8413 // Touch Window 1
8414 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8415 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008416 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008417
8418 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008419 touchedPoints.push_back(PointF{150, 150});
8420 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008421
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008422 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008423
8424 // Move both windows
8425 touchedPoints = {{20, 20}, {175, 175}};
8426 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8427 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8428
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008429 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008430
chaviw9eaa22c2020-07-01 16:21:27 -07008431 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008432 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008433 expectedPoints.pop_back();
8434
8435 // Touch Window 2
8436 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008437 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008438 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008439 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008440
8441 // Move both windows
8442 touchedPoints = {{20, 20}, {175, 175}};
8443 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8444 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8445
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008446 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008447}
8448
8449TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
8450 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008451 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008452
8453 // Touch Window 1
8454 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8455 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008456 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008457
8458 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008459 touchedPoints.push_back(PointF{150, 150});
8460 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008461
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008462 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008463
8464 // Move both windows
8465 touchedPoints = {{20, 20}, {175, 175}};
8466 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8467 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8468
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008469 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008470}
8471
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07008472/**
8473 * When one of the windows is slippery, the touch should not slip into the other window with the
8474 * same input channel.
8475 */
8476TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
8477 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008478 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07008479
8480 // Touch down in window 1
8481 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8482 ADISPLAY_ID_DEFAULT, {{50, 50}}));
8483 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
8484
8485 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
8486 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
8487 // getting generated.
8488 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8489 ADISPLAY_ID_DEFAULT, {{150, 150}}));
8490
8491 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
8492}
8493
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008494/**
8495 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
8496 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
8497 * that the pointer is hovering over may have a different transform.
8498 */
8499TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008500 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008501
8502 // Start hover in window 1
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008503 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN)
8504 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8505 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008506 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
8507 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008508 // Move hover to window 2.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008509 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8510 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
8511 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008512 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008513 consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER,
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008514 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
8515}
8516
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008517class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
8518 virtual void SetUp() override {
8519 InputDispatcherTest::SetUp();
8520
Chris Yea209fde2020-07-22 13:54:51 -07008521 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008522 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008523 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
8524 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008525 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008526 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07008527 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008528
8529 // Set focused application.
8530 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
8531
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008532 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008533 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008534 mWindow->consumeFocusEvent(true);
8535 }
8536
8537 virtual void TearDown() override {
8538 InputDispatcherTest::TearDown();
8539 mWindow.clear();
8540 }
8541
8542protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008543 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07008544 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008545 sp<FakeWindowHandle> mWindow;
8546 static constexpr PointF WINDOW_LOCATION = {20, 20};
8547
8548 void tapOnWindow() {
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008549 const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER)
8550 .x(WINDOW_LOCATION.x)
8551 .y(WINDOW_LOCATION.y);
8552 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8553 .pointer(touchingPointer)
8554 .build());
8555 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
8556 .pointer(touchingPointer)
8557 .build());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008558 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008559
8560 sp<FakeWindowHandle> addSpyWindow() {
8561 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008562 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008563 spy->setTrustedOverlay(true);
8564 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008565 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008566 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008567 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008568 return spy;
8569 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008570};
8571
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008572// Send a tap and respond, which should not cause an ANR.
8573TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
8574 tapOnWindow();
8575 mWindow->consumeMotionDown();
8576 mWindow->consumeMotionUp();
8577 ASSERT_TRUE(mDispatcher->waitForIdle());
8578 mFakePolicy->assertNotifyAnrWasNotCalled();
8579}
8580
8581// Send a regular key and respond, which should not cause an ANR.
8582TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008583 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008584 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
8585 ASSERT_TRUE(mDispatcher->waitForIdle());
8586 mFakePolicy->assertNotifyAnrWasNotCalled();
8587}
8588
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008589TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
8590 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008591 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008592 mWindow->consumeFocusEvent(false);
8593
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008594 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008595 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
8596 InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00008597 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008598 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008599 // Key will not go to window because we have no focused window.
8600 // The 'no focused window' ANR timer should start instead.
8601
8602 // Now, the focused application goes away.
8603 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
8604 // The key should get dropped and there should be no ANR.
8605
8606 ASSERT_TRUE(mDispatcher->waitForIdle());
8607 mFakePolicy->assertNotifyAnrWasNotCalled();
8608}
8609
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008610// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008611// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
8612// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008613TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008614 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008615 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008616 WINDOW_LOCATION));
8617
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008618 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008619 ASSERT_TRUE(sequenceNum);
8620 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008621 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008622
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008623 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008624 mWindow->consumeMotionEvent(
8625 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008626 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008627 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008628}
8629
8630// Send a key to the app and have the app not respond right away.
8631TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
8632 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008634 const auto [sequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008635 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008636 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008637 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008638 ASSERT_TRUE(mDispatcher->waitForIdle());
8639}
8640
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008641// We have a focused application, but no focused window
8642TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07008643 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008644 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008645 mWindow->consumeFocusEvent(false);
8646
8647 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008649 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008650 WINDOW_LOCATION));
8651 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
8652 mDispatcher->waitForIdle();
8653 mFakePolicy->assertNotifyAnrWasNotCalled();
8654
8655 // Once a focused event arrives, we get an ANR for this application
8656 // We specify the injection timeout to be smaller than the application timeout, to ensure that
8657 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008658 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008659 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008660 InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008661 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008662 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07008663 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008664 ASSERT_TRUE(mDispatcher->waitForIdle());
8665}
8666
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008667/**
8668 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
8669 * there will not be an ANR.
8670 */
8671TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
8672 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008673 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008674 mWindow->consumeFocusEvent(false);
8675
8676 KeyEvent event;
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07008677 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
8678 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008679 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
8680 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
8681
8682 // Define a valid key down event that is stale (too old).
8683 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Harry Cutts101ee9b2023-07-06 18:04:14 +00008684 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00008685 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008686
8687 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
8688
8689 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00008690 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008691 InputEventInjectionSync::WAIT_FOR_RESULT,
8692 INJECT_EVENT_TIMEOUT, policyFlags);
8693 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
8694 << "Injection should fail because the event is stale";
8695
8696 ASSERT_TRUE(mDispatcher->waitForIdle());
8697 mFakePolicy->assertNotifyAnrWasNotCalled();
8698 mWindow->assertNoEvents();
8699}
8700
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008701// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008702// Make sure that we don't notify policy twice about the same ANR.
8703TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008704 const std::chrono::duration appTimeout = 400ms;
8705 mApplication->setDispatchingTimeout(appTimeout);
8706 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
8707
Vishnu Nair47074b82020-08-14 11:54:47 -07008708 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008709 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008710 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008711
8712 // Once a focused event arrives, we get an ANR for this application
8713 // We specify the injection timeout to be smaller than the application timeout, to ensure that
8714 // injection times out (instead of failing).
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008715 const std::chrono::duration eventInjectionTimeout = 100ms;
8716 ASSERT_LT(eventInjectionTimeout, appTimeout);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008717 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008718 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008719 InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout,
8720 /*allowKeyRepeat=*/false);
8721 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
8722 << "result=" << ftl::enum_string(result);
8723 // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
8724 // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
8725 std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
8726 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008727
Vishnu Naire4df8752022-09-08 09:17:55 -07008728 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008729 // ANR should not be raised again. It is up to policy to do that if it desires.
8730 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008731
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008732 // If we now get a focused window, the ANR should stop, but the policy handles that via
8733 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008734 ASSERT_TRUE(mDispatcher->waitForIdle());
8735}
8736
8737// We have a focused application, but no focused window
8738TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07008739 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008740 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008741 mWindow->consumeFocusEvent(false);
8742
8743 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008744 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008745
Vishnu Naire4df8752022-09-08 09:17:55 -07008746 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
8747 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008748
8749 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008750 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008751 ASSERT_TRUE(mDispatcher->waitForIdle());
8752 mWindow->assertNoEvents();
8753}
8754
8755/**
8756 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
8757 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
8758 * If we process 1 of the events, but ANR on the second event with the same timestamp,
8759 * the ANR mechanism should still work.
8760 *
8761 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
8762 * DOWN event, while not responding on the second one.
8763 */
8764TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
8765 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008766 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008767 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
8768 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8769 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008770 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008771
8772 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008773 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008774 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
8775 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8776 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008777 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008778
8779 // We have now sent down and up. Let's consume first event and then ANR on the second.
8780 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8781 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008782 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008783}
8784
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008785// A spy window can receive an ANR
8786TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
8787 sp<FakeWindowHandle> spy = addSpyWindow();
8788
8789 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008790 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008791 WINDOW_LOCATION));
8792 mWindow->consumeMotionDown();
8793
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008794 const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008795 ASSERT_TRUE(sequenceNum);
8796 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008797 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008798
8799 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008800 spy->consumeMotionEvent(
8801 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008802 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008803 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008804}
8805
8806// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008807// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008808TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
8809 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008810
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008811 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008812 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008813 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008815
8816 // Stuck on the ACTION_UP
8817 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008818 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008819
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008820 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008821 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008822 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8823 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008824
8825 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
8826 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008827 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008828 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008829 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008830}
8831
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008832// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008833// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008834TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
8835 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008836
8837 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008838 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8839 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008840
8841 mWindow->consumeMotionDown();
8842 // Stuck on the ACTION_UP
8843 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008844 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008845
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008846 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008847 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008848 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8849 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008850
8851 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
8852 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008853 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008854 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008855 spy->assertNoEvents();
8856}
8857
8858TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008859 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008860
Prabir Pradhanfb549072023-10-05 19:17:36 +00008861 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008862
8863 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008864 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008865 WINDOW_LOCATION));
8866
8867 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8868 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
8869 ASSERT_TRUE(consumeSeq);
8870
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008871 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
8872 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008873
8874 monitor.finishEvent(*consumeSeq);
8875 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
8876
8877 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008878 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008879}
8880
8881// If a window is unresponsive, then you get anr. if the window later catches up and starts to
8882// process events, you don't get an anr. When the window later becomes unresponsive again, you
8883// get an ANR again.
8884// 1. tap -> block on ACTION_UP -> receive ANR
8885// 2. consume all pending events (= queue becomes healthy again)
8886// 3. tap again -> block on ACTION_UP again -> receive ANR second time
8887TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
8888 tapOnWindow();
8889
8890 mWindow->consumeMotionDown();
8891 // Block on ACTION_UP
8892 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008893 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008894 mWindow->consumeMotionUp(); // Now the connection should be healthy again
8895 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008896 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008897 mWindow->assertNoEvents();
8898
8899 tapOnWindow();
8900 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008901 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008902 mWindow->consumeMotionUp();
8903
8904 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008905 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008906 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008907 mWindow->assertNoEvents();
8908}
8909
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008910// If a connection remains unresponsive for a while, make sure policy is only notified once about
8911// it.
8912TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008914 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008915 WINDOW_LOCATION));
8916
8917 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008918 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008919 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008920 // 'notifyConnectionUnresponsive' should only be called once per connection
8921 mFakePolicy->assertNotifyAnrWasNotCalled();
8922 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008923 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008924 mWindow->consumeMotionEvent(
8925 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008926 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008927 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008928 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008929 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008930}
8931
8932/**
8933 * If a window is processing a motion event, and then a key event comes in, the key event should
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008934 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008935 */
8936TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008937 // The timeouts in this test are established by relying on the fact that the "key waiting for
8938 // events timeout" is equal to 500ms.
8939 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008940 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008941 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008942
8943 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008944 const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008945 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008946 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008947 ASSERT_TRUE(upSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008948
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008949 // Don't finish the events yet, and send a key
8950 mDispatcher->notifyKey(
8951 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8952 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8953 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008954 // Key will not be sent to the window, yet, because the window is still processing events
8955 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008956 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008957 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008958
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008959 std::this_thread::sleep_for(400ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008960 // if we wait long enough though, dispatcher will give up, and still send the key
8961 // to the focused window, even though we have not yet finished the motion event
8962 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8963 mWindow->finishEvent(*downSequenceNum);
8964 mWindow->finishEvent(*upSequenceNum);
8965}
8966
8967/**
8968 * If a window is processing a motion event, and then a key event comes in, the key event should
8969 * not go to the focused window until the motion is processed.
8970 * If then a new motion comes in, then the pending key event should be going to the currently
8971 * focused window right away.
8972 */
8973TEST_F(InputDispatcherSingleWindowAnr,
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008974 PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) {
8975 // The timeouts in this test are established by relying on the fact that the "key waiting for
8976 // events timeout" is equal to 500ms.
8977 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008978 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008979 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008980
8981 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008982 const auto& [downSequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008983 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008984 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008985 ASSERT_TRUE(upSequenceNum);
8986 // Don't finish the events yet, and send a key
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008987 mDispatcher->notifyKey(
8988 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8989 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8990 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008991 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008992 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008993
8994 // Now tap down again. It should cause the pending key to go to the focused window right away.
8995 tapOnWindow();
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008996 // Now that we tapped, we should receive the key immediately.
8997 // Since there's still room for slowness, we use 200ms, which is much less than
8998 // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration.
8999 std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms);
9000 ASSERT_NE(nullptr, keyEvent);
9001 ASSERT_EQ(InputEventType::KEY, keyEvent->getType());
9002 ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN));
9003 // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any
9004 // order.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009005 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
9006 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08009007 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9008 mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009009 mWindow->assertNoEvents();
9010}
9011
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07009012/**
9013 * Send an event to the app and have the app not respond right away.
9014 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
9015 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
9016 * At some point, the window becomes responsive again.
9017 * Ensure that subsequent events get dropped, and the next gesture is delivered.
9018 */
9019TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
9020 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9021 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
9022 .build());
9023
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009024 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07009025 ASSERT_TRUE(sequenceNum);
9026 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9027 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
9028
9029 mWindow->finishEvent(*sequenceNum);
9030 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
9031 ASSERT_TRUE(mDispatcher->waitForIdle());
9032 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
9033
9034 // Now that the window is responsive, let's continue the gesture.
9035 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
9036 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9037 .build());
9038
9039 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9040 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9041 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
9042 .build());
9043
9044 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
9045 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9046 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
9047 .build());
9048 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9049 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9050 .build());
9051 // We already canceled this pointer, so the window shouldn't get any new events.
9052 mWindow->assertNoEvents();
9053
9054 // Start another one.
9055 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9056 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
9057 .build());
9058 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9059}
9060
Prabir Pradhanfc364722024-02-08 17:51:20 +00009061// Send an event to the app and have the app not respond right away. Then remove the app window.
9062// When the window is removed, the dispatcher will cancel the events for that window.
9063// So InputDispatcher will enqueue ACTION_CANCEL event as well.
9064TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) {
9065 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9066 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9067 {WINDOW_LOCATION}));
9068
9069 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
9070 ASSERT_TRUE(sequenceNum);
9071
9072 // Remove the window, but the input channel should remain alive.
9073 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
9074
9075 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9076 // Since the window was removed, Dispatcher does not know the PID associated with the window
9077 // anymore, so the policy is notified without the PID.
9078 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(),
9079 /*pid=*/std::nullopt);
9080
9081 mWindow->finishEvent(*sequenceNum);
9082 // The cancellation was generated when the window was removed, along with the focus event.
9083 mWindow->consumeMotionEvent(
9084 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
9085 mWindow->consumeFocusEvent(false);
9086 ASSERT_TRUE(mDispatcher->waitForIdle());
9087 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
9088}
9089
9090// Send an event to the app and have the app not respond right away. Wait for the policy to be
9091// notified of the unresponsive window, then remove the app window.
9092TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) {
9093 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9094 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9095 {WINDOW_LOCATION}));
9096
9097 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
9098 ASSERT_TRUE(sequenceNum);
9099 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9100 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
9101
9102 // Remove the window, but the input channel should remain alive.
9103 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
9104
9105 mWindow->finishEvent(*sequenceNum);
9106 // The cancellation was generated during the ANR, and the window lost focus when it was removed.
9107 mWindow->consumeMotionEvent(
9108 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
9109 mWindow->consumeFocusEvent(false);
9110 ASSERT_TRUE(mDispatcher->waitForIdle());
9111 // Since the window was removed, Dispatcher does not know the PID associated with the window
9112 // becoming responsive, so the policy is notified without the PID.
9113 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
9114}
9115
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009116class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
9117 virtual void SetUp() override {
9118 InputDispatcherTest::SetUp();
9119
Chris Yea209fde2020-07-22 13:54:51 -07009120 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009121 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009122 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
9123 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009124 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009125 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009126 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009127
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009128 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
9129 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009130 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009131 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009132
9133 // Set focused application.
9134 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07009135 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009136
9137 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009138 mDispatcher->onWindowInfosChanged(
9139 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009140 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009141 mFocusedWindow->consumeFocusEvent(true);
9142 }
9143
9144 virtual void TearDown() override {
9145 InputDispatcherTest::TearDown();
9146
9147 mUnfocusedWindow.clear();
9148 mFocusedWindow.clear();
9149 }
9150
9151protected:
Chris Yea209fde2020-07-22 13:54:51 -07009152 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009153 sp<FakeWindowHandle> mUnfocusedWindow;
9154 sp<FakeWindowHandle> mFocusedWindow;
9155 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
9156 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
9157 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
9158
9159 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
9160
9161 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
9162
9163private:
9164 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009165 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009166 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009167 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009169 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009170 location));
9171 }
9172};
9173
9174// If we have 2 windows that are both unresponsive, the one with the shortest timeout
9175// should be ANR'd first.
9176TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009178 injectMotionEvent(*mDispatcher,
9179 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9180 AINPUT_SOURCE_TOUCHSCREEN)
9181 .pointer(PointerBuilder(0, ToolType::FINGER)
9182 .x(FOCUSED_WINDOW_LOCATION.x)
9183 .y(FOCUSED_WINDOW_LOCATION.y))
9184 .build()));
9185 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9186 injectMotionEvent(*mDispatcher,
9187 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
9188 AINPUT_SOURCE_TOUCHSCREEN)
9189 .pointer(PointerBuilder(0, ToolType::FINGER)
9190 .x(FOCUSED_WINDOW_LOCATION.x)
9191 .y(FOCUSED_WINDOW_LOCATION.y))
9192 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009193 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009194 mFocusedWindow->consumeMotionUp();
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009195 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009196 // We consumed all events, so no ANR
9197 ASSERT_TRUE(mDispatcher->waitForIdle());
9198 mFakePolicy->assertNotifyAnrWasNotCalled();
9199
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009201 injectMotionEvent(*mDispatcher,
9202 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9203 AINPUT_SOURCE_TOUCHSCREEN)
9204 .pointer(PointerBuilder(0, ToolType::FINGER)
9205 .x(FOCUSED_WINDOW_LOCATION.x)
9206 .y(FOCUSED_WINDOW_LOCATION.y))
9207 .build()));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009208 const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009209 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009210
9211 const std::chrono::duration timeout =
9212 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009213 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009214
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009215 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009216 mFocusedWindow->consumeMotionDown();
9217 // This cancel is generated because the connection was unresponsive
9218 mFocusedWindow->consumeMotionCancel();
9219 mFocusedWindow->assertNoEvents();
9220 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009221 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009222 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9223 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009224 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009225}
9226
9227// If we have 2 windows with identical timeouts that are both unresponsive,
9228// it doesn't matter which order they should have ANR.
9229// But we should receive ANR for both.
9230TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
9231 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009232 mUnfocusedWindow->setDispatchingTimeout(
9233 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009234 mDispatcher->onWindowInfosChanged(
9235 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009236
9237 tapOnFocusedWindow();
9238 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009239 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009240 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
9241 mFocusedWindow->getDispatchingTimeout(
9242 DISPATCHING_TIMEOUT)),
9243 mFakePolicy->getUnresponsiveWindowToken(0ms)};
9244
9245 ASSERT_THAT(anrConnectionTokens,
9246 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
9247 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009248
9249 ASSERT_TRUE(mDispatcher->waitForIdle());
9250 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009251
9252 mFocusedWindow->consumeMotionDown();
9253 mFocusedWindow->consumeMotionUp();
9254 mUnfocusedWindow->consumeMotionOutside();
9255
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009256 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
9257 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009258
9259 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009260 ASSERT_THAT(responsiveTokens,
9261 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
9262 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009263 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009264}
9265
9266// If a window is already not responding, the second tap on the same window should be ignored.
9267// We should also log an error to account for the dropped event (not tested here).
9268// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
9269TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
9270 tapOnFocusedWindow();
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009271 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009272 // Receive the events, but don't respond
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009273 const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009274 ASSERT_TRUE(downEventSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009275 const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009276 ASSERT_TRUE(upEventSequenceNum);
9277 const std::chrono::duration timeout =
9278 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009279 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009280
9281 // Tap once again
9282 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009283 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009284 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009285 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009286 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009287 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009288 FOCUSED_WINDOW_LOCATION));
9289 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
9290 // valid touch target
9291 mUnfocusedWindow->assertNoEvents();
9292
9293 // Consume the first tap
9294 mFocusedWindow->finishEvent(*downEventSequenceNum);
9295 mFocusedWindow->finishEvent(*upEventSequenceNum);
9296 ASSERT_TRUE(mDispatcher->waitForIdle());
9297 // The second tap did not go to the focused window
9298 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009299 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08009300 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9301 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009302 mFakePolicy->assertNotifyAnrWasNotCalled();
9303}
9304
9305// If you tap outside of all windows, there will not be ANR
9306TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009307 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009308 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009309 LOCATION_OUTSIDE_ALL_WINDOWS));
9310 ASSERT_TRUE(mDispatcher->waitForIdle());
9311 mFakePolicy->assertNotifyAnrWasNotCalled();
9312}
9313
9314// Since the focused window is paused, tapping on it should not produce any events
9315TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
9316 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009317 mDispatcher->onWindowInfosChanged(
9318 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009319
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009320 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009321 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009322 FOCUSED_WINDOW_LOCATION));
9323
9324 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
9325 ASSERT_TRUE(mDispatcher->waitForIdle());
9326 // Should not ANR because the window is paused, and touches shouldn't go to it
9327 mFakePolicy->assertNotifyAnrWasNotCalled();
9328
9329 mFocusedWindow->assertNoEvents();
9330 mUnfocusedWindow->assertNoEvents();
9331}
9332
9333/**
9334 * If a window is processing a motion event, and then a key event comes in, the key event should
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009335 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009336 * If a different window becomes focused at this time, the key should go to that window instead.
9337 *
9338 * Warning!!!
9339 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
9340 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009341 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009342 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
9343 *
9344 * If that value changes, this test should also change.
9345 */
9346TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
9347 // Set a long ANR timeout to prevent it from triggering
9348 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009349 mDispatcher->onWindowInfosChanged(
9350 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009351
9352 tapOnUnfocusedWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009353 const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009354 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009355 const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009356 ASSERT_TRUE(upSequenceNum);
9357 // Don't finish the events yet, and send a key
9358 // Injection will succeed because we will eventually give up and send the key to the focused
9359 // window even if motions are still being processed.
9360
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009361 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009362 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9363 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009365 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009366 // and the key remains pending, waiting for the touch events to be processed.
9367 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
9368 // under the hood.
9369 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
9370 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009371
9372 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07009373 mFocusedWindow->setFocusable(false);
9374 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009375 mDispatcher->onWindowInfosChanged(
9376 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009377 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009378
9379 // Focus events should precede the key events
9380 mUnfocusedWindow->consumeFocusEvent(true);
9381 mFocusedWindow->consumeFocusEvent(false);
9382
9383 // Finish the tap events, which should unblock dispatcher
9384 mUnfocusedWindow->finishEvent(*downSequenceNum);
9385 mUnfocusedWindow->finishEvent(*upSequenceNum);
9386
9387 // Now that all queues are cleared and no backlog in the connections, the key event
9388 // can finally go to the newly focused "mUnfocusedWindow".
9389 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9390 mFocusedWindow->assertNoEvents();
9391 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009392 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009393}
9394
9395// When the touch stream is split across 2 windows, and one of them does not respond,
9396// then ANR should be raised and the touch should be canceled for the unresponsive window.
9397// The other window should not be affected by that.
9398TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
9399 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00009400 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9401 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9402 {FOCUSED_WINDOW_LOCATION}));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009403 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009404
9405 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00009406 mDispatcher->notifyMotion(
9407 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9408 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009409
9410 const std::chrono::duration timeout =
9411 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009412 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009413
9414 mUnfocusedWindow->consumeMotionDown();
9415 mFocusedWindow->consumeMotionDown();
9416 // Focused window may or may not receive ACTION_MOVE
9417 // But it should definitely receive ACTION_CANCEL due to the ANR
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009418 const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009419 ASSERT_TRUE(moveOrCancelSequenceNum);
9420 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
9421 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07009422 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009423 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
9424 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
9425 mFocusedWindow->consumeMotionCancel();
9426 } else {
9427 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
9428 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009429 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009430 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9431 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009432
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009433 mUnfocusedWindow->assertNoEvents();
9434 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009435 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009436}
9437
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009438/**
9439 * If we have no focused window, and a key comes in, we start the ANR timer.
9440 * The focused application should add a focused window before the timer runs out to prevent ANR.
9441 *
9442 * If the user touches another application during this time, the key should be dropped.
9443 * Next, if a new focused window comes in, without toggling the focused application,
9444 * then no ANR should occur.
9445 *
9446 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
9447 * but in some cases the policy may not update the focused application.
9448 */
9449TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
9450 std::shared_ptr<FakeApplicationHandle> focusedApplication =
9451 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07009452 focusedApplication->setDispatchingTimeout(300ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009453 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
9454 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
9455 mFocusedWindow->setFocusable(false);
9456
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009457 mDispatcher->onWindowInfosChanged(
9458 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009459 mFocusedWindow->consumeFocusEvent(false);
9460
9461 // Send a key. The ANR timer should start because there is no focused window.
9462 // 'focusedApplication' will get blamed if this timer completes.
9463 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009464 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009465 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9466 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +00009467 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009469
9470 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
9471 // then the injected touches won't cause the focused event to get dropped.
9472 // The dispatcher only checks for whether the queue should be pruned upon queueing.
9473 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
9474 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
9475 // For this test, it means that the key would get delivered to the window once it becomes
9476 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009477 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009478
9479 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00009480 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9481 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9482 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009483
9484 // We do not consume the motion right away, because that would require dispatcher to first
9485 // process (== drop) the key event, and by that time, ANR will be raised.
9486 // Set the focused window first.
9487 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009488 mDispatcher->onWindowInfosChanged(
9489 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009490 setFocusedWindow(mFocusedWindow);
9491 mFocusedWindow->consumeFocusEvent(true);
9492 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
9493 // to another application. This could be a bug / behaviour in the policy.
9494
9495 mUnfocusedWindow->consumeMotionDown();
9496
9497 ASSERT_TRUE(mDispatcher->waitForIdle());
9498 // Should not ANR because we actually have a focused window. It was just added too slowly.
9499 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
9500}
9501
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08009502/**
9503 * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having
9504 * an inconsistent event stream inside the dispatcher. In this test, we make sure that the
9505 * dispatcher doesn't prune pointer events incorrectly.
9506 *
9507 * This test reproduces a crash in InputDispatcher.
9508 * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur.
9509 *
9510 * Keep the currently focused application (mApplication), and have no focused window.
9511 * We set up two additional windows:
9512 * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the
9513 * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application
9514 * window. This window is not focusable, but is touchable.
9515 *
9516 * We first touch the navigation bar, which causes it to inject a key. Since there's no focused
9517 * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now
9518 * blocked. The dispatcher is waiting for 'mApplication' to add a focused window.
9519 *
9520 * Now, we touch "Another window". This window is owned by a different application than
9521 * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused
9522 * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start
9523 * dropping the events from its queue. Ensure that no crash occurs.
9524 *
9525 * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale.
9526 * This does not affect the test running time.
9527 */
9528TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) {
9529 std::shared_ptr<FakeApplicationHandle> systemUiApplication =
9530 std::make_shared<FakeApplicationHandle>();
9531 systemUiApplication->setDispatchingTimeout(3000ms);
9532 mFakePolicy->setStaleEventTimeout(3000ms);
9533 sp<FakeWindowHandle> navigationBar =
9534 sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar",
9535 ADISPLAY_ID_DEFAULT);
9536 navigationBar->setFocusable(false);
9537 navigationBar->setWatchOutsideTouch(true);
9538 navigationBar->setFrame(Rect(0, 0, 100, 100));
9539
9540 mApplication->setDispatchingTimeout(3000ms);
9541 // 'mApplication' is already focused, but we call it again here to make it explicit.
9542 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
9543
9544 std::shared_ptr<FakeApplicationHandle> anotherApplication =
9545 std::make_shared<FakeApplicationHandle>();
9546 sp<FakeWindowHandle> appWindow =
9547 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window",
9548 ADISPLAY_ID_DEFAULT);
9549 appWindow->setFocusable(false);
9550 appWindow->setFrame(Rect(100, 100, 200, 200));
9551
9552 mDispatcher->onWindowInfosChanged(
9553 {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0});
9554 // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus
9555 mFocusedWindow->consumeFocusEvent(false);
9556
9557 // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher
9558 // in response.
9559 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9560 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9561 .build());
9562 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9563
9564 // Key will not be sent anywhere because we have no focused window. It will remain pending.
9565 // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is.
9566 InputEventInjectionResult result =
9567 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9568 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
9569 /*allowKeyRepeat=*/false);
9570 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
9571
9572 // Finish the gesture - lift up finger and inject ACTION_UP key event
9573 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9574 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9575 .build());
9576 result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9577 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
9578 /*allowKeyRepeat=*/false);
9579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
9580 // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be
9581 // getting any events yet.
9582 navigationBar->assertNoEvents();
9583
9584 // Now touch "Another window". This touch is going to a different application than the one we
9585 // are waiting for (which is 'mApplication').
9586 // This should cause the dispatcher to drop the pending focus-dispatched events (like the key
9587 // trying to be injected) and to continue processing the rest of the events in the original
9588 // order.
9589 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9590 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
9591 .build());
9592 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP));
9593 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE));
9594 appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9595
9596 appWindow->assertNoEvents();
9597 navigationBar->assertNoEvents();
9598}
9599
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009600// These tests ensure we cannot send touch events to a window that's positioned behind a window
9601// that has feature NO_INPUT_CHANNEL.
9602// Layout:
9603// Top (closest to user)
9604// mNoInputWindow (above all windows)
9605// mBottomWindow
9606// Bottom (furthest from user)
9607class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
9608 virtual void SetUp() override {
9609 InputDispatcherTest::SetUp();
9610
9611 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009612 mNoInputWindow =
9613 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
9614 "Window without input channel", ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00009615 /*createInputChannel=*/false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009616 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009617 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
9618 // It's perfectly valid for this window to not have an associated input channel
9619
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009620 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
9621 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009622 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
9623
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009624 mDispatcher->onWindowInfosChanged(
9625 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009626 }
9627
9628protected:
9629 std::shared_ptr<FakeApplicationHandle> mApplication;
9630 sp<FakeWindowHandle> mNoInputWindow;
9631 sp<FakeWindowHandle> mBottomWindow;
9632};
9633
9634TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
9635 PointF touchedPoint = {10, 10};
9636
Prabir Pradhan678438e2023-04-13 19:32:51 +00009637 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9638 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9639 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009640
9641 mNoInputWindow->assertNoEvents();
9642 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
9643 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
9644 // and therefore should prevent mBottomWindow from receiving touches
9645 mBottomWindow->assertNoEvents();
9646}
9647
9648/**
9649 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
9650 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
9651 */
9652TEST_F(InputDispatcherMultiWindowOcclusionTests,
9653 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009654 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
9655 "Window with input channel and NO_INPUT_CHANNEL",
9656 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009657
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009658 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009659 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009660 mDispatcher->onWindowInfosChanged(
9661 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009662
9663 PointF touchedPoint = {10, 10};
9664
Prabir Pradhan678438e2023-04-13 19:32:51 +00009665 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9666 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9667 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009668
9669 mNoInputWindow->assertNoEvents();
9670 mBottomWindow->assertNoEvents();
9671}
9672
Vishnu Nair958da932020-08-21 17:12:37 -07009673class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
9674protected:
9675 std::shared_ptr<FakeApplicationHandle> mApp;
9676 sp<FakeWindowHandle> mWindow;
9677 sp<FakeWindowHandle> mMirror;
9678
9679 virtual void SetUp() override {
9680 InputDispatcherTest::SetUp();
9681 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009682 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00009683 mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07009684 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
9685 mWindow->setFocusable(true);
9686 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009687 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009688 }
9689};
9690
9691TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
9692 // Request focus on a mirrored window
9693 setFocusedWindow(mMirror);
9694
9695 // window gets focused
9696 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009697 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009698 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009699 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
9700}
9701
9702// A focused & mirrored window remains focused only if the window and its mirror are both
9703// focusable.
9704TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
9705 setFocusedWindow(mMirror);
9706
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009707 // window gets focused because it is above the mirror
Vishnu Nair958da932020-08-21 17:12:37 -07009708 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009710 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009711 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009713 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009714 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9715
9716 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009717 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009718
9719 // window loses focus since one of the windows associated with the token in not focusable
9720 mWindow->consumeFocusEvent(false);
9721
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009722 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009723 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009724 mWindow->assertNoEvents();
9725}
9726
9727// A focused & mirrored window remains focused until the window and its mirror both become
9728// invisible.
9729TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
9730 setFocusedWindow(mMirror);
9731
9732 // window gets focused
9733 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009735 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009736 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009737 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009738 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009739 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9740
9741 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009742 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009743
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009744 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009745 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009746 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009747 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009748 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009749 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9750
9751 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009752 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009753
9754 // window loses focus only after all windows associated with the token become invisible.
9755 mWindow->consumeFocusEvent(false);
9756
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009757 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009758 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009759 mWindow->assertNoEvents();
9760}
9761
9762// A focused & mirrored window remains focused until both windows are removed.
9763TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
9764 setFocusedWindow(mMirror);
9765
9766 // window gets focused
9767 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009769 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009770 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009771 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009772 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009773 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9774
9775 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009776 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009777
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009778 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009779 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009780 mMirror->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009781 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009782 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009783 mMirror->consumeKeyUp(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07009784
9785 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009786 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009787 mWindow->consumeFocusEvent(false);
9788
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009789 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009790 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009791 mWindow->assertNoEvents();
9792}
9793
9794// Focus request can be pending until one window becomes visible.
9795TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
9796 // Request focus on an invisible mirror.
9797 mWindow->setVisible(false);
9798 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009799 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009800 setFocusedWindow(mMirror);
9801
9802 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009803 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009804 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9805 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07009806
9807 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009808 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009809
9810 // window gets focused
9811 mWindow->consumeFocusEvent(true);
9812 // window gets the pending key event
9813 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9814}
Prabir Pradhan99987712020-11-10 18:43:05 -08009815
9816class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
9817protected:
9818 std::shared_ptr<FakeApplicationHandle> mApp;
9819 sp<FakeWindowHandle> mWindow;
9820 sp<FakeWindowHandle> mSecondWindow;
9821
9822 void SetUp() override {
9823 InputDispatcherTest::SetUp();
9824 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009825 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08009826 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009827 mSecondWindow =
9828 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08009829 mSecondWindow->setFocusable(true);
9830
9831 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009832 mDispatcher->onWindowInfosChanged(
9833 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -08009834
9835 setFocusedWindow(mWindow);
9836 mWindow->consumeFocusEvent(true);
9837 }
9838
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009839 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009840 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08009841 }
9842
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009843 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
9844 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08009845 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009846 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
9847 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08009848 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009849 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08009850 }
9851};
9852
9853TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
9854 // Ensure that capture cannot be obtained for unfocused windows.
9855 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
9856 mFakePolicy->assertSetPointerCaptureNotCalled();
9857 mSecondWindow->assertNoEvents();
9858
9859 // Ensure that capture can be enabled from the focus window.
9860 requestAndVerifyPointerCapture(mWindow, true);
9861
9862 // Ensure that capture cannot be disabled from a window that does not have capture.
9863 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
9864 mFakePolicy->assertSetPointerCaptureNotCalled();
9865
9866 // Ensure that capture can be disabled from the window with capture.
9867 requestAndVerifyPointerCapture(mWindow, false);
9868}
9869
9870TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009871 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08009872
9873 setFocusedWindow(mSecondWindow);
9874
9875 // Ensure that the capture disabled event was sent first.
9876 mWindow->consumeCaptureEvent(false);
9877 mWindow->consumeFocusEvent(false);
9878 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009879 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08009880
9881 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009882 notifyPointerCaptureChanged({});
9883 notifyPointerCaptureChanged(request);
9884 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08009885 mWindow->assertNoEvents();
9886 mSecondWindow->assertNoEvents();
9887 mFakePolicy->assertSetPointerCaptureNotCalled();
9888}
9889
9890TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009891 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08009892
9893 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009894 notifyPointerCaptureChanged({});
9895 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08009896
9897 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009898 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08009899 mWindow->consumeCaptureEvent(false);
9900 mWindow->assertNoEvents();
9901}
9902
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009903TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
9904 requestAndVerifyPointerCapture(mWindow, true);
9905
9906 // The first window loses focus.
9907 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009908 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009909 mWindow->consumeCaptureEvent(false);
9910
9911 // Request Pointer Capture from the second window before the notification from InputReader
9912 // arrives.
9913 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009914 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009915
9916 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009917 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009918
9919 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009920 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009921
9922 mSecondWindow->consumeFocusEvent(true);
9923 mSecondWindow->consumeCaptureEvent(true);
9924}
9925
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009926TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
9927 // App repeatedly enables and disables capture.
9928 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9929 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9930 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
9931 mFakePolicy->assertSetPointerCaptureCalled(false);
9932 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9933 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9934
9935 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
9936 // first request is now stale, this should do nothing.
9937 notifyPointerCaptureChanged(firstRequest);
9938 mWindow->assertNoEvents();
9939
9940 // InputReader notifies that the second request was enabled.
9941 notifyPointerCaptureChanged(secondRequest);
9942 mWindow->consumeCaptureEvent(true);
9943}
9944
Prabir Pradhan7092e262022-05-03 16:51:09 +00009945TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
9946 requestAndVerifyPointerCapture(mWindow, true);
9947
9948 // App toggles pointer capture off and on.
9949 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
9950 mFakePolicy->assertSetPointerCaptureCalled(false);
9951
9952 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9953 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9954
9955 // InputReader notifies that the latest "enable" request was processed, while skipping over the
9956 // preceding "disable" request.
9957 notifyPointerCaptureChanged(enableRequest);
9958
9959 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
9960 // any notifications.
9961 mWindow->assertNoEvents();
9962}
9963
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009964/**
9965 * One window. Hover mouse in the window, and then start capture. Make sure that the relative
9966 * mouse movements don't affect the previous mouse hovering state.
9967 * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no
9968 * HOVER_MOVE events).
9969 */
9970TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) {
9971 // Mouse hover on the window
9972 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
9973 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
9974 .build());
9975 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
9976 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
9977 .build());
9978
9979 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER)));
9980 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE)));
9981
9982 // Start pointer capture
9983 requestAndVerifyPointerCapture(mWindow, true);
9984
9985 // Send some relative mouse movements and receive them in the window.
9986 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE)
9987 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11))
9988 .build());
9989 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11),
9990 WithSource(AINPUT_SOURCE_MOUSE_RELATIVE)));
9991
9992 // Stop pointer capture
9993 requestAndVerifyPointerCapture(mWindow, false);
9994
9995 // Continue hovering on the window
9996 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
9997 .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115))
9998 .build());
9999 mWindow->consumeMotionEvent(
10000 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
10001
10002 mWindow->assertNoEvents();
10003}
10004
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010005class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
10006protected:
10007 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +000010008
10009 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
10010 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
10011
10012 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
10013 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
10014
10015 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
10016 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
10017 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
10018 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
10019 MAXIMUM_OBSCURING_OPACITY);
10020
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010021 static constexpr gui::Uid TOUCHED_APP_UID{10001};
10022 static constexpr gui::Uid APP_B_UID{10002};
10023 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010024
10025 sp<FakeWindowHandle> mTouchWindow;
10026
10027 virtual void SetUp() override {
10028 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010029 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010030 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
10031 }
10032
10033 virtual void TearDown() override {
10034 InputDispatcherTest::TearDown();
10035 mTouchWindow.clear();
10036 }
10037
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010038 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -050010039 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010040 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010041 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010042 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010043 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010044 return window;
10045 }
10046
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010047 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010048 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
10049 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010050 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010051 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010052 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010053 return window;
10054 }
10055
10056 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000010057 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10058 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10059 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010060 }
10061};
10062
10063TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010064 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010065 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010066 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010067
10068 touch();
10069
10070 mTouchWindow->assertNoEvents();
10071}
10072
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010073TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +000010074 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
10075 const sp<FakeWindowHandle>& w =
10076 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010077 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010078
10079 touch();
10080
10081 mTouchWindow->assertNoEvents();
10082}
10083
10084TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010085 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
10086 const sp<FakeWindowHandle>& w =
10087 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010088 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010089
10090 touch();
10091
10092 w->assertNoEvents();
10093}
10094
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010095TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010096 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010097 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010098
10099 touch();
10100
10101 mTouchWindow->consumeAnyMotionDown();
10102}
10103
10104TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010105 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010106 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010107 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010108 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010109
10110 touch({PointF{100, 100}});
10111
10112 mTouchWindow->consumeAnyMotionDown();
10113}
10114
10115TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010116 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010117 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010118 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010119
10120 touch();
10121
10122 mTouchWindow->consumeAnyMotionDown();
10123}
10124
10125TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
10126 const sp<FakeWindowHandle>& w =
10127 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010128 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010129
10130 touch();
10131
10132 mTouchWindow->consumeAnyMotionDown();
10133}
10134
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010135TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
10136 const sp<FakeWindowHandle>& w =
10137 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010138 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010139
10140 touch();
10141
10142 w->assertNoEvents();
10143}
10144
10145/**
10146 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
10147 * inside) while letting them pass-through. Note that even though touch passes through the occluding
10148 * window, the occluding window will still receive ACTION_OUTSIDE event.
10149 */
10150TEST_F(InputDispatcherUntrustedTouchesTest,
10151 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
10152 const sp<FakeWindowHandle>& w =
10153 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010154 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010155 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010156
10157 touch();
10158
10159 w->consumeMotionOutside();
10160}
10161
10162TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
10163 const sp<FakeWindowHandle>& w =
10164 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010165 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010166 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010167
10168 touch();
10169
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080010170 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010171}
10172
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010173TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010174 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010175 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10176 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010177 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010178
10179 touch();
10180
10181 mTouchWindow->consumeAnyMotionDown();
10182}
10183
10184TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
10185 const sp<FakeWindowHandle>& w =
10186 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10187 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010188 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010189
10190 touch();
10191
10192 mTouchWindow->consumeAnyMotionDown();
10193}
10194
10195TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010196 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010197 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10198 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010199 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010200
10201 touch();
10202
10203 mTouchWindow->assertNoEvents();
10204}
10205
10206TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
10207 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
10208 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010209 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
10210 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010211 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010212 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
10213 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010214 mDispatcher->onWindowInfosChanged(
10215 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010216
10217 touch();
10218
10219 mTouchWindow->assertNoEvents();
10220}
10221
10222TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
10223 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
10224 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010225 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
10226 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010227 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010228 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
10229 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010230 mDispatcher->onWindowInfosChanged(
10231 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010232
10233 touch();
10234
10235 mTouchWindow->consumeAnyMotionDown();
10236}
10237
10238TEST_F(InputDispatcherUntrustedTouchesTest,
10239 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
10240 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010241 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10242 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010243 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010244 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10245 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010246 mDispatcher->onWindowInfosChanged(
10247 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010248
10249 touch();
10250
10251 mTouchWindow->consumeAnyMotionDown();
10252}
10253
10254TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
10255 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010256 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10257 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010258 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010259 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10260 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010261 mDispatcher->onWindowInfosChanged(
10262 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010263
10264 touch();
10265
10266 mTouchWindow->assertNoEvents();
10267}
10268
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010269TEST_F(InputDispatcherUntrustedTouchesTest,
10270 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
10271 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010272 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10273 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010274 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010275 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10276 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010277 mDispatcher->onWindowInfosChanged(
10278 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010279
10280 touch();
10281
10282 mTouchWindow->assertNoEvents();
10283}
10284
10285TEST_F(InputDispatcherUntrustedTouchesTest,
10286 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
10287 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010288 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10289 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010290 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010291 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10292 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010293 mDispatcher->onWindowInfosChanged(
10294 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010295
10296 touch();
10297
10298 mTouchWindow->consumeAnyMotionDown();
10299}
10300
10301TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
10302 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010303 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10304 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010305 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010306
10307 touch();
10308
10309 mTouchWindow->consumeAnyMotionDown();
10310}
10311
10312TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
10313 const sp<FakeWindowHandle>& w =
10314 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010315 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010316
10317 touch();
10318
10319 mTouchWindow->consumeAnyMotionDown();
10320}
10321
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010322TEST_F(InputDispatcherUntrustedTouchesTest,
10323 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
10324 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
10325 const sp<FakeWindowHandle>& w =
10326 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010327 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010328
10329 touch();
10330
10331 mTouchWindow->assertNoEvents();
10332}
10333
10334TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
10335 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
10336 const sp<FakeWindowHandle>& w =
10337 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010338 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010339
10340 touch();
10341
10342 mTouchWindow->consumeAnyMotionDown();
10343}
10344
10345TEST_F(InputDispatcherUntrustedTouchesTest,
10346 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
10347 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
10348 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010349 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10350 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010351 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010352
10353 touch();
10354
10355 mTouchWindow->consumeAnyMotionDown();
10356}
10357
10358TEST_F(InputDispatcherUntrustedTouchesTest,
10359 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
10360 const sp<FakeWindowHandle>& w1 =
10361 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
10362 OPACITY_BELOW_THRESHOLD);
10363 const sp<FakeWindowHandle>& w2 =
10364 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10365 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010366 mDispatcher->onWindowInfosChanged(
10367 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010368
10369 touch();
10370
10371 mTouchWindow->assertNoEvents();
10372}
10373
10374/**
10375 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
10376 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
10377 * (which alone would result in allowing touches) does not affect the blocking behavior.
10378 */
10379TEST_F(InputDispatcherUntrustedTouchesTest,
10380 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
10381 const sp<FakeWindowHandle>& wB =
10382 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
10383 OPACITY_BELOW_THRESHOLD);
10384 const sp<FakeWindowHandle>& wC =
10385 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10386 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010387 mDispatcher->onWindowInfosChanged(
10388 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010389
10390 touch();
10391
10392 mTouchWindow->assertNoEvents();
10393}
10394
10395/**
10396 * This test is testing that a window from a different UID but with same application token doesn't
10397 * block the touch. Apps can share the application token for close UI collaboration for example.
10398 */
10399TEST_F(InputDispatcherUntrustedTouchesTest,
10400 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
10401 const sp<FakeWindowHandle>& w =
10402 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
10403 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010404 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010405
10406 touch();
10407
10408 mTouchWindow->consumeAnyMotionDown();
10409}
10410
arthurhungb89ccb02020-12-30 16:19:01 +080010411class InputDispatcherDragTests : public InputDispatcherTest {
10412protected:
10413 std::shared_ptr<FakeApplicationHandle> mApp;
10414 sp<FakeWindowHandle> mWindow;
10415 sp<FakeWindowHandle> mSecondWindow;
10416 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010417 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010418 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
10419 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +080010420
10421 void SetUp() override {
10422 InputDispatcherTest::SetUp();
10423 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010424 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080010425 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080010426
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010427 mSecondWindow =
10428 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080010429 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080010430
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010431 mSpyWindow =
10432 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010433 mSpyWindow->setSpy(true);
10434 mSpyWindow->setTrustedOverlay(true);
10435 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
10436
arthurhungb89ccb02020-12-30 16:19:01 +080010437 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010438 mDispatcher->onWindowInfosChanged(
10439 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
10440 {},
10441 0,
10442 0});
arthurhungb89ccb02020-12-30 16:19:01 +080010443 }
10444
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010445 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
10446 switch (fromSource) {
10447 case AINPUT_SOURCE_TOUCHSCREEN:
10448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010449 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010450 ADISPLAY_ID_DEFAULT, {50, 50}))
10451 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10452 break;
10453 case AINPUT_SOURCE_STYLUS:
10454 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010455 injectMotionEvent(*mDispatcher,
10456 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10457 AINPUT_SOURCE_STYLUS)
10458 .buttonState(
10459 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
10460 .pointer(PointerBuilder(0, ToolType::STYLUS)
10461 .x(50)
10462 .y(50))
10463 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010464 break;
10465 case AINPUT_SOURCE_MOUSE:
10466 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010467 injectMotionEvent(*mDispatcher,
10468 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10469 AINPUT_SOURCE_MOUSE)
10470 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
10471 .pointer(PointerBuilder(MOUSE_POINTER_ID,
10472 ToolType::MOUSE)
10473 .x(50)
10474 .y(50))
10475 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010476 break;
10477 default:
10478 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
10479 }
arthurhungb89ccb02020-12-30 16:19:01 +080010480
10481 // Window should receive motion event.
10482 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010483 // Spy window should also receive motion event
10484 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000010485 }
10486
10487 // Start performing drag, we will create a drag window and transfer touch to it.
10488 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
10489 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010490 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +000010491 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010492 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +000010493 }
arthurhungb89ccb02020-12-30 16:19:01 +080010494
10495 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010496 mDragWindow =
10497 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010498 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010499 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
10500 *mWindow->getInfo(), *mSecondWindow->getInfo()},
10501 {},
10502 0,
10503 0});
arthurhungb89ccb02020-12-30 16:19:01 +080010504
10505 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +000010506 bool transferred =
10507 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +000010508 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +000010509 if (transferred) {
10510 mWindow->consumeMotionCancel();
10511 mDragWindow->consumeMotionDown();
10512 }
10513 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +080010514 }
10515};
10516
10517TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010518 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +080010519
10520 // Move on window.
10521 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010522 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010523 ADISPLAY_ID_DEFAULT, {50, 50}))
10524 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10525 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10526 mWindow->consumeDragEvent(false, 50, 50);
10527 mSecondWindow->assertNoEvents();
10528
10529 // Move to another window.
10530 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010531 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010532 ADISPLAY_ID_DEFAULT, {150, 50}))
10533 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10534 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10535 mWindow->consumeDragEvent(true, 150, 50);
10536 mSecondWindow->consumeDragEvent(false, 50, 50);
10537
10538 // Move back to original window.
10539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010540 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010541 ADISPLAY_ID_DEFAULT, {50, 50}))
10542 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10543 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10544 mWindow->consumeDragEvent(false, 50, 50);
10545 mSecondWindow->consumeDragEvent(true, -50, 50);
10546
10547 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010548 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10549 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080010550 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10551 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
10552 mWindow->assertNoEvents();
10553 mSecondWindow->assertNoEvents();
10554}
10555
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010556TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010557 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010558
10559 // No cancel event after drag start
10560 mSpyWindow->assertNoEvents();
10561
10562 const MotionEvent secondFingerDownEvent =
10563 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10564 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010565 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10566 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010567 .build();
10568 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010569 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010570 InputEventInjectionSync::WAIT_FOR_RESULT))
10571 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10572
10573 // Receives cancel for first pointer after next pointer down
10574 mSpyWindow->consumeMotionCancel();
10575 mSpyWindow->consumeMotionDown();
10576
10577 mSpyWindow->assertNoEvents();
10578}
10579
arthurhungf452d0b2021-01-06 00:19:52 +080010580TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010581 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +080010582
10583 // Move on window.
10584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010585 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +080010586 ADISPLAY_ID_DEFAULT, {50, 50}))
10587 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10588 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10589 mWindow->consumeDragEvent(false, 50, 50);
10590 mSecondWindow->assertNoEvents();
10591
10592 // Move to another window.
10593 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010594 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +080010595 ADISPLAY_ID_DEFAULT, {150, 50}))
10596 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10597 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10598 mWindow->consumeDragEvent(true, 150, 50);
10599 mSecondWindow->consumeDragEvent(false, 50, 50);
10600
10601 // drop to another window.
10602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010603 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +080010604 {150, 50}))
10605 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10606 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010607 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +080010608 mWindow->assertNoEvents();
10609 mSecondWindow->assertNoEvents();
10610}
10611
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010612TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) {
10613 startDrag();
10614
10615 // No cancel event after drag start
10616 mSpyWindow->assertNoEvents();
10617
10618 const MotionEvent secondFingerDownEvent =
10619 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10620 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
10621 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10622 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
10623 .build();
10624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10625 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
10626 InputEventInjectionSync::WAIT_FOR_RESULT))
10627 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10628
10629 // Receives cancel for first pointer after next pointer down
10630 mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -080010631 mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1})));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010632 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
10633
10634 mSpyWindow->assertNoEvents();
10635
10636 // Spy window calls pilfer pointers
10637 EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken()));
10638 mDragWindow->assertNoEvents();
10639
10640 const MotionEvent firstFingerMoveEvent =
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080010641 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010642 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
10643 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60))
10644 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
10645 .build();
10646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080010647 injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010648 InputEventInjectionSync::WAIT_FOR_RESULT))
10649 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10650
10651 // Drag window should still receive the new event
10652 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
10653 mDragWindow->assertNoEvents();
10654}
10655
arthurhung6d4bed92021-03-17 11:59:33 +080010656TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010657 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +080010658
10659 // Move on window and keep button pressed.
10660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010661 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010662 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10663 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010664 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010665 .build()))
10666 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10667 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10668 mWindow->consumeDragEvent(false, 50, 50);
10669 mSecondWindow->assertNoEvents();
10670
10671 // Move to another window and release button, expect to drop item.
10672 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010673 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010674 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10675 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010676 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010677 .build()))
10678 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10679 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10680 mWindow->assertNoEvents();
10681 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010682 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +080010683
10684 // nothing to the window.
10685 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010686 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010687 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
10688 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010689 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010690 .build()))
10691 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10692 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
10693 mWindow->assertNoEvents();
10694 mSecondWindow->assertNoEvents();
10695}
10696
Arthur Hung54745652022-04-20 07:17:41 +000010697TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010698 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +080010699
10700 // Set second window invisible.
10701 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010702 mDispatcher->onWindowInfosChanged(
10703 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +080010704
10705 // Move on window.
10706 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010707 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010708 ADISPLAY_ID_DEFAULT, {50, 50}))
10709 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10710 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10711 mWindow->consumeDragEvent(false, 50, 50);
10712 mSecondWindow->assertNoEvents();
10713
10714 // Move to another window.
10715 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010716 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010717 ADISPLAY_ID_DEFAULT, {150, 50}))
10718 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10719 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10720 mWindow->consumeDragEvent(true, 150, 50);
10721 mSecondWindow->assertNoEvents();
10722
10723 // drop to another window.
10724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010725 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010726 {150, 50}))
10727 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10728 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010729 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +080010730 mWindow->assertNoEvents();
10731 mSecondWindow->assertNoEvents();
10732}
10733
Arthur Hung54745652022-04-20 07:17:41 +000010734TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010735 // Ensure window could track pointerIds if it didn't support split touch.
10736 mWindow->setPreventSplitting(true);
10737
Arthur Hung54745652022-04-20 07:17:41 +000010738 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010739 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +000010740 {50, 50}))
10741 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10742 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10743
10744 const MotionEvent secondFingerDownEvent =
10745 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10746 .displayId(ADISPLAY_ID_DEFAULT)
10747 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010748 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10749 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010750 .build();
10751 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010752 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010753 InputEventInjectionSync::WAIT_FOR_RESULT))
10754 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000010755 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +000010756
10757 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010758 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000010759}
10760
10761TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
10762 // First down on second window.
10763 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010764 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +000010765 {150, 50}))
10766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10767
10768 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10769
10770 // Second down on first window.
10771 const MotionEvent secondFingerDownEvent =
10772 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10773 .displayId(ADISPLAY_ID_DEFAULT)
10774 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010775 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10776 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010777 .build();
10778 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010779 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010780 InputEventInjectionSync::WAIT_FOR_RESULT))
10781 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10782 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10783
10784 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010785 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000010786
10787 // Move on window.
10788 const MotionEvent secondFingerMoveEvent =
10789 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10790 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010791 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10792 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010793 .build();
10794 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010795 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010796 InputEventInjectionSync::WAIT_FOR_RESULT));
10797 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10798 mWindow->consumeDragEvent(false, 50, 50);
10799 mSecondWindow->consumeMotionMove();
10800
10801 // Release the drag pointer should perform drop.
10802 const MotionEvent secondFingerUpEvent =
10803 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
10804 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010805 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10806 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010807 .build();
10808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010809 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010810 InputEventInjectionSync::WAIT_FOR_RESULT));
10811 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010812 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +000010813 mWindow->assertNoEvents();
10814 mSecondWindow->consumeMotionMove();
10815}
10816
Arthur Hung3915c1f2022-05-31 07:17:17 +000010817TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010818 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +000010819
10820 // Update window of second display.
10821 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010822 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010823 mDispatcher->onWindowInfosChanged(
10824 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
10825 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
10826 {},
10827 0,
10828 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000010829
10830 // Let second display has a touch state.
10831 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010832 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010833 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10834 AINPUT_SOURCE_TOUCHSCREEN)
10835 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010836 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +000010837 .build()));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +000010838 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +000010839 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010840 mDispatcher->onWindowInfosChanged(
10841 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
10842 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
10843 {},
10844 0,
10845 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000010846
10847 // Move on window.
10848 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010849 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010850 ADISPLAY_ID_DEFAULT, {50, 50}))
10851 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10852 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10853 mWindow->consumeDragEvent(false, 50, 50);
10854 mSecondWindow->assertNoEvents();
10855
10856 // Move to another window.
10857 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010858 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010859 ADISPLAY_ID_DEFAULT, {150, 50}))
10860 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10861 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10862 mWindow->consumeDragEvent(true, 150, 50);
10863 mSecondWindow->consumeDragEvent(false, 50, 50);
10864
10865 // drop to another window.
10866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010867 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010868 {150, 50}))
10869 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10870 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010871 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +000010872 mWindow->assertNoEvents();
10873 mSecondWindow->assertNoEvents();
10874}
10875
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010876TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
10877 startDrag(true, AINPUT_SOURCE_MOUSE);
10878 // Move on window.
10879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010880 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010881 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
10882 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010883 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010884 .x(50)
10885 .y(50))
10886 .build()))
10887 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10888 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10889 mWindow->consumeDragEvent(false, 50, 50);
10890 mSecondWindow->assertNoEvents();
10891
10892 // Move to another window.
10893 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010894 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010895 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
10896 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010897 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010898 .x(150)
10899 .y(50))
10900 .build()))
10901 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10902 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10903 mWindow->consumeDragEvent(true, 150, 50);
10904 mSecondWindow->consumeDragEvent(false, 50, 50);
10905
10906 // drop to another window.
10907 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010908 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010909 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
10910 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010911 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010912 .x(150)
10913 .y(50))
10914 .build()))
10915 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10916 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010917 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010918 mWindow->assertNoEvents();
10919 mSecondWindow->assertNoEvents();
10920}
10921
Linnan Li5af92f92023-07-14 14:36:22 +080010922/**
10923 * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag
10924 * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash.
10925 */
10926TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) {
10927 // Down on second window
10928 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10929 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10930 {150, 50}))
10931 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10932
10933 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown());
10934 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown());
10935
10936 // Down on first window
10937 const MotionEvent secondFingerDownEvent =
10938 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10939 .displayId(ADISPLAY_ID_DEFAULT)
10940 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10941 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
10942 .build();
10943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10944 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
10945 InputEventInjectionSync::WAIT_FOR_RESULT))
10946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10947 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
10948 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove());
10949 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1));
10950
10951 // Start drag on first window
10952 ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN));
10953
10954 // Trigger cancel
10955 mDispatcher->cancelCurrentTouch();
10956 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel());
10957 ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel());
10958 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel());
10959
10960 ASSERT_TRUE(mDispatcher->waitForIdle());
10961 // The D&D finished with nullptr
10962 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
10963
10964 // Remove drag window
10965 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
10966
10967 // Inject a simple gesture, ensure dispatcher not crashed
10968 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10969 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10970 PointF{50, 50}))
10971 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10972 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
10973
10974 const MotionEvent moveEvent =
10975 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10976 .displayId(ADISPLAY_ID_DEFAULT)
10977 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10978 .build();
10979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10980 injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT,
10981 InputEventInjectionSync::WAIT_FOR_RESULT))
10982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10983 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove());
10984
10985 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10986 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10987 {50, 50}))
10988 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10989 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp());
10990}
10991
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000010992TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) {
10993 // Start hovering over the window.
10994 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10995 injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE,
10996 ADISPLAY_ID_DEFAULT, {50, 50}));
10997
10998 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
10999 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
11000
11001 ASSERT_FALSE(startDrag(/*sendDown=*/false))
11002 << "Drag and drop should not work with a hovering pointer";
11003}
11004
Vishnu Nair062a8672021-09-03 16:07:44 -070011005class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
11006
11007TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
11008 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011009 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11010 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011011 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011012 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11013 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011014 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011015 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011016 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011017
11018 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011019 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011020 window->assertNoEvents();
11021
Prabir Pradhan678438e2023-04-13 19:32:51 +000011022 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11023 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011024 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11025 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -080011026 mDispatcher->waitForIdle();
Vishnu Nair062a8672021-09-03 16:07:44 -070011027 window->assertNoEvents();
11028
11029 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011030 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011031 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011032
Prabir Pradhan678438e2023-04-13 19:32:51 +000011033 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011034 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11035
Prabir Pradhan678438e2023-04-13 19:32:51 +000011036 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11037 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011038 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11039 window->assertNoEvents();
11040}
11041
11042TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
11043 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
11044 std::make_shared<FakeApplicationHandle>();
11045 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011046 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
11047 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070011048 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011049 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011050 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070011051 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011052 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11053 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011054 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011055 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070011056 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11057 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011058 mDispatcher->onWindowInfosChanged(
11059 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011060 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011061 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011062
11063 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011064 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011065 window->assertNoEvents();
11066
Prabir Pradhan678438e2023-04-13 19:32:51 +000011067 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11068 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011069 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11070 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011071 window->assertNoEvents();
11072
11073 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011074 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011075 mDispatcher->onWindowInfosChanged(
11076 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011077
Prabir Pradhan678438e2023-04-13 19:32:51 +000011078 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011079 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11080
Prabir Pradhan678438e2023-04-13 19:32:51 +000011081 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11082 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011083 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
11084 window->assertNoEvents();
11085}
11086
11087TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
11088 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
11089 std::make_shared<FakeApplicationHandle>();
11090 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011091 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
11092 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070011093 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011094 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011095 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070011096 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011097 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11098 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011099 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011100 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070011101 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11102 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011103 mDispatcher->onWindowInfosChanged(
11104 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011105 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011106 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011107
11108 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011109 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011110 window->assertNoEvents();
11111
Prabir Pradhan678438e2023-04-13 19:32:51 +000011112 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11113 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011114 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11115 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011116 window->assertNoEvents();
11117
11118 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011119 mDispatcher->onWindowInfosChanged(
11120 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011121
Prabir Pradhan678438e2023-04-13 19:32:51 +000011122 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011123 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11124
Prabir Pradhan678438e2023-04-13 19:32:51 +000011125 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11126 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011127 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11128 window->assertNoEvents();
11129}
11130
Antonio Kantekf16f2832021-09-28 04:39:20 +000011131class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
11132protected:
11133 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +000011134 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +000011135 sp<FakeWindowHandle> mWindow;
11136 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +000011137 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +000011138
11139 void SetUp() override {
11140 InputDispatcherTest::SetUp();
11141
11142 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +000011143 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011144 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011145 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011146 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011147 mSecondWindow =
11148 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011149 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +000011150 mThirdWindow =
11151 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
11152 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
11153 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011154
11155 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011156 mDispatcher->onWindowInfosChanged(
11157 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
11158 {},
11159 0,
11160 0});
Antonio Kantek15beb512022-06-13 22:35:41 +000011161 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011162 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011163
Antonio Kantek15beb512022-06-13 22:35:41 +000011164 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +000011165 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000011166 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -070011167 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
11168 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000011169 mThirdWindow->assertNoEvents();
11170 }
11171
11172 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
11173 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000011174 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +000011175 SECOND_DISPLAY_ID)) {
11176 mWindow->assertNoEvents();
11177 mSecondWindow->assertNoEvents();
11178 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -070011179 }
Antonio Kantekf16f2832021-09-28 04:39:20 +000011180 }
11181
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011182 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +000011183 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -070011184 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
11185 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000011186 mWindow->consumeTouchModeEvent(inTouchMode);
11187 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000011188 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +000011189 }
11190};
11191
Antonio Kantek26defcf2022-02-08 01:12:27 +000011192TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080011193 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +000011194 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
11195 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011196 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011197}
11198
Antonio Kantek26defcf2022-02-08 01:12:27 +000011199TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
11200 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011201 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011202 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011203 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011204 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000011205 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -070011206 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +000011207 mWindow->assertNoEvents();
11208 mSecondWindow->assertNoEvents();
11209}
11210
11211TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
11212 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011213 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011214 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011215 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +000011216 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000011217 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011218}
11219
Antonio Kantekf16f2832021-09-28 04:39:20 +000011220TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080011221 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +000011222 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
11223 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011224 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000011225 mWindow->assertNoEvents();
11226 mSecondWindow->assertNoEvents();
11227}
11228
Antonio Kantek15beb512022-06-13 22:35:41 +000011229TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
11230 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
11231 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
11232 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011233 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +000011234 mWindow->assertNoEvents();
11235 mSecondWindow->assertNoEvents();
11236 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
11237}
11238
Antonio Kantek48710e42022-03-24 14:19:30 -070011239TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
11240 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011241 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11242 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -070011243 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11244 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
11245
11246 // Then remove focus.
11247 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011248 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -070011249
11250 // Assert that caller can switch touch mode by owning one of the last interacted window.
11251 const WindowInfo& windowInfo = *mWindow->getInfo();
11252 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
11253 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011254 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -070011255}
11256
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011257class InputDispatcherSpyWindowTest : public InputDispatcherTest {
11258public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011259 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011260 std::shared_ptr<FakeApplicationHandle> application =
11261 std::make_shared<FakeApplicationHandle>();
11262 std::string name = "Fake Spy ";
11263 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011264 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
11265 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011266 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011267 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011268 return spy;
11269 }
11270
11271 sp<FakeWindowHandle> createForeground() {
11272 std::shared_ptr<FakeApplicationHandle> application =
11273 std::make_shared<FakeApplicationHandle>();
11274 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011275 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
11276 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011277 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011278 return window;
11279 }
11280
11281private:
11282 int mSpyCount{0};
11283};
11284
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011285using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011286/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011287 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
11288 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011289TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -070011290 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011291 ScopedSilentDeath _silentDeath;
11292
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011293 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011294 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011295 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011296 ".* not a trusted overlay");
11297}
11298
11299/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011300 * Input injection into a display with a spy window but no foreground windows should succeed.
11301 */
11302TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011303 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011304 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011305
11306 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011307 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011308 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11309 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11310}
11311
11312/**
11313 * Verify the order in which different input windows receive events. The touched foreground window
11314 * (if there is one) should always receive the event first. When there are multiple spy windows, the
11315 * spy windows will receive the event according to their Z-order, where the top-most spy window will
11316 * receive events before ones belows it.
11317 *
11318 * Here, we set up a scenario with four windows in the following Z order from the top:
11319 * spy1, spy2, window, spy3.
11320 * We then inject an event and verify that the foreground "window" receives it first, followed by
11321 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
11322 * window.
11323 */
11324TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
11325 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011326 auto spy1 = createSpy();
11327 auto spy2 = createSpy();
11328 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011329 mDispatcher->onWindowInfosChanged(
11330 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011331 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
11332 const size_t numChannels = channels.size();
11333
Michael Wright8e9a8562022-02-09 13:44:29 +000011334 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011335 if (!epollFd.ok()) {
11336 FAIL() << "Failed to create epoll fd";
11337 }
11338
11339 for (size_t i = 0; i < numChannels; i++) {
11340 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
11341 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
11342 FAIL() << "Failed to add fd to epoll";
11343 }
11344 }
11345
11346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011347 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011348 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11349
11350 std::vector<size_t> eventOrder;
11351 std::vector<struct epoll_event> events(numChannels);
11352 for (;;) {
11353 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
11354 (100ms).count());
11355 if (nFds < 0) {
11356 FAIL() << "Failed to call epoll_wait";
11357 }
11358 if (nFds == 0) {
11359 break; // epoll_wait timed out
11360 }
11361 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -070011362 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -070011363 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011364 channels[i]->consumeMotionDown();
11365 }
11366 }
11367
11368 // Verify the order in which the events were received.
11369 EXPECT_EQ(3u, eventOrder.size());
11370 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
11371 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
11372 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
11373}
11374
11375/**
11376 * A spy window using the NOT_TOUCHABLE flag does not receive events.
11377 */
11378TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
11379 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011380 auto spy = createSpy();
11381 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011382 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011383
11384 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011385 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011386 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11387 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11388 spy->assertNoEvents();
11389}
11390
11391/**
11392 * A spy window will only receive gestures that originate within its touchable region. Gestures that
11393 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
11394 * to the window.
11395 */
11396TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
11397 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011398 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011399 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011400 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011401
11402 // Inject an event outside the spy window's touchable region.
11403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011404 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011405 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11406 window->consumeMotionDown();
11407 spy->assertNoEvents();
11408 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011409 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011410 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11411 window->consumeMotionUp();
11412 spy->assertNoEvents();
11413
11414 // Inject an event inside the spy window's touchable region.
11415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011416 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011417 {5, 10}))
11418 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11419 window->consumeMotionDown();
11420 spy->consumeMotionDown();
11421}
11422
11423/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011424 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011425 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011426 */
11427TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
11428 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011429 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011430 auto spy = createSpy();
11431 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011432 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011433 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011434 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011435
11436 // Inject an event outside the spy window's frame and touchable region.
11437 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011438 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011439 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011440 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11441 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011442 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011443}
11444
11445/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011446 * Even when a spy window spans over multiple foreground windows, the spy should receive all
11447 * pointers that are down within its bounds.
11448 */
11449TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
11450 auto windowLeft = createForeground();
11451 windowLeft->setFrame({0, 0, 100, 200});
11452 auto windowRight = createForeground();
11453 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011454 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011455 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011456 mDispatcher->onWindowInfosChanged(
11457 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011458
11459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011460 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011461 {50, 50}))
11462 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11463 windowLeft->consumeMotionDown();
11464 spy->consumeMotionDown();
11465
11466 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011467 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011468 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011469 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11470 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011471 .build();
11472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011473 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011474 InputEventInjectionSync::WAIT_FOR_RESULT))
11475 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11476 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +000011477 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011478}
11479
11480/**
11481 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
11482 * the spy should receive the second pointer with ACTION_DOWN.
11483 */
11484TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
11485 auto window = createForeground();
11486 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011487 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011488 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011489 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011490
11491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011492 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011493 {50, 50}))
11494 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11495 window->consumeMotionDown();
11496 spyRight->assertNoEvents();
11497
11498 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011499 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011500 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011501 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11502 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011503 .build();
11504 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011505 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011506 InputEventInjectionSync::WAIT_FOR_RESULT))
11507 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000011508 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011509 spyRight->consumeMotionDown();
11510}
11511
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011512/**
11513 * The spy window should not be able to affect whether or not touches are split. Only the foreground
11514 * windows should be allowed to control split touch.
11515 */
11516TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080011517 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011518 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011519 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080011520 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011521
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011522 auto window = createForeground();
11523 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011524
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011525 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011526
11527 // First finger down, no window touched.
11528 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011529 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011530 {100, 200}))
11531 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11532 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11533 window->assertNoEvents();
11534
11535 // Second finger down on window, the window should receive touch down.
11536 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011537 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011538 .displayId(ADISPLAY_ID_DEFAULT)
11539 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011540 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11541 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011542 .build();
11543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011544 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011545 InputEventInjectionSync::WAIT_FOR_RESULT))
11546 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11547
11548 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +000011549 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011550}
11551
11552/**
11553 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
11554 * do not receive key events.
11555 */
11556TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011557 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011558 spy->setFocusable(false);
11559
11560 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011561 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011562 setFocusedWindow(window);
11563 window->consumeFocusEvent(true);
11564
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011566 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11567 window->consumeKeyDown(ADISPLAY_ID_NONE);
11568
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011570 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11571 window->consumeKeyUp(ADISPLAY_ID_NONE);
11572
11573 spy->assertNoEvents();
11574}
11575
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011576using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
11577
11578/**
11579 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
11580 * are currently sent to any other windows - including other spy windows - will also be cancelled.
11581 */
11582TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
11583 auto window = createForeground();
11584 auto spy1 = createSpy();
11585 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011586 mDispatcher->onWindowInfosChanged(
11587 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011588
11589 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011590 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011591 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11592 window->consumeMotionDown();
11593 spy1->consumeMotionDown();
11594 spy2->consumeMotionDown();
11595
11596 // Pilfer pointers from the second spy window.
11597 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
11598 spy2->assertNoEvents();
11599 spy1->consumeMotionCancel();
11600 window->consumeMotionCancel();
11601
11602 // The rest of the gesture should only be sent to the second spy window.
11603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011604 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011605 ADISPLAY_ID_DEFAULT))
11606 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11607 spy2->consumeMotionMove();
11608 spy1->assertNoEvents();
11609 window->assertNoEvents();
11610}
11611
11612/**
11613 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
11614 * in the middle of the gesture.
11615 */
11616TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
11617 auto window = createForeground();
11618 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011619 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011620
11621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011622 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011623 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11624 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11625 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11626
11627 window->releaseChannel();
11628
11629 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11630
11631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011632 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011633 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11634 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
11635}
11636
11637/**
11638 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
11639 * the spy, but not to any other windows.
11640 */
11641TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
11642 auto spy = createSpy();
11643 auto window = createForeground();
11644
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011645 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011646
11647 // First finger down on the window and the spy.
11648 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011649 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011650 {100, 200}))
11651 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11652 spy->consumeMotionDown();
11653 window->consumeMotionDown();
11654
11655 // Spy window pilfers the pointers.
11656 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11657 window->consumeMotionCancel();
11658
11659 // Second finger down on the window and spy, but the window should not receive the pointer down.
11660 const MotionEvent secondFingerDownEvent =
11661 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11662 .displayId(ADISPLAY_ID_DEFAULT)
11663 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011664 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11665 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011666 .build();
11667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011668 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011669 InputEventInjectionSync::WAIT_FOR_RESULT))
11670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11671
Harry Cutts33476232023-01-30 19:57:29 +000011672 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011673
11674 // Third finger goes down outside all windows, so injection should fail.
11675 const MotionEvent thirdFingerDownEvent =
11676 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11677 .displayId(ADISPLAY_ID_DEFAULT)
11678 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011679 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11680 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
11681 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011682 .build();
11683 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011684 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011685 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080011686 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011687
11688 spy->assertNoEvents();
11689 window->assertNoEvents();
11690}
11691
11692/**
11693 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
11694 */
11695TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
11696 auto spy = createSpy();
11697 spy->setFrame(Rect(0, 0, 100, 100));
11698 auto window = createForeground();
11699 window->setFrame(Rect(0, 0, 200, 200));
11700
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011701 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011702
11703 // First finger down on the window only
11704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011705 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011706 {150, 150}))
11707 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11708 window->consumeMotionDown();
11709
11710 // Second finger down on the spy and window
11711 const MotionEvent secondFingerDownEvent =
11712 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11713 .displayId(ADISPLAY_ID_DEFAULT)
11714 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011715 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
11716 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011717 .build();
11718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011719 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011720 InputEventInjectionSync::WAIT_FOR_RESULT))
11721 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11722 spy->consumeMotionDown();
11723 window->consumeMotionPointerDown(1);
11724
11725 // Third finger down on the spy and window
11726 const MotionEvent thirdFingerDownEvent =
11727 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11728 .displayId(ADISPLAY_ID_DEFAULT)
11729 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011730 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
11731 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
11732 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011733 .build();
11734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011735 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011736 InputEventInjectionSync::WAIT_FOR_RESULT))
11737 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11738 spy->consumeMotionPointerDown(1);
11739 window->consumeMotionPointerDown(2);
11740
11741 // Spy window pilfers the pointers.
11742 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Harry Cutts101ee9b2023-07-06 18:04:14 +000011743 window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
11744 window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011745
11746 spy->assertNoEvents();
11747 window->assertNoEvents();
11748}
11749
11750/**
11751 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
11752 * other windows should be canceled. If this results in the cancellation of all pointers for some
11753 * window, then that window should receive ACTION_CANCEL.
11754 */
11755TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
11756 auto spy = createSpy();
11757 spy->setFrame(Rect(0, 0, 100, 100));
11758 auto window = createForeground();
11759 window->setFrame(Rect(0, 0, 200, 200));
11760
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011761 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011762
11763 // First finger down on both spy and window
11764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011765 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011766 {10, 10}))
11767 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11768 window->consumeMotionDown();
11769 spy->consumeMotionDown();
11770
11771 // Second finger down on the spy and window
11772 const MotionEvent secondFingerDownEvent =
11773 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11774 .displayId(ADISPLAY_ID_DEFAULT)
11775 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011776 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
11777 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011778 .build();
11779 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011780 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011781 InputEventInjectionSync::WAIT_FOR_RESULT))
11782 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11783 spy->consumeMotionPointerDown(1);
11784 window->consumeMotionPointerDown(1);
11785
11786 // Spy window pilfers the pointers.
11787 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11788 window->consumeMotionCancel();
11789
11790 spy->assertNoEvents();
11791 window->assertNoEvents();
11792}
11793
11794/**
11795 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
11796 * be sent to other windows
11797 */
11798TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
11799 auto spy = createSpy();
11800 spy->setFrame(Rect(0, 0, 100, 100));
11801 auto window = createForeground();
11802 window->setFrame(Rect(0, 0, 200, 200));
11803
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011804 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011805
11806 // First finger down on both window and spy
11807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011808 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011809 {10, 10}))
11810 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11811 window->consumeMotionDown();
11812 spy->consumeMotionDown();
11813
11814 // Spy window pilfers the pointers.
11815 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11816 window->consumeMotionCancel();
11817
11818 // Second finger down on the window only
11819 const MotionEvent secondFingerDownEvent =
11820 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11821 .displayId(ADISPLAY_ID_DEFAULT)
11822 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011823 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
11824 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011825 .build();
11826 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011827 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011828 InputEventInjectionSync::WAIT_FOR_RESULT))
11829 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11830 window->consumeMotionDown();
11831 window->assertNoEvents();
11832
11833 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
11834 spy->consumeMotionMove();
11835 spy->assertNoEvents();
11836}
11837
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011838/**
11839 * A window on the left and a window on the right. Also, a spy window that's above all of the
11840 * windows, and spanning both left and right windows.
11841 * Send simultaneous motion streams from two different devices, one to the left window, and another
11842 * to the right window.
11843 * Pilfer from spy window.
11844 * Check that the pilfering only affects the pointers that are actually being received by the spy.
11845 */
11846TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) {
11847 sp<FakeWindowHandle> spy = createSpy();
11848 spy->setFrame(Rect(0, 0, 200, 200));
11849 sp<FakeWindowHandle> leftWindow = createForeground();
11850 leftWindow->setFrame(Rect(0, 0, 100, 100));
11851
11852 sp<FakeWindowHandle> rightWindow = createForeground();
11853 rightWindow->setFrame(Rect(100, 0, 200, 100));
11854
11855 constexpr int32_t stylusDeviceId = 1;
11856 constexpr int32_t touchDeviceId = 2;
11857
11858 mDispatcher->onWindowInfosChanged(
11859 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
11860
11861 // Stylus down on left window and spy
11862 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
11863 .deviceId(stylusDeviceId)
11864 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
11865 .build());
11866 leftWindow->consumeMotionEvent(
11867 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
11868 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
11869
11870 // Finger down on right window and spy - but spy already has stylus
11871 mDispatcher->notifyMotion(
11872 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11873 .deviceId(touchDeviceId)
11874 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
11875 .build());
11876 rightWindow->consumeMotionEvent(
11877 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011878 spy->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011879
11880 // Act: pilfer from spy. Spy is currently receiving touch events.
11881 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011882 leftWindow->consumeMotionEvent(
11883 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011884 rightWindow->consumeMotionEvent(
11885 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
11886
11887 // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus
11888 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11889 .deviceId(stylusDeviceId)
11890 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
11891 .build());
11892 mDispatcher->notifyMotion(
11893 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
11894 .deviceId(touchDeviceId)
11895 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
11896 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011897 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011898
11899 spy->assertNoEvents();
11900 leftWindow->assertNoEvents();
11901 rightWindow->assertNoEvents();
11902}
11903
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000011904TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) {
11905 auto window = createForeground();
11906 auto spy = createSpy();
11907 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
11908
11909 mDispatcher->notifyMotion(
11910 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
11911 .deviceId(1)
11912 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200))
11913 .build());
11914 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
11915 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
11916
11917 // Pilfer pointers from the spy window should fail.
11918 EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken()));
11919 spy->assertNoEvents();
11920 window->assertNoEvents();
11921}
11922
Prabir Pradhand65552b2021-10-07 11:23:50 -070011923class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
11924public:
11925 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
11926 std::shared_ptr<FakeApplicationHandle> overlayApplication =
11927 std::make_shared<FakeApplicationHandle>();
11928 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011929 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
11930 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011931 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011932 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011933 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011934 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011935 overlay->setTrustedOverlay(true);
11936
11937 std::shared_ptr<FakeApplicationHandle> application =
11938 std::make_shared<FakeApplicationHandle>();
11939 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011940 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
11941 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011942 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011943 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011944
11945 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011946 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011947 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011948 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011949 return {std::move(overlay), std::move(window)};
11950 }
11951
11952 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000011953 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -070011954 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +000011955 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -070011956 }
11957
11958 void sendStylusEvent(int32_t action) {
11959 NotifyMotionArgs motionArgs =
11960 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
11961 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011962 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +000011963 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011964 }
11965};
11966
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011967using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
11968
11969TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -070011970 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011971 ScopedSilentDeath _silentDeath;
11972
Prabir Pradhand65552b2021-10-07 11:23:50 -070011973 auto [overlay, window] = setupStylusOverlayScenario();
11974 overlay->setTrustedOverlay(false);
11975 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011976 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
11977 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -070011978 ".* not a trusted overlay");
11979}
11980
11981TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
11982 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011983 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011984
11985 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
11986 overlay->consumeMotionDown();
11987 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
11988 overlay->consumeMotionUp();
11989
11990 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
11991 window->consumeMotionDown();
11992 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
11993 window->consumeMotionUp();
11994
11995 overlay->assertNoEvents();
11996 window->assertNoEvents();
11997}
11998
11999TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
12000 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012001 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012002 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070012003
12004 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
12005 overlay->consumeMotionDown();
12006 window->consumeMotionDown();
12007 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
12008 overlay->consumeMotionUp();
12009 window->consumeMotionUp();
12010
12011 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
12012 window->consumeMotionDown();
12013 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
12014 window->consumeMotionUp();
12015
12016 overlay->assertNoEvents();
12017 window->assertNoEvents();
12018}
12019
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012020/**
12021 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
12022 * The scenario is as follows:
12023 * - The stylus interceptor overlay is configured as a spy window.
12024 * - The stylus interceptor spy receives the start of a new stylus gesture.
12025 * - It pilfers pointers and then configures itself to no longer be a spy.
12026 * - The stylus interceptor continues to receive the rest of the gesture.
12027 */
12028TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
12029 auto [overlay, window] = setupStylusOverlayScenario();
12030 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012031 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012032
12033 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
12034 overlay->consumeMotionDown();
12035 window->consumeMotionDown();
12036
12037 // The interceptor pilfers the pointers.
12038 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
12039 window->consumeMotionCancel();
12040
12041 // The interceptor configures itself so that it is no longer a spy.
12042 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012043 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012044
12045 // It continues to receive the rest of the stylus gesture.
12046 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
12047 overlay->consumeMotionMove();
12048 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
12049 overlay->consumeMotionUp();
12050
12051 window->assertNoEvents();
12052}
12053
Prabir Pradhan5735a322022-04-11 17:23:34 +000012054struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012055 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012056 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +000012057 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
12058 std::unique_ptr<InputDispatcher>& mDispatcher;
12059
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012060 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +000012061 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
12062
12063 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012064 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012065 ADISPLAY_ID_DEFAULT, {100, 200},
12066 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
12067 AMOTION_EVENT_INVALID_CURSOR_POSITION},
12068 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
12069 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
12070 }
12071
12072 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012073 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012074 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +000012075 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +000012076 mPolicyFlags);
12077 }
12078
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012079 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +000012080 std::shared_ptr<FakeApplicationHandle> overlayApplication =
12081 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012082 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
12083 name, ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +000012084 window->setOwnerInfo(mPid, mUid);
12085 return window;
12086 }
12087};
12088
12089using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
12090
12091TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012092 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012093 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012094 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012095
12096 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12097 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12098 window->consumeMotionDown();
12099
12100 setFocusedWindow(window);
12101 window->consumeFocusEvent(true);
12102
12103 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12104 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
12105 window->consumeKeyDown(ADISPLAY_ID_NONE);
12106}
12107
12108TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012109 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012110 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012111 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012112
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012113 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012114 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
12115 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12116
12117 setFocusedWindow(window);
12118 window->consumeFocusEvent(true);
12119
12120 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
12121 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
12122 window->assertNoEvents();
12123}
12124
12125TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012126 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012127 auto window = owner.createWindow("Owned window");
12128 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012129 spy->setSpy(true);
12130 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012131 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012132
12133 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12134 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12135 spy->consumeMotionDown();
12136 window->consumeMotionDown();
12137}
12138
12139TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012140 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012141 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012142
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012143 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012144 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012145 randosSpy->setSpy(true);
12146 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012147 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012148
12149 // The event is targeted at owner's window, so injection should succeed, but the spy should
12150 // not receive the event.
12151 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12152 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12153 randosSpy->assertNoEvents();
12154 window->consumeMotionDown();
12155}
12156
12157TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012158 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012159 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012160
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012161 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012162 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012163 randosSpy->setSpy(true);
12164 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012165 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012166
12167 // A user that has injection permission can inject into any window.
12168 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012169 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012170 ADISPLAY_ID_DEFAULT));
12171 randosSpy->consumeMotionDown();
12172 window->consumeMotionDown();
12173
12174 setFocusedWindow(randosSpy);
12175 randosSpy->consumeFocusEvent(true);
12176
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012177 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Prabir Pradhan5735a322022-04-11 17:23:34 +000012178 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
12179 window->assertNoEvents();
12180}
12181
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012182TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012183 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012184 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012185
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012186 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012187 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012188 randosWindow->setFrame(Rect{-10, -10, -5, -5});
12189 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012190 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012191
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012192 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +000012193 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12194 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12195 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012196 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +000012197}
12198
Prabir Pradhan64f21d22023-11-28 21:19:42 +000012199using InputDispatcherPointerInWindowTest = InputDispatcherTest;
12200
12201TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) {
12202 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12203
12204 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12205 ADISPLAY_ID_DEFAULT);
12206 left->setFrame(Rect(0, 0, 100, 100));
12207 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12208 "Right Window", ADISPLAY_ID_DEFAULT);
12209 right->setFrame(Rect(100, 0, 200, 100));
12210 sp<FakeWindowHandle> spy =
12211 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
12212 spy->setFrame(Rect(0, 0, 200, 100));
12213 spy->setTrustedOverlay(true);
12214 spy->setSpy(true);
12215
12216 mDispatcher->onWindowInfosChanged(
12217 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
12218
12219 // Hover into the left window.
12220 mDispatcher->notifyMotion(
12221 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
12222 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50))
12223 .build());
12224
12225 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12226 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12227
12228 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12229 /*pointerId=*/0));
12230 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12231 /*pointerId=*/0));
12232 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12233 /*pointerId=*/0));
12234
12235 // Hover move to the right window.
12236 mDispatcher->notifyMotion(
12237 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
12238 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
12239 .build());
12240
12241 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12242 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12243 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
12244
12245 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12246 /*pointerId=*/0));
12247 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12248 /*pointerId=*/0));
12249 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12250 /*pointerId=*/0));
12251
12252 // Stop hovering.
12253 mDispatcher->notifyMotion(
12254 MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
12255 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
12256 .build());
12257
12258 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12259 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12260
12261 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12262 /*pointerId=*/0));
12263 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12264 /*pointerId=*/0));
12265 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12266 /*pointerId=*/0));
12267}
12268
12269TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) {
12270 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12271
12272 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12273 ADISPLAY_ID_DEFAULT);
12274 left->setFrame(Rect(0, 0, 100, 100));
12275 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12276 "Right Window", ADISPLAY_ID_DEFAULT);
12277 right->setFrame(Rect(100, 0, 200, 100));
12278 sp<FakeWindowHandle> spy =
12279 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
12280 spy->setFrame(Rect(0, 0, 200, 100));
12281 spy->setTrustedOverlay(true);
12282 spy->setSpy(true);
12283
12284 mDispatcher->onWindowInfosChanged(
12285 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
12286
12287 // First pointer down on left window.
12288 mDispatcher->notifyMotion(
12289 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
12290 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12291 .build());
12292
12293 left->consumeMotionDown();
12294 spy->consumeMotionDown();
12295
12296 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12297 /*pointerId=*/0));
12298 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12299 /*pointerId=*/0));
12300 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12301 /*pointerId=*/0));
12302
12303 // Second pointer down on right window.
12304 mDispatcher->notifyMotion(
12305 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
12306 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12307 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
12308 .build());
12309
12310 left->consumeMotionMove();
12311 right->consumeMotionDown();
12312 spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
12313
12314 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12315 /*pointerId=*/0));
12316 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12317 /*pointerId=*/0));
12318 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12319 /*pointerId=*/0));
12320 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12321 /*pointerId=*/1));
12322 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12323 /*pointerId=*/1));
12324 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12325 /*pointerId=*/1));
12326
12327 // Second pointer up.
12328 mDispatcher->notifyMotion(
12329 MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
12330 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12331 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
12332 .build());
12333
12334 left->consumeMotionMove();
12335 right->consumeMotionUp();
12336 spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
12337
12338 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12339 /*pointerId=*/0));
12340 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12341 /*pointerId=*/0));
12342 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12343 /*pointerId=*/0));
12344 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12345 /*pointerId=*/1));
12346 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12347 /*pointerId=*/1));
12348 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12349 /*pointerId=*/1));
12350
12351 // First pointer up.
12352 mDispatcher->notifyMotion(
12353 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
12354 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12355 .build());
12356
12357 left->consumeMotionUp();
12358 spy->consumeMotionUp();
12359
12360 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12361 /*pointerId=*/0));
12362 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12363 /*pointerId=*/0));
12364 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12365 /*pointerId=*/0));
12366}
12367
12368TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) {
12369 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12370
12371 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12372 ADISPLAY_ID_DEFAULT);
12373 left->setFrame(Rect(0, 0, 100, 100));
12374 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12375 "Right Window", ADISPLAY_ID_DEFAULT);
12376 right->setFrame(Rect(100, 0, 200, 100));
12377
12378 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
12379
12380 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12381 /*pointerId=*/0));
12382 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12383 /*pointerId=*/0));
12384
12385 // Hover move into the window.
12386 mDispatcher->notifyMotion(
12387 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12388 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
12389 .rawXCursorPosition(50)
12390 .rawYCursorPosition(50)
12391 .deviceId(DEVICE_ID)
12392 .build());
12393
12394 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12395
12396 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12397 /*pointerId=*/0));
12398
12399 // Move the mouse with another device. This cancels the hovering pointer from the first device.
12400 mDispatcher->notifyMotion(
12401 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12402 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
12403 .rawXCursorPosition(51)
12404 .rawYCursorPosition(50)
12405 .deviceId(SECOND_DEVICE_ID)
12406 .build());
12407
12408 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12409 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12410
12411 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
12412 // a HOVER_EXIT from the first device.
12413 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12414 /*pointerId=*/0));
12415 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT,
12416 SECOND_DEVICE_ID,
12417 /*pointerId=*/0));
12418
12419 // Move the mouse outside the window. Document the current behavior, where the window does not
12420 // receive HOVER_EXIT even though the mouse left the window.
12421 mDispatcher->notifyMotion(
12422 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12423 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
12424 .rawXCursorPosition(150)
12425 .rawYCursorPosition(50)
12426 .deviceId(SECOND_DEVICE_ID)
12427 .build());
12428
12429 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12430 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12431 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12432 /*pointerId=*/0));
12433 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT,
12434 SECOND_DEVICE_ID,
12435 /*pointerId=*/0));
12436}
12437
Garfield Tane84e6f92019-08-29 17:28:41 -070012438} // namespace android::inputdispatcher