blob: eccbb57374abc12f07627ee9a475ccc2694755bb [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 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003869
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00003870 const std::unique_ptr<MotionEvent> firstDown =
3871 window1->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN)));
3872 ASSERT_EQ(firstDown->getDownTime(), firstDown->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003873 window2->assertNoEvents();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003874
3875 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003876 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003877 mDispatcher->waitForIdle();
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00003878
3879 const std::unique_ptr<MotionEvent> secondDown =
3880 window2->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN)));
3881 ASSERT_EQ(secondDown->getDownTime(), secondDown->getEventTime());
3882 ASSERT_NE(firstDown->getDownTime(), secondDown->getDownTime());
3883 // We currently send MOVE events to all windows receiving a split touch when there is any change
3884 // in the touch state, even when none of the pointers in the split window actually moved.
3885 // Document this behavior in the test.
3886 window1->consumeMotionMove();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003887
3888 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003889 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003890 mDispatcher->waitForIdle();
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00003891
3892 window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime()));
3893 window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime()));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003894
3895 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003896 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003897 mDispatcher->waitForIdle();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003898
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00003899 window2->consumeMotionEvent(
3900 AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(secondDown->getDownTime())));
3901 window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime()));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003902
3903 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003904 mDispatcher->notifyMotion(
3905 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003906 mDispatcher->waitForIdle();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003907
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00003908 window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime()));
3909 window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime()));
3910
3911 // Now add new touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003912 mDispatcher->notifyMotion(
3913 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003914 mDispatcher->waitForIdle();
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +00003915
3916 window1->consumeMotionEvent(
3917 AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(firstDown->getDownTime())));
3918 window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime()));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003919}
3920
Garfield Tandf26e862020-07-01 20:18:19 -07003921TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003922 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003923 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003924 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003925 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003926 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003927 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003928 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003929
3930 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3931
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003932 mDispatcher->onWindowInfosChanged(
3933 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07003934
3935 // Start cursor position in right window so that we can move the cursor to 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(900).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_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003943
3944 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003945 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003946 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003947 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3948 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003949 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003950 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003951 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3952 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003953
3954 // Inject a series of mouse events for a mouse click
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_DOWN, AINPUT_SOURCE_MOUSE)
3958 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003959 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003960 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003961 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3962 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003963
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003964 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003965 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003966 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3967 AINPUT_SOURCE_MOUSE)
3968 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3969 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003970 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003971 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003972 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003973
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003974 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003975 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003976 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3977 AINPUT_SOURCE_MOUSE)
3978 .buttonState(0)
3979 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003980 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003981 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003982 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003983
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_UP, AINPUT_SOURCE_MOUSE)
3987 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003988 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003989 .build()));
3990 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3991
3992 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003994 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003995 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3996 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003997 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003998 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003999 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004000
4001 // No more events
4002 windowLeft->assertNoEvents();
4003 windowRight->assertNoEvents();
4004}
4005
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004006/**
4007 * Put two fingers down (and don't release them) and click the mouse button.
4008 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
4009 * currently active gesture should be canceled, and the new one should proceed.
4010 */
4011TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
4012 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4013 sp<FakeWindowHandle> window =
4014 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4015 window->setFrame(Rect(0, 0, 600, 800));
4016
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004017 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004018
4019 const int32_t touchDeviceId = 4;
4020 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004021
4022 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004023 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4024 .deviceId(touchDeviceId)
4025 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4026 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004027
Prabir Pradhan678438e2023-04-13 19:32:51 +00004028 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4029 .deviceId(touchDeviceId)
4030 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4031 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
4032 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004033 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4034 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4035
4036 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00004037 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4038 .deviceId(mouseDeviceId)
4039 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4040 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4041 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004042 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
4043 WithPointerCount(2u)));
4044 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4045
Prabir Pradhan678438e2023-04-13 19:32:51 +00004046 mDispatcher->notifyMotion(
4047 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4048 .deviceId(mouseDeviceId)
4049 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4050 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4051 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4052 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004053 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4054
4055 // Try to send more touch events while the mouse is down. Since it's a continuation of an
4056 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004057 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4058 .deviceId(touchDeviceId)
4059 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
4060 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
4061 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004062 window->assertNoEvents();
4063}
4064
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004065TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
4066 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4067
4068 sp<FakeWindowHandle> spyWindow =
4069 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4070 spyWindow->setFrame(Rect(0, 0, 600, 800));
4071 spyWindow->setTrustedOverlay(true);
4072 spyWindow->setSpy(true);
4073 sp<FakeWindowHandle> window =
4074 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4075 window->setFrame(Rect(0, 0, 600, 800));
4076
4077 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004078 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004079
4080 // Send mouse cursor to the window
4081 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004082 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004083 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4084 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004085 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004086 .build()));
4087
4088 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4089 WithSource(AINPUT_SOURCE_MOUSE)));
4090 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4091 WithSource(AINPUT_SOURCE_MOUSE)));
4092
4093 window->assertNoEvents();
4094 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004095}
4096
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004097TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
4098 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4099
4100 sp<FakeWindowHandle> spyWindow =
4101 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4102 spyWindow->setFrame(Rect(0, 0, 600, 800));
4103 spyWindow->setTrustedOverlay(true);
4104 spyWindow->setSpy(true);
4105 sp<FakeWindowHandle> window =
4106 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4107 window->setFrame(Rect(0, 0, 600, 800));
4108
4109 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004110 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004111
4112 // Send mouse cursor to the window
4113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004114 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004115 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4116 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004117 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004118 .build()));
4119
4120 // Move mouse cursor
4121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004122 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004123 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4124 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004125 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004126 .build()));
4127
4128 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4129 WithSource(AINPUT_SOURCE_MOUSE)));
4130 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4131 WithSource(AINPUT_SOURCE_MOUSE)));
4132 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4133 WithSource(AINPUT_SOURCE_MOUSE)));
4134 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4135 WithSource(AINPUT_SOURCE_MOUSE)));
4136 // Touch down on the window
4137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004138 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004139 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4140 AINPUT_SOURCE_TOUCHSCREEN)
4141 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004142 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004143 .build()));
4144 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4145 WithSource(AINPUT_SOURCE_MOUSE)));
4146 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4147 WithSource(AINPUT_SOURCE_MOUSE)));
4148 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4149 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4150 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4151 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4152
4153 // pilfer the motion, retaining the gesture on the spy window.
4154 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
4155 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4156 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4157
4158 // Touch UP on the window
4159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004160 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004161 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4162 AINPUT_SOURCE_TOUCHSCREEN)
4163 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004164 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004165 .build()));
4166 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4167 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4168
4169 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
4170 // to send a new gesture. It should again go to both windows (spy and the window below), just
4171 // like the first gesture did, before pilfering. The window configuration has not changed.
4172
4173 // One more tap - DOWN
4174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004175 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004176 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4177 AINPUT_SOURCE_TOUCHSCREEN)
4178 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004179 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004180 .build()));
4181 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4182 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4183 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4184 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4185
4186 // Touch UP on the window
4187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004188 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004189 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4190 AINPUT_SOURCE_TOUCHSCREEN)
4191 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004192 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004193 .build()));
4194 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4195 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4196 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4197 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4198
4199 window->assertNoEvents();
4200 spyWindow->assertNoEvents();
4201}
4202
Garfield Tandf26e862020-07-01 20:18:19 -07004203// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
4204// directly in this test.
4205TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07004206 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07004207 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004208 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004209 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07004210
4211 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4212
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004213 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07004214
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004216 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004217 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4218 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004219 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004220 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004221 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004222 // Inject a series of mouse events for a mouse click
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_DOWN, AINPUT_SOURCE_MOUSE)
4226 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004227 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004228 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004229 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4230 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07004231
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004233 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004234 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
4235 AINPUT_SOURCE_MOUSE)
4236 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4237 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004238 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004239 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004240 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07004241
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004242 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004243 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004244 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
4245 AINPUT_SOURCE_MOUSE)
4246 .buttonState(0)
4247 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004248 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004249 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004250 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07004251
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004252 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004253 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004254 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
4255 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004256 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004257 .build()));
4258 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4259
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07004260 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
4261 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
4262 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004263 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004264 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
4265 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004266 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004267 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004268 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004269}
4270
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004271/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004272 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
4273 * is generated.
4274 */
4275TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
4276 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4277 sp<FakeWindowHandle> window =
4278 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4279 window->setFrame(Rect(0, 0, 1200, 800));
4280
4281 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4282
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004283 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004284
4285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004286 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004287 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4288 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004289 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004290 .build()));
4291 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4292
4293 // Remove the window, but keep the channel.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004294 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004295 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4296}
4297
4298/**
Daniel Norman7487dfa2023-08-02 16:39:45 -07004299 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
4300 */
Ameer Armalycff4fa52023-10-04 23:45:11 +00004301TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash,
4302 REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags,
4303 a11y_crash_on_inconsistent_event_stream))) {
Daniel Norman7487dfa2023-08-02 16:39:45 -07004304 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4305 sp<FakeWindowHandle> window =
4306 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4307 window->setFrame(Rect(0, 0, 1200, 800));
4308
4309 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4310
4311 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4312
4313 MotionEventBuilder hoverEnterBuilder =
4314 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4315 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4316 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4318 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4319 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4320 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4321 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4322 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4323}
4324
4325/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004326 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
4327 */
4328TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
4329 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4330 sp<FakeWindowHandle> window =
4331 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4332 window->setFrame(Rect(0, 0, 100, 100));
4333
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004334 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004335
4336 const int32_t mouseDeviceId = 7;
4337 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004338
4339 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00004340 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4341 .deviceId(mouseDeviceId)
4342 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4343 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004344 window->consumeMotionEvent(
4345 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4346
4347 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004348 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4349 .deviceId(touchDeviceId)
4350 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4351 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004352
4353 window->consumeMotionEvent(
4354 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4355 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4356}
4357
4358/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004359 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004360 * The tap causes a HOVER_EXIT event to be generated because the current event
4361 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004362 */
4363TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
4364 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4365 sp<FakeWindowHandle> window =
4366 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4367 window->setFrame(Rect(0, 0, 100, 100));
4368
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004369 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004370 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4371 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4372 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004373 ASSERT_NO_FATAL_FAILURE(
4374 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4375 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004376
4377 // Tap on the window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004378 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4379 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4380 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004381 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004382 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4383 WithSource(AINPUT_SOURCE_MOUSE))));
4384
4385 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004386 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4387 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4388
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004389 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4390 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4391 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004392 ASSERT_NO_FATAL_FAILURE(
4393 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4394 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4395}
4396
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004397TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
4398 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4399 sp<FakeWindowHandle> windowDefaultDisplay =
4400 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4401 ADISPLAY_ID_DEFAULT);
4402 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
4403 sp<FakeWindowHandle> windowSecondDisplay =
4404 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
4405 SECOND_DISPLAY_ID);
4406 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
4407
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004408 mDispatcher->onWindowInfosChanged(
4409 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004410
4411 // Set cursor position in window in default display and check that hover enter and move
4412 // events are generated.
4413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004414 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004415 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4416 AINPUT_SOURCE_MOUSE)
4417 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004418 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004419 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004420 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004421
4422 // Remove all windows in secondary display and check that no event happens on window in
4423 // primary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004424 mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0});
4425
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004426 windowDefaultDisplay->assertNoEvents();
4427
4428 // Move cursor position in window in default display and check that only hover move
4429 // event is generated and not hover enter event.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004430 mDispatcher->onWindowInfosChanged(
4431 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004432 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004433 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004434 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4435 AINPUT_SOURCE_MOUSE)
4436 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004437 .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004438 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004439 windowDefaultDisplay->consumeMotionEvent(
4440 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4441 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004442 windowDefaultDisplay->assertNoEvents();
4443}
4444
Garfield Tan00f511d2019-06-12 16:55:40 -07004445TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07004446 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07004447
4448 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004449 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004450 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004451 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004452 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004453 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004454
4455 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4456
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004457 mDispatcher->onWindowInfosChanged(
4458 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tan00f511d2019-06-12 16:55:40 -07004459
4460 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
4461 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004462 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004463 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004464 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004465 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004466 windowRight->assertNoEvents();
4467}
4468
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004469TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004470 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004471 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4472 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07004473 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004474
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004475 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07004476 setFocusedWindow(window);
4477
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004478 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004479
Prabir Pradhan678438e2023-04-13 19:32:51 +00004480 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004481
4482 // Window should receive key down event.
4483 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4484
4485 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
4486 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004487 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00004488 window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004489}
4490
4491TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004492 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004493 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4494 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004495
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004496 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004497
Prabir Pradhan678438e2023-04-13 19:32:51 +00004498 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4499 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004500
4501 // Window should receive motion down event.
4502 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4503
4504 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
4505 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004506 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004507 window->consumeMotionEvent(
4508 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004509}
4510
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004511TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
4512 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4513 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4514 "Fake Window", ADISPLAY_ID_DEFAULT);
4515
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004516 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004517
4518 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4519 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
4520 .build());
4521
4522 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4523
4524 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
4525 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
4526 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4527
4528 // After the device has been reset, a new hovering stream can be sent to the window
4529 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4530 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
4531 .build());
4532 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4533}
4534
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004535TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
4536 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004537 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4538 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004539 window->setFocusable(true);
4540
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004541 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004542 setFocusedWindow(window);
4543
4544 window->consumeFocusEvent(true);
4545
Prabir Pradhan678438e2023-04-13 19:32:51 +00004546 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004547 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
4548 const nsecs_t injectTime = keyArgs.eventTime;
4549 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00004550 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004551 // The dispatching time should be always greater than or equal to intercept key timeout.
4552 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4553 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
4554 std::chrono::nanoseconds(interceptKeyTimeout).count());
4555}
4556
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004557/**
4558 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
4559 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004560TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
4561 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004562 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4563 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004564 window->setFocusable(true);
4565
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004566 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004567 setFocusedWindow(window);
4568
4569 window->consumeFocusEvent(true);
4570
Prabir Pradhan678438e2023-04-13 19:32:51 +00004571 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004572 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004573
4574 // Set a value that's significantly larger than the default consumption timeout. If the
4575 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
4576 mFakePolicy->setInterceptKeyTimeout(600ms);
4577 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
4578 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004579 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4580}
4581
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004582/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004583 * Two windows. First is a regular window. Second does not overlap with the first, and has
4584 * WATCH_OUTSIDE_TOUCH.
4585 * Both windows are owned by the same UID.
4586 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
4587 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
4588 */
4589TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
4590 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004591 sp<FakeWindowHandle> window =
4592 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004593 window->setFrame(Rect{0, 0, 100, 100});
4594
4595 sp<FakeWindowHandle> outsideWindow =
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004596 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004597 ADISPLAY_ID_DEFAULT);
4598 outsideWindow->setFrame(Rect{100, 100, 200, 200});
4599 outsideWindow->setWatchOutsideTouch(true);
4600 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004601 mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004602
4603 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004604 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4605 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4606 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004607 window->consumeMotionDown();
4608 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
4609 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
4610 outsideWindow->consumeMotionEvent(
4611 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
Prabir Pradhan502a7252023-12-01 16:11:24 +00004612
4613 // Ensure outsideWindow doesn't get any more events for the gesture.
4614 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4615 ADISPLAY_ID_DEFAULT, {PointF{51, 51}}));
4616 window->consumeMotionMove();
4617 outsideWindow->assertNoEvents();
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004618}
4619
4620/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004621 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
4622 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
4623 * ACTION_OUTSIDE event is sent per gesture.
4624 */
4625TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
4626 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
4627 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004628 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4629 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004630 window->setWatchOutsideTouch(true);
4631 window->setFrame(Rect{0, 0, 100, 100});
4632 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004633 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4634 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004635 secondWindow->setFrame(Rect{100, 100, 200, 200});
4636 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004637 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
4638 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004639 thirdWindow->setFrame(Rect{200, 200, 300, 300});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004640 mDispatcher->onWindowInfosChanged(
4641 {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0});
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004642
4643 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004644 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4645 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4646 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004647 window->assertNoEvents();
4648 secondWindow->assertNoEvents();
4649
4650 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
4651 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004652 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4653 ADISPLAY_ID_DEFAULT,
4654 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004655 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
4656 window->consumeMotionEvent(
4657 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004658 secondWindow->consumeMotionDown();
4659 thirdWindow->assertNoEvents();
4660
4661 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
4662 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004663 mDispatcher->notifyMotion(
4664 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4665 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004666 window->assertNoEvents();
4667 secondWindow->consumeMotionMove();
4668 thirdWindow->consumeMotionDown();
4669}
4670
Prabir Pradhan814fe082022-07-22 20:22:18 +00004671TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
4672 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004673 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4674 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004675 window->setFocusable(true);
4676
Patrick Williamsd828f302023-04-28 17:52:08 -05004677 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004678 setFocusedWindow(window);
4679
4680 window->consumeFocusEvent(true);
4681
Prabir Pradhan678438e2023-04-13 19:32:51 +00004682 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
4683 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
4684 mDispatcher->notifyKey(keyDown);
4685 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004686
4687 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4688 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4689
4690 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05004691 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004692
4693 window->consumeFocusEvent(false);
4694
Prabir Pradhan678438e2023-04-13 19:32:51 +00004695 mDispatcher->notifyKey(keyDown);
4696 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004697 window->assertNoEvents();
4698}
4699
Arthur Hung96483742022-11-15 03:30:48 +00004700TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
4701 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4702 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4703 "Fake Window", ADISPLAY_ID_DEFAULT);
4704 // Ensure window is non-split and have some transform.
4705 window->setPreventSplitting(true);
4706 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05004707 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00004708
4709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004710 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung96483742022-11-15 03:30:48 +00004711 {50, 50}))
4712 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4713 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4714
4715 const MotionEvent secondFingerDownEvent =
4716 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4717 .displayId(ADISPLAY_ID_DEFAULT)
4718 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004719 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4720 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00004721 .build();
4722 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004723 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung96483742022-11-15 03:30:48 +00004724 InputEventInjectionSync::WAIT_FOR_RESULT))
4725 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4726
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004727 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
4728 ASSERT_NE(nullptr, event);
4729 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4730 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4731 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4732 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4733 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
Arthur Hung96483742022-11-15 03:30:48 +00004734}
4735
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07004736/**
4737 * Two windows: a splittable and a non-splittable.
4738 * The non-splittable window shouldn't receive any "incomplete" gestures.
4739 * Send the first pointer to the splittable window, and then touch the non-splittable window.
4740 * The second pointer should be dropped because the initial window is splittable, so it won't get
4741 * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any
4742 * "incomplete" gestures.
4743 */
4744TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) {
4745 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4746 sp<FakeWindowHandle> leftWindow =
4747 sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window",
4748 ADISPLAY_ID_DEFAULT);
4749 leftWindow->setPreventSplitting(false);
4750 leftWindow->setFrame(Rect(0, 0, 100, 100));
4751 sp<FakeWindowHandle> rightWindow =
4752 sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window",
4753 ADISPLAY_ID_DEFAULT);
4754 rightWindow->setPreventSplitting(true);
4755 rightWindow->setFrame(Rect(100, 100, 200, 200));
4756 mDispatcher->onWindowInfosChanged(
4757 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
4758
4759 // Touch down on left, splittable window
4760 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4761 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4762 .build());
4763 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4764
4765 mDispatcher->notifyMotion(
4766 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4767 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4768 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
4769 .build());
4770 leftWindow->assertNoEvents();
4771 rightWindow->assertNoEvents();
4772}
4773
Harry Cuttsb166c002023-05-09 13:06:05 +00004774TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4775 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4776 sp<FakeWindowHandle> window =
4777 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4778 window->setFrame(Rect(0, 0, 400, 400));
4779 sp<FakeWindowHandle> trustedOverlay =
4780 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4781 ADISPLAY_ID_DEFAULT);
4782 trustedOverlay->setSpy(true);
4783 trustedOverlay->setTrustedOverlay(true);
4784
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004785 mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004786
4787 // Start a three-finger touchpad swipe
4788 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4789 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4790 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4791 .build());
4792 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4793 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4794 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4795 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4796 .build());
4797 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4798 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4799 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4800 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4801 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4802 .build());
4803
4804 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4805 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4806 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4807
4808 // Move the swipe a bit
4809 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4810 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4811 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4812 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4813 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4814 .build());
4815
4816 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4817
4818 // End the swipe
4819 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4820 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4821 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4822 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4823 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4824 .build());
4825 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4826 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4827 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4828 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4829 .build());
4830 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4831 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4832 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4833 .build());
4834
4835 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4836 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4837 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4838
4839 window->assertNoEvents();
4840}
4841
4842TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4843 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4844 sp<FakeWindowHandle> window =
4845 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4846 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004847 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004848
4849 // Start a three-finger touchpad swipe
4850 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4851 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4852 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4853 .build());
4854 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4855 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4856 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4857 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4858 .build());
4859 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4860 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4861 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4862 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4863 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4864 .build());
4865
4866 // Move the swipe a bit
4867 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4868 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4869 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4870 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4871 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4872 .build());
4873
4874 // End the swipe
4875 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4876 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4877 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4878 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4879 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4880 .build());
4881 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4882 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4883 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4884 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4885 .build());
4886 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4887 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4888 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4889 .build());
4890
4891 window->assertNoEvents();
4892}
4893
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004894/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004895 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
4896 * the first pointer.
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004897 * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004898 */
4899TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
4900 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4901 sp<FakeWindowHandle> window =
4902 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4903 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004904 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004905
4906 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
4907 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4908 .downTime(baseTime + 10)
4909 .eventTime(baseTime + 10)
4910 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4911 .build());
4912
4913 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4914
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004915 // Change the transform so that the orientation is now different from original.
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004916 window->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004917
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004918 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004919
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004920 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4921 .downTime(baseTime + 10)
4922 .eventTime(baseTime + 30)
4923 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4924 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4925 .build());
4926
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004927 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4928
4929 // Finish the gesture and start a new one. Ensure all events are sent to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004930 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
4931 .downTime(baseTime + 10)
4932 .eventTime(baseTime + 40)
4933 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4934 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4935 .build());
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004936
4937 window->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4938
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004939 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4940 .downTime(baseTime + 10)
4941 .eventTime(baseTime + 50)
4942 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4943 .build());
4944
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004945 window->consumeMotionEvent(WithMotionAction(ACTION_UP));
4946
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004947 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4948 .downTime(baseTime + 60)
4949 .eventTime(baseTime + 60)
4950 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
4951 .build());
4952
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004953 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004954}
4955
4956/**
Hu Guo771a7692023-09-17 20:51:08 +08004957 * When there are multiple screens, such as screen projection to TV or screen recording, if the
4958 * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and
4959 * its coordinates should be converted by the transform of the windows of target screen.
4960 */
4961TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) {
4962 // This case will create a window and a spy window on the default display and mirror
4963 // window on the second display. cancel event is sent through spy window pilferPointers
4964 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4965
4966 sp<FakeWindowHandle> spyWindowDefaultDisplay =
4967 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4968 spyWindowDefaultDisplay->setTrustedOverlay(true);
4969 spyWindowDefaultDisplay->setSpy(true);
4970
4971 sp<FakeWindowHandle> windowDefaultDisplay =
4972 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4973 ADISPLAY_ID_DEFAULT);
4974 windowDefaultDisplay->setWindowTransform(1, 0, 0, 1);
4975
4976 sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID);
4977 windowSecondDisplay->setWindowTransform(2, 0, 0, 2);
4978
4979 // Add the windows to the dispatcher
4980 mDispatcher->onWindowInfosChanged(
4981 {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(),
4982 *windowSecondDisplay->getInfo()},
4983 {},
4984 0,
4985 0});
4986
4987 // Send down to ADISPLAY_ID_DEFAULT
4988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4989 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4990 {100, 100}))
4991 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4992
4993 spyWindowDefaultDisplay->consumeMotionDown();
4994 windowDefaultDisplay->consumeMotionDown();
4995
4996 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken()));
4997
4998 // windowDefaultDisplay gets cancel
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004999 std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent();
5000 ASSERT_NE(nullptr, event);
5001 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction());
Hu Guo771a7692023-09-17 20:51:08 +08005002
5003 // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the
5004 // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y
5005 // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of
5006 // SECOND_DISPLAY_ID, the x and y coordinates are 200
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005007 EXPECT_EQ(100, event->getX(0));
5008 EXPECT_EQ(100, event->getY(0));
Hu Guo771a7692023-09-17 20:51:08 +08005009}
5010
5011/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005012 * Ensure the correct coordinate spaces are used by InputDispatcher.
5013 *
5014 * InputDispatcher works in the display space, so its coordinate system is relative to the display
5015 * panel. Windows get events in the window space, and get raw coordinates in the logical display
5016 * space.
5017 */
5018class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
5019public:
5020 void SetUp() override {
5021 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005022 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005023 }
5024
5025 void addDisplayInfo(int displayId, const ui::Transform& transform) {
5026 gui::DisplayInfo info;
5027 info.displayId = displayId;
5028 info.transform = transform;
5029 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05005030 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005031 }
5032
5033 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
5034 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05005035 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005036 }
5037
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005038 void removeAllWindowsAndDisplays() {
5039 mDisplayInfos.clear();
5040 mWindowInfos.clear();
5041 }
5042
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005043 // Set up a test scenario where the display has a scaled projection and there are two windows
5044 // on the display.
5045 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
5046 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
5047 // respectively.
5048 ui::Transform displayTransform;
5049 displayTransform.set(2, 0, 0, 4);
5050 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5051
5052 std::shared_ptr<FakeApplicationHandle> application =
5053 std::make_shared<FakeApplicationHandle>();
5054
5055 // Add two windows to the display. Their frames are represented in the display space.
5056 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005057 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5058 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005059 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
5060 addWindow(firstWindow);
5061
5062 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005063 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5064 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005065 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
5066 addWindow(secondWindow);
5067 return {std::move(firstWindow), std::move(secondWindow)};
5068 }
5069
5070private:
5071 std::vector<gui::DisplayInfo> mDisplayInfos;
5072 std::vector<gui::WindowInfo> mWindowInfos;
5073};
5074
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005075TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005076 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5077 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005078 // selected so that if the hit test was performed with the point and the bounds being in
5079 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005080 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5081 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5082 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005083
5084 firstWindow->consumeMotionDown();
5085 secondWindow->assertNoEvents();
5086}
5087
5088// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
5089// the event should be treated as being in the logical display space.
5090TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
5091 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5092 // Send down to the first window. The point is represented in the logical display space. The
5093 // point is selected so that if the hit test was done in logical display space, then it would
5094 // end up in the incorrect window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005095 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005096 PointF{75 * 2, 55 * 4});
5097
5098 firstWindow->consumeMotionDown();
5099 secondWindow->assertNoEvents();
5100}
5101
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005102// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
5103// event should be treated as being in the logical display space.
5104TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
5105 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5106
5107 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5108 ui::Transform injectedEventTransform;
5109 injectedEventTransform.set(matrix);
5110 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
5111 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
5112
5113 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5114 .displayId(ADISPLAY_ID_DEFAULT)
5115 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005116 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005117 .x(untransformedPoint.x)
5118 .y(untransformedPoint.y))
5119 .build();
5120 event.transform(matrix);
5121
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005122 injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT,
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005123 InputEventInjectionSync::WAIT_FOR_RESULT);
5124
5125 firstWindow->consumeMotionDown();
5126 secondWindow->assertNoEvents();
5127}
5128
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005129TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
5130 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5131
5132 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005133 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5134 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5135 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005136
5137 firstWindow->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005138 std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent();
5139 ASSERT_NE(nullptr, event);
5140 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005141
5142 // Ensure that the events from the "getRaw" API are in logical display coordinates.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005143 EXPECT_EQ(300, event->getRawX(0));
5144 EXPECT_EQ(880, event->getRawY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005145
5146 // Ensure that the x and y values are in the window's coordinate space.
5147 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
5148 // the logical display space. This will be the origin of the window space.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005149 EXPECT_EQ(100, event->getX(0));
5150 EXPECT_EQ(80, event->getY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005151}
5152
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005153TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) {
5154 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5155 // The monitor will always receive events in the logical display's coordinate space, because
5156 // it does not have a window.
Prabir Pradhanfb549072023-10-05 19:17:36 +00005157 FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005158
5159 // Send down to the first window.
5160 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5161 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
5162 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5163 monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5164
5165 // Second pointer goes down on second window.
5166 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5167 ADISPLAY_ID_DEFAULT,
5168 {PointF{50, 100}, PointF{150, 220}}));
5169 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80)));
5170 const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}},
5171 {1, PointF{300, 880}}};
5172 monitor.consumeMotionEvent(
5173 AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers)));
5174
5175 mDispatcher->cancelCurrentTouch();
5176
5177 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5178 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80)));
5179 monitor.consumeMotionEvent(
5180 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers)));
5181}
5182
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005183TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) {
5184 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5185
5186 // Send down to the first window.
5187 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5188 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
5189 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5190
5191 // The pointer is transferred to the second window, and the second window receives it in the
5192 // correct coordinate space.
5193 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
5194 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5195 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400)));
5196}
5197
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005198TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) {
5199 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5200
5201 // Send hover move to the second window, and ensure it shows up as hover enter.
5202 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
5203 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5204 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5205 WithCoords(100, 80), WithRawCoords(300, 880)));
5206
5207 // Touch down at the same location and ensure a hover exit is synthesized.
5208 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
5209 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5210 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5211 WithRawCoords(300, 880)));
5212 secondWindow->consumeMotionEvent(
5213 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5214 secondWindow->assertNoEvents();
5215 firstWindow->assertNoEvents();
5216}
5217
Prabir Pradhan453ae732023-10-13 14:30:14 +00005218// Same as above, but while the window is being mirrored.
5219TEST_F(InputDispatcherDisplayProjectionTest,
5220 SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) {
5221 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5222
5223 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5224 ui::Transform secondDisplayTransform;
5225 secondDisplayTransform.set(matrix);
5226 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5227
5228 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5229 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5230 addWindow(secondWindowClone);
5231
5232 // Send hover move to the second window, and ensure it shows up as hover enter.
5233 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
5234 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5235 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5236 WithCoords(100, 80), WithRawCoords(300, 880)));
5237
5238 // Touch down at the same location and ensure a hover exit is synthesized for the correct
5239 // display.
5240 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
5241 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5242 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5243 WithRawCoords(300, 880)));
5244 secondWindow->consumeMotionEvent(
5245 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5246 secondWindow->assertNoEvents();
5247 firstWindow->assertNoEvents();
5248}
5249
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005250TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) {
5251 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5252
5253 // Send hover enter to second window
5254 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
5255 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5256 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5257 WithCoords(100, 80), WithRawCoords(300, 880)));
5258
5259 mDispatcher->cancelCurrentTouch();
5260
5261 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5262 WithRawCoords(300, 880)));
5263 secondWindow->assertNoEvents();
5264 firstWindow->assertNoEvents();
5265}
5266
Prabir Pradhan453ae732023-10-13 14:30:14 +00005267// Same as above, but while the window is being mirrored.
Prabir Pradhan16463382023-10-12 23:03:19 +00005268TEST_F(InputDispatcherDisplayProjectionTest,
5269 SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) {
5270 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5271
5272 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5273 ui::Transform secondDisplayTransform;
5274 secondDisplayTransform.set(matrix);
5275 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5276
5277 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5278 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5279 addWindow(secondWindowClone);
5280
5281 // Send hover enter to second window
5282 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
5283 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5284 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5285 WithCoords(100, 80), WithRawCoords(300, 880),
5286 WithDisplayId(ADISPLAY_ID_DEFAULT)));
5287
5288 mDispatcher->cancelCurrentTouch();
5289
5290 // Ensure the cancelation happens with the correct displayId and the correct coordinates.
5291 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5292 WithRawCoords(300, 880),
5293 WithDisplayId(ADISPLAY_ID_DEFAULT)));
5294 secondWindow->assertNoEvents();
5295 firstWindow->assertNoEvents();
5296}
5297
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005298/** Ensure consistent behavior of InputDispatcher in all orientations. */
5299class InputDispatcherDisplayOrientationFixture
5300 : public InputDispatcherDisplayProjectionTest,
5301 public ::testing::WithParamInterface<ui::Rotation> {};
5302
5303// This test verifies the touchable region of a window for all rotations of the display by tapping
5304// in different locations on the display, specifically points close to the four corners of a
5305// window.
5306TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
5307 constexpr static int32_t displayWidth = 400;
5308 constexpr static int32_t displayHeight = 800;
5309
5310 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5311
5312 const auto rotation = GetParam();
5313
5314 // Set up the display with the specified rotation.
5315 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
5316 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
5317 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
5318 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
5319 logicalDisplayWidth, logicalDisplayHeight);
5320 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5321
5322 // Create a window with its bounds determined in the logical display.
5323 const Rect frameInLogicalDisplay(100, 100, 200, 300);
5324 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
5325 sp<FakeWindowHandle> window =
5326 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5327 window->setFrame(frameInDisplay, displayTransform);
5328 addWindow(window);
5329
5330 // The following points in logical display space should be inside the window.
5331 static const std::array<vec2, 4> insidePoints{
5332 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
5333 for (const auto pointInsideWindow : insidePoints) {
5334 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
5335 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005336 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5337 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5338 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005339 window->consumeMotionDown();
5340
Prabir Pradhan678438e2023-04-13 19:32:51 +00005341 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5342 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5343 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005344 window->consumeMotionUp();
5345 }
5346
5347 // The following points in logical display space should be outside the window.
5348 static const std::array<vec2, 5> outsidePoints{
5349 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
5350 for (const auto pointOutsideWindow : outsidePoints) {
5351 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
5352 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005353 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5354 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5355 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005356
Prabir Pradhan678438e2023-04-13 19:32:51 +00005357 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5358 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5359 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005360 }
5361 window->assertNoEvents();
5362}
5363
5364// Run the precision tests for all rotations.
5365INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
5366 InputDispatcherDisplayOrientationFixture,
5367 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
5368 ui::ROTATION_270),
5369 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
5370 return ftl::enum_string(testParamInfo.param);
5371 });
5372
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005373using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
5374 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005375
5376class TransferTouchFixture : public InputDispatcherTest,
5377 public ::testing::WithParamInterface<TransferFunction> {};
5378
5379TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07005380 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005381
5382 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005383 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005384 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5385 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005386 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005387 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005388 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5389 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005390 sp<FakeWindowHandle> wallpaper =
5391 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
5392 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005393 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005394 mDispatcher->onWindowInfosChanged(
5395 {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005396
5397 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005398 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5399 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005400
Svet Ganov5d3bc372020-01-26 23:11:07 -08005401 // Only the first window should get the down event
5402 firstWindow->consumeMotionDown();
5403 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005404 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005405
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005406 // Transfer touch to the second window
5407 TransferFunction f = GetParam();
5408 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5409 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005410 // The first window gets cancel and the second gets down
5411 firstWindow->consumeMotionCancel();
5412 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005413 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005414
5415 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005416 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5417 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005418 // The first window gets no events and the second gets up
5419 firstWindow->assertNoEvents();
5420 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005421 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005422}
5423
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005424/**
5425 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
5426 * from. When we have spy windows, there are several windows to choose from: either spy, or the
5427 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
5428 * natural to the user.
5429 * In this test, we are sending a pointer to both spy window and first window. We then try to
5430 * transfer touch to the second window. The dispatcher should identify the first window as the
5431 * one that should lose the gesture, and therefore the action should be to move the gesture from
5432 * the first window to the second.
5433 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
5434 * the other API, as well.
5435 */
5436TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
5437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5438
5439 // Create a couple of windows + a spy window
5440 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005441 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005442 spyWindow->setTrustedOverlay(true);
5443 spyWindow->setSpy(true);
5444 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005445 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005446 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005447 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005448
5449 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005450 mDispatcher->onWindowInfosChanged(
5451 {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005452
5453 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005454 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5455 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005456 // Only the first window and spy should get the down event
5457 spyWindow->consumeMotionDown();
5458 firstWindow->consumeMotionDown();
5459
5460 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
5461 // if f === 'transferTouch'.
5462 TransferFunction f = GetParam();
5463 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5464 ASSERT_TRUE(success);
5465 // The first window gets cancel and the second gets down
5466 firstWindow->consumeMotionCancel();
5467 secondWindow->consumeMotionDown();
5468
5469 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005470 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5471 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005472 // The first window gets no events and the second+spy get up
5473 firstWindow->assertNoEvents();
5474 spyWindow->consumeMotionUp();
5475 secondWindow->consumeMotionUp();
5476}
5477
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005478TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005479 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005480
5481 PointF touchPoint = {10, 10};
5482
5483 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005484 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005485 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5486 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005487 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005488 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005489 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5490 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005491 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005492
5493 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005494 mDispatcher->onWindowInfosChanged(
5495 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005496
5497 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005498 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5499 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5500 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005501 // Only the first window should get the down event
5502 firstWindow->consumeMotionDown();
5503 secondWindow->assertNoEvents();
5504
5505 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005506 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5507 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005508 // Only the first window should get the pointer down event
5509 firstWindow->consumeMotionPointerDown(1);
5510 secondWindow->assertNoEvents();
5511
5512 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005513 TransferFunction f = GetParam();
5514 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5515 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005516 // The first window gets cancel and the second gets down and pointer down
5517 firstWindow->consumeMotionCancel();
5518 secondWindow->consumeMotionDown();
5519 secondWindow->consumeMotionPointerDown(1);
5520
5521 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005522 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5523 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005524 // The first window gets nothing and the second gets pointer up
5525 firstWindow->assertNoEvents();
5526 secondWindow->consumeMotionPointerUp(1);
5527
5528 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005529 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5530 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005531 // The first window gets nothing and the second gets up
5532 firstWindow->assertNoEvents();
5533 secondWindow->consumeMotionUp();
5534}
5535
Arthur Hungc539dbb2022-12-08 07:45:36 +00005536TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
5537 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5538
5539 // Create a couple of windows
5540 sp<FakeWindowHandle> firstWindow =
5541 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5542 ADISPLAY_ID_DEFAULT);
5543 firstWindow->setDupTouchToWallpaper(true);
5544 sp<FakeWindowHandle> secondWindow =
5545 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5546 ADISPLAY_ID_DEFAULT);
5547 secondWindow->setDupTouchToWallpaper(true);
5548
5549 sp<FakeWindowHandle> wallpaper1 =
5550 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
5551 wallpaper1->setIsWallpaper(true);
5552
5553 sp<FakeWindowHandle> wallpaper2 =
5554 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
5555 wallpaper2->setIsWallpaper(true);
5556 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005557 mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(),
5558 *secondWindow->getInfo(), *wallpaper2->getInfo()},
5559 {},
5560 0,
5561 0});
Arthur Hungc539dbb2022-12-08 07:45:36 +00005562
5563 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005564 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5565 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005566
5567 // Only the first window should get the down event
5568 firstWindow->consumeMotionDown();
5569 secondWindow->assertNoEvents();
5570 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5571 wallpaper2->assertNoEvents();
5572
5573 // Transfer touch focus to the second window
5574 TransferFunction f = GetParam();
5575 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5576 ASSERT_TRUE(success);
5577
5578 // The first window gets cancel and the second gets down
5579 firstWindow->consumeMotionCancel();
5580 secondWindow->consumeMotionDown();
5581 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5582 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5583
5584 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005585 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5586 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005587 // The first window gets no events and the second gets up
5588 firstWindow->assertNoEvents();
5589 secondWindow->consumeMotionUp();
5590 wallpaper1->assertNoEvents();
5591 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5592}
5593
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005594// For the cases of single pointer touch and two pointers non-split touch, the api's
5595// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
5596// for the case where there are multiple pointers split across several windows.
5597INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
5598 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005599 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
5600 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005601 return dispatcher->transferTouch(destChannelToken,
5602 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005603 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005604 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
5605 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005606 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00005607 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005608 }));
5609
Svet Ganov5d3bc372020-01-26 23:11:07 -08005610TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005611 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005612
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005613 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005614 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5615 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005616 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005617
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005618 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005619 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5620 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005621 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005622
5623 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005624 mDispatcher->onWindowInfosChanged(
5625 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005626
5627 PointF pointInFirst = {300, 200};
5628 PointF pointInSecond = {300, 600};
5629
5630 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005631 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5632 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5633 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005634 // Only the first window should get the down event
5635 firstWindow->consumeMotionDown();
5636 secondWindow->assertNoEvents();
5637
5638 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005639 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5640 ADISPLAY_ID_DEFAULT,
5641 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005642 // The first window gets a move and the second a down
5643 firstWindow->consumeMotionMove();
5644 secondWindow->consumeMotionDown();
5645
5646 // Transfer touch focus to the second window
5647 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
5648 // The first window gets cancel and the new gets pointer down (it already saw down)
5649 firstWindow->consumeMotionCancel();
5650 secondWindow->consumeMotionPointerDown(1);
5651
5652 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005653 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5654 ADISPLAY_ID_DEFAULT,
5655 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005656 // The first window gets nothing and the second gets pointer up
5657 firstWindow->assertNoEvents();
5658 secondWindow->consumeMotionPointerUp(1);
5659
5660 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005661 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5662 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005663 // The first window gets nothing and the second gets up
5664 firstWindow->assertNoEvents();
5665 secondWindow->consumeMotionUp();
5666}
5667
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005668// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
5669// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
5670// touch is not supported, so the touch should continue on those windows and the transferred-to
5671// window should get nothing.
5672TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
5673 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5674
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005675 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005676 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5677 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005678 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005679
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005680 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005681 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5682 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005683 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005684
5685 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005686 mDispatcher->onWindowInfosChanged(
5687 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005688
5689 PointF pointInFirst = {300, 200};
5690 PointF pointInSecond = {300, 600};
5691
5692 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005693 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5694 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5695 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005696 // Only the first window should get the down event
5697 firstWindow->consumeMotionDown();
5698 secondWindow->assertNoEvents();
5699
5700 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005701 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5702 ADISPLAY_ID_DEFAULT,
5703 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005704 // The first window gets a move and the second a down
5705 firstWindow->consumeMotionMove();
5706 secondWindow->consumeMotionDown();
5707
5708 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005709 const bool transferred =
5710 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005711 // The 'transferTouch' call should not succeed, because there are 2 touched windows
5712 ASSERT_FALSE(transferred);
5713 firstWindow->assertNoEvents();
5714 secondWindow->assertNoEvents();
5715
5716 // The rest of the dispatch should proceed as normal
5717 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005718 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5719 ADISPLAY_ID_DEFAULT,
5720 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005721 // The first window gets MOVE and the second gets pointer up
5722 firstWindow->consumeMotionMove();
5723 secondWindow->consumeMotionUp();
5724
5725 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005726 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5727 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005728 // The first window gets nothing and the second gets up
5729 firstWindow->consumeMotionUp();
5730 secondWindow->assertNoEvents();
5731}
5732
Arthur Hungabbb9d82021-09-01 14:52:30 +00005733// This case will create two windows and one mirrored window on the default display and mirror
5734// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
5735// the windows info of second display before default display.
5736TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
5737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5738 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005739 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005740 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005741 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005742 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005743 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005744
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005745 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005746 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005747
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005748 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005749 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005750
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005751 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005752 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005753
5754 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005755 mDispatcher->onWindowInfosChanged(
5756 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5757 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5758 *secondWindowInPrimary->getInfo()},
5759 {},
5760 0,
5761 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005762
5763 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005764 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005765 {50, 50}))
5766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5767
5768 // Window should receive motion event.
5769 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5770
5771 // Transfer touch focus
5772 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
5773 secondWindowInPrimary->getToken()));
5774 // The first window gets cancel.
5775 firstWindowInPrimary->consumeMotionCancel();
5776 secondWindowInPrimary->consumeMotionDown();
5777
5778 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005779 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005780 ADISPLAY_ID_DEFAULT, {150, 50}))
5781 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5782 firstWindowInPrimary->assertNoEvents();
5783 secondWindowInPrimary->consumeMotionMove();
5784
5785 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005786 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005787 {150, 50}))
5788 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5789 firstWindowInPrimary->assertNoEvents();
5790 secondWindowInPrimary->consumeMotionUp();
5791}
5792
5793// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
5794// 'transferTouch' api.
5795TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
5796 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5797 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005798 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005799 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005800 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005801 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005802 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005803
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005804 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005805 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005806
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005807 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005808 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005809
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005810 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005811 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005812
5813 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005814 mDispatcher->onWindowInfosChanged(
5815 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5816 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5817 *secondWindowInPrimary->getInfo()},
5818 {},
5819 0,
5820 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005821
5822 // Touch on second display.
5823 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005824 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
5825 {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005826 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5827
5828 // Window should receive motion event.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005829 firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005830
5831 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005832 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005833
5834 // The first window gets cancel.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005835 firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
5836 secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005837
5838 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005839 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005840 SECOND_DISPLAY_ID, {150, 50}))
5841 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005842 firstWindowInSecondary->assertNoEvents();
5843 secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005844
5845 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005846 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005847 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005848 firstWindowInSecondary->assertNoEvents();
5849 secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005850}
5851
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005852TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005853 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005854 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5855 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005856
Vishnu Nair47074b82020-08-14 11:54:47 -07005857 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005858 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005859 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005860
5861 window->consumeFocusEvent(true);
5862
Prabir Pradhan678438e2023-04-13 19:32:51 +00005863 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005864
5865 // Window should receive key down event.
5866 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00005867
5868 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005869 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005870 mFakePolicy->assertUserActivityPoked();
5871}
5872
5873TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
5874 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5875 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5876 "Fake Window", ADISPLAY_ID_DEFAULT);
5877
5878 window->setDisableUserActivity(true);
5879 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005880 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005881 setFocusedWindow(window);
5882
5883 window->consumeFocusEvent(true);
5884
5885 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5886
5887 // Window should receive key down event.
5888 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5889
5890 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005891 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005892 mFakePolicy->assertUserActivityNotPoked();
5893}
5894
5895TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
5896 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5897 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5898 "Fake Window", ADISPLAY_ID_DEFAULT);
5899
5900 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005901 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005902 setFocusedWindow(window);
5903
5904 window->consumeFocusEvent(true);
5905
5906 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5907 mDispatcher->waitForIdle();
5908
5909 // System key is not passed down
5910 window->assertNoEvents();
5911
5912 // Should have poked user activity
5913 mFakePolicy->assertUserActivityPoked();
5914}
5915
5916TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
5917 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5918 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5919 "Fake Window", ADISPLAY_ID_DEFAULT);
5920
5921 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005922 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005923 setFocusedWindow(window);
5924
5925 window->consumeFocusEvent(true);
5926
5927 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5928 mDispatcher->waitForIdle();
5929
5930 // System key is not passed down
5931 window->assertNoEvents();
5932
5933 // Should have poked user activity
5934 mFakePolicy->assertUserActivityPoked();
5935}
5936
5937TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
5938 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5939 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5940 "Fake Window", ADISPLAY_ID_DEFAULT);
5941
5942 window->setDisableUserActivity(true);
5943 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005944 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005945 setFocusedWindow(window);
5946
5947 window->consumeFocusEvent(true);
5948
5949 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5950 mDispatcher->waitForIdle();
5951
5952 // System key is not passed down
5953 window->assertNoEvents();
5954
5955 // Should have poked user activity
5956 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005957}
5958
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005959TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
5960 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5961 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5962 "Fake Window", ADISPLAY_ID_DEFAULT);
5963
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005964 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005965
5966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005967 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005968 ADISPLAY_ID_DEFAULT, {100, 100}))
5969 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5970
5971 window->consumeMotionEvent(
5972 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
5973
5974 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005975 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005976 mFakePolicy->assertUserActivityPoked();
5977}
5978
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005979TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005980 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005981 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5982 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005983
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005984 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005985
Prabir Pradhan678438e2023-04-13 19:32:51 +00005986 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005987 mDispatcher->waitForIdle();
5988
5989 window->assertNoEvents();
5990}
5991
5992// If a window is touchable, but does not have focus, it should receive motion events, but not keys
5993TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07005994 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005995 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5996 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005997
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005998 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005999
6000 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00006001 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006002 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00006003 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6004 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006005
6006 // Window should receive only the motion event
6007 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6008 window->assertNoEvents(); // Key event or focus event will not be received
6009}
6010
arthurhungea3f4fc2020-12-21 23:18:53 +08006011TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
6012 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6013
arthurhungea3f4fc2020-12-21 23:18:53 +08006014 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006015 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
6016 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006017 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08006018
arthurhungea3f4fc2020-12-21 23:18:53 +08006019 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006020 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
6021 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006022 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08006023
6024 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006025 mDispatcher->onWindowInfosChanged(
6026 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
arthurhungea3f4fc2020-12-21 23:18:53 +08006027
6028 PointF pointInFirst = {300, 200};
6029 PointF pointInSecond = {300, 600};
6030
6031 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006032 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6033 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6034 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006035 // Only the first window should get the down event
6036 firstWindow->consumeMotionDown();
6037 secondWindow->assertNoEvents();
6038
6039 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006040 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6041 ADISPLAY_ID_DEFAULT,
6042 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006043 // The first window gets a move and the second a down
6044 firstWindow->consumeMotionMove();
6045 secondWindow->consumeMotionDown();
6046
6047 // Send pointer cancel to the second window
6048 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006049 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08006050 {pointInFirst, pointInSecond});
6051 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00006052 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08006053 // The first window gets move and the second gets cancel.
6054 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6055 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6056
6057 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006058 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6059 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08006060 // The first window gets up and the second gets nothing.
6061 firstWindow->consumeMotionUp();
6062 secondWindow->assertNoEvents();
6063}
6064
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006065TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
6066 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6067
6068 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006069 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006070 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006071 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
6072 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
6073 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
6074
Harry Cutts33476232023-01-30 19:57:29 +00006075 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006076 window->assertNoEvents();
6077 mDispatcher->waitForIdle();
6078}
6079
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006080using InputDispatcherMonitorTest = InputDispatcherTest;
6081
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006082/**
6083 * Two entities that receive touch: A window, and a global monitor.
6084 * The touch goes to the window, and then the window disappears.
6085 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
6086 * for the monitor, as well.
6087 * 1. foregroundWindow
6088 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
6089 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006090TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006091 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6092 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006093 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006094
Prabir Pradhanfb549072023-10-05 19:17:36 +00006095 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006096
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006097 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006099 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006100 {100, 200}))
6101 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6102
6103 // Both the foreground window and the global monitor should receive the touch down
6104 window->consumeMotionDown();
6105 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6106
6107 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006108 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006109 ADISPLAY_ID_DEFAULT, {110, 200}))
6110 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6111
6112 window->consumeMotionMove();
6113 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
6114
6115 // Now the foreground window goes away
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006116 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006117 window->consumeMotionCancel();
6118 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
6119
6120 // If more events come in, there will be no more foreground window to send them to. This will
6121 // cause a cancel for the monitor, as well.
6122 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006123 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006124 ADISPLAY_ID_DEFAULT, {120, 200}))
6125 << "Injection should fail because the window was removed";
6126 window->assertNoEvents();
6127 // Global monitor now gets the cancel
6128 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6129}
6130
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006131TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07006132 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006133 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6134 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006135 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006136
Prabir Pradhanfb549072023-10-05 19:17:36 +00006137 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006138
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006139 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006140 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006141 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00006142 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08006143 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006144}
6145
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006146TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
Prabir Pradhanfb549072023-10-05 19:17:36 +00006147 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006148
Chris Yea209fde2020-07-22 13:54:51 -07006149 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006150 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6151 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006152 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006153
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006154 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006155 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006156 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08006157 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006158 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006159
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006160 // Pilfer pointers from the monitor.
6161 // This should not do anything and the window should continue to receive events.
6162 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00006163
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006164 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006165 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006166 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006167 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006168
6169 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
6170 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006171}
6172
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006173TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07006174 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006175 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6176 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006177 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Evan Rosky84f07f02021-04-16 10:42:42 -07006178 window->setWindowOffset(20, 40);
6179 window->setWindowTransform(0, 1, -1, 0);
6180
Prabir Pradhanfb549072023-10-05 19:17:36 +00006181 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07006182
6183 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006184 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Evan Rosky84f07f02021-04-16 10:42:42 -07006185 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6186 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006187 std::unique_ptr<MotionEvent> event = monitor.consumeMotion();
6188 ASSERT_NE(nullptr, event);
Evan Rosky84f07f02021-04-16 10:42:42 -07006189 // Even though window has transform, gesture monitor must not.
6190 ASSERT_EQ(ui::Transform(), event->getTransform());
6191}
6192
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006193TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00006194 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhanfb549072023-10-05 19:17:36 +00006195 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00006196
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006197 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006198 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006199 << "Injection should fail if there is a monitor, but no touchable window";
6200 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00006201}
6202
Linnan Lid8150952024-01-26 18:07:17 +00006203/**
6204 * Two displays
6205 * The first monitor has a foreground window, a monitor
6206 * The second window has only one monitor.
6207 * We first inject a Down event into the first display, this injection should succeed and both
6208 * the foreground window and monitor should receive a down event, then inject a Down event into
6209 * the second display as well, this injection should fail, at this point, the first display
6210 * window and monitor should not receive a cancel or any other event.
6211 * Continue to inject Move and UP events to the first display, the events should be received
6212 * normally by the foreground window and monitor.
6213 */
6214TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) {
6215 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6216 sp<FakeWindowHandle> window =
6217 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6218
6219 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6220 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6221
6222 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6224 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6225 {100, 200}))
6226 << "The down event injected into the first display should succeed";
6227
6228 window->consumeMotionDown();
6229 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006230
6231 ASSERT_EQ(InputEventInjectionResult::FAILED,
6232 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6233 {100, 200}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006234 << "The down event injected into the second display should fail since there's no "
6235 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006236
6237 // Continue to inject event to first display.
6238 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6239 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6240 ADISPLAY_ID_DEFAULT, {110, 220}))
6241 << "The move event injected into the first display should succeed";
6242
6243 window->consumeMotionMove();
6244 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006245
6246 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6247 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6248 {110, 220}))
6249 << "The up event injected into the first display should succeed";
6250
6251 window->consumeMotionUp();
6252 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006253
6254 window->assertNoEvents();
6255 monitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00006256 secondMonitor.assertNoEvents();
6257}
6258
6259/**
6260 * Two displays
6261 * There is a monitor and foreground window on each display.
6262 * First, we inject down events into each of the two displays, at this point, the foreground windows
6263 * and monitors on both displays should receive down events.
6264 * At this point, the foreground window of the second display goes away, the gone window should
6265 * receive the cancel event, and the other windows and monitors should not receive any events.
6266 * Inject a move event into the second display. At this point, the injection should fail because
6267 * the second display no longer has a foreground window. At this point, the monitor on the second
6268 * display should receive a cancel event, and any windows or monitors on the first display should
6269 * not receive any events, and any subsequent injection of events into the second display should
6270 * also fail.
6271 * Continue to inject events into the first display, and the events should all be injected
6272 * successfully and received normally.
6273 */
6274TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) {
6275 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6276 sp<FakeWindowHandle> window =
6277 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6278 sp<FakeWindowHandle> secondWindow =
6279 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground",
6280 SECOND_DISPLAY_ID);
6281
6282 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6283 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6284
6285 // There is a foreground window on both displays.
6286 mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
6287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6288 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6289 {100, 200}))
6290 << "The down event injected into the first display should succeed";
6291
6292 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6293 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006294
6295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6296 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6297 {100, 200}))
6298 << "The down event injected into the second display should succeed";
6299
Linnan Lid8150952024-01-26 18:07:17 +00006300 secondWindow->consumeMotionDown(SECOND_DISPLAY_ID);
6301 secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID);
6302
6303 // Now second window is gone away.
6304 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6305
6306 // The gone window should receive a cancel, and the monitor on the second display should not
6307 // receive any events.
Linnan Lid8150952024-01-26 18:07:17 +00006308 secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID);
6309 secondMonitor.assertNoEvents();
6310
6311 ASSERT_EQ(InputEventInjectionResult::FAILED,
6312 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6313 SECOND_DISPLAY_ID, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006314 << "The move event injected into the second display should fail because there's no "
6315 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006316 // Now the monitor on the second display should receive a cancel event.
6317 secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID);
Linnan Lid8150952024-01-26 18:07:17 +00006318
6319 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6320 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6321 ADISPLAY_ID_DEFAULT, {110, 200}))
6322 << "The move event injected into the first display should succeed";
6323
6324 window->consumeMotionMove();
6325 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006326
6327 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006328 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6329 {110, 220}))
6330 << "The up event injected into the second display should fail because there's no "
6331 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006332
6333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6334 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6335 {110, 220}))
6336 << "The up event injected into the first display should succeed";
6337
6338 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6339 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006340
Linnan Lid8150952024-01-26 18:07:17 +00006341 window->assertNoEvents();
6342 monitor.assertNoEvents();
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006343 secondWindow->assertNoEvents();
6344 secondMonitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00006345}
6346
6347/**
6348 * One display with transform
6349 * There is a foreground window and a monitor on the display
6350 * Inject down event and move event sequentially, the foreground window and monitor can receive down
6351 * event and move event, then let the foreground window go away, the foreground window receives
6352 * cancel event, inject move event again, the monitor receives cancel event, all the events received
6353 * by the monitor should be with the same transform as the display
6354 */
6355TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) {
6356 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6357 sp<FakeWindowHandle> window =
6358 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6359 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6360
6361 ui::Transform transform;
6362 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6363
6364 gui::DisplayInfo displayInfo;
6365 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6366 displayInfo.transform = transform;
6367
6368 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
6369
6370 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6371 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6372 {100, 200}))
6373 << "The down event injected should succeed";
6374
6375 window->consumeMotionDown();
6376 std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion();
6377 EXPECT_EQ(transform, downMotionEvent->getTransform());
6378 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction());
6379
6380 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6381 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6382 ADISPLAY_ID_DEFAULT, {110, 220}))
6383 << "The move event injected should succeed";
6384
6385 window->consumeMotionMove();
6386 std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion();
6387 EXPECT_EQ(transform, moveMotionEvent->getTransform());
6388 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction());
6389
6390 // Let foreground window gone
6391 mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0});
6392
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006393 // Foreground window should receive a cancel event, but not the monitor.
Linnan Lid8150952024-01-26 18:07:17 +00006394 window->consumeMotionCancel();
Linnan Lid8150952024-01-26 18:07:17 +00006395
6396 ASSERT_EQ(InputEventInjectionResult::FAILED,
6397 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6398 ADISPLAY_ID_DEFAULT, {110, 220}))
6399 << "The move event injected should failed";
6400 // Now foreground should not receive any events, but monitor should receive a cancel event
6401 // with transform that same as display's display.
Linnan Lid8150952024-01-26 18:07:17 +00006402 std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion();
6403 EXPECT_EQ(transform, cancelMotionEvent->getTransform());
6404 EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId());
6405 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction());
6406
6407 // Other event inject to this display should fail.
6408 ASSERT_EQ(InputEventInjectionResult::FAILED,
6409 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6410 ADISPLAY_ID_DEFAULT, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006411 << "The up event injected should fail because the touched window was removed";
Linnan Lid8150952024-01-26 18:07:17 +00006412 window->assertNoEvents();
6413 monitor.assertNoEvents();
6414}
6415
chaviw81e2bb92019-12-18 15:03:51 -08006416TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006417 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006418 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6419 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08006420
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006421 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
chaviw81e2bb92019-12-18 15:03:51 -08006422
6423 NotifyMotionArgs motionArgs =
6424 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6425 ADISPLAY_ID_DEFAULT);
6426
Prabir Pradhan678438e2023-04-13 19:32:51 +00006427 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08006428 // Window should receive motion down event.
6429 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6430
6431 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08006432 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08006433 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6434 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
6435 motionArgs.pointerCoords[0].getX() - 10);
6436
Prabir Pradhan678438e2023-04-13 19:32:51 +00006437 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00006438 window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08006439}
6440
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006441/**
6442 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
6443 * the device default right away. In the test scenario, we check both the default value,
6444 * and the action of enabling / disabling.
6445 */
6446TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07006447 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006448 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6449 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08006450 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006451
6452 // Set focused application.
6453 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006454 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006455
6456 SCOPED_TRACE("Check default value of touch mode");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006457 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006458 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006459 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006460
6461 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07006462 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006463 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00006464 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006465
6466 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08006467 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00006468 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006469 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07006470 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006471 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006472 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006473 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006474
6475 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07006476 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006477 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00006478 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006479
6480 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08006481 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00006482 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006483 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07006484 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006485 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006486 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006487 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006488
6489 window->assertNoEvents();
6490}
6491
Gang Wange9087892020-01-07 12:17:14 -05006492TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006493 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006494 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6495 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05006496
6497 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006498 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05006499
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006500 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006501 setFocusedWindow(window);
6502
Harry Cutts33476232023-01-30 19:57:29 +00006503 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05006504
Prabir Pradhan678438e2023-04-13 19:32:51 +00006505 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
6506 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05006507
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006508 std::unique_ptr<KeyEvent> event = window->consumeKey();
6509 ASSERT_NE(event, nullptr);
6510 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Gang Wange9087892020-01-07 12:17:14 -05006511 ASSERT_NE(verified, nullptr);
6512 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
6513
6514 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
6515 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
6516 ASSERT_EQ(keyArgs.source, verified->source);
6517 ASSERT_EQ(keyArgs.displayId, verified->displayId);
6518
6519 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
6520
6521 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05006522 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006523 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05006524 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
6525 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
6526 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
6527 ASSERT_EQ(0, verifiedKey.repeatCount);
6528}
6529
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006530TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006531 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006532 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6533 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006534
6535 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6536
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006537 ui::Transform transform;
6538 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6539
6540 gui::DisplayInfo displayInfo;
6541 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6542 displayInfo.transform = transform;
6543
Patrick Williamsd828f302023-04-28 17:52:08 -05006544 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006545
Prabir Pradhan678438e2023-04-13 19:32:51 +00006546 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006547 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6548 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006549 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006550
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006551 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
6552 ASSERT_NE(nullptr, event);
6553 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006554 ASSERT_NE(verified, nullptr);
6555 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
6556
6557 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
6558 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
6559 EXPECT_EQ(motionArgs.source, verified->source);
6560 EXPECT_EQ(motionArgs.displayId, verified->displayId);
6561
6562 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
6563
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006564 const vec2 rawXY =
6565 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
6566 motionArgs.pointerCoords[0].getXYValue());
6567 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
6568 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006569 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006570 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006571 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006572 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
6573 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
6574}
6575
chaviw09c8d2d2020-08-24 15:48:26 -07006576/**
6577 * Ensure that separate calls to sign the same data are generating the same key.
6578 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
6579 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
6580 * tests.
6581 */
6582TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
6583 KeyEvent event = getTestKeyEvent();
6584 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6585
6586 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
6587 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
6588 ASSERT_EQ(hmac1, hmac2);
6589}
6590
6591/**
6592 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
6593 */
6594TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
6595 KeyEvent event = getTestKeyEvent();
6596 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6597 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
6598
6599 verifiedEvent.deviceId += 1;
6600 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6601
6602 verifiedEvent.source += 1;
6603 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6604
6605 verifiedEvent.eventTimeNanos += 1;
6606 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6607
6608 verifiedEvent.displayId += 1;
6609 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6610
6611 verifiedEvent.action += 1;
6612 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6613
6614 verifiedEvent.downTimeNanos += 1;
6615 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6616
6617 verifiedEvent.flags += 1;
6618 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6619
6620 verifiedEvent.keyCode += 1;
6621 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6622
6623 verifiedEvent.scanCode += 1;
6624 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6625
6626 verifiedEvent.metaState += 1;
6627 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6628
6629 verifiedEvent.repeatCount += 1;
6630 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6631}
6632
Vishnu Nair958da932020-08-21 17:12:37 -07006633TEST_F(InputDispatcherTest, SetFocusedWindow) {
6634 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6635 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006636 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006637 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006638 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006639 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6640
6641 // Top window is also focusable but is not granted focus.
6642 windowTop->setFocusable(true);
6643 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006644 mDispatcher->onWindowInfosChanged(
6645 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006646 setFocusedWindow(windowSecond);
6647
6648 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006649 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006650 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006651
6652 // Focused window should receive event.
6653 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6654 windowTop->assertNoEvents();
6655}
6656
6657TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
6658 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6659 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006660 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006661 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6662
6663 window->setFocusable(true);
6664 // Release channel for window is no longer valid.
6665 window->releaseChannel();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006666 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006667 setFocusedWindow(window);
6668
6669 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006670 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006671
6672 // window channel is invalid, so it should not receive any input event.
6673 window->assertNoEvents();
6674}
6675
6676TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
6677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6678 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006679 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006680 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07006681 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6682
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006683 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006684 setFocusedWindow(window);
6685
6686 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006687 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006688
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006689 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07006690 window->assertNoEvents();
6691}
6692
6693TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
6694 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6695 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006696 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006697 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006698 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006699 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6700
6701 windowTop->setFocusable(true);
6702 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006703 mDispatcher->onWindowInfosChanged(
6704 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006705 setFocusedWindow(windowTop);
6706 windowTop->consumeFocusEvent(true);
6707
Chavi Weingarten847e8512023-03-29 00:26:09 +00006708 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006709 mDispatcher->onWindowInfosChanged(
6710 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006711 windowSecond->consumeFocusEvent(true);
6712 windowTop->consumeFocusEvent(false);
6713
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006714 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006715 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006716
6717 // Focused window should receive event.
6718 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6719}
6720
Chavi Weingarten847e8512023-03-29 00:26:09 +00006721TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07006722 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6723 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006724 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006725 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006726 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006727 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6728
6729 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00006730 windowSecond->setFocusable(false);
6731 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006732 mDispatcher->onWindowInfosChanged(
6733 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Chavi Weingarten847e8512023-03-29 00:26:09 +00006734 setFocusedWindow(windowTop);
6735 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07006736
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006737 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Chavi Weingarten847e8512023-03-29 00:26:09 +00006738 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006739
6740 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00006741 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07006742 windowSecond->assertNoEvents();
6743}
6744
6745TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
6746 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6747 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006748 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006749 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006750 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
6751 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006752 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6753
6754 window->setFocusable(true);
6755 previousFocusedWindow->setFocusable(true);
6756 window->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006757 mDispatcher->onWindowInfosChanged(
6758 {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006759 setFocusedWindow(previousFocusedWindow);
6760 previousFocusedWindow->consumeFocusEvent(true);
6761
6762 // Requesting focus on invisible window takes focus from currently focused window.
6763 setFocusedWindow(window);
6764 previousFocusedWindow->consumeFocusEvent(false);
6765
6766 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006768 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
6769 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006770
6771 // Window does not get focus event or key down.
6772 window->assertNoEvents();
6773
6774 // Window becomes visible.
6775 window->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006776 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006777
6778 // Window receives focus event.
6779 window->consumeFocusEvent(true);
6780 // Focused window receives key down.
6781 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6782}
6783
Vishnu Nair599f1412021-06-21 10:39:58 -07006784TEST_F(InputDispatcherTest, DisplayRemoved) {
6785 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6786 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006787 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07006788 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6789
6790 // window is granted focus.
6791 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006792 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair599f1412021-06-21 10:39:58 -07006793 setFocusedWindow(window);
6794 window->consumeFocusEvent(true);
6795
6796 // When a display is removed window loses focus.
6797 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
6798 window->consumeFocusEvent(false);
6799}
6800
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006801/**
6802 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
6803 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
6804 * of the 'slipperyEnterWindow'.
6805 *
6806 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
6807 * a way so that the touched location is no longer covered by the top window.
6808 *
6809 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
6810 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
6811 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
6812 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
6813 * with ACTION_DOWN).
6814 * Thus, the touch has been transferred from the top window into the bottom window, because the top
6815 * window moved itself away from the touched location and had Flag::SLIPPERY.
6816 *
6817 * Even though the top window moved away from the touched location, it is still obscuring the bottom
6818 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
6819 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
6820 *
6821 * In this test, we ensure that the event received by the bottom window has
6822 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
6823 */
6824TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006825 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006826 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006827
6828 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6829 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6830
6831 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006832 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006833 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006834 // Make sure this one overlaps the bottom window
6835 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
6836 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
6837 // one. Windows with the same owner are not considered to be occluding each other.
6838 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
6839
6840 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006841 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006842 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
6843
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
6847 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00006848 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6849 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6850 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006851 slipperyExitWindow->consumeMotionDown();
6852 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006853 mDispatcher->onWindowInfosChanged(
6854 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006855
Prabir Pradhan678438e2023-04-13 19:32:51 +00006856 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
6857 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6858 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006859
6860 slipperyExitWindow->consumeMotionCancel();
6861
6862 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
6863 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6864}
6865
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006866/**
6867 * Two windows, one on the left and another on the right. The left window is slippery. The right
6868 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
6869 * touch moves from the left window into the right window, the gesture should continue to go to the
6870 * left window. Touch shouldn't slip because the right window can't receive touches. This test
6871 * reproduces a crash.
6872 */
6873TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
6874 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6875
6876 sp<FakeWindowHandle> leftSlipperyWindow =
6877 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6878 leftSlipperyWindow->setSlippery(true);
6879 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
6880
6881 sp<FakeWindowHandle> rightDropTouchesWindow =
6882 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6883 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
6884 rightDropTouchesWindow->setDropInput(true);
6885
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006886 mDispatcher->onWindowInfosChanged(
6887 {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006888
6889 // Start touch in the left window
6890 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6891 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6892 .build());
6893 leftSlipperyWindow->consumeMotionDown();
6894
6895 // And move it into the right window
6896 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6897 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
6898 .build());
6899
6900 // Since the right window isn't eligible to receive input, touch does not slip.
6901 // The left window continues to receive the gesture.
6902 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6903 rightDropTouchesWindow->assertNoEvents();
6904}
6905
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07006906/**
6907 * A single window is on screen first. Touch is injected into that window. Next, a second window
6908 * appears. Since the first window is slippery, touch will move from the first window to the second.
6909 */
6910TEST_F(InputDispatcherTest, InjectedTouchSlips) {
6911 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6912 sp<FakeWindowHandle> originalWindow =
6913 sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT);
6914 originalWindow->setFrame(Rect(0, 0, 200, 200));
6915 originalWindow->setSlippery(true);
6916
6917 sp<FakeWindowHandle> appearingWindow =
6918 sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT);
6919 appearingWindow->setFrame(Rect(0, 0, 200, 200));
6920
6921 mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0});
6922
6923 // Touch down on the original window
6924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6925 injectMotionEvent(*mDispatcher,
6926 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6927 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
6928 .build()));
6929 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6930
6931 // Now, a new window appears. This could be, for example, a notification shade that appears
6932 // after user starts to drag down on the launcher window.
6933 mDispatcher->onWindowInfosChanged(
6934 {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0});
6935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6936 injectMotionEvent(*mDispatcher,
6937 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6938 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110))
6939 .build()));
6940 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
6941 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6942 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6943 injectMotionEvent(*mDispatcher,
6944 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6945 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
6946 .build()));
6947 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6948
6949 originalWindow->assertNoEvents();
6950 appearingWindow->assertNoEvents();
6951}
6952
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006953TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006954 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006955 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6956
6957 sp<FakeWindowHandle> leftWindow =
6958 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6959 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006960 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006961
6962 sp<FakeWindowHandle> rightSpy =
6963 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
6964 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006965 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006966 rightSpy->setSpy(true);
6967 rightSpy->setTrustedOverlay(true);
6968
6969 sp<FakeWindowHandle> rightWindow =
6970 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6971 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006972 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006973
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006974 mDispatcher->onWindowInfosChanged(
6975 {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006976
6977 // Touch in the left window
6978 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6979 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6980 .build());
6981 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
6982 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006983 ASSERT_NO_FATAL_FAILURE(
6984 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006985
6986 // Touch another finger over the right windows
6987 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6988 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6989 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6990 .build());
6991 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
6992 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
6993 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
6994 mDispatcher->waitForIdle();
6995 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006996 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
6997 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006998
6999 // Release finger over left window. The UP actions are not treated as device interaction.
7000 // The windows that did not receive the UP pointer will receive MOVE events, but since this
7001 // is part of the UP action, we do not treat this as device interaction.
7002 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
7003 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7004 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7005 .build());
7006 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
7007 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
7008 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7009 mDispatcher->waitForIdle();
7010 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7011
7012 // Move remaining finger
7013 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7014 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7015 .build());
7016 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
7017 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7018 mDispatcher->waitForIdle();
7019 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007020 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007021
7022 // Release all fingers
7023 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7024 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7025 .build());
7026 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
7027 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
7028 mDispatcher->waitForIdle();
7029 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7030}
7031
7032TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
7033 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7034
7035 sp<FakeWindowHandle> window =
7036 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
7037 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007038 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007039
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007040 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007041 setFocusedWindow(window);
7042 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
7043
7044 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
7045 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
7046 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007047 ASSERT_NO_FATAL_FAILURE(
7048 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007049
7050 // The UP actions are not treated as device interaction.
7051 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
7052 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
7053 mDispatcher->waitForIdle();
7054 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7055}
7056
Prabir Pradhan5893d362023-11-17 04:30:40 +00007057TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) {
7058 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7059
7060 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
7061 ADISPLAY_ID_DEFAULT);
7062 left->setFrame(Rect(0, 0, 100, 100));
7063 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
7064 "Right Window", ADISPLAY_ID_DEFAULT);
7065 right->setFrame(Rect(100, 0, 200, 100));
7066 sp<FakeWindowHandle> spy =
7067 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
7068 spy->setFrame(Rect(0, 0, 200, 100));
7069 spy->setTrustedOverlay(true);
7070 spy->setSpy(true);
7071
7072 mDispatcher->onWindowInfosChanged(
7073 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
7074
7075 // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId.
7076 NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
7077 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
7078 mDispatcher->notifyMotion(notifyArgs);
7079
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007080 std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00007081 AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)),
7082 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007083 ASSERT_NE(nullptr, leftEnter);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007084 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
7085 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007086 Not(WithEventId(leftEnter->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00007087 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
7088
7089 // Send move to the right window, and ensure hover exit and enter are synthesized with new ids.
7090 notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
7091 {PointF{150, 50}});
7092 mDispatcher->notifyMotion(notifyArgs);
7093
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007094 std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00007095 AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)),
7096 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007097 ASSERT_NE(nullptr, leftExit);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007098 right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
7099 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007100 Not(WithEventId(leftExit->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00007101 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
7102
7103 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id)));
7104}
7105
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00007106class InputDispatcherFallbackKeyTest : public InputDispatcherTest {
7107protected:
7108 std::shared_ptr<FakeApplicationHandle> mApp;
7109 sp<FakeWindowHandle> mWindow;
7110
7111 virtual void SetUp() override {
7112 InputDispatcherTest::SetUp();
7113
7114 mApp = std::make_shared<FakeApplicationHandle>();
7115
7116 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
7117 mWindow->setFrame(Rect(0, 0, 100, 100));
7118
7119 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
7120 setFocusedWindow(mWindow);
7121 ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true));
7122 }
7123
7124 void setFallback(int32_t keycode) {
7125 mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) {
7126 return KeyEventBuilder(event).keyCode(keycode).build();
7127 });
7128 }
7129
7130 void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007131 std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled);
7132 ASSERT_NE(nullptr, event);
7133 ASSERT_THAT(*event, matcher);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00007134 }
7135};
7136
7137TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) {
7138 mDispatcher->notifyKey(
7139 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7140 consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
7141 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7142}
7143
7144TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) {
7145 mDispatcher->notifyKey(
7146 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7147 consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
7148 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7149}
7150
7151TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) {
7152 mDispatcher->notifyKey(
7153 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7154
7155 // Do not handle this key event.
7156 consumeKey(/*handled=*/false,
7157 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7158 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7159
7160 // Since the policy did not request any fallback to be generated, ensure there are no events.
7161 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7162}
7163
7164TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) {
7165 setFallback(AKEYCODE_B);
7166 mDispatcher->notifyKey(
7167 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7168
7169 // Do not handle this key event.
7170 consumeKey(/*handled=*/false,
7171 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7172
7173 // Since the key was not handled, ensure the fallback event was dispatched instead.
7174 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7175 consumeKey(/*handled=*/true,
7176 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7177 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7178
7179 // Release the original key, and ensure the fallback key is also released.
7180 mDispatcher->notifyKey(
7181 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7182 consumeKey(/*handled=*/false,
7183 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7184 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7185 consumeKey(/*handled=*/true,
7186 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7187 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7188
7189 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7190 mWindow->assertNoEvents();
7191}
7192
7193TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) {
7194 setFallback(AKEYCODE_B);
7195 mDispatcher->notifyKey(
7196 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7197
7198 // Do not handle this key event, but handle the fallback.
7199 consumeKey(/*handled=*/false,
7200 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7201 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7202 consumeKey(/*handled=*/true,
7203 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7204 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7205
7206 // Release the original key, and ensure the fallback key is also released.
7207 mDispatcher->notifyKey(
7208 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7209 // But this time, the app handles the original key.
7210 consumeKey(/*handled=*/true,
7211 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7212 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7213 // Ensure the fallback key is canceled.
7214 consumeKey(/*handled=*/true,
7215 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7216 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7217
7218 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7219 mWindow->assertNoEvents();
7220}
7221
7222TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) {
7223 setFallback(AKEYCODE_B);
7224 mDispatcher->notifyKey(
7225 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7226
7227 // Do not handle this key event.
7228 consumeKey(/*handled=*/false,
7229 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7230 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7231 // App does not handle the fallback either, so ensure another fallback is not generated.
7232 setFallback(AKEYCODE_C);
7233 consumeKey(/*handled=*/false,
7234 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7235 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7236
7237 // Release the original key, and ensure the fallback key is also released.
7238 setFallback(AKEYCODE_B);
7239 mDispatcher->notifyKey(
7240 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7241 consumeKey(/*handled=*/false,
7242 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7243 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7244 consumeKey(/*handled=*/false,
7245 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7246 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7247
7248 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7249 mWindow->assertNoEvents();
7250}
7251
7252TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) {
7253 setFallback(AKEYCODE_B);
7254 mDispatcher->notifyKey(
7255 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7256
7257 // Do not handle this key event, so fallback is generated.
7258 consumeKey(/*handled=*/false,
7259 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7260 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7261 consumeKey(/*handled=*/true,
7262 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7263 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7264
7265 // Release the original key, but assume the policy is misbehaving and it
7266 // generates an inconsistent fallback to the one from the DOWN event.
7267 setFallback(AKEYCODE_C);
7268 mDispatcher->notifyKey(
7269 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7270 consumeKey(/*handled=*/false,
7271 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7272 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7273 // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency.
7274 consumeKey(/*handled=*/true,
7275 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7276 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7277
7278 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7279 mWindow->assertNoEvents();
7280}
7281
7282TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) {
7283 setFallback(AKEYCODE_B);
7284 mDispatcher->notifyKey(
7285 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7286
7287 // Do not handle this key event, so fallback is generated.
7288 consumeKey(/*handled=*/false,
7289 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7290 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7291 consumeKey(/*handled=*/true,
7292 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7293 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7294
7295 // The original key is canceled.
7296 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD)
7297 .keyCode(AKEYCODE_A)
7298 .addFlag(AKEY_EVENT_FLAG_CANCELED)
7299 .build());
7300 consumeKey(/*handled=*/false,
7301 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
7302 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7303 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7304 // Ensure the fallback key is also canceled due to the original key being canceled.
7305 consumeKey(/*handled=*/true,
7306 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7307 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7308
7309 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7310 mWindow->assertNoEvents();
7311}
7312
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00007313TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) {
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00007314 setFallback(AKEYCODE_B);
7315 mDispatcher->notifyKey(
7316 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7317
7318 // Do not handle this key event.
7319 consumeKey(/*handled=*/false,
7320 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7321 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7322 consumeKey(/*handled=*/true,
7323 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7324 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7325
7326 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
7327 // When the unhandled key is reported to the policy next, remove the input channel.
7328 mDispatcher->removeInputChannel(mWindow->getToken());
7329 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
7330 });
7331 // Release the original key, and let the app now handle the previously unhandled key.
7332 // This should result in the previously generated fallback key to be cancelled.
7333 // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified
7334 // of the UP event for consistency. The Dispatcher calls into the policy from its own thread
7335 // without holding the lock, because it need to synchronously fetch the fallback key. While in
7336 // the policy call, we will now remove the input channel. Once the policy call returns, the
7337 // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does
7338 // not cause any crashes.
7339 mDispatcher->notifyKey(
7340 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7341 consumeKey(/*handled=*/true,
7342 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7343 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7344}
7345
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00007346TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) {
7347 setFallback(AKEYCODE_B);
7348 mDispatcher->notifyKey(
7349 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7350
7351 // Do not handle this key event.
7352 consumeKey(/*handled=*/false,
7353 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7355 consumeKey(/*handled=*/true,
7356 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7357 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7358
7359 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
7360 // When the unhandled key is reported to the policy next, remove the window.
7361 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
7362 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
7363 });
7364 // Release the original key, which the app will not handle. When this unhandled key is reported
7365 // to the policy, the window will be removed.
7366 mDispatcher->notifyKey(
7367 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7368 consumeKey(/*handled=*/false,
7369 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7370 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7371
7372 // Since the window was removed, it loses focus, and the channel state will be reset.
7373 consumeKey(/*handled=*/true,
7374 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7375 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7376 mWindow->consumeFocusEvent(false);
7377 mWindow->assertNoEvents();
7378}
7379
7380TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) {
7381 setFallback(AKEYCODE_B);
7382 mDispatcher->notifyKey(
7383 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7384
7385 // Do not handle this key event.
7386 consumeKey(/*handled=*/false,
7387 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7388 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7389 const auto [seq, event] = mWindow->receiveEvent();
7390 ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event";
7391 ASSERT_EQ(event->getType(), InputEventType::KEY);
7392 ASSERT_THAT(static_cast<const KeyEvent&>(*event),
7393 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7394 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7395
7396 // Remove the window now, which should generate a cancellations and make the window lose focus.
7397 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
7398 consumeKey(/*handled=*/true,
7399 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
7400 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7401 consumeKey(/*handled=*/true,
7402 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7403 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7404 mWindow->consumeFocusEvent(false);
7405
7406 // Finish the event by reporting it as handled.
7407 mWindow->finishEvent(*seq);
7408 mWindow->assertNoEvents();
7409}
7410
Garfield Tan1c7bc862020-01-28 13:24:04 -08007411class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
7412protected:
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007413 static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms;
7414 static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007415
Chris Yea209fde2020-07-22 13:54:51 -07007416 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007417 sp<FakeWindowHandle> mWindow;
7418
7419 virtual void SetUp() override {
Prabir Pradhandae52792023-12-15 07:36:40 +00007420 InputDispatcherTest::SetUp();
Garfield Tan1c7bc862020-01-28 13:24:04 -08007421
Prabir Pradhandae52792023-12-15 07:36:40 +00007422 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007423 setUpWindow();
7424 }
7425
7426 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07007427 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007428 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007429
Vishnu Nair47074b82020-08-14 11:54:47 -07007430 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007431 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007432 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007433 mWindow->consumeFocusEvent(true);
7434 }
7435
Chris Ye2ad95392020-09-01 13:44:44 -07007436 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08007437 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07007438 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007439 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00007440 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007441
7442 // Window should receive key down event.
7443 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7444 }
7445
7446 void expectKeyRepeatOnce(int32_t repeatCount) {
7447 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007448 mWindow->consumeKeyEvent(
7449 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount)));
Garfield Tan1c7bc862020-01-28 13:24:04 -08007450 }
7451
Chris Ye2ad95392020-09-01 13:44:44 -07007452 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08007453 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07007454 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007455 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00007456 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007457
7458 // Window should receive key down event.
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00007459 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007460 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007461 }
7462};
7463
7464TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00007465 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007466 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7467 expectKeyRepeatOnce(repeatCount);
7468 }
7469}
7470
7471TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00007472 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007473 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7474 expectKeyRepeatOnce(repeatCount);
7475 }
Harry Cutts33476232023-01-30 19:57:29 +00007476 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007477 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08007478 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7479 expectKeyRepeatOnce(repeatCount);
7480 }
7481}
7482
7483TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007484 sendAndConsumeKeyDown(/*deviceId=*/1);
7485 expectKeyRepeatOnce(/*repeatCount=*/1);
7486 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007487 mWindow->assertNoEvents();
7488}
7489
7490TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007491 sendAndConsumeKeyDown(/*deviceId=*/1);
7492 expectKeyRepeatOnce(/*repeatCount=*/1);
7493 sendAndConsumeKeyDown(/*deviceId=*/2);
7494 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007495 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00007496 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007497 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00007498 expectKeyRepeatOnce(/*repeatCount=*/2);
7499 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07007500 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00007501 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007502 mWindow->assertNoEvents();
7503}
7504
7505TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007506 sendAndConsumeKeyDown(/*deviceId=*/1);
7507 expectKeyRepeatOnce(/*repeatCount=*/1);
7508 sendAndConsumeKeyDown(/*deviceId=*/2);
7509 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007510 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00007511 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007512 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08007513 mWindow->assertNoEvents();
7514}
7515
liushenxiang42232912021-05-21 20:24:09 +08007516TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
7517 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00007518 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007519 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08007520 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
7521 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
7522 mWindow->assertNoEvents();
7523}
7524
Garfield Tan1c7bc862020-01-28 13:24:04 -08007525TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00007526 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00007527 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007528 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007529 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
7530 ASSERT_NE(nullptr, repeatEvent);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007531 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007532 IdGenerator::getSource(repeatEvent->getId()));
Garfield Tan1c7bc862020-01-28 13:24:04 -08007533 }
7534}
7535
7536TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00007537 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00007538 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007539
7540 std::unordered_set<int32_t> idSet;
7541 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007542 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
7543 ASSERT_NE(nullptr, repeatEvent);
7544 int32_t id = repeatEvent->getId();
Garfield Tan1c7bc862020-01-28 13:24:04 -08007545 EXPECT_EQ(idSet.end(), idSet.find(id));
7546 idSet.insert(id);
7547 }
7548}
7549
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007550/* Test InputDispatcher for MultiDisplay */
7551class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
7552public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007553 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007554 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08007555
Chris Yea209fde2020-07-22 13:54:51 -07007556 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007557 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007558 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007559
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007560 // Set focus window for primary display, but focused display would be second one.
7561 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07007562 windowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007563 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
7564
Vishnu Nair958da932020-08-21 17:12:37 -07007565 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007566 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08007567
Chris Yea209fde2020-07-22 13:54:51 -07007568 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007569 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007570 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007571 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007572 // Set focus display to second one.
7573 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
7574 // Set focus window for second display.
7575 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07007576 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007577 mDispatcher->onWindowInfosChanged(
7578 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007579 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007580 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007581 }
7582
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007583 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007584 InputDispatcherTest::TearDown();
7585
Chris Yea209fde2020-07-22 13:54:51 -07007586 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007587 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07007588 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007589 windowInSecondary.clear();
7590 }
7591
7592protected:
Chris Yea209fde2020-07-22 13:54:51 -07007593 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007594 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07007595 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007596 sp<FakeWindowHandle> windowInSecondary;
7597};
7598
7599TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
7600 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007601 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007602 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007603 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007604 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08007605 windowInSecondary->assertNoEvents();
7606
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007607 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007608 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007609 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007610 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08007611 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007612 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08007613}
7614
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007615TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08007616 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08007617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007618 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007619 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007620 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08007621 windowInSecondary->assertNoEvents();
7622
7623 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007625 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08007626 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007627 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08007628
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007629 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007630 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08007631
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007632 // Old focus should receive a cancel event.
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00007633 windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08007634
7635 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007636 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08007637 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007638 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08007639 windowInSecondary->assertNoEvents();
7640}
7641
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007642// Test per-display input monitors for motion event.
7643TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08007644 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007645 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007646 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007647 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007648
7649 // Test touch down on primary 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, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007652 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007653 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007654 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007655 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007656 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007657
7658 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007660 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007662 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007663 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007664 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08007665 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007666
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007667 // Lift up the touch from the second display
7668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007669 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7671 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
7672 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
7673
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007674 // Test inject a non-pointer motion event.
7675 // If specific a display, it will dispatch to the focused window of particular display,
7676 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007678 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007679 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007680 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007681 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007682 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007683 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007684}
7685
7686// Test per-display input monitors for key event.
7687TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007688 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08007689 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007690 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007691 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007692 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007693
7694 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007696 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007697 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007698 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007699 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007700 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007701}
7702
Vishnu Nair958da932020-08-21 17:12:37 -07007703TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
7704 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007705 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007706 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007707 mDispatcher->onWindowInfosChanged(
7708 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
7709 *windowInSecondary->getInfo()},
7710 {},
7711 0,
7712 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007713 setFocusedWindow(secondWindowInPrimary);
7714 windowInPrimary->consumeFocusEvent(false);
7715 secondWindowInPrimary->consumeFocusEvent(true);
7716
7717 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7719 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007720 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007721 windowInPrimary->assertNoEvents();
7722 windowInSecondary->assertNoEvents();
7723 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7724}
7725
Arthur Hungdfd528e2021-12-08 13:23:04 +00007726TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
7727 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007728 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007729 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007730 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007731
7732 // Test touch down on primary display.
7733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007734 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007735 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7736 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7737 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
7738
7739 // Test touch down on second display.
7740 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007741 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007742 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7743 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
7744 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
7745
7746 // Trigger cancel touch.
7747 mDispatcher->cancelCurrentTouch();
7748 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7749 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7750 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
7751 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
7752
7753 // Test inject a move motion event, no window/monitor should receive the event.
7754 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007755 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007756 ADISPLAY_ID_DEFAULT, {110, 200}))
7757 << "Inject motion event should return InputEventInjectionResult::FAILED";
7758 windowInPrimary->assertNoEvents();
7759 monitorInPrimary.assertNoEvents();
7760
7761 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007762 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007763 SECOND_DISPLAY_ID, {110, 200}))
7764 << "Inject motion event should return InputEventInjectionResult::FAILED";
7765 windowInSecondary->assertNoEvents();
7766 monitorInSecondary.assertNoEvents();
7767}
7768
Hu Guocb134f12023-12-23 13:42:44 +00007769/**
7770 * Send a key to the primary display and to the secondary display.
7771 * Then cause the key on the primary display to be canceled by sending in a stale key.
7772 * Ensure that the key on the primary display is canceled, and that the key on the secondary display
7773 * does not get canceled.
7774 */
7775TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) {
7776 // Send a key down on primary display
7777 mDispatcher->notifyKey(
7778 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
7779 .displayId(ADISPLAY_ID_DEFAULT)
7780 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7781 .build());
7782 windowInPrimary->consumeKeyEvent(
7783 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
7784 windowInSecondary->assertNoEvents();
7785
7786 // Send a key down on second display
7787 mDispatcher->notifyKey(
7788 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
7789 .displayId(SECOND_DISPLAY_ID)
7790 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7791 .build());
7792 windowInSecondary->consumeKeyEvent(
7793 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
7794 windowInPrimary->assertNoEvents();
7795
7796 // Send a valid key up event on primary display that will be dropped because it is stale
7797 NotifyKeyArgs staleKeyUp =
7798 KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
7799 .displayId(ADISPLAY_ID_DEFAULT)
7800 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7801 .build();
7802 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
7803 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
7804 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
7805 mDispatcher->notifyKey(staleKeyUp);
7806
7807 // Only the key gesture corresponding to the dropped event should receive the cancel event.
7808 // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not
7809 // receive any events.
7810 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
7811 WithDisplayId(ADISPLAY_ID_DEFAULT),
7812 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7813 windowInSecondary->assertNoEvents();
7814}
7815
7816/**
7817 * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events.
7818 */
7819TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) {
7820 // Send touch down on primary display.
7821 mDispatcher->notifyMotion(
7822 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7823 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7824 .displayId(ADISPLAY_ID_DEFAULT)
7825 .build());
7826 windowInPrimary->consumeMotionEvent(
7827 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
7828 windowInSecondary->assertNoEvents();
7829
7830 // Send touch down on second display.
7831 mDispatcher->notifyMotion(
7832 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7833 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7834 .displayId(SECOND_DISPLAY_ID)
7835 .build());
7836 windowInPrimary->assertNoEvents();
7837 windowInSecondary->consumeMotionEvent(
7838 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
7839
7840 // inject a valid MotionEvent on primary display that will be stale when it arrives.
7841 NotifyMotionArgs staleMotionUp =
7842 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7843 .displayId(ADISPLAY_ID_DEFAULT)
7844 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7845 .build();
7846 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
7847 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
7848 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
7849 mDispatcher->notifyMotion(staleMotionUp);
7850
7851 // For stale motion events, we let the gesture to complete. This behaviour is different from key
7852 // events, where we would cancel the current keys instead.
7853 windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP));
7854 windowInSecondary->assertNoEvents();
7855}
7856
Jackal Guof9696682018-10-05 12:23:23 +08007857class InputFilterTest : public InputDispatcherTest {
7858protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007859 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
7860 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08007861 NotifyMotionArgs motionArgs;
7862
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007863 motionArgs =
7864 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007865 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007866 motionArgs =
7867 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007868 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007869 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007870 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007871 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007872 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08007873 } else {
7874 mFakePolicy->assertFilterInputEventWasNotCalled();
7875 }
7876 }
7877
7878 void testNotifyKey(bool expectToBeFiltered) {
7879 NotifyKeyArgs keyArgs;
7880
7881 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007882 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007883 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007884 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007885 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007886
7887 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08007888 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007889 } else {
7890 mFakePolicy->assertFilterInputEventWasNotCalled();
7891 }
7892 }
7893};
7894
7895// Test InputFilter for MotionEvent
7896TEST_F(InputFilterTest, MotionEvent_InputFilter) {
7897 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007898 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7899 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007900
7901 // Enable InputFilter
7902 mDispatcher->setInputFilterEnabled(true);
7903 // Test touch on both primary and second display, and check if both events are filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007904 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true);
7905 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007906
7907 // Disable InputFilter
7908 mDispatcher->setInputFilterEnabled(false);
7909 // Test touch on both primary and second display, and check if both events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007910 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7911 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007912}
7913
7914// Test InputFilter for KeyEvent
7915TEST_F(InputFilterTest, KeyEvent_InputFilter) {
7916 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007917 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007918
7919 // Enable InputFilter
7920 mDispatcher->setInputFilterEnabled(true);
7921 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007922 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007923
7924 // Disable InputFilter
7925 mDispatcher->setInputFilterEnabled(false);
7926 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007927 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007928}
7929
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007930// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
7931// logical display coordinate space.
7932TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
7933 ui::Transform firstDisplayTransform;
7934 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7935 ui::Transform secondDisplayTransform;
7936 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
7937
7938 std::vector<gui::DisplayInfo> displayInfos(2);
7939 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
7940 displayInfos[0].transform = firstDisplayTransform;
7941 displayInfos[1].displayId = SECOND_DISPLAY_ID;
7942 displayInfos[1].transform = secondDisplayTransform;
7943
Patrick Williamsd828f302023-04-28 17:52:08 -05007944 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007945
7946 // Enable InputFilter
7947 mDispatcher->setInputFilterEnabled(true);
7948
7949 // Ensure the correct transforms are used for the displays.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007950 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform);
7951 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007952}
7953
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007954class InputFilterInjectionPolicyTest : public InputDispatcherTest {
7955protected:
7956 virtual void SetUp() override {
7957 InputDispatcherTest::SetUp();
7958
7959 /**
7960 * We don't need to enable input filter to test the injected event policy, but we enabled it
7961 * here to make the tests more realistic, since this policy only matters when inputfilter is
7962 * on.
7963 */
7964 mDispatcher->setInputFilterEnabled(true);
7965
7966 std::shared_ptr<InputApplicationHandle> application =
7967 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007968 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
7969 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007970
7971 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7972 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007973 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007974 setFocusedWindow(mWindow);
7975 mWindow->consumeFocusEvent(true);
7976 }
7977
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007978 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7979 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007980 KeyEvent event;
7981
7982 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7983 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
7984 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00007985 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007986 const int32_t additionalPolicyFlags =
7987 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
7988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007989 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00007990 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007991 policyFlags | additionalPolicyFlags));
7992
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007993 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007994 }
7995
7996 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7997 int32_t flags) {
7998 MotionEvent event;
7999 PointerProperties pointerProperties[1];
8000 PointerCoords pointerCoords[1];
8001 pointerProperties[0].clear();
8002 pointerProperties[0].id = 0;
8003 pointerCoords[0].clear();
8004 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
8005 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
8006
8007 ui::Transform identityTransform;
8008 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
8009 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
8010 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
8011 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
8012 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07008013 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07008014 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00008015 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008016
8017 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
8018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00008019 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00008020 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008021 policyFlags | additionalPolicyFlags));
8022
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008023 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008024 }
8025
8026private:
8027 sp<FakeWindowHandle> mWindow;
8028};
8029
8030TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008031 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
8032 // filter. Without it, the event will no different from a regularly injected event, and the
8033 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00008034 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
8035 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008036}
8037
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008038TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008039 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00008040 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008041 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
8042}
8043
8044TEST_F(InputFilterInjectionPolicyTest,
8045 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
8046 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00008047 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008048 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008049}
8050
8051TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00008052 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
8053 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008054}
8055
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08008056class InputDispatcherUserActivityPokeTests : public InputDispatcherTest {
8057protected:
8058 virtual void SetUp() override {
8059 InputDispatcherTest::SetUp();
8060
8061 std::shared_ptr<FakeApplicationHandle> application =
8062 std::make_shared<FakeApplicationHandle>();
8063 application->setDispatchingTimeout(100ms);
8064 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
8065 ADISPLAY_ID_DEFAULT);
Yeabkal Wubshit222d83d2024-01-24 18:00:09 +00008066 mWindow->setFrame(Rect(0, 0, 100, 100));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08008067 mWindow->setDispatchingTimeout(100ms);
8068 mWindow->setFocusable(true);
8069
8070 // Set focused application.
8071 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8072
8073 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
8074 setFocusedWindow(mWindow);
8075 mWindow->consumeFocusEvent(true);
8076 }
8077
8078 void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId,
8079 nsecs_t eventTime) {
8080 mDispatcher->notifyMotion(MotionArgsBuilder(action, source)
8081 .displayId(displayId)
8082 .eventTime(eventTime)
8083 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8084 .build());
8085 mWindow->consumeMotionEvent(WithMotionAction(action));
8086 }
8087
8088private:
8089 sp<FakeWindowHandle> mWindow;
8090};
8091
8092TEST_F_WITH_FLAGS(
8093 InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
8094 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8095 rate_limit_user_activity_poke_in_dispatcher))) {
8096 mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
8097
8098 // First event of type TOUCH. Should poke.
8099 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8100 milliseconds_to_nanoseconds(50));
8101 mFakePolicy->assertUserActivityPoked(
8102 {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8103
8104 // 80ns > 50ns has passed since previous TOUCH event. Should poke.
8105 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8106 milliseconds_to_nanoseconds(130));
8107 mFakePolicy->assertUserActivityPoked(
8108 {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8109
8110 // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
8111 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8112 milliseconds_to_nanoseconds(135));
8113 mFakePolicy->assertUserActivityPoked(
8114 {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}});
8115
8116 // Within 50ns of previous TOUCH event. Should NOT poke.
8117 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8118 milliseconds_to_nanoseconds(140));
8119 mFakePolicy->assertUserActivityNotPoked();
8120
8121 // Within 50ns of previous OTHER event. Should NOT poke.
8122 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8123 milliseconds_to_nanoseconds(150));
8124 mFakePolicy->assertUserActivityNotPoked();
8125
8126 // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
8127 // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
8128 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
8129 milliseconds_to_nanoseconds(160));
8130 mFakePolicy->assertUserActivityNotPoked();
8131
8132 // 65ns > 50ns has passed since previous OTHER event. Should poke.
8133 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8134 milliseconds_to_nanoseconds(200));
8135 mFakePolicy->assertUserActivityPoked(
8136 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}});
8137
8138 // 170ns > 50ns has passed since previous TOUCH event. Should poke.
8139 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
8140 milliseconds_to_nanoseconds(300));
8141 mFakePolicy->assertUserActivityPoked(
8142 {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8143
8144 // Assert that there's no more user activity poke event.
8145 mFakePolicy->assertUserActivityNotPoked();
8146}
8147
8148TEST_F_WITH_FLAGS(
8149 InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
8150 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8151 rate_limit_user_activity_poke_in_dispatcher))) {
8152 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8153 milliseconds_to_nanoseconds(200));
8154 mFakePolicy->assertUserActivityPoked(
8155 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8156
8157 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8158 milliseconds_to_nanoseconds(280));
8159 mFakePolicy->assertUserActivityNotPoked();
8160
8161 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8162 milliseconds_to_nanoseconds(340));
8163 mFakePolicy->assertUserActivityPoked(
8164 {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8165}
8166
8167TEST_F_WITH_FLAGS(
8168 InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
8169 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8170 rate_limit_user_activity_poke_in_dispatcher))) {
8171 mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
8172
8173 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20);
8174 mFakePolicy->assertUserActivityPoked();
8175
8176 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30);
8177 mFakePolicy->assertUserActivityPoked();
8178}
8179
chaviwfd6d3512019-03-25 13:23:49 -07008180class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008181 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07008182 InputDispatcherTest::SetUp();
8183
Chris Yea209fde2020-07-22 13:54:51 -07008184 std::shared_ptr<FakeApplicationHandle> application =
8185 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008186 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008187 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07008188 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07008189
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008190 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008191 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008192 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07008193
8194 // Set focused application.
8195 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07008196 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07008197
8198 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008199 mDispatcher->onWindowInfosChanged(
8200 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008201 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008202 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07008203 }
8204
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008205 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07008206 InputDispatcherTest::TearDown();
8207
8208 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008209 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07008210 }
8211
8212protected:
8213 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008214 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008215 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07008216};
8217
8218// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
8219// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
8220// the onPointerDownOutsideFocus callback.
8221TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008223 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008224 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008225 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008226 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008227
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008228 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07008229 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
8230}
8231
8232// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
8233// DOWN on the window that doesn't have focus. Ensure no window received the
8234// onPointerDownOutsideFocus callback.
8235TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008237 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT,
8238 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008239 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008240 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008241
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008242 ASSERT_TRUE(mDispatcher->waitForIdle());
8243 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008244}
8245
8246// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
8247// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
8248TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08008249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008250 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008251 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008252 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07008253
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008254 ASSERT_TRUE(mDispatcher->waitForIdle());
8255 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008256}
8257
8258// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
8259// DOWN on the window that already has focus. Ensure no window received the
8260// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008261TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008262 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008263 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008264 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008265 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008266 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008267
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008268 ASSERT_TRUE(mDispatcher->waitForIdle());
8269 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008270}
8271
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008272// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
8273// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
8274TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
8275 const MotionEvent event =
8276 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8277 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008278 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008279 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
8280 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008281 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008282 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8283 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
8284
8285 ASSERT_TRUE(mDispatcher->waitForIdle());
8286 mFakePolicy->assertOnPointerDownWasNotCalled();
8287 // Ensure that the unfocused window did not receive any FOCUS events.
8288 mUnfocusedWindow->assertNoEvents();
8289}
8290
chaviwaf87b3e2019-10-01 16:59:28 -07008291// These tests ensures we can send touch events to a single client when there are multiple input
8292// windows that point to the same client token.
8293class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
8294 virtual void SetUp() override {
8295 InputDispatcherTest::SetUp();
8296
Chris Yea209fde2020-07-22 13:54:51 -07008297 std::shared_ptr<FakeApplicationHandle> application =
8298 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008299 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
8300 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07008301 mWindow1->setFrame(Rect(0, 0, 100, 100));
8302
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00008303 mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07008304 mWindow2->setFrame(Rect(100, 100, 200, 200));
8305
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008306 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07008307 }
8308
8309protected:
8310 sp<FakeWindowHandle> mWindow1;
8311 sp<FakeWindowHandle> mWindow2;
8312
8313 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05008314 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07008315 vec2 vals = windowInfo->transform.transform(point.x, point.y);
8316 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07008317 }
8318
8319 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
8320 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008321 const std::string name = window->getName();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008322 std::unique_ptr<MotionEvent> motionEvent =
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00008323 window->consumeMotionEvent(WithMotionAction(expectedAction));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008324 ASSERT_NE(nullptr, motionEvent);
8325 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07008326
8327 for (size_t i = 0; i < points.size(); i++) {
8328 float expectedX = points[i].x;
8329 float expectedY = points[i].y;
8330
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008331 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07008332 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008333 << ", got " << motionEvent->getX(i);
8334 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07008335 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008336 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07008337 }
8338 }
chaviw9eaa22c2020-07-01 16:21:27 -07008339
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008340 void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action,
8341 const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07008342 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00008343 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
8344 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07008345
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008346 consumeMotionEvent(touchedWindow, action, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008347 }
chaviwaf87b3e2019-10-01 16:59:28 -07008348};
8349
8350TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
8351 // Touch Window 1
8352 PointF touchedPoint = {10, 10};
8353 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008354 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008355
8356 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008357 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008358
8359 // Touch Window 2
8360 touchedPoint = {150, 150};
8361 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008362 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008363}
8364
chaviw9eaa22c2020-07-01 16:21:27 -07008365TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
8366 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07008367 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008368 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07008369
8370 // Touch Window 1
8371 PointF touchedPoint = {10, 10};
8372 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008373 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008374 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008375 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008376
8377 // Touch Window 2
8378 touchedPoint = {150, 150};
8379 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008380 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
8381 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008382
chaviw9eaa22c2020-07-01 16:21:27 -07008383 // Update the transform so rotation is set
8384 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008385 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008386 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008387 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008388}
8389
chaviw9eaa22c2020-07-01 16:21:27 -07008390TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008391 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008392 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008393
8394 // Touch Window 1
8395 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8396 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008397 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008398
8399 // Touch Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008400 // Since this is part of the same touch gesture that has already been dispatched to Window 1,
8401 // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream
8402 // will continue to be dispatched through Window 1.
chaviw9eaa22c2020-07-01 16:21:27 -07008403 touchedPoints.push_back(PointF{150, 150});
8404 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008405 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008406
chaviw9eaa22c2020-07-01 16:21:27 -07008407 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008408 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008409 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008410
chaviw9eaa22c2020-07-01 16:21:27 -07008411 // Update the transform so rotation is set for Window 2
8412 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008413 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008414 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008415 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008416}
8417
chaviw9eaa22c2020-07-01 16:21:27 -07008418TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008419 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008420 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008421
8422 // Touch Window 1
8423 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8424 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008425 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008426
8427 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008428 touchedPoints.push_back(PointF{150, 150});
8429 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008430
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008431 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008432
8433 // Move both windows
8434 touchedPoints = {{20, 20}, {175, 175}};
8435 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8436 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8437
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008438 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008439
chaviw9eaa22c2020-07-01 16:21:27 -07008440 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008441 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008442 expectedPoints.pop_back();
8443
8444 // Touch Window 2
8445 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008446 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008447 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008448 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008449
8450 // Move both windows
8451 touchedPoints = {{20, 20}, {175, 175}};
8452 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8453 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8454
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008455 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008456}
8457
8458TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
8459 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008460 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008461
8462 // Touch Window 1
8463 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8464 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008465 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008466
8467 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008468 touchedPoints.push_back(PointF{150, 150});
8469 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008470
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008471 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008472
8473 // Move both windows
8474 touchedPoints = {{20, 20}, {175, 175}};
8475 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8476 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8477
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008478 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008479}
8480
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07008481/**
8482 * When one of the windows is slippery, the touch should not slip into the other window with the
8483 * same input channel.
8484 */
8485TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
8486 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008487 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07008488
8489 // Touch down in window 1
8490 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8491 ADISPLAY_ID_DEFAULT, {{50, 50}}));
8492 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
8493
8494 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
8495 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
8496 // getting generated.
8497 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8498 ADISPLAY_ID_DEFAULT, {{150, 150}}));
8499
8500 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
8501}
8502
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008503/**
8504 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
8505 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
8506 * that the pointer is hovering over may have a different transform.
8507 */
8508TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008509 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008510
8511 // Start hover in window 1
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008512 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN)
8513 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8514 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008515 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
8516 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008517 // Move hover to window 2.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008518 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8519 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
8520 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008521 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008522 consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER,
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008523 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
8524}
8525
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008526class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
8527 virtual void SetUp() override {
8528 InputDispatcherTest::SetUp();
8529
Chris Yea209fde2020-07-22 13:54:51 -07008530 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008531 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008532 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
8533 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008534 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008535 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07008536 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008537
8538 // Set focused application.
8539 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
8540
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008541 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008542 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008543 mWindow->consumeFocusEvent(true);
8544 }
8545
8546 virtual void TearDown() override {
8547 InputDispatcherTest::TearDown();
8548 mWindow.clear();
8549 }
8550
8551protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008552 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07008553 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008554 sp<FakeWindowHandle> mWindow;
8555 static constexpr PointF WINDOW_LOCATION = {20, 20};
8556
8557 void tapOnWindow() {
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008558 const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER)
8559 .x(WINDOW_LOCATION.x)
8560 .y(WINDOW_LOCATION.y);
8561 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8562 .pointer(touchingPointer)
8563 .build());
8564 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
8565 .pointer(touchingPointer)
8566 .build());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008567 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008568
8569 sp<FakeWindowHandle> addSpyWindow() {
8570 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008571 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008572 spy->setTrustedOverlay(true);
8573 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008574 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008575 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008576 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008577 return spy;
8578 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008579};
8580
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008581// Send a tap and respond, which should not cause an ANR.
8582TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
8583 tapOnWindow();
8584 mWindow->consumeMotionDown();
8585 mWindow->consumeMotionUp();
8586 ASSERT_TRUE(mDispatcher->waitForIdle());
8587 mFakePolicy->assertNotifyAnrWasNotCalled();
8588}
8589
8590// Send a regular key and respond, which should not cause an ANR.
8591TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008592 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008593 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
8594 ASSERT_TRUE(mDispatcher->waitForIdle());
8595 mFakePolicy->assertNotifyAnrWasNotCalled();
8596}
8597
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008598TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
8599 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008600 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008601 mWindow->consumeFocusEvent(false);
8602
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008603 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008604 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
8605 InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00008606 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008607 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008608 // Key will not go to window because we have no focused window.
8609 // The 'no focused window' ANR timer should start instead.
8610
8611 // Now, the focused application goes away.
8612 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
8613 // The key should get dropped and there should be no ANR.
8614
8615 ASSERT_TRUE(mDispatcher->waitForIdle());
8616 mFakePolicy->assertNotifyAnrWasNotCalled();
8617}
8618
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008619// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008620// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
8621// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008622TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008623 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008624 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008625 WINDOW_LOCATION));
8626
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008627 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008628 ASSERT_TRUE(sequenceNum);
8629 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008630 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008631
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008632 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008633 mWindow->consumeMotionEvent(
8634 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008635 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008636 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008637}
8638
8639// Send a key to the app and have the app not respond right away.
8640TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
8641 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008642 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008643 const auto [sequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008644 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008645 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008646 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008647 ASSERT_TRUE(mDispatcher->waitForIdle());
8648}
8649
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008650// We have a focused application, but no focused window
8651TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07008652 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008653 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008654 mWindow->consumeFocusEvent(false);
8655
8656 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008658 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008659 WINDOW_LOCATION));
8660 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
8661 mDispatcher->waitForIdle();
8662 mFakePolicy->assertNotifyAnrWasNotCalled();
8663
8664 // Once a focused event arrives, we get an ANR for this application
8665 // We specify the injection timeout to be smaller than the application timeout, to ensure that
8666 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008667 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008668 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008669 InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008670 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008671 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07008672 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008673 ASSERT_TRUE(mDispatcher->waitForIdle());
8674}
8675
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008676/**
8677 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
8678 * there will not be an ANR.
8679 */
8680TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
8681 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008682 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008683 mWindow->consumeFocusEvent(false);
8684
8685 KeyEvent event;
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07008686 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
8687 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008688 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
8689 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
8690
8691 // Define a valid key down event that is stale (too old).
8692 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Harry Cutts101ee9b2023-07-06 18:04:14 +00008693 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00008694 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008695
8696 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
8697
8698 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00008699 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008700 InputEventInjectionSync::WAIT_FOR_RESULT,
8701 INJECT_EVENT_TIMEOUT, policyFlags);
8702 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
8703 << "Injection should fail because the event is stale";
8704
8705 ASSERT_TRUE(mDispatcher->waitForIdle());
8706 mFakePolicy->assertNotifyAnrWasNotCalled();
8707 mWindow->assertNoEvents();
8708}
8709
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008710// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008711// Make sure that we don't notify policy twice about the same ANR.
8712TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008713 const std::chrono::duration appTimeout = 400ms;
8714 mApplication->setDispatchingTimeout(appTimeout);
8715 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
8716
Vishnu Nair47074b82020-08-14 11:54:47 -07008717 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008718 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008719 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008720
8721 // Once a focused event arrives, we get an ANR for this application
8722 // We specify the injection timeout to be smaller than the application timeout, to ensure that
8723 // injection times out (instead of failing).
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008724 const std::chrono::duration eventInjectionTimeout = 100ms;
8725 ASSERT_LT(eventInjectionTimeout, appTimeout);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008726 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008727 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008728 InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout,
8729 /*allowKeyRepeat=*/false);
8730 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
8731 << "result=" << ftl::enum_string(result);
8732 // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
8733 // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
8734 std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
8735 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008736
Vishnu Naire4df8752022-09-08 09:17:55 -07008737 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008738 // ANR should not be raised again. It is up to policy to do that if it desires.
8739 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008740
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008741 // If we now get a focused window, the ANR should stop, but the policy handles that via
8742 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008743 ASSERT_TRUE(mDispatcher->waitForIdle());
8744}
8745
8746// We have a focused application, but no focused window
8747TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07008748 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008749 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008750 mWindow->consumeFocusEvent(false);
8751
8752 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008753 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008754
Vishnu Naire4df8752022-09-08 09:17:55 -07008755 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
8756 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008757
8758 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008759 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008760 ASSERT_TRUE(mDispatcher->waitForIdle());
8761 mWindow->assertNoEvents();
8762}
8763
8764/**
8765 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
8766 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
8767 * If we process 1 of the events, but ANR on the second event with the same timestamp,
8768 * the ANR mechanism should still work.
8769 *
8770 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
8771 * DOWN event, while not responding on the second one.
8772 */
8773TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
8774 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008775 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008776 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
8777 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8778 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008779 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008780
8781 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008782 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008783 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
8784 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8785 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008786 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008787
8788 // We have now sent down and up. Let's consume first event and then ANR on the second.
8789 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8790 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008791 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008792}
8793
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008794// A spy window can receive an ANR
8795TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
8796 sp<FakeWindowHandle> spy = addSpyWindow();
8797
8798 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008799 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008800 WINDOW_LOCATION));
8801 mWindow->consumeMotionDown();
8802
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008803 const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008804 ASSERT_TRUE(sequenceNum);
8805 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008806 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008807
8808 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008809 spy->consumeMotionEvent(
8810 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008811 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008812 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008813}
8814
8815// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008816// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008817TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
8818 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008819
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008820 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008821 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008822 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008823 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008824
8825 // Stuck on the ACTION_UP
8826 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008827 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008828
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008829 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008830 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008831 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8832 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008833
8834 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
8835 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008836 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008837 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008838 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008839}
8840
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008841// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008842// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008843TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
8844 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008845
8846 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008847 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8848 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008849
8850 mWindow->consumeMotionDown();
8851 // Stuck on the ACTION_UP
8852 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008853 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008854
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008855 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008856 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008857 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8858 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008859
8860 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
8861 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008862 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008863 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008864 spy->assertNoEvents();
8865}
8866
8867TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008868 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008869
Prabir Pradhanfb549072023-10-05 19:17:36 +00008870 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008871
8872 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008873 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008874 WINDOW_LOCATION));
8875
8876 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8877 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
8878 ASSERT_TRUE(consumeSeq);
8879
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008880 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
8881 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008882
8883 monitor.finishEvent(*consumeSeq);
8884 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
8885
8886 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008887 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008888}
8889
8890// If a window is unresponsive, then you get anr. if the window later catches up and starts to
8891// process events, you don't get an anr. When the window later becomes unresponsive again, you
8892// get an ANR again.
8893// 1. tap -> block on ACTION_UP -> receive ANR
8894// 2. consume all pending events (= queue becomes healthy again)
8895// 3. tap again -> block on ACTION_UP again -> receive ANR second time
8896TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
8897 tapOnWindow();
8898
8899 mWindow->consumeMotionDown();
8900 // Block on ACTION_UP
8901 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008902 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008903 mWindow->consumeMotionUp(); // Now the connection should be healthy again
8904 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008905 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008906 mWindow->assertNoEvents();
8907
8908 tapOnWindow();
8909 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008910 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008911 mWindow->consumeMotionUp();
8912
8913 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008914 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008915 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008916 mWindow->assertNoEvents();
8917}
8918
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008919// If a connection remains unresponsive for a while, make sure policy is only notified once about
8920// it.
8921TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008922 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008923 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008924 WINDOW_LOCATION));
8925
8926 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008927 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008928 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008929 // 'notifyConnectionUnresponsive' should only be called once per connection
8930 mFakePolicy->assertNotifyAnrWasNotCalled();
8931 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008932 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008933 mWindow->consumeMotionEvent(
8934 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008935 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008936 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008937 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008938 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008939}
8940
8941/**
8942 * 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 -07008943 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008944 */
8945TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008946 // The timeouts in this test are established by relying on the fact that the "key waiting for
8947 // events timeout" is equal to 500ms.
8948 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008949 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008950 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008951
8952 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008953 const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008954 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008955 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008956 ASSERT_TRUE(upSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008957
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008958 // Don't finish the events yet, and send a key
8959 mDispatcher->notifyKey(
8960 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8961 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8962 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008963 // Key will not be sent to the window, yet, because the window is still processing events
8964 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008965 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008966 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008967
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008968 std::this_thread::sleep_for(400ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008969 // if we wait long enough though, dispatcher will give up, and still send the key
8970 // to the focused window, even though we have not yet finished the motion event
8971 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8972 mWindow->finishEvent(*downSequenceNum);
8973 mWindow->finishEvent(*upSequenceNum);
8974}
8975
8976/**
8977 * If a window is processing a motion event, and then a key event comes in, the key event should
8978 * not go to the focused window until the motion is processed.
8979 * If then a new motion comes in, then the pending key event should be going to the currently
8980 * focused window right away.
8981 */
8982TEST_F(InputDispatcherSingleWindowAnr,
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008983 PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) {
8984 // The timeouts in this test are established by relying on the fact that the "key waiting for
8985 // events timeout" is equal to 500ms.
8986 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008987 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008988 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008989
8990 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008991 const auto& [downSequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008992 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008993 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008994 ASSERT_TRUE(upSequenceNum);
8995 // Don't finish the events yet, and send a key
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008996 mDispatcher->notifyKey(
8997 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8998 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8999 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009000 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08009001 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009002
9003 // Now tap down again. It should cause the pending key to go to the focused window right away.
9004 tapOnWindow();
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08009005 // Now that we tapped, we should receive the key immediately.
9006 // Since there's still room for slowness, we use 200ms, which is much less than
9007 // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration.
9008 std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms);
9009 ASSERT_NE(nullptr, keyEvent);
9010 ASSERT_EQ(InputEventType::KEY, keyEvent->getType());
9011 ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN));
9012 // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any
9013 // order.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009014 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
9015 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08009016 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9017 mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009018 mWindow->assertNoEvents();
9019}
9020
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07009021/**
9022 * Send an event to the app and have the app not respond right away.
9023 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
9024 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
9025 * At some point, the window becomes responsive again.
9026 * Ensure that subsequent events get dropped, and the next gesture is delivered.
9027 */
9028TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
9029 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9030 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
9031 .build());
9032
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009033 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07009034 ASSERT_TRUE(sequenceNum);
9035 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9036 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
9037
9038 mWindow->finishEvent(*sequenceNum);
9039 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
9040 ASSERT_TRUE(mDispatcher->waitForIdle());
9041 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
9042
9043 // Now that the window is responsive, let's continue the gesture.
9044 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
9045 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9046 .build());
9047
9048 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9049 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9050 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
9051 .build());
9052
9053 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
9054 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9055 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
9056 .build());
9057 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9058 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9059 .build());
9060 // We already canceled this pointer, so the window shouldn't get any new events.
9061 mWindow->assertNoEvents();
9062
9063 // Start another one.
9064 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9065 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
9066 .build());
9067 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9068}
9069
Prabir Pradhanfc364722024-02-08 17:51:20 +00009070// Send an event to the app and have the app not respond right away. Then remove the app window.
9071// When the window is removed, the dispatcher will cancel the events for that window.
9072// So InputDispatcher will enqueue ACTION_CANCEL event as well.
9073TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) {
9074 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9075 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9076 {WINDOW_LOCATION}));
9077
9078 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
9079 ASSERT_TRUE(sequenceNum);
9080
9081 // Remove the window, but the input channel should remain alive.
9082 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
9083
9084 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9085 // Since the window was removed, Dispatcher does not know the PID associated with the window
9086 // anymore, so the policy is notified without the PID.
9087 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(),
9088 /*pid=*/std::nullopt);
9089
9090 mWindow->finishEvent(*sequenceNum);
9091 // The cancellation was generated when the window was removed, along with the focus event.
9092 mWindow->consumeMotionEvent(
9093 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
9094 mWindow->consumeFocusEvent(false);
9095 ASSERT_TRUE(mDispatcher->waitForIdle());
9096 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
9097}
9098
9099// Send an event to the app and have the app not respond right away. Wait for the policy to be
9100// notified of the unresponsive window, then remove the app window.
9101TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) {
9102 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9103 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9104 {WINDOW_LOCATION}));
9105
9106 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
9107 ASSERT_TRUE(sequenceNum);
9108 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9109 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
9110
9111 // Remove the window, but the input channel should remain alive.
9112 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
9113
9114 mWindow->finishEvent(*sequenceNum);
9115 // The cancellation was generated during the ANR, and the window lost focus when it was removed.
9116 mWindow->consumeMotionEvent(
9117 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
9118 mWindow->consumeFocusEvent(false);
9119 ASSERT_TRUE(mDispatcher->waitForIdle());
9120 // Since the window was removed, Dispatcher does not know the PID associated with the window
9121 // becoming responsive, so the policy is notified without the PID.
9122 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
9123}
9124
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009125class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
9126 virtual void SetUp() override {
9127 InputDispatcherTest::SetUp();
9128
Chris Yea209fde2020-07-22 13:54:51 -07009129 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009130 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009131 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
9132 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009133 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009134 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009135 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009136
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009137 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
9138 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009139 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009140 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009141
9142 // Set focused application.
9143 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07009144 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009145
9146 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009147 mDispatcher->onWindowInfosChanged(
9148 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009149 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009150 mFocusedWindow->consumeFocusEvent(true);
9151 }
9152
9153 virtual void TearDown() override {
9154 InputDispatcherTest::TearDown();
9155
9156 mUnfocusedWindow.clear();
9157 mFocusedWindow.clear();
9158 }
9159
9160protected:
Chris Yea209fde2020-07-22 13:54:51 -07009161 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009162 sp<FakeWindowHandle> mUnfocusedWindow;
9163 sp<FakeWindowHandle> mFocusedWindow;
9164 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
9165 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
9166 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
9167
9168 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
9169
9170 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
9171
9172private:
9173 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009175 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009176 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009178 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009179 location));
9180 }
9181};
9182
9183// If we have 2 windows that are both unresponsive, the one with the shortest timeout
9184// should be ANR'd first.
9185TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009186 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009187 injectMotionEvent(*mDispatcher,
9188 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9189 AINPUT_SOURCE_TOUCHSCREEN)
9190 .pointer(PointerBuilder(0, ToolType::FINGER)
9191 .x(FOCUSED_WINDOW_LOCATION.x)
9192 .y(FOCUSED_WINDOW_LOCATION.y))
9193 .build()));
9194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9195 injectMotionEvent(*mDispatcher,
9196 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
9197 AINPUT_SOURCE_TOUCHSCREEN)
9198 .pointer(PointerBuilder(0, ToolType::FINGER)
9199 .x(FOCUSED_WINDOW_LOCATION.x)
9200 .y(FOCUSED_WINDOW_LOCATION.y))
9201 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009202 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009203 mFocusedWindow->consumeMotionUp();
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009204 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009205 // We consumed all events, so no ANR
9206 ASSERT_TRUE(mDispatcher->waitForIdle());
9207 mFakePolicy->assertNotifyAnrWasNotCalled();
9208
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009209 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009210 injectMotionEvent(*mDispatcher,
9211 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9212 AINPUT_SOURCE_TOUCHSCREEN)
9213 .pointer(PointerBuilder(0, ToolType::FINGER)
9214 .x(FOCUSED_WINDOW_LOCATION.x)
9215 .y(FOCUSED_WINDOW_LOCATION.y))
9216 .build()));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009217 const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009218 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009219
9220 const std::chrono::duration timeout =
9221 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009222 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009223
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009224 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009225 mFocusedWindow->consumeMotionDown();
9226 // This cancel is generated because the connection was unresponsive
9227 mFocusedWindow->consumeMotionCancel();
9228 mFocusedWindow->assertNoEvents();
9229 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009230 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009231 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9232 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009233 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009234}
9235
9236// If we have 2 windows with identical timeouts that are both unresponsive,
9237// it doesn't matter which order they should have ANR.
9238// But we should receive ANR for both.
9239TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
9240 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009241 mUnfocusedWindow->setDispatchingTimeout(
9242 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009243 mDispatcher->onWindowInfosChanged(
9244 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009245
9246 tapOnFocusedWindow();
9247 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009248 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009249 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
9250 mFocusedWindow->getDispatchingTimeout(
9251 DISPATCHING_TIMEOUT)),
9252 mFakePolicy->getUnresponsiveWindowToken(0ms)};
9253
9254 ASSERT_THAT(anrConnectionTokens,
9255 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
9256 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009257
9258 ASSERT_TRUE(mDispatcher->waitForIdle());
9259 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009260
9261 mFocusedWindow->consumeMotionDown();
9262 mFocusedWindow->consumeMotionUp();
9263 mUnfocusedWindow->consumeMotionOutside();
9264
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009265 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
9266 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009267
9268 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009269 ASSERT_THAT(responsiveTokens,
9270 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
9271 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009272 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009273}
9274
9275// If a window is already not responding, the second tap on the same window should be ignored.
9276// We should also log an error to account for the dropped event (not tested here).
9277// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
9278TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
9279 tapOnFocusedWindow();
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009280 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009281 // Receive the events, but don't respond
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009282 const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009283 ASSERT_TRUE(downEventSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009284 const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009285 ASSERT_TRUE(upEventSequenceNum);
9286 const std::chrono::duration timeout =
9287 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009288 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009289
9290 // Tap once again
9291 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009292 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009293 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009294 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009295 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009296 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009297 FOCUSED_WINDOW_LOCATION));
9298 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
9299 // valid touch target
9300 mUnfocusedWindow->assertNoEvents();
9301
9302 // Consume the first tap
9303 mFocusedWindow->finishEvent(*downEventSequenceNum);
9304 mFocusedWindow->finishEvent(*upEventSequenceNum);
9305 ASSERT_TRUE(mDispatcher->waitForIdle());
9306 // The second tap did not go to the focused window
9307 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009308 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08009309 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9310 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009311 mFakePolicy->assertNotifyAnrWasNotCalled();
9312}
9313
9314// If you tap outside of all windows, there will not be ANR
9315TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009316 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009317 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009318 LOCATION_OUTSIDE_ALL_WINDOWS));
9319 ASSERT_TRUE(mDispatcher->waitForIdle());
9320 mFakePolicy->assertNotifyAnrWasNotCalled();
9321}
9322
9323// Since the focused window is paused, tapping on it should not produce any events
9324TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
9325 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009326 mDispatcher->onWindowInfosChanged(
9327 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009328
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009329 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009330 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009331 FOCUSED_WINDOW_LOCATION));
9332
9333 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
9334 ASSERT_TRUE(mDispatcher->waitForIdle());
9335 // Should not ANR because the window is paused, and touches shouldn't go to it
9336 mFakePolicy->assertNotifyAnrWasNotCalled();
9337
9338 mFocusedWindow->assertNoEvents();
9339 mUnfocusedWindow->assertNoEvents();
9340}
9341
9342/**
9343 * 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 -07009344 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009345 * If a different window becomes focused at this time, the key should go to that window instead.
9346 *
9347 * Warning!!!
9348 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
9349 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009350 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009351 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
9352 *
9353 * If that value changes, this test should also change.
9354 */
9355TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
9356 // Set a long ANR timeout to prevent it from triggering
9357 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009358 mDispatcher->onWindowInfosChanged(
9359 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009360
9361 tapOnUnfocusedWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009362 const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009363 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009364 const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009365 ASSERT_TRUE(upSequenceNum);
9366 // Don't finish the events yet, and send a key
9367 // Injection will succeed because we will eventually give up and send the key to the focused
9368 // window even if motions are still being processed.
9369
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009370 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009371 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9372 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009373 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009374 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009375 // and the key remains pending, waiting for the touch events to be processed.
9376 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
9377 // under the hood.
9378 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
9379 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009380
9381 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07009382 mFocusedWindow->setFocusable(false);
9383 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009384 mDispatcher->onWindowInfosChanged(
9385 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009386 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009387
9388 // Focus events should precede the key events
9389 mUnfocusedWindow->consumeFocusEvent(true);
9390 mFocusedWindow->consumeFocusEvent(false);
9391
9392 // Finish the tap events, which should unblock dispatcher
9393 mUnfocusedWindow->finishEvent(*downSequenceNum);
9394 mUnfocusedWindow->finishEvent(*upSequenceNum);
9395
9396 // Now that all queues are cleared and no backlog in the connections, the key event
9397 // can finally go to the newly focused "mUnfocusedWindow".
9398 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9399 mFocusedWindow->assertNoEvents();
9400 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009401 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009402}
9403
9404// When the touch stream is split across 2 windows, and one of them does not respond,
9405// then ANR should be raised and the touch should be canceled for the unresponsive window.
9406// The other window should not be affected by that.
9407TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
9408 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00009409 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9410 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9411 {FOCUSED_WINDOW_LOCATION}));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009412 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009413
9414 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00009415 mDispatcher->notifyMotion(
9416 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9417 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009418
9419 const std::chrono::duration timeout =
9420 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009421 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009422
9423 mUnfocusedWindow->consumeMotionDown();
9424 mFocusedWindow->consumeMotionDown();
9425 // Focused window may or may not receive ACTION_MOVE
9426 // But it should definitely receive ACTION_CANCEL due to the ANR
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009427 const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009428 ASSERT_TRUE(moveOrCancelSequenceNum);
9429 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
9430 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07009431 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009432 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
9433 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
9434 mFocusedWindow->consumeMotionCancel();
9435 } else {
9436 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
9437 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009438 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009439 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9440 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009441
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009442 mUnfocusedWindow->assertNoEvents();
9443 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009444 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009445}
9446
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009447/**
9448 * If we have no focused window, and a key comes in, we start the ANR timer.
9449 * The focused application should add a focused window before the timer runs out to prevent ANR.
9450 *
9451 * If the user touches another application during this time, the key should be dropped.
9452 * Next, if a new focused window comes in, without toggling the focused application,
9453 * then no ANR should occur.
9454 *
9455 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
9456 * but in some cases the policy may not update the focused application.
9457 */
9458TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
9459 std::shared_ptr<FakeApplicationHandle> focusedApplication =
9460 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07009461 focusedApplication->setDispatchingTimeout(300ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009462 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
9463 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
9464 mFocusedWindow->setFocusable(false);
9465
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009466 mDispatcher->onWindowInfosChanged(
9467 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009468 mFocusedWindow->consumeFocusEvent(false);
9469
9470 // Send a key. The ANR timer should start because there is no focused window.
9471 // 'focusedApplication' will get blamed if this timer completes.
9472 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009473 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009474 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9475 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +00009476 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009477 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009478
9479 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
9480 // then the injected touches won't cause the focused event to get dropped.
9481 // The dispatcher only checks for whether the queue should be pruned upon queueing.
9482 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
9483 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
9484 // For this test, it means that the key would get delivered to the window once it becomes
9485 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009486 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009487
9488 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00009489 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9490 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9491 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009492
9493 // We do not consume the motion right away, because that would require dispatcher to first
9494 // process (== drop) the key event, and by that time, ANR will be raised.
9495 // Set the focused window first.
9496 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009497 mDispatcher->onWindowInfosChanged(
9498 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009499 setFocusedWindow(mFocusedWindow);
9500 mFocusedWindow->consumeFocusEvent(true);
9501 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
9502 // to another application. This could be a bug / behaviour in the policy.
9503
9504 mUnfocusedWindow->consumeMotionDown();
9505
9506 ASSERT_TRUE(mDispatcher->waitForIdle());
9507 // Should not ANR because we actually have a focused window. It was just added too slowly.
9508 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
9509}
9510
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08009511/**
9512 * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having
9513 * an inconsistent event stream inside the dispatcher. In this test, we make sure that the
9514 * dispatcher doesn't prune pointer events incorrectly.
9515 *
9516 * This test reproduces a crash in InputDispatcher.
9517 * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur.
9518 *
9519 * Keep the currently focused application (mApplication), and have no focused window.
9520 * We set up two additional windows:
9521 * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the
9522 * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application
9523 * window. This window is not focusable, but is touchable.
9524 *
9525 * We first touch the navigation bar, which causes it to inject a key. Since there's no focused
9526 * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now
9527 * blocked. The dispatcher is waiting for 'mApplication' to add a focused window.
9528 *
9529 * Now, we touch "Another window". This window is owned by a different application than
9530 * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused
9531 * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start
9532 * dropping the events from its queue. Ensure that no crash occurs.
9533 *
9534 * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale.
9535 * This does not affect the test running time.
9536 */
9537TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) {
9538 std::shared_ptr<FakeApplicationHandle> systemUiApplication =
9539 std::make_shared<FakeApplicationHandle>();
9540 systemUiApplication->setDispatchingTimeout(3000ms);
9541 mFakePolicy->setStaleEventTimeout(3000ms);
9542 sp<FakeWindowHandle> navigationBar =
9543 sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar",
9544 ADISPLAY_ID_DEFAULT);
9545 navigationBar->setFocusable(false);
9546 navigationBar->setWatchOutsideTouch(true);
9547 navigationBar->setFrame(Rect(0, 0, 100, 100));
9548
9549 mApplication->setDispatchingTimeout(3000ms);
9550 // 'mApplication' is already focused, but we call it again here to make it explicit.
9551 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
9552
9553 std::shared_ptr<FakeApplicationHandle> anotherApplication =
9554 std::make_shared<FakeApplicationHandle>();
9555 sp<FakeWindowHandle> appWindow =
9556 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window",
9557 ADISPLAY_ID_DEFAULT);
9558 appWindow->setFocusable(false);
9559 appWindow->setFrame(Rect(100, 100, 200, 200));
9560
9561 mDispatcher->onWindowInfosChanged(
9562 {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0});
9563 // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus
9564 mFocusedWindow->consumeFocusEvent(false);
9565
9566 // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher
9567 // in response.
9568 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9569 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9570 .build());
9571 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9572
9573 // Key will not be sent anywhere because we have no focused window. It will remain pending.
9574 // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is.
9575 InputEventInjectionResult result =
9576 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9577 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
9578 /*allowKeyRepeat=*/false);
9579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
9580
9581 // Finish the gesture - lift up finger and inject ACTION_UP key event
9582 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9583 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9584 .build());
9585 result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9586 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
9587 /*allowKeyRepeat=*/false);
9588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
9589 // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be
9590 // getting any events yet.
9591 navigationBar->assertNoEvents();
9592
9593 // Now touch "Another window". This touch is going to a different application than the one we
9594 // are waiting for (which is 'mApplication').
9595 // This should cause the dispatcher to drop the pending focus-dispatched events (like the key
9596 // trying to be injected) and to continue processing the rest of the events in the original
9597 // order.
9598 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9599 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
9600 .build());
9601 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP));
9602 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE));
9603 appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9604
9605 appWindow->assertNoEvents();
9606 navigationBar->assertNoEvents();
9607}
9608
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009609// These tests ensure we cannot send touch events to a window that's positioned behind a window
9610// that has feature NO_INPUT_CHANNEL.
9611// Layout:
9612// Top (closest to user)
9613// mNoInputWindow (above all windows)
9614// mBottomWindow
9615// Bottom (furthest from user)
9616class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
9617 virtual void SetUp() override {
9618 InputDispatcherTest::SetUp();
9619
9620 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009621 mNoInputWindow =
9622 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
9623 "Window without input channel", ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00009624 /*createInputChannel=*/false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009625 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009626 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
9627 // It's perfectly valid for this window to not have an associated input channel
9628
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009629 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
9630 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009631 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
9632
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009633 mDispatcher->onWindowInfosChanged(
9634 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009635 }
9636
9637protected:
9638 std::shared_ptr<FakeApplicationHandle> mApplication;
9639 sp<FakeWindowHandle> mNoInputWindow;
9640 sp<FakeWindowHandle> mBottomWindow;
9641};
9642
9643TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
9644 PointF touchedPoint = {10, 10};
9645
Prabir Pradhan678438e2023-04-13 19:32:51 +00009646 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9647 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9648 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009649
9650 mNoInputWindow->assertNoEvents();
9651 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
9652 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
9653 // and therefore should prevent mBottomWindow from receiving touches
9654 mBottomWindow->assertNoEvents();
9655}
9656
9657/**
9658 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
9659 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
9660 */
9661TEST_F(InputDispatcherMultiWindowOcclusionTests,
9662 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009663 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
9664 "Window with input channel and NO_INPUT_CHANNEL",
9665 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009666
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009667 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009668 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009669 mDispatcher->onWindowInfosChanged(
9670 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009671
9672 PointF touchedPoint = {10, 10};
9673
Prabir Pradhan678438e2023-04-13 19:32:51 +00009674 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9675 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9676 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009677
9678 mNoInputWindow->assertNoEvents();
9679 mBottomWindow->assertNoEvents();
9680}
9681
Vishnu Nair958da932020-08-21 17:12:37 -07009682class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
9683protected:
9684 std::shared_ptr<FakeApplicationHandle> mApp;
9685 sp<FakeWindowHandle> mWindow;
9686 sp<FakeWindowHandle> mMirror;
9687
9688 virtual void SetUp() override {
9689 InputDispatcherTest::SetUp();
9690 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009691 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00009692 mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07009693 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
9694 mWindow->setFocusable(true);
9695 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009696 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009697 }
9698};
9699
9700TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
9701 // Request focus on a mirrored window
9702 setFocusedWindow(mMirror);
9703
9704 // window gets focused
9705 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009706 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009707 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009708 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
9709}
9710
9711// A focused & mirrored window remains focused only if the window and its mirror are both
9712// focusable.
9713TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
9714 setFocusedWindow(mMirror);
9715
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009716 // window gets focused because it is above the mirror
Vishnu Nair958da932020-08-21 17:12:37 -07009717 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009719 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009720 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009722 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009723 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9724
9725 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009726 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009727
9728 // window loses focus since one of the windows associated with the token in not focusable
9729 mWindow->consumeFocusEvent(false);
9730
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009731 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009732 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009733 mWindow->assertNoEvents();
9734}
9735
9736// A focused & mirrored window remains focused until the window and its mirror both become
9737// invisible.
9738TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
9739 setFocusedWindow(mMirror);
9740
9741 // window gets focused
9742 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009743 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009744 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009745 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009747 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009748 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9749
9750 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009751 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009752
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009753 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009754 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009755 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009756 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009757 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009758 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9759
9760 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009761 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009762
9763 // window loses focus only after all windows associated with the token become invisible.
9764 mWindow->consumeFocusEvent(false);
9765
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009766 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009767 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009768 mWindow->assertNoEvents();
9769}
9770
9771// A focused & mirrored window remains focused until both windows are removed.
9772TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
9773 setFocusedWindow(mMirror);
9774
9775 // window gets focused
9776 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009778 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009779 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009780 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009781 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009782 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9783
9784 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009785 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009786
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009788 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009789 mMirror->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009790 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009791 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009792 mMirror->consumeKeyUp(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07009793
9794 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009795 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009796 mWindow->consumeFocusEvent(false);
9797
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009798 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009799 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009800 mWindow->assertNoEvents();
9801}
9802
9803// Focus request can be pending until one window becomes visible.
9804TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
9805 // Request focus on an invisible mirror.
9806 mWindow->setVisible(false);
9807 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009808 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009809 setFocusedWindow(mMirror);
9810
9811 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009812 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009813 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9814 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07009815
9816 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009817 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009818
9819 // window gets focused
9820 mWindow->consumeFocusEvent(true);
9821 // window gets the pending key event
9822 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9823}
Prabir Pradhan99987712020-11-10 18:43:05 -08009824
9825class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
9826protected:
9827 std::shared_ptr<FakeApplicationHandle> mApp;
9828 sp<FakeWindowHandle> mWindow;
9829 sp<FakeWindowHandle> mSecondWindow;
9830
9831 void SetUp() override {
9832 InputDispatcherTest::SetUp();
9833 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009834 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08009835 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009836 mSecondWindow =
9837 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08009838 mSecondWindow->setFocusable(true);
9839
9840 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009841 mDispatcher->onWindowInfosChanged(
9842 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -08009843
9844 setFocusedWindow(mWindow);
9845 mWindow->consumeFocusEvent(true);
9846 }
9847
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009848 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009849 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08009850 }
9851
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009852 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
9853 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08009854 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009855 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
9856 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08009857 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009858 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08009859 }
9860};
9861
9862TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
9863 // Ensure that capture cannot be obtained for unfocused windows.
9864 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
9865 mFakePolicy->assertSetPointerCaptureNotCalled();
9866 mSecondWindow->assertNoEvents();
9867
9868 // Ensure that capture can be enabled from the focus window.
9869 requestAndVerifyPointerCapture(mWindow, true);
9870
9871 // Ensure that capture cannot be disabled from a window that does not have capture.
9872 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
9873 mFakePolicy->assertSetPointerCaptureNotCalled();
9874
9875 // Ensure that capture can be disabled from the window with capture.
9876 requestAndVerifyPointerCapture(mWindow, false);
9877}
9878
9879TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009880 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08009881
9882 setFocusedWindow(mSecondWindow);
9883
9884 // Ensure that the capture disabled event was sent first.
9885 mWindow->consumeCaptureEvent(false);
9886 mWindow->consumeFocusEvent(false);
9887 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009888 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08009889
9890 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009891 notifyPointerCaptureChanged({});
9892 notifyPointerCaptureChanged(request);
9893 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08009894 mWindow->assertNoEvents();
9895 mSecondWindow->assertNoEvents();
9896 mFakePolicy->assertSetPointerCaptureNotCalled();
9897}
9898
9899TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009900 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08009901
9902 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009903 notifyPointerCaptureChanged({});
9904 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08009905
9906 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009907 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08009908 mWindow->consumeCaptureEvent(false);
9909 mWindow->assertNoEvents();
9910}
9911
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009912TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
9913 requestAndVerifyPointerCapture(mWindow, true);
9914
9915 // The first window loses focus.
9916 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009917 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009918 mWindow->consumeCaptureEvent(false);
9919
9920 // Request Pointer Capture from the second window before the notification from InputReader
9921 // arrives.
9922 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009923 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009924
9925 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009926 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009927
9928 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009929 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009930
9931 mSecondWindow->consumeFocusEvent(true);
9932 mSecondWindow->consumeCaptureEvent(true);
9933}
9934
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009935TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
9936 // App repeatedly enables and disables capture.
9937 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9938 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9939 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
9940 mFakePolicy->assertSetPointerCaptureCalled(false);
9941 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9942 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9943
9944 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
9945 // first request is now stale, this should do nothing.
9946 notifyPointerCaptureChanged(firstRequest);
9947 mWindow->assertNoEvents();
9948
9949 // InputReader notifies that the second request was enabled.
9950 notifyPointerCaptureChanged(secondRequest);
9951 mWindow->consumeCaptureEvent(true);
9952}
9953
Prabir Pradhan7092e262022-05-03 16:51:09 +00009954TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
9955 requestAndVerifyPointerCapture(mWindow, true);
9956
9957 // App toggles pointer capture off and on.
9958 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
9959 mFakePolicy->assertSetPointerCaptureCalled(false);
9960
9961 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9962 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9963
9964 // InputReader notifies that the latest "enable" request was processed, while skipping over the
9965 // preceding "disable" request.
9966 notifyPointerCaptureChanged(enableRequest);
9967
9968 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
9969 // any notifications.
9970 mWindow->assertNoEvents();
9971}
9972
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009973/**
9974 * One window. Hover mouse in the window, and then start capture. Make sure that the relative
9975 * mouse movements don't affect the previous mouse hovering state.
9976 * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no
9977 * HOVER_MOVE events).
9978 */
9979TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) {
9980 // Mouse hover on the window
9981 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
9982 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
9983 .build());
9984 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
9985 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
9986 .build());
9987
9988 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER)));
9989 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE)));
9990
9991 // Start pointer capture
9992 requestAndVerifyPointerCapture(mWindow, true);
9993
9994 // Send some relative mouse movements and receive them in the window.
9995 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE)
9996 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11))
9997 .build());
9998 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11),
9999 WithSource(AINPUT_SOURCE_MOUSE_RELATIVE)));
10000
10001 // Stop pointer capture
10002 requestAndVerifyPointerCapture(mWindow, false);
10003
10004 // Continue hovering on the window
10005 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
10006 .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115))
10007 .build());
10008 mWindow->consumeMotionEvent(
10009 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
10010
10011 mWindow->assertNoEvents();
10012}
10013
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010014class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
10015protected:
10016 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +000010017
10018 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
10019 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
10020
10021 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
10022 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
10023
10024 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
10025 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
10026 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
10027 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
10028 MAXIMUM_OBSCURING_OPACITY);
10029
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010030 static constexpr gui::Uid TOUCHED_APP_UID{10001};
10031 static constexpr gui::Uid APP_B_UID{10002};
10032 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010033
10034 sp<FakeWindowHandle> mTouchWindow;
10035
10036 virtual void SetUp() override {
10037 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010038 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010039 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
10040 }
10041
10042 virtual void TearDown() override {
10043 InputDispatcherTest::TearDown();
10044 mTouchWindow.clear();
10045 }
10046
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010047 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -050010048 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010049 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010050 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010051 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010052 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010053 return window;
10054 }
10055
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010056 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010057 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
10058 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010059 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010060 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010061 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010062 return window;
10063 }
10064
10065 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000010066 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10067 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10068 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010069 }
10070};
10071
10072TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010073 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010074 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010075 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010076
10077 touch();
10078
10079 mTouchWindow->assertNoEvents();
10080}
10081
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010082TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +000010083 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
10084 const sp<FakeWindowHandle>& w =
10085 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010086 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010087
10088 touch();
10089
10090 mTouchWindow->assertNoEvents();
10091}
10092
10093TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010094 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
10095 const sp<FakeWindowHandle>& w =
10096 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010097 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010098
10099 touch();
10100
10101 w->assertNoEvents();
10102}
10103
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010104TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010105 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010106 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010107
10108 touch();
10109
10110 mTouchWindow->consumeAnyMotionDown();
10111}
10112
10113TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010114 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010115 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010116 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010117 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010118
10119 touch({PointF{100, 100}});
10120
10121 mTouchWindow->consumeAnyMotionDown();
10122}
10123
10124TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010125 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010126 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010127 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010128
10129 touch();
10130
10131 mTouchWindow->consumeAnyMotionDown();
10132}
10133
10134TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
10135 const sp<FakeWindowHandle>& w =
10136 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010137 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010138
10139 touch();
10140
10141 mTouchWindow->consumeAnyMotionDown();
10142}
10143
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010144TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
10145 const sp<FakeWindowHandle>& w =
10146 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010147 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010148
10149 touch();
10150
10151 w->assertNoEvents();
10152}
10153
10154/**
10155 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
10156 * inside) while letting them pass-through. Note that even though touch passes through the occluding
10157 * window, the occluding window will still receive ACTION_OUTSIDE event.
10158 */
10159TEST_F(InputDispatcherUntrustedTouchesTest,
10160 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
10161 const sp<FakeWindowHandle>& w =
10162 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010163 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010164 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010165
10166 touch();
10167
10168 w->consumeMotionOutside();
10169}
10170
10171TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
10172 const sp<FakeWindowHandle>& w =
10173 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010174 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010175 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010176
10177 touch();
10178
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080010179 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010180}
10181
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010182TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010183 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010184 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10185 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010186 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010187
10188 touch();
10189
10190 mTouchWindow->consumeAnyMotionDown();
10191}
10192
10193TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
10194 const sp<FakeWindowHandle>& w =
10195 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10196 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010197 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010198
10199 touch();
10200
10201 mTouchWindow->consumeAnyMotionDown();
10202}
10203
10204TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010205 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010206 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10207 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010208 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010209
10210 touch();
10211
10212 mTouchWindow->assertNoEvents();
10213}
10214
10215TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
10216 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
10217 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010218 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
10219 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010220 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010221 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
10222 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010223 mDispatcher->onWindowInfosChanged(
10224 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010225
10226 touch();
10227
10228 mTouchWindow->assertNoEvents();
10229}
10230
10231TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
10232 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
10233 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010234 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
10235 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010236 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010237 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
10238 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010239 mDispatcher->onWindowInfosChanged(
10240 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010241
10242 touch();
10243
10244 mTouchWindow->consumeAnyMotionDown();
10245}
10246
10247TEST_F(InputDispatcherUntrustedTouchesTest,
10248 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
10249 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010250 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10251 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010252 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010253 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10254 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010255 mDispatcher->onWindowInfosChanged(
10256 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010257
10258 touch();
10259
10260 mTouchWindow->consumeAnyMotionDown();
10261}
10262
10263TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
10264 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010265 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10266 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010267 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010268 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10269 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010270 mDispatcher->onWindowInfosChanged(
10271 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010272
10273 touch();
10274
10275 mTouchWindow->assertNoEvents();
10276}
10277
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010278TEST_F(InputDispatcherUntrustedTouchesTest,
10279 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
10280 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010281 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10282 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010283 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010284 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10285 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010286 mDispatcher->onWindowInfosChanged(
10287 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010288
10289 touch();
10290
10291 mTouchWindow->assertNoEvents();
10292}
10293
10294TEST_F(InputDispatcherUntrustedTouchesTest,
10295 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
10296 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010297 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10298 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010299 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010300 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10301 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010302 mDispatcher->onWindowInfosChanged(
10303 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010304
10305 touch();
10306
10307 mTouchWindow->consumeAnyMotionDown();
10308}
10309
10310TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
10311 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010312 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10313 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010314 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010315
10316 touch();
10317
10318 mTouchWindow->consumeAnyMotionDown();
10319}
10320
10321TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
10322 const sp<FakeWindowHandle>& w =
10323 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010324 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010325
10326 touch();
10327
10328 mTouchWindow->consumeAnyMotionDown();
10329}
10330
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010331TEST_F(InputDispatcherUntrustedTouchesTest,
10332 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
10333 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
10334 const sp<FakeWindowHandle>& w =
10335 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010336 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010337
10338 touch();
10339
10340 mTouchWindow->assertNoEvents();
10341}
10342
10343TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
10344 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
10345 const sp<FakeWindowHandle>& w =
10346 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010347 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010348
10349 touch();
10350
10351 mTouchWindow->consumeAnyMotionDown();
10352}
10353
10354TEST_F(InputDispatcherUntrustedTouchesTest,
10355 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
10356 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
10357 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010358 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10359 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010360 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010361
10362 touch();
10363
10364 mTouchWindow->consumeAnyMotionDown();
10365}
10366
10367TEST_F(InputDispatcherUntrustedTouchesTest,
10368 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
10369 const sp<FakeWindowHandle>& w1 =
10370 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
10371 OPACITY_BELOW_THRESHOLD);
10372 const sp<FakeWindowHandle>& w2 =
10373 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10374 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010375 mDispatcher->onWindowInfosChanged(
10376 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010377
10378 touch();
10379
10380 mTouchWindow->assertNoEvents();
10381}
10382
10383/**
10384 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
10385 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
10386 * (which alone would result in allowing touches) does not affect the blocking behavior.
10387 */
10388TEST_F(InputDispatcherUntrustedTouchesTest,
10389 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
10390 const sp<FakeWindowHandle>& wB =
10391 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
10392 OPACITY_BELOW_THRESHOLD);
10393 const sp<FakeWindowHandle>& wC =
10394 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10395 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010396 mDispatcher->onWindowInfosChanged(
10397 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010398
10399 touch();
10400
10401 mTouchWindow->assertNoEvents();
10402}
10403
10404/**
10405 * This test is testing that a window from a different UID but with same application token doesn't
10406 * block the touch. Apps can share the application token for close UI collaboration for example.
10407 */
10408TEST_F(InputDispatcherUntrustedTouchesTest,
10409 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
10410 const sp<FakeWindowHandle>& w =
10411 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
10412 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010413 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010414
10415 touch();
10416
10417 mTouchWindow->consumeAnyMotionDown();
10418}
10419
arthurhungb89ccb02020-12-30 16:19:01 +080010420class InputDispatcherDragTests : public InputDispatcherTest {
10421protected:
10422 std::shared_ptr<FakeApplicationHandle> mApp;
10423 sp<FakeWindowHandle> mWindow;
10424 sp<FakeWindowHandle> mSecondWindow;
10425 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010426 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010427 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
10428 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +080010429
10430 void SetUp() override {
10431 InputDispatcherTest::SetUp();
10432 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010433 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080010434 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080010435
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010436 mSecondWindow =
10437 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080010438 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080010439
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010440 mSpyWindow =
10441 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010442 mSpyWindow->setSpy(true);
10443 mSpyWindow->setTrustedOverlay(true);
10444 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
10445
arthurhungb89ccb02020-12-30 16:19:01 +080010446 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010447 mDispatcher->onWindowInfosChanged(
10448 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
10449 {},
10450 0,
10451 0});
arthurhungb89ccb02020-12-30 16:19:01 +080010452 }
10453
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010454 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
10455 switch (fromSource) {
10456 case AINPUT_SOURCE_TOUCHSCREEN:
10457 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010458 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010459 ADISPLAY_ID_DEFAULT, {50, 50}))
10460 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10461 break;
10462 case AINPUT_SOURCE_STYLUS:
10463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010464 injectMotionEvent(*mDispatcher,
10465 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10466 AINPUT_SOURCE_STYLUS)
10467 .buttonState(
10468 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
10469 .pointer(PointerBuilder(0, ToolType::STYLUS)
10470 .x(50)
10471 .y(50))
10472 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010473 break;
10474 case AINPUT_SOURCE_MOUSE:
10475 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010476 injectMotionEvent(*mDispatcher,
10477 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10478 AINPUT_SOURCE_MOUSE)
10479 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
10480 .pointer(PointerBuilder(MOUSE_POINTER_ID,
10481 ToolType::MOUSE)
10482 .x(50)
10483 .y(50))
10484 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010485 break;
10486 default:
10487 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
10488 }
arthurhungb89ccb02020-12-30 16:19:01 +080010489
10490 // Window should receive motion event.
10491 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010492 // Spy window should also receive motion event
10493 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000010494 }
10495
10496 // Start performing drag, we will create a drag window and transfer touch to it.
10497 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
10498 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010499 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +000010500 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010501 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +000010502 }
arthurhungb89ccb02020-12-30 16:19:01 +080010503
10504 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010505 mDragWindow =
10506 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010507 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010508 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
10509 *mWindow->getInfo(), *mSecondWindow->getInfo()},
10510 {},
10511 0,
10512 0});
arthurhungb89ccb02020-12-30 16:19:01 +080010513
10514 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +000010515 bool transferred =
10516 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +000010517 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +000010518 if (transferred) {
10519 mWindow->consumeMotionCancel();
10520 mDragWindow->consumeMotionDown();
10521 }
10522 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +080010523 }
10524};
10525
10526TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010527 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +080010528
10529 // Move on 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, {50, 50}))
10533 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10534 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10535 mWindow->consumeDragEvent(false, 50, 50);
10536 mSecondWindow->assertNoEvents();
10537
10538 // Move to another 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, {150, 50}))
10542 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10543 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10544 mWindow->consumeDragEvent(true, 150, 50);
10545 mSecondWindow->consumeDragEvent(false, 50, 50);
10546
10547 // Move back to original window.
10548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010549 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010550 ADISPLAY_ID_DEFAULT, {50, 50}))
10551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10552 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10553 mWindow->consumeDragEvent(false, 50, 50);
10554 mSecondWindow->consumeDragEvent(true, -50, 50);
10555
10556 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010557 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10558 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080010559 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10560 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
10561 mWindow->assertNoEvents();
10562 mSecondWindow->assertNoEvents();
10563}
10564
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010565TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010566 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010567
10568 // No cancel event after drag start
10569 mSpyWindow->assertNoEvents();
10570
10571 const MotionEvent secondFingerDownEvent =
10572 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10573 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010574 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10575 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010576 .build();
10577 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010578 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010579 InputEventInjectionSync::WAIT_FOR_RESULT))
10580 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10581
10582 // Receives cancel for first pointer after next pointer down
10583 mSpyWindow->consumeMotionCancel();
10584 mSpyWindow->consumeMotionDown();
10585
10586 mSpyWindow->assertNoEvents();
10587}
10588
arthurhungf452d0b2021-01-06 00:19:52 +080010589TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010590 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +080010591
10592 // Move on 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, {50, 50}))
10596 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10597 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10598 mWindow->consumeDragEvent(false, 50, 50);
10599 mSecondWindow->assertNoEvents();
10600
10601 // Move to another window.
10602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010603 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +080010604 ADISPLAY_ID_DEFAULT, {150, 50}))
10605 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10606 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10607 mWindow->consumeDragEvent(true, 150, 50);
10608 mSecondWindow->consumeDragEvent(false, 50, 50);
10609
10610 // drop to another window.
10611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010612 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +080010613 {150, 50}))
10614 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10615 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010616 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +080010617 mWindow->assertNoEvents();
10618 mSecondWindow->assertNoEvents();
10619}
10620
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010621TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) {
10622 startDrag();
10623
10624 // No cancel event after drag start
10625 mSpyWindow->assertNoEvents();
10626
10627 const MotionEvent secondFingerDownEvent =
10628 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10629 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
10630 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10631 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
10632 .build();
10633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10634 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
10635 InputEventInjectionSync::WAIT_FOR_RESULT))
10636 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10637
10638 // Receives cancel for first pointer after next pointer down
10639 mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -080010640 mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1})));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010641 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
10642
10643 mSpyWindow->assertNoEvents();
10644
10645 // Spy window calls pilfer pointers
10646 EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken()));
10647 mDragWindow->assertNoEvents();
10648
10649 const MotionEvent firstFingerMoveEvent =
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080010650 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010651 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
10652 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60))
10653 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
10654 .build();
10655 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080010656 injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010657 InputEventInjectionSync::WAIT_FOR_RESULT))
10658 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10659
10660 // Drag window should still receive the new event
10661 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
10662 mDragWindow->assertNoEvents();
10663}
10664
arthurhung6d4bed92021-03-17 11:59:33 +080010665TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010666 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +080010667
10668 // Move on window and keep button pressed.
10669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010670 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010671 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10672 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010673 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010674 .build()))
10675 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10676 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10677 mWindow->consumeDragEvent(false, 50, 50);
10678 mSecondWindow->assertNoEvents();
10679
10680 // Move to another window and release button, expect to drop item.
10681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010682 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010683 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10684 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010685 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010686 .build()))
10687 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10688 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10689 mWindow->assertNoEvents();
10690 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010691 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +080010692
10693 // nothing to the window.
10694 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010695 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010696 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
10697 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010698 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010699 .build()))
10700 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10701 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
10702 mWindow->assertNoEvents();
10703 mSecondWindow->assertNoEvents();
10704}
10705
Arthur Hung54745652022-04-20 07:17:41 +000010706TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010707 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +080010708
10709 // Set second window invisible.
10710 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010711 mDispatcher->onWindowInfosChanged(
10712 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +080010713
10714 // Move on 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, {50, 50}))
10718 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10719 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10720 mWindow->consumeDragEvent(false, 50, 50);
10721 mSecondWindow->assertNoEvents();
10722
10723 // Move to another window.
10724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010725 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010726 ADISPLAY_ID_DEFAULT, {150, 50}))
10727 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10728 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10729 mWindow->consumeDragEvent(true, 150, 50);
10730 mSecondWindow->assertNoEvents();
10731
10732 // drop to another window.
10733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010734 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010735 {150, 50}))
10736 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10737 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010738 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +080010739 mWindow->assertNoEvents();
10740 mSecondWindow->assertNoEvents();
10741}
10742
Arthur Hung54745652022-04-20 07:17:41 +000010743TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010744 // Ensure window could track pointerIds if it didn't support split touch.
10745 mWindow->setPreventSplitting(true);
10746
Arthur Hung54745652022-04-20 07:17:41 +000010747 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010748 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +000010749 {50, 50}))
10750 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10751 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10752
10753 const MotionEvent secondFingerDownEvent =
10754 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10755 .displayId(ADISPLAY_ID_DEFAULT)
10756 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010757 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10758 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010759 .build();
10760 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010761 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010762 InputEventInjectionSync::WAIT_FOR_RESULT))
10763 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000010764 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +000010765
10766 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010767 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000010768}
10769
10770TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
10771 // First down on second window.
10772 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010773 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +000010774 {150, 50}))
10775 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10776
10777 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10778
10779 // Second down on first window.
10780 const MotionEvent secondFingerDownEvent =
10781 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10782 .displayId(ADISPLAY_ID_DEFAULT)
10783 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010784 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10785 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010786 .build();
10787 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010788 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010789 InputEventInjectionSync::WAIT_FOR_RESULT))
10790 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10791 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan9d5f9ce2024-01-24 00:03:41 +000010792 mSecondWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000010793
10794 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010795 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000010796
10797 // Move on window.
10798 const MotionEvent secondFingerMoveEvent =
10799 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10800 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010801 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10802 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010803 .build();
10804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010805 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010806 InputEventInjectionSync::WAIT_FOR_RESULT));
10807 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10808 mWindow->consumeDragEvent(false, 50, 50);
10809 mSecondWindow->consumeMotionMove();
10810
10811 // Release the drag pointer should perform drop.
10812 const MotionEvent secondFingerUpEvent =
10813 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
10814 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010815 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10816 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010817 .build();
10818 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010819 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010820 InputEventInjectionSync::WAIT_FOR_RESULT));
10821 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010822 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +000010823 mWindow->assertNoEvents();
10824 mSecondWindow->consumeMotionMove();
10825}
10826
Arthur Hung3915c1f2022-05-31 07:17:17 +000010827TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010828 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +000010829
10830 // Update window of second display.
10831 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010832 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010833 mDispatcher->onWindowInfosChanged(
10834 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
10835 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
10836 {},
10837 0,
10838 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000010839
10840 // Let second display has a touch state.
10841 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010842 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010843 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10844 AINPUT_SOURCE_TOUCHSCREEN)
10845 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010846 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +000010847 .build()));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +000010848 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +000010849 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010850 mDispatcher->onWindowInfosChanged(
10851 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
10852 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
10853 {},
10854 0,
10855 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000010856
10857 // Move on window.
10858 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010859 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010860 ADISPLAY_ID_DEFAULT, {50, 50}))
10861 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10862 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10863 mWindow->consumeDragEvent(false, 50, 50);
10864 mSecondWindow->assertNoEvents();
10865
10866 // Move to another window.
10867 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010868 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010869 ADISPLAY_ID_DEFAULT, {150, 50}))
10870 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10871 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10872 mWindow->consumeDragEvent(true, 150, 50);
10873 mSecondWindow->consumeDragEvent(false, 50, 50);
10874
10875 // drop to another window.
10876 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010877 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010878 {150, 50}))
10879 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10880 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010881 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +000010882 mWindow->assertNoEvents();
10883 mSecondWindow->assertNoEvents();
10884}
10885
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010886TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
10887 startDrag(true, AINPUT_SOURCE_MOUSE);
10888 // Move on window.
10889 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010890 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010891 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
10892 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010893 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010894 .x(50)
10895 .y(50))
10896 .build()))
10897 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10898 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10899 mWindow->consumeDragEvent(false, 50, 50);
10900 mSecondWindow->assertNoEvents();
10901
10902 // Move to another window.
10903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010904 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010905 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
10906 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010907 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010908 .x(150)
10909 .y(50))
10910 .build()))
10911 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10912 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10913 mWindow->consumeDragEvent(true, 150, 50);
10914 mSecondWindow->consumeDragEvent(false, 50, 50);
10915
10916 // drop to another window.
10917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010918 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010919 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
10920 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010921 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010922 .x(150)
10923 .y(50))
10924 .build()))
10925 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10926 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010927 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010928 mWindow->assertNoEvents();
10929 mSecondWindow->assertNoEvents();
10930}
10931
Linnan Li5af92f92023-07-14 14:36:22 +080010932/**
10933 * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag
10934 * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash.
10935 */
10936TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) {
10937 // Down on second window
10938 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10939 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10940 {150, 50}))
10941 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10942
10943 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown());
10944 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown());
10945
10946 // Down on first window
10947 const MotionEvent secondFingerDownEvent =
10948 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10949 .displayId(ADISPLAY_ID_DEFAULT)
10950 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10951 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
10952 .build();
10953 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10954 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
10955 InputEventInjectionSync::WAIT_FOR_RESULT))
10956 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10957 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
10958 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove());
10959 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1));
10960
10961 // Start drag on first window
10962 ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN));
10963
10964 // Trigger cancel
10965 mDispatcher->cancelCurrentTouch();
10966 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel());
10967 ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel());
10968 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel());
10969
10970 ASSERT_TRUE(mDispatcher->waitForIdle());
10971 // The D&D finished with nullptr
10972 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
10973
10974 // Remove drag window
10975 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
10976
10977 // Inject a simple gesture, ensure dispatcher not crashed
10978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10979 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10980 PointF{50, 50}))
10981 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10982 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
10983
10984 const MotionEvent moveEvent =
10985 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10986 .displayId(ADISPLAY_ID_DEFAULT)
10987 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10988 .build();
10989 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10990 injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT,
10991 InputEventInjectionSync::WAIT_FOR_RESULT))
10992 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10993 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove());
10994
10995 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10996 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10997 {50, 50}))
10998 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10999 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp());
11000}
11001
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000011002TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) {
11003 // Start hovering over the window.
11004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11005 injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE,
11006 ADISPLAY_ID_DEFAULT, {50, 50}));
11007
11008 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
11009 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
11010
11011 ASSERT_FALSE(startDrag(/*sendDown=*/false))
11012 << "Drag and drop should not work with a hovering pointer";
11013}
11014
Vishnu Nair062a8672021-09-03 16:07:44 -070011015class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
11016
11017TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
11018 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011019 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11020 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011021 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011022 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11023 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011024 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011025 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011026 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011027
11028 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011029 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011030 window->assertNoEvents();
11031
Prabir Pradhan678438e2023-04-13 19:32:51 +000011032 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11033 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011034 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11035 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -080011036 mDispatcher->waitForIdle();
Vishnu Nair062a8672021-09-03 16:07:44 -070011037 window->assertNoEvents();
11038
11039 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011040 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011041 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011042
Prabir Pradhan678438e2023-04-13 19:32:51 +000011043 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011044 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11045
Prabir Pradhan678438e2023-04-13 19:32:51 +000011046 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11047 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011048 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11049 window->assertNoEvents();
11050}
11051
11052TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
11053 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
11054 std::make_shared<FakeApplicationHandle>();
11055 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011056 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
11057 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070011058 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011059 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011060 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070011061 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011062 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11063 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011064 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011065 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070011066 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11067 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011068 mDispatcher->onWindowInfosChanged(
11069 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011070 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011071 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011072
11073 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011074 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011075 window->assertNoEvents();
11076
Prabir Pradhan678438e2023-04-13 19:32:51 +000011077 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11078 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011079 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11080 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011081 window->assertNoEvents();
11082
11083 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011084 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011085 mDispatcher->onWindowInfosChanged(
11086 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011087
Prabir Pradhan678438e2023-04-13 19:32:51 +000011088 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011089 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11090
Prabir Pradhan678438e2023-04-13 19:32:51 +000011091 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11092 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011093 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
11094 window->assertNoEvents();
11095}
11096
11097TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
11098 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
11099 std::make_shared<FakeApplicationHandle>();
11100 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011101 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
11102 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070011103 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011104 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011105 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070011106 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011107 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11108 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011109 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011110 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070011111 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11112 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011113 mDispatcher->onWindowInfosChanged(
11114 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011115 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011116 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011117
11118 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011119 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011120 window->assertNoEvents();
11121
Prabir Pradhan678438e2023-04-13 19:32:51 +000011122 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11123 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011124 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11125 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011126 window->assertNoEvents();
11127
11128 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011129 mDispatcher->onWindowInfosChanged(
11130 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011131
Prabir Pradhan678438e2023-04-13 19:32:51 +000011132 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011133 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11134
Prabir Pradhan678438e2023-04-13 19:32:51 +000011135 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11136 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011137 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11138 window->assertNoEvents();
11139}
11140
Antonio Kantekf16f2832021-09-28 04:39:20 +000011141class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
11142protected:
11143 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +000011144 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +000011145 sp<FakeWindowHandle> mWindow;
11146 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +000011147 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +000011148
11149 void SetUp() override {
11150 InputDispatcherTest::SetUp();
11151
11152 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +000011153 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011154 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011155 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011156 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011157 mSecondWindow =
11158 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011159 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +000011160 mThirdWindow =
11161 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
11162 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
11163 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011164
11165 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011166 mDispatcher->onWindowInfosChanged(
11167 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
11168 {},
11169 0,
11170 0});
Antonio Kantek15beb512022-06-13 22:35:41 +000011171 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011172 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011173
Antonio Kantek15beb512022-06-13 22:35:41 +000011174 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +000011175 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000011176 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -070011177 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
11178 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000011179 mThirdWindow->assertNoEvents();
11180 }
11181
11182 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
11183 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000011184 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +000011185 SECOND_DISPLAY_ID)) {
11186 mWindow->assertNoEvents();
11187 mSecondWindow->assertNoEvents();
11188 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -070011189 }
Antonio Kantekf16f2832021-09-28 04:39:20 +000011190 }
11191
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011192 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +000011193 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -070011194 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
11195 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000011196 mWindow->consumeTouchModeEvent(inTouchMode);
11197 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000011198 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +000011199 }
11200};
11201
Antonio Kantek26defcf2022-02-08 01:12:27 +000011202TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080011203 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +000011204 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
11205 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011206 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011207}
11208
Antonio Kantek26defcf2022-02-08 01:12:27 +000011209TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
11210 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011211 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011212 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011213 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011214 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000011215 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -070011216 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +000011217 mWindow->assertNoEvents();
11218 mSecondWindow->assertNoEvents();
11219}
11220
11221TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
11222 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011223 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011224 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011225 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +000011226 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000011227 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011228}
11229
Antonio Kantekf16f2832021-09-28 04:39:20 +000011230TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080011231 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +000011232 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
11233 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011234 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000011235 mWindow->assertNoEvents();
11236 mSecondWindow->assertNoEvents();
11237}
11238
Antonio Kantek15beb512022-06-13 22:35:41 +000011239TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
11240 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
11241 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
11242 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011243 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +000011244 mWindow->assertNoEvents();
11245 mSecondWindow->assertNoEvents();
11246 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
11247}
11248
Antonio Kantek48710e42022-03-24 14:19:30 -070011249TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
11250 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11252 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -070011253 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11254 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
11255
11256 // Then remove focus.
11257 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011258 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -070011259
11260 // Assert that caller can switch touch mode by owning one of the last interacted window.
11261 const WindowInfo& windowInfo = *mWindow->getInfo();
11262 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
11263 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011264 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -070011265}
11266
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011267class InputDispatcherSpyWindowTest : public InputDispatcherTest {
11268public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011269 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011270 std::shared_ptr<FakeApplicationHandle> application =
11271 std::make_shared<FakeApplicationHandle>();
11272 std::string name = "Fake Spy ";
11273 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011274 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
11275 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011276 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011277 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011278 return spy;
11279 }
11280
11281 sp<FakeWindowHandle> createForeground() {
11282 std::shared_ptr<FakeApplicationHandle> application =
11283 std::make_shared<FakeApplicationHandle>();
11284 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011285 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
11286 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011287 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011288 return window;
11289 }
11290
11291private:
11292 int mSpyCount{0};
11293};
11294
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011295using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011296/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011297 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
11298 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011299TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -070011300 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011301 ScopedSilentDeath _silentDeath;
11302
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011303 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011304 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011305 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011306 ".* not a trusted overlay");
11307}
11308
11309/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011310 * Input injection into a display with a spy window but no foreground windows should succeed.
11311 */
11312TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011313 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011314 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011315
11316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011317 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011318 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11319 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11320}
11321
11322/**
11323 * Verify the order in which different input windows receive events. The touched foreground window
11324 * (if there is one) should always receive the event first. When there are multiple spy windows, the
11325 * spy windows will receive the event according to their Z-order, where the top-most spy window will
11326 * receive events before ones belows it.
11327 *
11328 * Here, we set up a scenario with four windows in the following Z order from the top:
11329 * spy1, spy2, window, spy3.
11330 * We then inject an event and verify that the foreground "window" receives it first, followed by
11331 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
11332 * window.
11333 */
11334TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
11335 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011336 auto spy1 = createSpy();
11337 auto spy2 = createSpy();
11338 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011339 mDispatcher->onWindowInfosChanged(
11340 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011341 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
11342 const size_t numChannels = channels.size();
11343
Michael Wright8e9a8562022-02-09 13:44:29 +000011344 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011345 if (!epollFd.ok()) {
11346 FAIL() << "Failed to create epoll fd";
11347 }
11348
11349 for (size_t i = 0; i < numChannels; i++) {
11350 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
11351 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
11352 FAIL() << "Failed to add fd to epoll";
11353 }
11354 }
11355
11356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011357 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011358 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11359
11360 std::vector<size_t> eventOrder;
11361 std::vector<struct epoll_event> events(numChannels);
11362 for (;;) {
11363 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
11364 (100ms).count());
11365 if (nFds < 0) {
11366 FAIL() << "Failed to call epoll_wait";
11367 }
11368 if (nFds == 0) {
11369 break; // epoll_wait timed out
11370 }
11371 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -070011372 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -070011373 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011374 channels[i]->consumeMotionDown();
11375 }
11376 }
11377
11378 // Verify the order in which the events were received.
11379 EXPECT_EQ(3u, eventOrder.size());
11380 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
11381 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
11382 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
11383}
11384
11385/**
11386 * A spy window using the NOT_TOUCHABLE flag does not receive events.
11387 */
11388TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
11389 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011390 auto spy = createSpy();
11391 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011392 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011393
11394 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011395 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011396 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11397 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11398 spy->assertNoEvents();
11399}
11400
11401/**
11402 * A spy window will only receive gestures that originate within its touchable region. Gestures that
11403 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
11404 * to the window.
11405 */
11406TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
11407 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011408 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011409 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011410 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011411
11412 // Inject an event outside the spy window's touchable region.
11413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011414 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011415 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11416 window->consumeMotionDown();
11417 spy->assertNoEvents();
11418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011419 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011420 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11421 window->consumeMotionUp();
11422 spy->assertNoEvents();
11423
11424 // Inject an event inside the spy window's touchable region.
11425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011426 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011427 {5, 10}))
11428 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11429 window->consumeMotionDown();
11430 spy->consumeMotionDown();
11431}
11432
11433/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011434 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011435 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011436 */
11437TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
11438 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011439 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011440 auto spy = createSpy();
11441 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011442 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011443 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011444 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011445
11446 // Inject an event outside the spy window's frame and touchable region.
11447 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011448 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011449 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011450 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11451 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011452 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011453}
11454
11455/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011456 * Even when a spy window spans over multiple foreground windows, the spy should receive all
11457 * pointers that are down within its bounds.
11458 */
11459TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
11460 auto windowLeft = createForeground();
11461 windowLeft->setFrame({0, 0, 100, 200});
11462 auto windowRight = createForeground();
11463 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011464 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011465 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011466 mDispatcher->onWindowInfosChanged(
11467 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011468
11469 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011470 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011471 {50, 50}))
11472 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11473 windowLeft->consumeMotionDown();
11474 spy->consumeMotionDown();
11475
11476 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011477 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011478 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011479 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11480 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011481 .build();
11482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011483 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011484 InputEventInjectionSync::WAIT_FOR_RESULT))
11485 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11486 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +000011487 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011488}
11489
11490/**
11491 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
11492 * the spy should receive the second pointer with ACTION_DOWN.
11493 */
11494TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
11495 auto window = createForeground();
11496 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011497 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011498 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011499 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011500
11501 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011502 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011503 {50, 50}))
11504 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11505 window->consumeMotionDown();
11506 spyRight->assertNoEvents();
11507
11508 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011509 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011510 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011511 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11512 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011513 .build();
11514 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011515 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011516 InputEventInjectionSync::WAIT_FOR_RESULT))
11517 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000011518 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011519 spyRight->consumeMotionDown();
11520}
11521
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011522/**
11523 * The spy window should not be able to affect whether or not touches are split. Only the foreground
11524 * windows should be allowed to control split touch.
11525 */
11526TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080011527 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011528 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011529 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080011530 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011531
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011532 auto window = createForeground();
11533 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011534
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011535 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011536
11537 // First finger down, no window touched.
11538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011539 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011540 {100, 200}))
11541 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11542 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11543 window->assertNoEvents();
11544
11545 // Second finger down on window, the window should receive touch down.
11546 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011547 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011548 .displayId(ADISPLAY_ID_DEFAULT)
11549 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011550 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11551 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011552 .build();
11553 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011554 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011555 InputEventInjectionSync::WAIT_FOR_RESULT))
11556 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11557
11558 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +000011559 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011560}
11561
11562/**
11563 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
11564 * do not receive key events.
11565 */
11566TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011567 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011568 spy->setFocusable(false);
11569
11570 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011571 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011572 setFocusedWindow(window);
11573 window->consumeFocusEvent(true);
11574
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011575 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011576 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11577 window->consumeKeyDown(ADISPLAY_ID_NONE);
11578
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011580 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11581 window->consumeKeyUp(ADISPLAY_ID_NONE);
11582
11583 spy->assertNoEvents();
11584}
11585
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011586using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
11587
11588/**
11589 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
11590 * are currently sent to any other windows - including other spy windows - will also be cancelled.
11591 */
11592TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
11593 auto window = createForeground();
11594 auto spy1 = createSpy();
11595 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011596 mDispatcher->onWindowInfosChanged(
11597 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011598
11599 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011600 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011601 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11602 window->consumeMotionDown();
11603 spy1->consumeMotionDown();
11604 spy2->consumeMotionDown();
11605
11606 // Pilfer pointers from the second spy window.
11607 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
11608 spy2->assertNoEvents();
11609 spy1->consumeMotionCancel();
11610 window->consumeMotionCancel();
11611
11612 // The rest of the gesture should only be sent to the second spy window.
11613 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011614 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011615 ADISPLAY_ID_DEFAULT))
11616 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11617 spy2->consumeMotionMove();
11618 spy1->assertNoEvents();
11619 window->assertNoEvents();
11620}
11621
11622/**
11623 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
11624 * in the middle of the gesture.
11625 */
11626TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
11627 auto window = createForeground();
11628 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011629 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011630
11631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011632 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011633 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11634 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11635 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11636
11637 window->releaseChannel();
11638
11639 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11640
11641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011642 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11644 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
11645}
11646
11647/**
11648 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
11649 * the spy, but not to any other windows.
11650 */
11651TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
11652 auto spy = createSpy();
11653 auto window = createForeground();
11654
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011655 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011656
11657 // First finger down on the window and the spy.
11658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011659 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011660 {100, 200}))
11661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11662 spy->consumeMotionDown();
11663 window->consumeMotionDown();
11664
11665 // Spy window pilfers the pointers.
11666 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11667 window->consumeMotionCancel();
11668
11669 // Second finger down on the window and spy, but the window should not receive the pointer down.
11670 const MotionEvent secondFingerDownEvent =
11671 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11672 .displayId(ADISPLAY_ID_DEFAULT)
11673 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011674 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11675 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011676 .build();
11677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011678 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011679 InputEventInjectionSync::WAIT_FOR_RESULT))
11680 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11681
Harry Cutts33476232023-01-30 19:57:29 +000011682 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011683
11684 // Third finger goes down outside all windows, so injection should fail.
11685 const MotionEvent thirdFingerDownEvent =
11686 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11687 .displayId(ADISPLAY_ID_DEFAULT)
11688 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011689 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11690 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
11691 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011692 .build();
11693 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011694 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011695 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080011696 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011697
11698 spy->assertNoEvents();
11699 window->assertNoEvents();
11700}
11701
11702/**
11703 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
11704 */
11705TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
11706 auto spy = createSpy();
11707 spy->setFrame(Rect(0, 0, 100, 100));
11708 auto window = createForeground();
11709 window->setFrame(Rect(0, 0, 200, 200));
11710
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011711 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011712
11713 // First finger down on the window only
11714 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011715 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011716 {150, 150}))
11717 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11718 window->consumeMotionDown();
11719
11720 // Second finger down on the spy and window
11721 const MotionEvent secondFingerDownEvent =
11722 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11723 .displayId(ADISPLAY_ID_DEFAULT)
11724 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011725 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
11726 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011727 .build();
11728 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011729 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011730 InputEventInjectionSync::WAIT_FOR_RESULT))
11731 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11732 spy->consumeMotionDown();
11733 window->consumeMotionPointerDown(1);
11734
11735 // Third finger down on the spy and window
11736 const MotionEvent thirdFingerDownEvent =
11737 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11738 .displayId(ADISPLAY_ID_DEFAULT)
11739 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011740 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
11741 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
11742 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011743 .build();
11744 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011745 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011746 InputEventInjectionSync::WAIT_FOR_RESULT))
11747 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11748 spy->consumeMotionPointerDown(1);
11749 window->consumeMotionPointerDown(2);
11750
11751 // Spy window pilfers the pointers.
11752 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Harry Cutts101ee9b2023-07-06 18:04:14 +000011753 window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
11754 window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011755
11756 spy->assertNoEvents();
11757 window->assertNoEvents();
11758}
11759
11760/**
11761 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
11762 * other windows should be canceled. If this results in the cancellation of all pointers for some
11763 * window, then that window should receive ACTION_CANCEL.
11764 */
11765TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
11766 auto spy = createSpy();
11767 spy->setFrame(Rect(0, 0, 100, 100));
11768 auto window = createForeground();
11769 window->setFrame(Rect(0, 0, 200, 200));
11770
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011771 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011772
11773 // First finger down on both spy and window
11774 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011775 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011776 {10, 10}))
11777 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11778 window->consumeMotionDown();
11779 spy->consumeMotionDown();
11780
11781 // Second finger down on the spy and window
11782 const MotionEvent secondFingerDownEvent =
11783 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11784 .displayId(ADISPLAY_ID_DEFAULT)
11785 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011786 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
11787 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011788 .build();
11789 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011790 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011791 InputEventInjectionSync::WAIT_FOR_RESULT))
11792 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11793 spy->consumeMotionPointerDown(1);
11794 window->consumeMotionPointerDown(1);
11795
11796 // Spy window pilfers the pointers.
11797 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11798 window->consumeMotionCancel();
11799
11800 spy->assertNoEvents();
11801 window->assertNoEvents();
11802}
11803
11804/**
11805 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
11806 * be sent to other windows
11807 */
11808TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
11809 auto spy = createSpy();
11810 spy->setFrame(Rect(0, 0, 100, 100));
11811 auto window = createForeground();
11812 window->setFrame(Rect(0, 0, 200, 200));
11813
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011814 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011815
11816 // First finger down on both window and spy
11817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011818 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011819 {10, 10}))
11820 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11821 window->consumeMotionDown();
11822 spy->consumeMotionDown();
11823
11824 // Spy window pilfers the pointers.
11825 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11826 window->consumeMotionCancel();
11827
11828 // Second finger down on the window only
11829 const MotionEvent secondFingerDownEvent =
11830 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11831 .displayId(ADISPLAY_ID_DEFAULT)
11832 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011833 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
11834 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011835 .build();
11836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011837 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011838 InputEventInjectionSync::WAIT_FOR_RESULT))
11839 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11840 window->consumeMotionDown();
11841 window->assertNoEvents();
11842
11843 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
11844 spy->consumeMotionMove();
11845 spy->assertNoEvents();
11846}
11847
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011848/**
11849 * A window on the left and a window on the right. Also, a spy window that's above all of the
11850 * windows, and spanning both left and right windows.
11851 * Send simultaneous motion streams from two different devices, one to the left window, and another
11852 * to the right window.
11853 * Pilfer from spy window.
11854 * Check that the pilfering only affects the pointers that are actually being received by the spy.
11855 */
11856TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) {
11857 sp<FakeWindowHandle> spy = createSpy();
11858 spy->setFrame(Rect(0, 0, 200, 200));
11859 sp<FakeWindowHandle> leftWindow = createForeground();
11860 leftWindow->setFrame(Rect(0, 0, 100, 100));
11861
11862 sp<FakeWindowHandle> rightWindow = createForeground();
11863 rightWindow->setFrame(Rect(100, 0, 200, 100));
11864
11865 constexpr int32_t stylusDeviceId = 1;
11866 constexpr int32_t touchDeviceId = 2;
11867
11868 mDispatcher->onWindowInfosChanged(
11869 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
11870
11871 // Stylus down on left window and spy
11872 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
11873 .deviceId(stylusDeviceId)
11874 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
11875 .build());
11876 leftWindow->consumeMotionEvent(
11877 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
11878 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
11879
11880 // Finger down on right window and spy - but spy already has stylus
11881 mDispatcher->notifyMotion(
11882 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11883 .deviceId(touchDeviceId)
11884 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
11885 .build());
11886 rightWindow->consumeMotionEvent(
11887 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011888 spy->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011889
11890 // Act: pilfer from spy. Spy is currently receiving touch events.
11891 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011892 leftWindow->consumeMotionEvent(
11893 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011894 rightWindow->consumeMotionEvent(
11895 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
11896
11897 // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus
11898 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11899 .deviceId(stylusDeviceId)
11900 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
11901 .build());
11902 mDispatcher->notifyMotion(
11903 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
11904 .deviceId(touchDeviceId)
11905 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
11906 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011907 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011908
11909 spy->assertNoEvents();
11910 leftWindow->assertNoEvents();
11911 rightWindow->assertNoEvents();
11912}
11913
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000011914TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) {
11915 auto window = createForeground();
11916 auto spy = createSpy();
11917 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
11918
11919 mDispatcher->notifyMotion(
11920 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
11921 .deviceId(1)
11922 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200))
11923 .build());
11924 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
11925 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
11926
11927 // Pilfer pointers from the spy window should fail.
11928 EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken()));
11929 spy->assertNoEvents();
11930 window->assertNoEvents();
11931}
11932
Prabir Pradhand65552b2021-10-07 11:23:50 -070011933class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
11934public:
11935 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
11936 std::shared_ptr<FakeApplicationHandle> overlayApplication =
11937 std::make_shared<FakeApplicationHandle>();
11938 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011939 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
11940 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011941 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011942 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011943 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011944 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011945 overlay->setTrustedOverlay(true);
11946
11947 std::shared_ptr<FakeApplicationHandle> application =
11948 std::make_shared<FakeApplicationHandle>();
11949 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011950 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
11951 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011952 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011953 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011954
11955 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011956 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011957 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011958 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011959 return {std::move(overlay), std::move(window)};
11960 }
11961
11962 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000011963 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -070011964 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +000011965 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -070011966 }
11967
11968 void sendStylusEvent(int32_t action) {
11969 NotifyMotionArgs motionArgs =
11970 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
11971 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011972 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +000011973 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011974 }
11975};
11976
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011977using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
11978
11979TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -070011980 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011981 ScopedSilentDeath _silentDeath;
11982
Prabir Pradhand65552b2021-10-07 11:23:50 -070011983 auto [overlay, window] = setupStylusOverlayScenario();
11984 overlay->setTrustedOverlay(false);
11985 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011986 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
11987 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -070011988 ".* not a trusted overlay");
11989}
11990
11991TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
11992 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011993 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011994
11995 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
11996 overlay->consumeMotionDown();
11997 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
11998 overlay->consumeMotionUp();
11999
12000 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
12001 window->consumeMotionDown();
12002 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
12003 window->consumeMotionUp();
12004
12005 overlay->assertNoEvents();
12006 window->assertNoEvents();
12007}
12008
12009TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
12010 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012011 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012012 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070012013
12014 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
12015 overlay->consumeMotionDown();
12016 window->consumeMotionDown();
12017 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
12018 overlay->consumeMotionUp();
12019 window->consumeMotionUp();
12020
12021 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
12022 window->consumeMotionDown();
12023 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
12024 window->consumeMotionUp();
12025
12026 overlay->assertNoEvents();
12027 window->assertNoEvents();
12028}
12029
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012030/**
12031 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
12032 * The scenario is as follows:
12033 * - The stylus interceptor overlay is configured as a spy window.
12034 * - The stylus interceptor spy receives the start of a new stylus gesture.
12035 * - It pilfers pointers and then configures itself to no longer be a spy.
12036 * - The stylus interceptor continues to receive the rest of the gesture.
12037 */
12038TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
12039 auto [overlay, window] = setupStylusOverlayScenario();
12040 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012041 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012042
12043 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
12044 overlay->consumeMotionDown();
12045 window->consumeMotionDown();
12046
12047 // The interceptor pilfers the pointers.
12048 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
12049 window->consumeMotionCancel();
12050
12051 // The interceptor configures itself so that it is no longer a spy.
12052 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012053 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012054
12055 // It continues to receive the rest of the stylus gesture.
12056 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
12057 overlay->consumeMotionMove();
12058 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
12059 overlay->consumeMotionUp();
12060
12061 window->assertNoEvents();
12062}
12063
Prabir Pradhan5735a322022-04-11 17:23:34 +000012064struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012065 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012066 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +000012067 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
12068 std::unique_ptr<InputDispatcher>& mDispatcher;
12069
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012070 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +000012071 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
12072
12073 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012074 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012075 ADISPLAY_ID_DEFAULT, {100, 200},
12076 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
12077 AMOTION_EVENT_INVALID_CURSOR_POSITION},
12078 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
12079 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
12080 }
12081
12082 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012083 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012084 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +000012085 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +000012086 mPolicyFlags);
12087 }
12088
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012089 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +000012090 std::shared_ptr<FakeApplicationHandle> overlayApplication =
12091 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012092 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
12093 name, ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +000012094 window->setOwnerInfo(mPid, mUid);
12095 return window;
12096 }
12097};
12098
12099using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
12100
12101TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012102 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012103 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012104 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012105
12106 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12107 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12108 window->consumeMotionDown();
12109
12110 setFocusedWindow(window);
12111 window->consumeFocusEvent(true);
12112
12113 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12114 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
12115 window->consumeKeyDown(ADISPLAY_ID_NONE);
12116}
12117
12118TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012119 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012120 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012121 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012122
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012123 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012124 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
12125 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12126
12127 setFocusedWindow(window);
12128 window->consumeFocusEvent(true);
12129
12130 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
12131 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
12132 window->assertNoEvents();
12133}
12134
12135TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012136 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012137 auto window = owner.createWindow("Owned window");
12138 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012139 spy->setSpy(true);
12140 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012141 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012142
12143 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12144 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12145 spy->consumeMotionDown();
12146 window->consumeMotionDown();
12147}
12148
12149TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012150 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012151 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012152
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012153 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012154 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012155 randosSpy->setSpy(true);
12156 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012157 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012158
12159 // The event is targeted at owner's window, so injection should succeed, but the spy should
12160 // not receive the event.
12161 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12162 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12163 randosSpy->assertNoEvents();
12164 window->consumeMotionDown();
12165}
12166
12167TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012168 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012169 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012170
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012171 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012172 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012173 randosSpy->setSpy(true);
12174 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012175 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012176
12177 // A user that has injection permission can inject into any window.
12178 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012179 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012180 ADISPLAY_ID_DEFAULT));
12181 randosSpy->consumeMotionDown();
12182 window->consumeMotionDown();
12183
12184 setFocusedWindow(randosSpy);
12185 randosSpy->consumeFocusEvent(true);
12186
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012187 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Prabir Pradhan5735a322022-04-11 17:23:34 +000012188 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
12189 window->assertNoEvents();
12190}
12191
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012192TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012193 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012194 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012195
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012196 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012197 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012198 randosWindow->setFrame(Rect{-10, -10, -5, -5});
12199 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012200 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012201
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012202 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +000012203 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12204 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12205 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012206 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +000012207}
12208
Prabir Pradhan64f21d22023-11-28 21:19:42 +000012209using InputDispatcherPointerInWindowTest = InputDispatcherTest;
12210
12211TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) {
12212 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12213
12214 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12215 ADISPLAY_ID_DEFAULT);
12216 left->setFrame(Rect(0, 0, 100, 100));
12217 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12218 "Right Window", ADISPLAY_ID_DEFAULT);
12219 right->setFrame(Rect(100, 0, 200, 100));
12220 sp<FakeWindowHandle> spy =
12221 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
12222 spy->setFrame(Rect(0, 0, 200, 100));
12223 spy->setTrustedOverlay(true);
12224 spy->setSpy(true);
12225
12226 mDispatcher->onWindowInfosChanged(
12227 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
12228
12229 // Hover into the left window.
12230 mDispatcher->notifyMotion(
12231 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
12232 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50))
12233 .build());
12234
12235 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12236 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12237
12238 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12239 /*pointerId=*/0));
12240 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12241 /*pointerId=*/0));
12242 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12243 /*pointerId=*/0));
12244
12245 // Hover move to the right window.
12246 mDispatcher->notifyMotion(
12247 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
12248 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
12249 .build());
12250
12251 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12252 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12253 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
12254
12255 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12256 /*pointerId=*/0));
12257 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12258 /*pointerId=*/0));
12259 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12260 /*pointerId=*/0));
12261
12262 // Stop hovering.
12263 mDispatcher->notifyMotion(
12264 MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
12265 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
12266 .build());
12267
12268 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12269 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12270
12271 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12272 /*pointerId=*/0));
12273 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12274 /*pointerId=*/0));
12275 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12276 /*pointerId=*/0));
12277}
12278
12279TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) {
12280 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12281
12282 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12283 ADISPLAY_ID_DEFAULT);
12284 left->setFrame(Rect(0, 0, 100, 100));
12285 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12286 "Right Window", ADISPLAY_ID_DEFAULT);
12287 right->setFrame(Rect(100, 0, 200, 100));
12288 sp<FakeWindowHandle> spy =
12289 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
12290 spy->setFrame(Rect(0, 0, 200, 100));
12291 spy->setTrustedOverlay(true);
12292 spy->setSpy(true);
12293
12294 mDispatcher->onWindowInfosChanged(
12295 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
12296
12297 // First pointer down on left window.
12298 mDispatcher->notifyMotion(
12299 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
12300 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12301 .build());
12302
12303 left->consumeMotionDown();
12304 spy->consumeMotionDown();
12305
12306 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12307 /*pointerId=*/0));
12308 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12309 /*pointerId=*/0));
12310 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12311 /*pointerId=*/0));
12312
12313 // Second pointer down on right window.
12314 mDispatcher->notifyMotion(
12315 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
12316 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12317 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
12318 .build());
12319
12320 left->consumeMotionMove();
12321 right->consumeMotionDown();
12322 spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
12323
12324 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12325 /*pointerId=*/0));
12326 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12327 /*pointerId=*/0));
12328 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12329 /*pointerId=*/0));
12330 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12331 /*pointerId=*/1));
12332 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12333 /*pointerId=*/1));
12334 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12335 /*pointerId=*/1));
12336
12337 // Second pointer up.
12338 mDispatcher->notifyMotion(
12339 MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
12340 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12341 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
12342 .build());
12343
12344 left->consumeMotionMove();
12345 right->consumeMotionUp();
12346 spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
12347
12348 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12349 /*pointerId=*/0));
12350 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12351 /*pointerId=*/0));
12352 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12353 /*pointerId=*/0));
12354 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12355 /*pointerId=*/1));
12356 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12357 /*pointerId=*/1));
12358 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12359 /*pointerId=*/1));
12360
12361 // First pointer up.
12362 mDispatcher->notifyMotion(
12363 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
12364 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12365 .build());
12366
12367 left->consumeMotionUp();
12368 spy->consumeMotionUp();
12369
12370 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12371 /*pointerId=*/0));
12372 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12373 /*pointerId=*/0));
12374 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12375 /*pointerId=*/0));
12376}
12377
12378TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) {
12379 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12380
12381 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12382 ADISPLAY_ID_DEFAULT);
12383 left->setFrame(Rect(0, 0, 100, 100));
12384 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12385 "Right Window", ADISPLAY_ID_DEFAULT);
12386 right->setFrame(Rect(100, 0, 200, 100));
12387
12388 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
12389
12390 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12391 /*pointerId=*/0));
12392 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12393 /*pointerId=*/0));
12394
12395 // Hover move into the window.
12396 mDispatcher->notifyMotion(
12397 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12398 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
12399 .rawXCursorPosition(50)
12400 .rawYCursorPosition(50)
12401 .deviceId(DEVICE_ID)
12402 .build());
12403
12404 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12405
12406 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12407 /*pointerId=*/0));
12408
12409 // Move the mouse with another device. This cancels the hovering pointer from the first device.
12410 mDispatcher->notifyMotion(
12411 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12412 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
12413 .rawXCursorPosition(51)
12414 .rawYCursorPosition(50)
12415 .deviceId(SECOND_DEVICE_ID)
12416 .build());
12417
12418 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12419 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12420
12421 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
12422 // a HOVER_EXIT from the first device.
12423 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12424 /*pointerId=*/0));
12425 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT,
12426 SECOND_DEVICE_ID,
12427 /*pointerId=*/0));
12428
12429 // Move the mouse outside the window. Document the current behavior, where the window does not
12430 // receive HOVER_EXIT even though the mouse left the window.
12431 mDispatcher->notifyMotion(
12432 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12433 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
12434 .rawXCursorPosition(150)
12435 .rawYCursorPosition(50)
12436 .deviceId(SECOND_DEVICE_ID)
12437 .build());
12438
12439 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12440 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12441 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12442 /*pointerId=*/0));
12443 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT,
12444 SECOND_DEVICE_ID,
12445 /*pointerId=*/0));
12446}
12447
Garfield Tane84e6f92019-08-29 17:28:41 -070012448} // namespace android::inputdispatcher