blob: ceb3c412949731c7f21e020ca5c12660be62d9d9 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000018#include "../BlockingQueue.h"
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070019#include "FakeApplicationHandle.h"
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +000020#include "FakeInputTracingBackend.h"
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000021#include "TestEventMatchers.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080022
Cody Heiner166a5af2023-07-07 12:25:00 -070023#include <NotifyArgsBuilders.h>
Prabir Pradhan5893d362023-11-17 04:30:40 +000024#include <android-base/logging.h>
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070025#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080026#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080027#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070028#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070029#include <binder/Binder.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000030#include <com_android_input_flags.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000031#include <fcntl.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000032#include <flag_macros.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080033#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100035#include <input/Input.h>
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070036#include <input/PrintTools.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080038#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100039
Garfield Tan1c7bc862020-01-28 13:24:04 -080040#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080041#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070042#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080043#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080044#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Garfield Tan1c7bc862020-01-28 13:24:04 -080046using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050047using android::gui::FocusRequest;
48using android::gui::TouchOcclusionMode;
49using android::gui::WindowInfo;
50using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080051using android::os::InputEventInjectionResult;
52using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080053
Garfield Tane84e6f92019-08-29 17:28:41 -070054namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
Dominik Laskowski2f01d772022-03-23 16:01:29 -070056using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080057using testing::AllOf;
Prabir Pradhan5893d362023-11-17 04:30:40 +000058using testing::Not;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070059
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070060namespace {
61
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000063static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080064
65// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000066static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080067static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080068
Jeff Brownf086ddb2014-02-11 14:28:48 -080069// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000070static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
71static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080072
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000073// Ensure common actions are interchangeable between keys and motions for convenience.
74static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN);
75static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080076static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
77static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
78static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
79static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070080static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080081static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070082static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080083static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080084static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080085/**
86 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
87 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
88 * index 0) is the new pointer going down. The same pointer could have been placed at a different
89 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
90 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
91 * pointer id=0 leaves but the pointer id=1 remains.
92 */
93static constexpr int32_t POINTER_0_DOWN =
94 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080095static constexpr int32_t POINTER_1_DOWN =
96 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000097static constexpr int32_t POINTER_2_DOWN =
98 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000099static constexpr int32_t POINTER_3_DOWN =
100 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +0000101static constexpr int32_t POINTER_0_UP =
102 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800103static constexpr int32_t POINTER_1_UP =
104 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +0000105static constexpr int32_t POINTER_2_UP =
106 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800107
Antonio Kantek15beb512022-06-13 22:35:41 +0000108// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000109static constexpr gui::Pid WINDOW_PID{999};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000110static constexpr gui::Uid WINDOW_UID{1001};
Prabir Pradhan5735a322022-04-11 17:23:34 +0000111
Antonio Kantek15beb512022-06-13 22:35:41 +0000112// The default pid and uid for the windows created on the secondary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000113static constexpr gui::Pid SECONDARY_WINDOW_PID{1010};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000114static constexpr gui::Uid SECONDARY_WINDOW_UID{1012};
Antonio Kantek15beb512022-06-13 22:35:41 +0000115
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000116// An arbitrary pid of the gesture monitor window
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000117static constexpr gui::Pid MONITOR_PID{2001};
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000118
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700119/**
120 * If we expect to receive the event, the timeout can be made very long. When the test are running
121 * correctly, we will actually never wait until the end of the timeout because the wait will end
122 * when the event comes in. Still, this value shouldn't be infinite. During development, a local
123 * change may cause the test to fail. This timeout should be short enough to not annoy so that the
124 * developer can see the failure quickly (on human scale).
125 */
126static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms;
127/**
128 * When no event is expected, we can have a very short timeout. A large value here would slow down
129 * the tests. In the unlikely event of system being too slow, the event may still be present but the
130 * timeout would complete before it is consumed. This would result in test flakiness. If this
131 * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this
132 * would get noticed and addressed quickly.
133 */
134static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms;
135
Arthur Hungc539dbb2022-12-08 07:45:36 +0000136static constexpr int expectedWallpaperFlags =
137 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
138
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800139using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
140
Gang Wang342c9272020-01-13 13:15:04 -0500141/**
142 * Return a DOWN key event with KEYCODE_A.
143 */
144static KeyEvent getTestKeyEvent() {
145 KeyEvent event;
146
Garfield Tanfbe732e2020-01-24 11:26:14 -0800147 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
148 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
149 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500150 return event;
151}
152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153// --- FakeInputDispatcherPolicy ---
154
155class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000156 struct AnrResult {
157 sp<IBinder> token{};
Prabir Pradhanfc364722024-02-08 17:51:20 +0000158 std::optional<gui::Pid> pid{};
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000159 };
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800160 /* Stores data about a user-activity-poke event from the dispatcher. */
161 struct UserActivityPokeEvent {
162 nsecs_t eventTime;
163 int32_t eventType;
164 int32_t displayId;
165
166 bool operator==(const UserActivityPokeEvent& rhs) const = default;
167
168 friend std::ostream& operator<<(std::ostream& os, const UserActivityPokeEvent& ev) {
169 os << "UserActivityPokeEvent[time=" << ev.eventTime << ", eventType=" << ev.eventType
170 << ", displayId=" << ev.displayId << "]";
171 return os;
172 }
173 };
Prabir Pradhanedd96402022-02-15 01:46:16 -0800174
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000176 FakeInputDispatcherPolicy() = default;
177 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800178
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800179 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700180 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700181 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700182 EXPECT_EQ(event.getDisplayId(), args.displayId);
183
184 const auto& keyEvent = static_cast<const KeyEvent&>(event);
185 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
186 EXPECT_EQ(keyEvent.getAction(), args.action);
187 });
Jackal Guof9696682018-10-05 12:23:23 +0800188 }
189
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700190 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
191 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700192 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700193 EXPECT_EQ(event.getDisplayId(), args.displayId);
194
195 const auto& motionEvent = static_cast<const MotionEvent&>(event);
196 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
197 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000198 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
199 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
200 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
201 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700202 });
Jackal Guof9696682018-10-05 12:23:23 +0800203 }
204
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700205 void assertFilterInputEventWasNotCalled() {
206 std::scoped_lock lock(mLock);
207 ASSERT_EQ(nullptr, mFilteredEvent);
208 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800210 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700211 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800212 ASSERT_TRUE(mConfigurationChangedTime)
213 << "Timed out waiting for configuration changed call";
214 ASSERT_EQ(*mConfigurationChangedTime, when);
215 mConfigurationChangedTime = std::nullopt;
216 }
217
218 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700219 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800220 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800221 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800222 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
223 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
224 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
225 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
226 mLastNotifySwitch = std::nullopt;
227 }
228
chaviwfd6d3512019-03-25 13:23:49 -0700229 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700230 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800231 ASSERT_EQ(touchedToken, mOnPointerDownToken);
232 mOnPointerDownToken.clear();
233 }
234
235 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700236 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800237 ASSERT_TRUE(mOnPointerDownToken == nullptr)
238 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700239 }
240
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700241 // This function must be called soon after the expected ANR timer starts,
242 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500243 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700244 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500245 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800246 std::unique_lock lock(mLock);
247 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500248 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800249 ASSERT_NO_FATAL_FAILURE(
250 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500251 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700252 }
253
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000254 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800255 const sp<WindowInfoHandle>& window) {
256 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
257 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
258 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500259 }
260
Prabir Pradhanedd96402022-02-15 01:46:16 -0800261 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
262 const sp<IBinder>& expectedToken,
Prabir Pradhanfc364722024-02-08 17:51:20 +0000263 std::optional<gui::Pid> expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800264 std::unique_lock lock(mLock);
265 android::base::ScopedLockAssertion assumeLocked(mLock);
266 AnrResult result;
267 ASSERT_NO_FATAL_FAILURE(result =
268 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000269 ASSERT_EQ(expectedToken, result.token);
270 ASSERT_EQ(expectedPid, result.pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500271 }
272
Prabir Pradhanedd96402022-02-15 01:46:16 -0800273 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000274 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500275 std::unique_lock lock(mLock);
276 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800277 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
278 const auto& [token, _] = result;
279 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000280 }
281
Prabir Pradhanedd96402022-02-15 01:46:16 -0800282 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
Prabir Pradhanfc364722024-02-08 17:51:20 +0000283 std::optional<gui::Pid> expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800284 std::unique_lock lock(mLock);
285 android::base::ScopedLockAssertion assumeLocked(mLock);
286 AnrResult result;
287 ASSERT_NO_FATAL_FAILURE(
288 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000289 ASSERT_EQ(expectedToken, result.token);
290 ASSERT_EQ(expectedPid, result.pid);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800291 }
292
293 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000294 sp<IBinder> getResponsiveWindowToken() {
295 std::unique_lock lock(mLock);
296 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800297 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
298 const auto& [token, _] = result;
299 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700300 }
301
302 void assertNotifyAnrWasNotCalled() {
303 std::scoped_lock lock(mLock);
304 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800305 ASSERT_TRUE(mAnrWindows.empty());
306 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500307 << "ANR was not called, but please also consume the 'connection is responsive' "
308 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700309 }
310
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000311 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800312 std::unique_lock lock(mLock);
313 base::ScopedLockAssertion assumeLocked(mLock);
314
315 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
316 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000317 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800318 enabled;
319 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000320 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
321 << ") to be called.";
322 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800323 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000324 auto request = *mPointerCaptureRequest;
325 mPointerCaptureRequest.reset();
326 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800327 }
328
329 void assertSetPointerCaptureNotCalled() {
330 std::unique_lock lock(mLock);
331 base::ScopedLockAssertion assumeLocked(mLock);
332
333 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000334 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800335 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000336 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800337 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000338 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800339 }
340
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -0700341 void assertDropTargetEquals(const InputDispatcherInterface& dispatcher,
342 const sp<IBinder>& targetToken) {
343 dispatcher.waitForIdle();
arthurhungf452d0b2021-01-06 00:19:52 +0800344 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800345 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800346 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800347 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800348 }
349
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800350 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
351 std::unique_lock lock(mLock);
352 base::ScopedLockAssertion assumeLocked(mLock);
353 std::optional<sp<IBinder>> receivedToken =
354 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
355 mNotifyInputChannelBroken);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000356 ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token";
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800357 ASSERT_EQ(token, *receivedToken);
358 }
359
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800360 /**
361 * Set policy timeout. A value of zero means next key will not be intercepted.
362 */
363 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
364 mInterceptKeyTimeout = timeout;
365 }
366
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -0800367 std::chrono::nanoseconds getKeyWaitingForEventsTimeout() override { return 500ms; }
368
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700369 void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; }
370
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800371 void assertUserActivityNotPoked() {
372 std::unique_lock lock(mLock);
373 base::ScopedLockAssertion assumeLocked(mLock);
374
375 std::optional<UserActivityPokeEvent> pokeEvent =
376 getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock,
377 mNotifyUserActivity);
378
379 ASSERT_FALSE(pokeEvent) << "Expected user activity not to have been poked";
Josep del Riob3981622023-04-18 15:49:45 +0000380 }
381
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800382 /**
383 * Asserts that a user activity poke has happened. The earliest recorded poke event will be
384 * cleared after this call.
385 *
386 * If an expected UserActivityPokeEvent is provided, asserts that the given event is the
387 * earliest recorded poke event.
388 */
389 void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {}) {
390 std::unique_lock lock(mLock);
391 base::ScopedLockAssertion assumeLocked(mLock);
392
393 std::optional<UserActivityPokeEvent> pokeEvent =
394 getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock,
395 mNotifyUserActivity);
396 ASSERT_TRUE(pokeEvent) << "Expected a user poke event";
397
398 if (expectedPokeEvent) {
399 ASSERT_EQ(expectedPokeEvent, *pokeEvent);
400 }
Josep del Riob3981622023-04-18 15:49:45 +0000401 }
402
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000403 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000404 ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
405 }
406
407 void assertNotifyDeviceInteractionWasNotCalled() {
408 ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
409 }
410
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000411 void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) {
412 std::scoped_lock lock(mLock);
413 mUnhandledKeyHandler = handler;
414 }
415
416 void assertUnhandledKeyReported(int32_t keycode) {
417 std::unique_lock lock(mLock);
418 base::ScopedLockAssertion assumeLocked(mLock);
419 std::optional<int32_t> unhandledKeycode =
420 getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock,
421 mNotifyUnhandledKey);
422 ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported";
423 ASSERT_EQ(unhandledKeycode, keycode);
424 }
425
426 void assertUnhandledKeyNotReported() {
427 std::unique_lock lock(mLock);
428 base::ScopedLockAssertion assumeLocked(mLock);
429 std::optional<int32_t> unhandledKeycode =
430 getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock,
431 mNotifyUnhandledKey);
432 ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported";
433 }
434
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700436 std::mutex mLock;
437 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
438 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
439 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
440 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800441
Prabir Pradhan99987712020-11-10 18:43:05 -0800442 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000443
444 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800445
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700446 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700447 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800448 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
449 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700450 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800451 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
452 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700453
arthurhungf452d0b2021-01-06 00:19:52 +0800454 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800455 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800456
457 std::condition_variable mNotifyUserActivity;
458 std::queue<UserActivityPokeEvent> mUserActivityPokeEvents;
arthurhungf452d0b2021-01-06 00:19:52 +0800459
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800460 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
461
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700462 std::chrono::nanoseconds mStaleEventTimeout = 1000ms;
463
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000464 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000465
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000466 std::condition_variable mNotifyUnhandledKey;
467 std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock);
468 std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock);
469
Prabir Pradhanedd96402022-02-15 01:46:16 -0800470 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
471 // for a specific container to become non-empty. When the container is non-empty, return the
472 // first entry from the container and erase it.
473 template <class T>
474 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
475 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
476 // If there is an ANR, Dispatcher won't be idle because there are still events
477 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
478 // before checking if ANR was called.
479 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
480 // to provide it some time to act. 100ms seems reasonable.
481 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
482 const std::chrono::time_point start = std::chrono::steady_clock::now();
483 std::optional<T> token =
484 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
485 if (!token.has_value()) {
486 ADD_FAILURE() << "Did not receive the ANR callback";
487 return {};
488 }
489
490 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
491 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
492 // the dispatcher started counting before this function was called
493 if (std::chrono::abs(timeout - waited) > 100ms) {
494 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
495 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
496 << "ms, but waited "
497 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
498 << "ms instead";
499 }
500 return *token;
501 }
502
503 template <class T>
504 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
505 std::queue<T>& storage,
506 std::unique_lock<std::mutex>& lock,
507 std::condition_variable& condition)
508 REQUIRES(mLock) {
509 condition.wait_for(lock, timeout,
510 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
511 if (storage.empty()) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800512 return std::nullopt;
513 }
514 T item = storage.front();
515 storage.pop();
516 return std::make_optional(item);
517 }
518
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600519 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700520 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800521 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522 }
523
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000524 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800525 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700526 std::scoped_lock lock(mLock);
Prabir Pradhanfc364722024-02-08 17:51:20 +0000527 mAnrWindows.push({connectionToken, pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700528 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500529 }
530
Prabir Pradhanedd96402022-02-15 01:46:16 -0800531 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000532 std::optional<gui::Pid> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500533 std::scoped_lock lock(mLock);
Prabir Pradhanfc364722024-02-08 17:51:20 +0000534 mResponsiveWindows.push({connectionToken, pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500535 mNotifyAnr.notify_all();
536 }
537
538 void notifyNoFocusedWindowAnr(
539 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
540 std::scoped_lock lock(mLock);
541 mAnrApplications.push(applicationHandle);
542 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543 }
544
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800545 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
546 std::scoped_lock lock(mLock);
547 mBrokenInputChannels.push(connectionToken);
548 mNotifyInputChannelBroken.notify_all();
549 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600551 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700552
Chris Yef59a2f42020-10-16 12:55:26 -0700553 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
554 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
555 const std::vector<float>& values) override {}
556
557 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
558 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000559
Chris Yefb552902021-02-03 17:18:37 -0800560 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
561
Prabir Pradhana41d2442023-04-20 21:30:40 +0000562 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700563 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000564 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700565 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000566 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
567 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800568 break;
569 }
570
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700571 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000572 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
573 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800574 break;
575 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700576 default: {
577 ADD_FAILURE() << "Should only filter keys or motions";
578 break;
579 }
Jackal Guof9696682018-10-05 12:23:23 +0800580 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 return true;
582 }
583
Prabir Pradhana41d2442023-04-20 21:30:40 +0000584 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
585 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800586 // Clear intercept state when we handled the event.
587 mInterceptKeyTimeout = 0ms;
588 }
589 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590
Yeabkal Wubshit88a90412023-12-21 18:23:04 -0800591 void interceptMotionBeforeQueueing(int32_t, uint32_t, int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592
Prabir Pradhana41d2442023-04-20 21:30:40 +0000593 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800594 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
595 // Clear intercept state so we could dispatch the event in next wake.
596 mInterceptKeyTimeout = 0ms;
597 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 }
599
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000600 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event,
Prabir Pradhana41d2442023-04-20 21:30:40 +0000601 uint32_t) override {
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000602 std::scoped_lock lock(mLock);
603 mReportedUnhandledKeycodes.emplace(event.getKeyCode());
604 mNotifyUnhandledKey.notify_all();
605 return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 }
607
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600608 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
609 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700610 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800611 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
612 * essentially a passthrough for notifySwitch.
613 */
Prabir Pradhana8fe7c52023-12-14 22:07:23 +0000614 mLastNotifySwitch =
615 NotifySwitchArgs(InputEvent::nextId(), when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 }
617
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800618 void pokeUserActivity(nsecs_t eventTime, int32_t eventType, int32_t displayId) override {
Josep del Riob3981622023-04-18 15:49:45 +0000619 std::scoped_lock lock(mLock);
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800620 mNotifyUserActivity.notify_all();
621 mUserActivityPokeEvents.push({eventTime, eventType, displayId});
Josep del Riob3981622023-04-18 15:49:45 +0000622 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700624 bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override {
625 return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout;
626 }
627
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600628 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700629 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700630 mOnPointerDownToken = newToken;
631 }
632
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000633 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800634 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000635 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800636 mPointerCaptureChangedCondition.notify_all();
637 }
638
arthurhungf452d0b2021-01-06 00:19:52 +0800639 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
640 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800641 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800642 mDropTargetWindowToken = token;
643 }
644
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000645 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000646 const std::set<gui::Uid>& uids) override {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000647 ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids));
648 }
649
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700650 void assertFilterInputEventWasCalledInternal(
651 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700652 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800653 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700654 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800655 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800656 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657};
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700658} // namespace
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660// --- InputDispatcherTest ---
661
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000662// The trace is a global variable for now, to avoid having to pass it into all of the
663// FakeWindowHandles created throughout the tests.
664// TODO(b/210460522): Update the tests to avoid the need to have the trace be a global variable.
665static std::shared_ptr<VerifyingTrace> gVerifyingTrace = std::make_shared<VerifyingTrace>();
666
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667class InputDispatcherTest : public testing::Test {
668protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000669 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700670 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000672 void SetUp() override {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000673 gVerifyingTrace->reset();
Prabir Pradhana41d2442023-04-20 21:30:40 +0000674 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +0000675 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy,
676 std::make_unique<FakeInputTracingBackend>(
677 gVerifyingTrace));
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700678
Harry Cutts101ee9b2023-07-06 18:04:14 +0000679 mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000680 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700681 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 }
683
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000684 void TearDown() override {
Prabir Pradhan0eaf1402024-02-05 22:43:04 +0000685 ASSERT_NO_FATAL_FAILURE(gVerifyingTrace->verifyExpectedEventsTraced());
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700686 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000687 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700688 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700690
691 /**
692 * Used for debugging when writing the test
693 */
694 void dumpDispatcherState() {
695 std::string dump;
696 mDispatcher->dump(dump);
697 std::stringstream ss(dump);
698 std::string to;
699
700 while (std::getline(ss, to, '\n')) {
701 ALOGE("%s", to.c_str());
702 }
703 }
Vishnu Nair958da932020-08-21 17:12:37 -0700704
Chavi Weingarten847e8512023-03-29 00:26:09 +0000705 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700706 FocusRequest request;
707 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000708 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700709 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
710 request.displayId = window->getInfo()->displayId;
711 mDispatcher->setFocusedWindow(request);
712 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713};
714
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
716 KeyEvent event;
717
718 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800719 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
720 INVALID_HMAC,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000721 /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600722 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800723 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000724 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000725 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800726 << "Should reject key events with undefined action.";
727
728 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800729 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
730 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600731 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800732 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000733 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000734 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 << "Should reject key events with ACTION_MULTIPLE.";
736}
737
738TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
739 MotionEvent event;
740 PointerProperties pointerProperties[MAX_POINTERS + 1];
741 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800742 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 pointerProperties[i].clear();
744 pointerProperties[i].id = i;
745 pointerCoords[i].clear();
746 }
747
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800748 // Some constants commonly used below
749 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
750 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
751 constexpr int32_t metaState = AMETA_NONE;
752 constexpr MotionClassification classification = MotionClassification::NONE;
753
chaviw9eaa22c2020-07-01 16:21:27 -0700754 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800756 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000757 /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700758 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700759 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
760 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000761 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800762 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000763 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000764 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765 << "Should reject motion events with undefined action.";
766
767 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800768 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800769 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
770 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
771 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
772 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000773 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800774 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000775 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000776 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 << "Should reject motion events with pointer down index too large.";
778
Garfield Tanfbe732e2020-01-24 11:26:14 -0800779 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700780 AMOTION_EVENT_ACTION_POINTER_DOWN |
781 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700782 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
783 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700784 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000785 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800786 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000787 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000788 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 << "Should reject motion events with pointer down index too small.";
790
791 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800792 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800793 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
794 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
795 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
796 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000797 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800798 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000799 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000800 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801 << "Should reject motion events with pointer up index too large.";
802
Garfield Tanfbe732e2020-01-24 11:26:14 -0800803 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700804 AMOTION_EVENT_ACTION_POINTER_UP |
805 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700806 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
807 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700808 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000809 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800810 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000811 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000812 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 << "Should reject motion events with pointer up index too small.";
814
815 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800816 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
817 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700818 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700819 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
820 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000821 /*pointerCount=*/0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800822 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000823 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000824 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 << "Should reject motion events with 0 pointers.";
826
Garfield Tanfbe732e2020-01-24 11:26:14 -0800827 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
828 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700829 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700830 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
831 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000832 /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800833 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000834 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000835 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 << "Should reject motion events with more than MAX_POINTERS pointers.";
837
838 // Rejects motion events with invalid pointer ids.
839 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800840 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
841 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700842 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700843 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
844 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000845 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800846 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000847 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000848 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 << "Should reject motion events with pointer ids less than 0.";
850
851 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800852 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
853 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700854 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700855 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
856 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000857 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800858 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000859 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000860 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
862
863 // Rejects motion events with duplicate pointer ids.
864 pointerProperties[0].id = 1;
865 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800866 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
867 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700868 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700869 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
870 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000871 /*pointerCount=*/2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800872 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000873 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000874 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 << "Should reject motion events with duplicate pointer ids.";
876}
877
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800878/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
879
880TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
881 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000882 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800883 ASSERT_TRUE(mDispatcher->waitForIdle());
884
885 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
886}
887
888TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Prabir Pradhana8fe7c52023-12-14 22:07:23 +0000889 NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0,
890 /*switchValues=*/1,
Harry Cutts33476232023-01-30 19:57:29 +0000891 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000892 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800893
894 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
895 args.policyFlags |= POLICY_FLAG_TRUSTED;
896 mFakePolicy->assertNotifySwitchWasCalled(args);
897}
898
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700899namespace {
900
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700901static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700902// Default input dispatching timeout if there is no focused application or paused window
903// from which to determine an appropriate dispatching timeout.
904static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
905 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
906 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800907
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800908class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800909public:
Garfield Tan15601662020-09-22 15:32:38 -0700910 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700911 : mConsumer(std::move(clientChannel)), mName(name) {}
chaviwd1c23182019-12-20 18:44:56 -0800912
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800913 std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false) {
914 auto [consumeSeq, event] = receiveEvent(timeout);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700915 if (!consumeSeq) {
916 return nullptr;
917 }
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000918 finishEvent(*consumeSeq, handled);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800919 return std::move(event);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700920 }
921
922 /**
923 * Receive an event without acknowledging it.
924 * Return the sequence number that could later be used to send finished signal.
925 */
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800926 std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent(
927 std::chrono::milliseconds timeout) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800928 uint32_t consumeSeq;
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800929 std::unique_ptr<InputEvent> event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800930
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800931 std::chrono::time_point start = std::chrono::steady_clock::now();
932 status_t status = WOULD_BLOCK;
933 while (status == WOULD_BLOCK) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800934 InputEvent* rawEventPtr = nullptr;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700935 status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800936 &rawEventPtr);
937 event = std::unique_ptr<InputEvent>(rawEventPtr);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800938 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700939 if (elapsed > timeout) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800940 break;
941 }
942 }
943
944 if (status == WOULD_BLOCK) {
945 // Just means there's no event available.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800946 return std::make_pair(std::nullopt, nullptr);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800947 }
948
949 if (status != OK) {
950 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800951 return std::make_pair(std::nullopt, nullptr);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800952 }
953 if (event == nullptr) {
954 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800955 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800956 return std::make_pair(consumeSeq, std::move(event));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700957 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800958
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700959 /**
960 * To be used together with "receiveEvent" to complete the consumption of an event.
961 */
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000962 void finishEvent(uint32_t consumeSeq, bool handled = true) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700963 const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700964 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800965 }
966
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000967 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700968 const status_t status = mConsumer.sendTimeline(inputEventId, timeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000969 ASSERT_EQ(OK, status);
970 }
971
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700972 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000973 std::optional<int32_t> expectedDisplayId,
974 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -0800975 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800976
977 ASSERT_NE(nullptr, event) << mName.c_str()
978 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800979 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700980 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
981 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800982
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000983 if (expectedDisplayId.has_value()) {
984 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
985 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800986
Tiger Huang8664f8c2018-10-11 19:14:35 +0800987 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700988 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800989 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700990 ASSERT_THAT(keyEvent, WithKeyAction(expectedAction));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000991 if (expectedFlags.has_value()) {
992 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
993 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800994 break;
995 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700996 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800997 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700998 ASSERT_THAT(motionEvent, WithMotionAction(expectedAction));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000999 if (expectedFlags.has_value()) {
1000 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
1001 }
Tiger Huang8664f8c2018-10-11 19:14:35 +08001002 break;
1003 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001004 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001005 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
1006 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001007 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -08001008 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
1009 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001010 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001011 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
1012 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001013 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +08001014 FAIL() << "Use 'consumeDragEvent' for DRAG events";
1015 }
Tiger Huang8664f8c2018-10-11 19:14:35 +08001016 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001017 }
1018
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001019 std::unique_ptr<MotionEvent> consumeMotion() {
1020 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001021
1022 if (event == nullptr) {
1023 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
1024 return nullptr;
1025 }
1026
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001027 if (event->getType() != InputEventType::MOTION) {
1028 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001029 return nullptr;
1030 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001031 return std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release()));
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001032 }
1033
1034 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001035 std::unique_ptr<MotionEvent> motionEvent = consumeMotion();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001036 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1037 ASSERT_THAT(*motionEvent, matcher);
1038 }
1039
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001040 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001041 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001042 ASSERT_NE(nullptr, event) << mName.c_str()
1043 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001044 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1045 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001046
1047 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1048 << mName.c_str() << ": event displayId should always be NONE.";
1049
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001050 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1051 EXPECT_EQ(hasFocus, focusEvent.getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001052 }
1053
Prabir Pradhan99987712020-11-10 18:43:05 -08001054 void consumeCaptureEvent(bool hasCapture) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001055 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Prabir Pradhan99987712020-11-10 18:43:05 -08001056 ASSERT_NE(nullptr, event) << mName.c_str()
1057 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001058 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1059 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001060
1061 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1062 << mName.c_str() << ": event displayId should always be NONE.";
1063
1064 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1065 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1066 }
1067
arthurhungb89ccb02020-12-30 16:19:01 +08001068 void consumeDragEvent(bool isExiting, float x, float y) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001069 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
arthurhungb89ccb02020-12-30 16:19:01 +08001070 ASSERT_NE(nullptr, event) << mName.c_str()
1071 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001072 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001073
1074 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1075 << mName.c_str() << ": event displayId should always be NONE.";
1076
1077 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1078 EXPECT_EQ(isExiting, dragEvent.isExiting());
1079 EXPECT_EQ(x, dragEvent.getX());
1080 EXPECT_EQ(y, dragEvent.getY());
1081 }
1082
Antonio Kantekf16f2832021-09-28 04:39:20 +00001083 void consumeTouchModeEvent(bool inTouchMode) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001084 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Antonio Kantekf16f2832021-09-28 04:39:20 +00001085 ASSERT_NE(nullptr, event) << mName.c_str()
1086 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001087 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1088 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001089
1090 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1091 << mName.c_str() << ": event displayId should always be NONE.";
1092 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1093 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1094 }
1095
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001096 void assertNoEvents(std::chrono::milliseconds timeout) {
1097 std::unique_ptr<InputEvent> event = consume(timeout);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001098 if (event == nullptr) {
1099 return;
1100 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001101 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001102 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001103 ADD_FAILURE() << "Received key event " << keyEvent;
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001104 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001105 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001106 ADD_FAILURE() << "Received motion event " << motionEvent;
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001107 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001108 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1109 ADD_FAILURE() << "Received focus event, hasFocus = "
1110 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001111 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001112 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1113 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1114 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001115 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001116 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1117 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1118 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001119 }
1120 FAIL() << mName.c_str()
1121 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001122 }
1123
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001124 sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); }
chaviwd1c23182019-12-20 18:44:56 -08001125
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08001126 int getChannelFd() { return mConsumer.getChannel()->getFd(); }
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001127
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001128private:
1129 InputConsumer mConsumer;
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001130 DynamicInputEventFactory mEventFactory;
chaviwd1c23182019-12-20 18:44:56 -08001131
1132 std::string mName;
1133};
1134
chaviw3277faf2021-05-19 16:45:23 -05001135class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001136public:
1137 static const int32_t WIDTH = 600;
1138 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001139
Chris Yea209fde2020-07-22 13:54:51 -07001140 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001141 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00001142 int32_t displayId, bool createInputChannel = true)
chaviwd1c23182019-12-20 18:44:56 -08001143 : mName(name) {
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00001144 sp<IBinder> token;
1145 if (createInputChannel) {
Garfield Tan15601662020-09-22 15:32:38 -07001146 base::Result<std::unique_ptr<InputChannel>> channel =
1147 dispatcher->createInputChannel(name);
1148 token = (*channel)->getConnectionToken();
1149 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001150 }
1151
1152 inputApplicationHandle->updateInfo();
1153 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1154
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00001155 mInfo.token = token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001156 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001157 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001158 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001159 mInfo.alpha = 1.0;
Chavi Weingarten7f019192023-08-08 20:39:01 +00001160 mInfo.frame = Rect(0, 0, WIDTH, HEIGHT);
chaviw1ff3d1e2020-07-01 15:53:47 -07001161 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001162 mInfo.globalScaleFactor = 1.0;
1163 mInfo.touchableRegion.clear();
1164 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001165 mInfo.ownerPid = WINDOW_PID;
1166 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001167 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001168 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001169 }
1170
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001171 sp<FakeWindowHandle> clone(int32_t displayId) {
1172 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)");
1173 handle->mInfo = mInfo;
1174 handle->mInfo.displayId = displayId;
1175 handle->mInfo.id = sId++;
1176 handle->mInputReceiver = mInputReceiver;
1177 return handle;
1178 }
1179
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001180 void setTouchable(bool touchable) {
1181 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1182 }
chaviwd1c23182019-12-20 18:44:56 -08001183
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001184 void setFocusable(bool focusable) {
1185 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1186 }
1187
1188 void setVisible(bool visible) {
1189 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1190 }
Vishnu Nair958da932020-08-21 17:12:37 -07001191
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001192 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001193 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001194 }
1195
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001196 void setPaused(bool paused) {
1197 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1198 }
1199
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001200 void setPreventSplitting(bool preventSplitting) {
1201 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001202 }
1203
1204 void setSlippery(bool slippery) {
1205 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1206 }
1207
1208 void setWatchOutsideTouch(bool watchOutside) {
1209 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1210 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001211
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001212 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1213
1214 void setInterceptsStylus(bool interceptsStylus) {
1215 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1216 }
1217
1218 void setDropInput(bool dropInput) {
1219 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1220 }
1221
1222 void setDropInputIfObscured(bool dropInputIfObscured) {
1223 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1224 }
1225
1226 void setNoInputChannel(bool noInputChannel) {
1227 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1228 }
1229
Josep del Riob3981622023-04-18 15:49:45 +00001230 void setDisableUserActivity(bool disableUserActivity) {
1231 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1232 }
1233
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07001234 void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) {
1235 mInfo.setInputConfig(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH,
1236 shouldGlobalStylusBlockTouch);
1237 }
1238
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001239 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1240
chaviw3277faf2021-05-19 16:45:23 -05001241 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001242
Bernardo Rufino7393d172021-02-26 13:56:11 +00001243 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1244
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001245 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
Chavi Weingarten7f019192023-08-08 20:39:01 +00001246 mInfo.frame = frame;
chaviwd1c23182019-12-20 18:44:56 -08001247 mInfo.touchableRegion.clear();
1248 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001249
1250 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1251 ui::Transform translate;
1252 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1253 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001254 }
1255
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001256 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1257
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001258 void setIsWallpaper(bool isWallpaper) {
1259 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1260 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001261
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001262 void setDupTouchToWallpaper(bool hasWallpaper) {
1263 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1264 }
chaviwd1c23182019-12-20 18:44:56 -08001265
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001266 void setTrustedOverlay(bool trustedOverlay) {
1267 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1268 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001269
chaviw9eaa22c2020-07-01 16:21:27 -07001270 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1271 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1272 }
1273
1274 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001275
yunho.shinf4a80b82020-11-16 21:13:57 +09001276 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1277
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001278 std::unique_ptr<KeyEvent> consumeKey(bool handled = true) {
1279 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled);
1280 if (event == nullptr) {
1281 ADD_FAILURE() << "No event";
1282 return nullptr;
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001283 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001284 if (event->getType() != InputEventType::KEY) {
1285 ADD_FAILURE() << "Instead of key event, got " << event;
1286 return nullptr;
1287 }
1288 return std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event.release()));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001289 }
1290
1291 void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001292 std::unique_ptr<KeyEvent> keyEvent = consumeKey();
1293 ASSERT_NE(nullptr, keyEvent);
1294 ASSERT_THAT(*keyEvent, matcher);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001295 }
1296
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001297 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001298 consumeKeyEvent(AllOf(WithKeyAction(ACTION_DOWN), WithDisplayId(expectedDisplayId),
1299 WithFlags(expectedFlags)));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001300 }
1301
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001302 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001303 consumeKeyEvent(AllOf(WithKeyAction(ACTION_UP), WithDisplayId(expectedDisplayId),
1304 WithFlags(expectedFlags)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001305 }
1306
Svet Ganov5d3bc372020-01-26 23:11:07 -08001307 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001308 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001309 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1310 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001311 }
1312
1313 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001314 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001315 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1316 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001317 }
1318
1319 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001320 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001321 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1322 }
1323
1324 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1325 std::optional<int32_t> expectedFlags = std::nullopt) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001326 consumeMotionEvent(
1327 AllOf(WithMotionAction(ACTION_DOWN),
1328 testing::Conditional(expectedDisplayId.has_value(),
1329 WithDisplayId(*expectedDisplayId), testing::_),
1330 testing::Conditional(expectedFlags.has_value(), WithFlags(*expectedFlags),
1331 testing::_)));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001332 }
1333
Svet Ganov5d3bc372020-01-26 23:11:07 -08001334 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001335 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1336 int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001337 const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001338 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001339 consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId),
1340 WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001341 }
1342
1343 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001344 int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001345 const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001346 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001347 consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId),
1348 WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001349 }
1350
1351 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001352 int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001353 consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDisplayId(expectedDisplayId),
1354 WithFlags(expectedFlags)));
Michael Wright3a240c42019-12-10 20:53:41 +00001355 }
1356
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001357 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1358 int32_t expectedFlags = 0) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001359 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE),
1360 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001361 }
1362
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001363 void consumeMotionOutsideWithZeroedCoords() {
1364 consumeMotionEvent(
1365 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), WithRawCoords(0, 0)));
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001366 }
1367
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001368 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1369 ASSERT_NE(mInputReceiver, nullptr)
1370 << "Cannot consume events from a window with no receiver";
1371 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1372 }
1373
Prabir Pradhan99987712020-11-10 18:43:05 -08001374 void consumeCaptureEvent(bool hasCapture) {
1375 ASSERT_NE(mInputReceiver, nullptr)
1376 << "Cannot consume events from a window with no receiver";
1377 mInputReceiver->consumeCaptureEvent(hasCapture);
1378 }
1379
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001380 std::unique_ptr<MotionEvent> consumeMotionEvent(
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001381 const ::testing::Matcher<MotionEvent>& matcher = testing::_) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001382 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
1383 if (event == nullptr) {
1384 ADD_FAILURE() << "No event";
1385 return nullptr;
Prabir Pradhan5893d362023-11-17 04:30:40 +00001386 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001387 if (event->getType() != InputEventType::MOTION) {
1388 ADD_FAILURE() << "Instead of motion event, got " << *event;
1389 return nullptr;
1390 }
1391 std::unique_ptr<MotionEvent> motionEvent =
1392 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release()));
1393 EXPECT_THAT(*motionEvent, matcher);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001394 return motionEvent;
chaviwd1c23182019-12-20 18:44:56 -08001395 }
1396
arthurhungb89ccb02020-12-30 16:19:01 +08001397 void consumeDragEvent(bool isExiting, float x, float y) {
1398 mInputReceiver->consumeDragEvent(isExiting, x, y);
1399 }
1400
Antonio Kantekf16f2832021-09-28 04:39:20 +00001401 void consumeTouchModeEvent(bool inTouchMode) {
1402 ASSERT_NE(mInputReceiver, nullptr)
1403 << "Cannot consume events from a window with no receiver";
1404 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1405 }
1406
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001407 std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() {
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001408 return receive();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001409 }
1410
1411 void finishEvent(uint32_t sequenceNum) {
1412 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1413 mInputReceiver->finishEvent(sequenceNum);
1414 }
1415
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001416 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1417 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1418 mInputReceiver->sendTimeline(inputEventId, timeline);
1419 }
1420
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001421 void assertNoEvents(std::chrono::milliseconds timeout = CONSUME_TIMEOUT_NO_EVENT_EXPECTED) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001422 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001423 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001424 return; // Can't receive events if the window does not have input channel
1425 }
1426 ASSERT_NE(nullptr, mInputReceiver)
1427 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001428 mInputReceiver->assertNoEvents(timeout);
Arthur Hungb92218b2018-08-14 12:00:21 +08001429 }
1430
chaviwaf87b3e2019-10-01 16:59:28 -07001431 sp<IBinder> getToken() { return mInfo.token; }
1432
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001433 const std::string& getName() { return mName; }
1434
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00001435 void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001436 mInfo.ownerPid = ownerPid;
1437 mInfo.ownerUid = ownerUid;
1438 }
1439
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00001440 gui::Pid getPid() const { return mInfo.ownerPid; }
Prabir Pradhanedd96402022-02-15 01:46:16 -08001441
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001442 void destroyReceiver() { mInputReceiver = nullptr; }
1443
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001444 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1445
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001446 // FakeWindowHandle uses this consume method to ensure received events are added to the trace.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001447 std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001448 if (mInputReceiver == nullptr) {
1449 LOG(FATAL) << "Cannot consume event from a window with no input event receiver";
1450 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001451 std::unique_ptr<InputEvent> event = mInputReceiver->consume(timeout, handled);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001452 if (event == nullptr) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001453 ADD_FAILURE() << "Consume failed: no event";
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001454 }
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001455 expectReceivedEventTraced(event);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001456 return event;
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001457 }
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001458
1459private:
1460 FakeWindowHandle(std::string name) : mName(name){};
1461 const std::string mName;
1462 std::shared_ptr<FakeInputReceiver> mInputReceiver;
1463 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
1464 friend class sp<FakeWindowHandle>;
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001465
1466 // FakeWindowHandle uses this receive method to ensure received events are added to the trace.
1467 std::pair<std::optional<uint32_t /*seq*/>, std::unique_ptr<InputEvent>> receive() {
1468 if (mInputReceiver == nullptr) {
1469 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1470 return std::make_pair(std::nullopt, nullptr);
1471 }
1472 auto out = mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
1473 const auto& [_, event] = out;
1474 expectReceivedEventTraced(event);
1475 return std::move(out);
1476 }
1477
1478 void expectReceivedEventTraced(const std::unique_ptr<InputEvent>& event) {
1479 if (!event) {
1480 return;
1481 }
1482
1483 switch (event->getType()) {
1484 case InputEventType::KEY: {
Prabir Pradhan4497c862023-12-15 07:13:30 +00001485 gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), mInfo.id);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001486 break;
1487 }
1488 case InputEventType::MOTION: {
Prabir Pradhan4497c862023-12-15 07:13:30 +00001489 gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event),
1490 mInfo.id);
Prabir Pradhandc3a2ad2024-02-05 19:03:51 +00001491 break;
1492 }
1493 default:
1494 break;
1495 }
1496 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001497};
1498
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001499std::atomic<int32_t> FakeWindowHandle::sId{1};
1500
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001501class FakeMonitorReceiver {
1502public:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001503 FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId)
1504 : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {}
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001505
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001506 sp<IBinder> getToken() { return mInputReceiver.getToken(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001507
1508 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001509 mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1510 expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001511 }
1512
1513 std::optional<int32_t> receiveEvent() {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001514 const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
1515 return sequenceNum;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001516 }
1517
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001518 void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001519
1520 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001521 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
1522 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001523 }
1524
1525 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001526 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
1527 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001528 }
1529
1530 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001531 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
1532 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001533 }
1534
1535 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001536 mInputReceiver.consumeMotionEvent(
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001537 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
1538 WithDisplayId(expectedDisplayId),
1539 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
1540 }
1541
1542 void consumeMotionPointerDown(int32_t pointerIdx) {
1543 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1544 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001545 mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
1546 /*expectedFlags=*/0);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001547 }
1548
1549 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001550 mInputReceiver.consumeMotionEvent(matcher);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001551 }
1552
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001553 std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001554
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001555 void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001556
1557private:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001558 FakeInputReceiver mInputReceiver;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001559};
1560
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001561static InputEventInjectionResult injectKey(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001562 InputDispatcher& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001563 int32_t displayId = ADISPLAY_ID_NONE,
1564 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001565 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001566 bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {},
Prabir Pradhan5735a322022-04-11 17:23:34 +00001567 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001568 KeyEvent event;
1569 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1570
1571 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001572 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001573 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1574 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001575
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001576 if (!allowKeyRepeat) {
1577 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1578 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001579 // Inject event until dispatch out.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001580 return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001581}
1582
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001583static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) {
1584 InputEventInjectionResult result =
1585 injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE,
1586 InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
1587 if (result != InputEventInjectionResult::TIMED_OUT) {
1588 FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result);
1589 }
1590}
1591
1592static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001593 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001594 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001595}
1596
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001597// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1598// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1599// has to be woken up to process the repeating key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001600static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher,
1601 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001602 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001603 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001604 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001605}
1606
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001607static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001608 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001609 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001610}
1611
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001612static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001613 InputDispatcher& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001614 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001615 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001616 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001617 return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1618 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001619}
1620
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001621static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001622 InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId,
1623 const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001624 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001625 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1626 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001627 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001628 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001629 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001630 MotionEventBuilder motionBuilder =
1631 MotionEventBuilder(action, source)
1632 .displayId(displayId)
1633 .eventTime(eventTime)
1634 .rawXCursorPosition(cursorPosition.x)
1635 .rawYCursorPosition(cursorPosition.y)
1636 .pointer(
1637 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1638 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1639 motionBuilder.downTime(eventTime);
1640 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001641
1642 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001643 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1644 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001645}
1646
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001647static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source,
1648 int32_t displayId,
1649 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001650 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001651}
1652
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001653static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source,
1654 int32_t displayId,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001655 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001656 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001657}
1658
Jackal Guof9696682018-10-05 12:23:23 +08001659static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1660 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1661 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001662 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1663 AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action,
1664 /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001665
1666 return args;
1667}
1668
Josep del Riob3981622023-04-18 15:49:45 +00001669static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1670 int32_t displayId = ADISPLAY_ID_NONE) {
1671 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1672 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001673 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1674 AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C,
1675 AMETA_META_ON, currentTime);
Josep del Riob3981622023-04-18 15:49:45 +00001676
1677 return args;
1678}
1679
1680static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1681 int32_t displayId = ADISPLAY_ID_NONE) {
1682 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1683 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001684 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1685 AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST,
1686 KEY_ASSISTANT, AMETA_NONE, currentTime);
Josep del Riob3981622023-04-18 15:49:45 +00001687
1688 return args;
1689}
1690
Prabir Pradhan678438e2023-04-13 19:32:51 +00001691[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1692 int32_t displayId,
1693 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001694 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001695 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1696 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1697 }
1698
chaviwd1c23182019-12-20 18:44:56 -08001699 PointerProperties pointerProperties[pointerCount];
1700 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001701
chaviwd1c23182019-12-20 18:44:56 -08001702 for (size_t i = 0; i < pointerCount; i++) {
1703 pointerProperties[i].clear();
1704 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001705 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001706
chaviwd1c23182019-12-20 18:44:56 -08001707 pointerCoords[i].clear();
1708 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1709 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1710 }
Jackal Guof9696682018-10-05 12:23:23 +08001711
1712 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1713 // Define a valid motion event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001714 NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source,
1715 displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0,
1716 /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001717 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001718 pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001719 AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001720 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{});
Jackal Guof9696682018-10-05 12:23:23 +08001721
1722 return args;
1723}
1724
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001725static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1726 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1727}
1728
chaviwd1c23182019-12-20 18:44:56 -08001729static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1730 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1731}
1732
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001733static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1734 const PointerCaptureRequest& request) {
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001735 return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC),
1736 request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001737}
1738
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001739} // namespace
1740
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001741/**
1742 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1743 * broken channel.
1744 */
1745TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1746 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1747 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001748 sp<FakeWindowHandle>::make(application, mDispatcher,
1749 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001750
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001751 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001752
1753 // Window closes its channel, but the window remains.
1754 window->destroyReceiver();
1755 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1756}
1757
Arthur Hungb92218b2018-08-14 12:00:21 +08001758TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001759 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001760 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1761 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001762
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001763 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001765 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001767
1768 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001769 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001770}
1771
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -08001772using InputDispatcherDeathTest = InputDispatcherTest;
1773
1774/**
1775 * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher
1776 * should crash.
1777 */
1778TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) {
1779 testing::GTEST_FLAG(death_test_style) = "threadsafe";
1780 ScopedSilentDeath _silentDeath;
1781
1782 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1783 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1784 "Fake Window", ADISPLAY_ID_DEFAULT);
1785 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
1786 {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}),
1787 "Incorrect WindowInfosUpdate provided");
1788}
1789
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001790TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1791 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001792 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1793 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001794
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001795 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001796 // Inject a MotionEvent to an unknown display.
1797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001798 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001799 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1800
1801 // Window should receive motion event.
1802 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1803}
1804
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001805/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001806 * Calling onWindowInfosChanged once should not cause any issues.
1807 * This test serves as a sanity check for the next test, where onWindowInfosChanged is
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001808 * called twice.
1809 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001810TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001811 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001812 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1813 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001814 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001815
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001816 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001818 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001819 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001820 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001821
1822 // Window should receive motion event.
1823 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1824}
1825
1826/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001827 * Calling onWindowInfosChanged twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001828 */
1829TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001830 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001831 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1832 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001833 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001834
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001835 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1836 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001837 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001838 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001839 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001840 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001841
1842 // Window should receive motion event.
1843 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1844}
1845
Arthur Hungb92218b2018-08-14 12:00:21 +08001846// The foreground window should receive the first touch down event.
1847TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001848 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001849 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001850 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001851 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001852 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001853
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001854 mDispatcher->onWindowInfosChanged(
1855 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001856 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001857 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001858 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001859
1860 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001861 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001862 windowSecond->assertNoEvents();
1863}
1864
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001865/**
1866 * Two windows: A top window, and a wallpaper behind the window.
1867 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1868 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001869 * 1. foregroundWindow <-- dup touch to wallpaper
1870 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001871 */
1872TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1873 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1874 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001875 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001876 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001877 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001878 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001879 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001880
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001881 mDispatcher->onWindowInfosChanged(
1882 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001883 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001884 injectMotionEvent(*mDispatcher,
1885 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1886 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200))
1887 .build()))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001888 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1889
1890 // Both foreground window and its wallpaper should receive the touch down
1891 foregroundWindow->consumeMotionDown();
1892 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1893
1894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001895 injectMotionEvent(*mDispatcher,
1896 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1897 .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200))
1898 .build()))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001899 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1900
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001901 foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001902 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1903
1904 // Now the foreground window goes away, but the wallpaper stays
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001905 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001906 foregroundWindow->consumeMotionCancel();
1907 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1908 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1909}
1910
1911/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001912 * Two fingers down on the window, and lift off the first finger.
1913 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1914 * contains a single pointer.
1915 */
1916TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1917 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1918 sp<FakeWindowHandle> window =
1919 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1920
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001921 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001922 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001923 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1924 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1925 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001926 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001927 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1928 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1929 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1930 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001931 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001932 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1933 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1934 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1935 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001936 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1937 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1938 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1939
1940 // Remove the window. The gesture should be canceled
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001941 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001942 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1943 window->consumeMotionEvent(
1944 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1945}
1946
1947/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001948 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1949 * with the following differences:
1950 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1951 * clean up the connection.
1952 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1953 * Ensure that there's no crash in the dispatcher.
1954 */
1955TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1956 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1957 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001958 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001959 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001960 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001961 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001962 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001963
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001964 mDispatcher->onWindowInfosChanged(
1965 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001966 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001967 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001968 {100, 200}))
1969 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1970
1971 // Both foreground window and its wallpaper should receive the touch down
1972 foregroundWindow->consumeMotionDown();
1973 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1974
1975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001976 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001977 ADISPLAY_ID_DEFAULT, {110, 200}))
1978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1979
1980 foregroundWindow->consumeMotionMove();
1981 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1982
1983 // Wallpaper closes its channel, but the window remains.
1984 wallpaperWindow->destroyReceiver();
1985 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1986
1987 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1988 // is no longer valid.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001989 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001990 foregroundWindow->consumeMotionCancel();
1991}
1992
Arthur Hungc539dbb2022-12-08 07:45:36 +00001993class ShouldSplitTouchFixture : public InputDispatcherTest,
1994 public ::testing::WithParamInterface<bool> {};
1995INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1996 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001997/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001998 * A single window that receives touch (on top), and a wallpaper window underneath it.
1999 * The top window gets a multitouch gesture.
2000 * Ensure that wallpaper gets the same gesture.
2001 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002002TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002004 sp<FakeWindowHandle> foregroundWindow =
2005 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2006 foregroundWindow->setDupTouchToWallpaper(true);
2007 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002008
2009 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002010 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002011 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002012
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002013 mDispatcher->onWindowInfosChanged(
2014 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002015
2016 // Touch down on top window
2017 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002018 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002019 {100, 100}))
2020 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2021
2022 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002023 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002024 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2025
2026 // Second finger down on the top window
2027 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002028 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002029 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002030 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2031 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002032 .build();
2033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002034 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002035 InputEventInjectionSync::WAIT_FOR_RESULT))
2036 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2037
Harry Cutts33476232023-01-30 19:57:29 +00002038 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2039 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002040 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002041
2042 const MotionEvent secondFingerUpEvent =
2043 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2044 .displayId(ADISPLAY_ID_DEFAULT)
2045 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002046 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2047 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002048 .build();
2049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002050 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002051 InputEventInjectionSync::WAIT_FOR_RESULT))
2052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2053 foregroundWindow->consumeMotionPointerUp(0);
2054 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2055
2056 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002057 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002058 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2059 AINPUT_SOURCE_TOUCHSCREEN)
2060 .displayId(ADISPLAY_ID_DEFAULT)
2061 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts101ee9b2023-07-06 18:04:14 +00002062 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002063 .x(100)
2064 .y(100))
2065 .build(),
2066 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002067 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2068 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2069 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002070}
2071
2072/**
2073 * Two windows: a window on the left and window on the right.
2074 * A third window, wallpaper, is behind both windows, and spans both top windows.
2075 * The first touch down goes to the left window. A second pointer touches down on the right window.
2076 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2077 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2078 * ACTION_POINTER_DOWN(1).
2079 */
2080TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2081 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2082 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002083 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002084 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002085 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002086
2087 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002088 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002089 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002090 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002091
2092 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002093 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002094 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002095 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002096
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002097 mDispatcher->onWindowInfosChanged(
2098 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
2099 {},
2100 0,
2101 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002102
2103 // Touch down on left window
2104 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002105 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002106 {100, 100}))
2107 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2108
2109 // Both foreground window and its wallpaper should receive the touch down
2110 leftWindow->consumeMotionDown();
2111 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2112
2113 // Second finger down on the right window
2114 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002115 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002116 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002117 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2118 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002119 .build();
2120 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002121 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002122 InputEventInjectionSync::WAIT_FOR_RESULT))
2123 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2124
2125 leftWindow->consumeMotionMove();
2126 // Since the touch is split, right window gets ACTION_DOWN
2127 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002128 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002129 expectedWallpaperFlags);
2130
2131 // Now, leftWindow, which received the first finger, disappears.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002132 mDispatcher->onWindowInfosChanged(
2133 {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002134 leftWindow->consumeMotionCancel();
2135 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2136 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2137
2138 // The pointer that's still down on the right window moves, and goes to the right window only.
2139 // As far as the dispatcher's concerned though, both pointers are still present.
2140 const MotionEvent secondFingerMoveEvent =
2141 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2142 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002143 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2144 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002145 .build();
2146 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002147 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002148 InputEventInjectionSync::WAIT_FOR_RESULT));
2149 rightWindow->consumeMotionMove();
2150
2151 leftWindow->assertNoEvents();
2152 rightWindow->assertNoEvents();
2153 wallpaperWindow->assertNoEvents();
2154}
2155
Arthur Hungc539dbb2022-12-08 07:45:36 +00002156/**
2157 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2158 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2159 * The right window should receive ACTION_DOWN.
2160 */
2161TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002162 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002163 sp<FakeWindowHandle> leftWindow =
2164 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2165 leftWindow->setFrame(Rect(0, 0, 200, 200));
2166 leftWindow->setDupTouchToWallpaper(true);
2167 leftWindow->setSlippery(true);
2168
2169 sp<FakeWindowHandle> rightWindow =
2170 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2171 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002172
2173 sp<FakeWindowHandle> wallpaperWindow =
2174 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2175 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002176
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002177 mDispatcher->onWindowInfosChanged(
2178 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
2179 {},
2180 0,
2181 0});
Arthur Hung74c248d2022-11-23 07:09:59 +00002182
Arthur Hungc539dbb2022-12-08 07:45:36 +00002183 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002184 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002185 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002186 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002188
2189 // Both foreground window and its wallpaper should receive the touch down
2190 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002191 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2192
Arthur Hungc539dbb2022-12-08 07:45:36 +00002193 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002194 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002195 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002196 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002197 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2198
Arthur Hungc539dbb2022-12-08 07:45:36 +00002199 leftWindow->consumeMotionCancel();
2200 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2201 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002202}
2203
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002204/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002205 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2206 * interactive, it might stop sending this flag.
2207 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2208 * to have a consistent input stream.
2209 *
2210 * Test procedure:
2211 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2212 * DOWN (new gesture).
2213 *
2214 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2215 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2216 *
2217 * We technically just need a single window here, but we are using two windows (spy on top and a
2218 * regular window below) to emulate the actual situation where it happens on the device.
2219 */
2220TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2221 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2222 sp<FakeWindowHandle> spyWindow =
2223 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2224 spyWindow->setFrame(Rect(0, 0, 200, 200));
2225 spyWindow->setTrustedOverlay(true);
2226 spyWindow->setSpy(true);
2227
2228 sp<FakeWindowHandle> window =
2229 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2230 window->setFrame(Rect(0, 0, 200, 200));
2231
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002232 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002233 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002234
2235 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002236 mDispatcher->notifyMotion(
2237 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2238 .deviceId(touchDeviceId)
2239 .policyFlags(DEFAULT_POLICY_FLAGS)
2240 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2241 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002242
Prabir Pradhan678438e2023-04-13 19:32:51 +00002243 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2244 .deviceId(touchDeviceId)
2245 .policyFlags(DEFAULT_POLICY_FLAGS)
2246 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2247 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2248 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002249 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2250 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2251 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2252 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2253
2254 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002255 mDispatcher->notifyMotion(
2256 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2257 .deviceId(touchDeviceId)
2258 .policyFlags(0)
2259 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2260 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2261 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002262 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2263 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2264
2265 // We don't need to reset the device to reproduce the issue, but the reset event typically
2266 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002267 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002268
2269 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002270 mDispatcher->notifyMotion(
2271 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2272 .deviceId(touchDeviceId)
2273 .policyFlags(DEFAULT_POLICY_FLAGS)
2274 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2275 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002276 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2277 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2278
2279 // No more events
2280 spyWindow->assertNoEvents();
2281 window->assertNoEvents();
2282}
2283
2284/**
Linnan Li907ae732023-09-05 17:14:21 +08002285 * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers.
2286 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2287 * interactive, it might stop sending this flag.
2288 * We've already ensured the consistency of the touch event in this case, and we should also ensure
2289 * the consistency of the hover event in this case.
2290 *
2291 * Test procedure:
2292 * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT
2293 * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT
2294 *
2295 * We expect to receive two full streams of hover events.
2296 */
2297TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) {
2298 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2299
2300 sp<FakeWindowHandle> window =
2301 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2302 window->setFrame(Rect(0, 0, 300, 300));
2303
2304 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2305
2306 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2307 .policyFlags(DEFAULT_POLICY_FLAGS)
2308 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2309 .build());
2310 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2311
2312 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2313 .policyFlags(DEFAULT_POLICY_FLAGS)
2314 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2315 .build());
2316 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2317
2318 // Send hover exit without the default policy flags.
2319 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2320 .policyFlags(0)
2321 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2322 .build());
2323
2324 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2325
2326 // Send a simple hover event stream, ensure dispatcher not crashed and window can receive
2327 // right event.
2328 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2329 .policyFlags(DEFAULT_POLICY_FLAGS)
2330 .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201))
2331 .build());
2332 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2333
2334 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2335 .policyFlags(DEFAULT_POLICY_FLAGS)
2336 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
2337 .build());
2338 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2339
2340 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2341 .policyFlags(DEFAULT_POLICY_FLAGS)
2342 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
2343 .build());
2344 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2345}
2346
2347/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002348 * Two windows: a window on the left and a window on the right.
2349 * Mouse is hovered from the right window into the left window.
2350 * Next, we tap on the left window, where the cursor was last seen.
2351 * The second tap is done onto the right window.
2352 * The mouse and tap are from two different devices.
2353 * We technically don't need to set the downtime / eventtime for these events, but setting these
2354 * explicitly helps during debugging.
2355 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2356 * In the buggy implementation, a tap on the right window would cause a crash.
2357 */
2358TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2359 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2360 sp<FakeWindowHandle> leftWindow =
2361 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2362 leftWindow->setFrame(Rect(0, 0, 200, 200));
2363
2364 sp<FakeWindowHandle> rightWindow =
2365 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2366 rightWindow->setFrame(Rect(200, 0, 400, 200));
2367
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002368 mDispatcher->onWindowInfosChanged(
2369 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002370 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2371 // stale.
2372 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2373 const int32_t mouseDeviceId = 6;
2374 const int32_t touchDeviceId = 4;
2375 // Move the cursor from right
2376 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002377 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002378 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2379 AINPUT_SOURCE_MOUSE)
2380 .deviceId(mouseDeviceId)
2381 .downTime(baseTime + 10)
2382 .eventTime(baseTime + 20)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002383 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002384 .build()));
2385 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2386
2387 // .. to the left window
2388 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002389 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002390 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2391 AINPUT_SOURCE_MOUSE)
2392 .deviceId(mouseDeviceId)
2393 .downTime(baseTime + 10)
2394 .eventTime(baseTime + 30)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002395 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002396 .build()));
2397 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2398 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2399 // Now tap the left window
2400 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002401 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002402 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2403 AINPUT_SOURCE_TOUCHSCREEN)
2404 .deviceId(touchDeviceId)
2405 .downTime(baseTime + 40)
2406 .eventTime(baseTime + 40)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002407 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002408 .build()));
2409 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2410 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2411
2412 // release tap
2413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002414 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002415 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2416 AINPUT_SOURCE_TOUCHSCREEN)
2417 .deviceId(touchDeviceId)
2418 .downTime(baseTime + 40)
2419 .eventTime(baseTime + 50)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002420 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002421 .build()));
2422 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2423
2424 // Tap the window on the right
2425 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002426 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002427 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2428 AINPUT_SOURCE_TOUCHSCREEN)
2429 .deviceId(touchDeviceId)
2430 .downTime(baseTime + 60)
2431 .eventTime(baseTime + 60)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002432 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002433 .build()));
2434 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2435
2436 // release tap
2437 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002438 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002439 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2440 AINPUT_SOURCE_TOUCHSCREEN)
2441 .deviceId(touchDeviceId)
2442 .downTime(baseTime + 60)
2443 .eventTime(baseTime + 70)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002444 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002445 .build()));
2446 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2447
2448 // No more events
2449 leftWindow->assertNoEvents();
2450 rightWindow->assertNoEvents();
2451}
2452
2453/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002454 * Start hovering in a window. While this hover is still active, make another window appear on top.
2455 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2456 * While the top window is present, the hovering is stopped.
2457 * Later, hovering gets resumed again.
2458 * Ensure that new hover gesture is handled correctly.
2459 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2460 * to the window that's currently being hovered over.
2461 */
2462TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2463 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2464 sp<FakeWindowHandle> window =
2465 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2466 window->setFrame(Rect(0, 0, 200, 200));
2467
2468 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002469 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002470
2471 // Start hovering in the window
2472 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2473 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2474 .build());
2475 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2476
2477 // Now, an obscuring window appears!
2478 sp<FakeWindowHandle> obscuringWindow =
2479 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2480 ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00002481 /*createInputChannel=*/false);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002482 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2483 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2484 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2485 obscuringWindow->setNoInputChannel(true);
2486 obscuringWindow->setFocusable(false);
2487 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002488 mDispatcher->onWindowInfosChanged(
2489 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002490
2491 // While this new obscuring window is present, the hovering is stopped
2492 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2493 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2494 .build());
2495 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2496
2497 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002498 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002499
2500 // And a new hover gesture starts.
2501 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2502 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2503 .build());
2504 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2505}
2506
2507/**
2508 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2509 * the obscuring window.
2510 */
2511TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2512 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2513 sp<FakeWindowHandle> window =
2514 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2515 window->setFrame(Rect(0, 0, 200, 200));
2516
2517 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002518 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002519
2520 // Start hovering in the window
2521 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2522 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2523 .build());
2524 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2525
2526 // Now, an obscuring window appears!
2527 sp<FakeWindowHandle> obscuringWindow =
2528 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2529 ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00002530 /*createInputChannel=*/false);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002531 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2532 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2533 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2534 obscuringWindow->setNoInputChannel(true);
2535 obscuringWindow->setFocusable(false);
2536 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002537 mDispatcher->onWindowInfosChanged(
2538 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002539
2540 // While this new obscuring window is present, the hovering continues. The event can't go to the
2541 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2542 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2543 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2544 .build());
2545 obscuringWindow->assertNoEvents();
2546 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2547
2548 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002549 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002550
2551 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2552 // so it should generate a HOVER_ENTER
2553 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2554 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2555 .build());
2556 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2557
2558 // Now the MOVE should be getting dispatched normally
2559 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2560 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2561 .build());
2562 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2563}
2564
2565/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002566 * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll
2567 * events are delivered to the window.
2568 */
2569TEST_F(InputDispatcherTest, HoverMoveAndScroll) {
2570 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2571 sp<FakeWindowHandle> window =
2572 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2573 window->setFrame(Rect(0, 0, 200, 200));
2574 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2575
2576 // Start hovering in the window
2577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2578 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
2579 .build());
2580 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2581
2582 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
2583 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2584 .build());
2585 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2586
2587 // Scroll with the mouse
2588 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE)
2589 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2590 .build());
2591 window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL));
2592}
2593
2594using InputDispatcherMultiDeviceTest = InputDispatcherTest;
2595
2596/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002597 * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that
2598 * touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002599 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002600TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002601 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2602 sp<FakeWindowHandle> window =
2603 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2604 window->setFrame(Rect(0, 0, 200, 200));
2605
2606 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2607
2608 constexpr int32_t touchDeviceId = 4;
2609 constexpr int32_t stylusDeviceId = 2;
2610
2611 // Stylus down
2612 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2613 .deviceId(stylusDeviceId)
2614 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2615 .build());
2616 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2617
2618 // Touch down
2619 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2620 .deviceId(touchDeviceId)
2621 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2622 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002623
2624 // Touch move
2625 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2626 .deviceId(touchDeviceId)
2627 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2628 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002629 // Touch is ignored because stylus is already down
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002630
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002631 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002632 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2633 .deviceId(stylusDeviceId)
2634 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2635 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002636 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2637 WithCoords(101, 111)));
2638
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002639 window->assertNoEvents();
2640}
2641
2642/**
2643 * One window and one spy window. Stylus down on the window. Next, touch from another device goes
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002644 * down. Ensure that touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002645 * Similar test as above, but with added SPY window.
2646 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002647TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002648 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2649 sp<FakeWindowHandle> window =
2650 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2651 sp<FakeWindowHandle> spyWindow =
2652 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2653 spyWindow->setFrame(Rect(0, 0, 200, 200));
2654 spyWindow->setTrustedOverlay(true);
2655 spyWindow->setSpy(true);
2656 window->setFrame(Rect(0, 0, 200, 200));
2657
2658 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
2659
2660 constexpr int32_t touchDeviceId = 4;
2661 constexpr int32_t stylusDeviceId = 2;
2662
2663 // Stylus down
2664 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2665 .deviceId(stylusDeviceId)
2666 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2667 .build());
2668 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2669 spyWindow->consumeMotionEvent(
2670 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2671
2672 // Touch down
2673 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2674 .deviceId(touchDeviceId)
2675 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2676 .build());
2677
2678 // Touch move
2679 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2680 .deviceId(touchDeviceId)
2681 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2682 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002683
2684 // Touch is ignored because stylus is already down
2685
2686 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002687 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2688 .deviceId(stylusDeviceId)
2689 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2690 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002691 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2692 WithCoords(101, 111)));
2693 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2694 WithCoords(101, 111)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002695
2696 window->assertNoEvents();
2697 spyWindow->assertNoEvents();
2698}
2699
2700/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002701 * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002702 * touch is dropped, because stylus hover takes precedence.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002703 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002704TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002705 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2706 sp<FakeWindowHandle> window =
2707 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2708 window->setFrame(Rect(0, 0, 200, 200));
2709
2710 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2711
2712 constexpr int32_t touchDeviceId = 4;
2713 constexpr int32_t stylusDeviceId = 2;
2714
2715 // Stylus down on the window
2716 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2717 .deviceId(stylusDeviceId)
2718 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2719 .build());
2720 window->consumeMotionEvent(
2721 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2722
2723 // Touch down on window
2724 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2725 .deviceId(touchDeviceId)
2726 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2727 .build());
2728 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2729 .deviceId(touchDeviceId)
2730 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2731 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002732
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002733 // Touch is ignored because stylus is hovering
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002734
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002735 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002736 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2737 .deviceId(stylusDeviceId)
2738 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2739 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002740 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2741 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002742
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002743 // and subsequent touches continue to be ignored
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002744 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2745 .deviceId(touchDeviceId)
2746 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2747 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002748 window->assertNoEvents();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002749}
2750
2751/**
2752 * One window. Touch down on the window. Then, stylus hover on the window from another device.
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002753 * Ensure that touch is canceled, because stylus hover should take precedence.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002754 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002755TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002756 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2757 sp<FakeWindowHandle> window =
2758 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2759 window->setFrame(Rect(0, 0, 200, 200));
2760
2761 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2762
2763 constexpr int32_t touchDeviceId = 4;
2764 constexpr int32_t stylusDeviceId = 2;
2765
2766 // Touch down on window
2767 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2768 .deviceId(touchDeviceId)
2769 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2770 .build());
2771 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2772 .deviceId(touchDeviceId)
2773 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2774 .build());
2775 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2776 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2777
2778 // Stylus hover on the window
2779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2780 .deviceId(stylusDeviceId)
2781 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2782 .build());
2783 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2784 .deviceId(stylusDeviceId)
2785 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2786 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002787 // Stylus hover movement causes touch to be canceled
2788 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
2789 WithCoords(141, 146)));
2790 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
2791 WithDeviceId(stylusDeviceId), WithCoords(100, 110)));
2792 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2793 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002794
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002795 // Subsequent touch movements are ignored
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002796 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2797 .deviceId(touchDeviceId)
2798 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2799 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002800
2801 window->assertNoEvents();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002802}
2803
2804/**
2805 * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that
2806 * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should
2807 * become active.
2808 */
2809TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) {
2810 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2811 sp<FakeWindowHandle> window =
2812 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2813 window->setFrame(Rect(0, 0, 200, 200));
2814
2815 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2816
2817 constexpr int32_t stylusDeviceId1 = 3;
2818 constexpr int32_t stylusDeviceId2 = 5;
2819
2820 // Touch down on window
2821 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2822 .deviceId(stylusDeviceId1)
2823 .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100))
2824 .build());
2825 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2826 .deviceId(stylusDeviceId1)
2827 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2828 .build());
2829 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1)));
2830 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1)));
2831
2832 // Second stylus down
2833 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2834 .deviceId(stylusDeviceId2)
2835 .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10))
2836 .build());
2837 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2838 .deviceId(stylusDeviceId2)
2839 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11))
2840 .build());
2841
2842 // First stylus is canceled, second one takes over.
2843 window->consumeMotionEvent(
2844 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1)));
2845 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2)));
2846 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2)));
2847
2848 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2849 .deviceId(stylusDeviceId1)
2850 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2851 .build());
2852 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002853 window->assertNoEvents();
2854}
2855
2856/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002857 * One window. Touch down on the window. Then, stylus down on the window from another device.
2858 * Ensure that is canceled, because stylus down should be preferred over touch.
2859 */
2860TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) {
2861 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2862 sp<FakeWindowHandle> window =
2863 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2864 window->setFrame(Rect(0, 0, 200, 200));
2865
2866 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2867
2868 constexpr int32_t touchDeviceId = 4;
2869 constexpr int32_t stylusDeviceId = 2;
2870
2871 // Touch down on window
2872 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2873 .deviceId(touchDeviceId)
2874 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2875 .build());
2876 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2877 .deviceId(touchDeviceId)
2878 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2879 .build());
2880 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2881 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2882
2883 // Stylus down on the window
2884 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2885 .deviceId(stylusDeviceId)
2886 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2887 .build());
2888 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2889 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2890
2891 // Subsequent stylus movements are delivered correctly
2892 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2893 .deviceId(stylusDeviceId)
2894 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2895 .build());
2896 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2897 WithCoords(101, 111)));
2898}
2899
2900/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002901 * Two windows: a window on the left and a window on the right.
2902 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2903 * down. Then, on the left window, also place second touch pointer down.
2904 * This test tries to reproduce a crash.
2905 * In the buggy implementation, second pointer down on the left window would cause a crash.
2906 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002907TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002908 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2909 sp<FakeWindowHandle> leftWindow =
2910 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2911 leftWindow->setFrame(Rect(0, 0, 200, 200));
2912
2913 sp<FakeWindowHandle> rightWindow =
2914 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2915 rightWindow->setFrame(Rect(200, 0, 400, 200));
2916
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002917 mDispatcher->onWindowInfosChanged(
2918 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002919
2920 const int32_t touchDeviceId = 4;
2921 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002922
2923 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002924 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2925 .deviceId(mouseDeviceId)
2926 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2927 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002928 leftWindow->consumeMotionEvent(
2929 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2930
2931 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002932 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2933 .deviceId(mouseDeviceId)
2934 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2935 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2936 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002937
2938 leftWindow->consumeMotionEvent(
2939 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2940 leftWindow->consumeMotionEvent(
2941 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2942
Prabir Pradhan678438e2023-04-13 19:32:51 +00002943 mDispatcher->notifyMotion(
2944 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2945 .deviceId(mouseDeviceId)
2946 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2947 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2948 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2949 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002950 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2951
2952 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002953 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2954 .deviceId(touchDeviceId)
2955 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2956 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002957 leftWindow->assertNoEvents();
2958
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002959 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2960
2961 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002962 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2963 .deviceId(touchDeviceId)
2964 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2965 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2966 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002967 // Since this is now a new splittable pointer going down on the left window, and it's coming
2968 // from a different device, the current gesture in the left window (pointer down) should first
2969 // be canceled.
2970 leftWindow->consumeMotionEvent(
2971 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002972 leftWindow->consumeMotionEvent(
2973 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2974 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2975 // current implementation.
2976 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2977 rightWindow->consumeMotionEvent(
2978 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2979
2980 leftWindow->assertNoEvents();
2981 rightWindow->assertNoEvents();
2982}
2983
2984/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002985 * Two windows: a window on the left and a window on the right.
2986 * Mouse is hovered on the left window and stylus is hovered on the right window.
2987 */
2988TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) {
2989 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2990 sp<FakeWindowHandle> leftWindow =
2991 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2992 leftWindow->setFrame(Rect(0, 0, 200, 200));
2993
2994 sp<FakeWindowHandle> rightWindow =
2995 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2996 rightWindow->setFrame(Rect(200, 0, 400, 200));
2997
2998 mDispatcher->onWindowInfosChanged(
2999 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3000
3001 const int32_t stylusDeviceId = 3;
3002 const int32_t mouseDeviceId = 6;
3003
3004 // Start hovering over the left window
3005 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3006 .deviceId(mouseDeviceId)
3007 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
3008 .build());
3009 leftWindow->consumeMotionEvent(
3010 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3011
3012 // Stylus hovered on right window
3013 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3014 .deviceId(stylusDeviceId)
3015 .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100))
3016 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003017 rightWindow->consumeMotionEvent(
3018 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3019
3020 // Subsequent HOVER_MOVE events are dispatched correctly.
3021 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
3022 .deviceId(mouseDeviceId)
3023 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
3024 .build());
3025 leftWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003026 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003027
3028 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3029 .deviceId(stylusDeviceId)
3030 .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110))
3031 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003032 rightWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003033 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003034
3035 leftWindow->assertNoEvents();
3036 rightWindow->assertNoEvents();
3037}
3038
3039/**
3040 * Three windows: a window on the left and a window on the right.
3041 * And a spy window that's positioned above all of them.
3042 * Stylus down on the left window and remains down. Touch goes down on the right and remains down.
3043 * Check the stream that's received by the spy.
3044 */
3045TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) {
3046 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3047
3048 sp<FakeWindowHandle> spyWindow =
3049 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3050 spyWindow->setFrame(Rect(0, 0, 400, 400));
3051 spyWindow->setTrustedOverlay(true);
3052 spyWindow->setSpy(true);
3053
3054 sp<FakeWindowHandle> leftWindow =
3055 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3056 leftWindow->setFrame(Rect(0, 0, 200, 200));
3057
3058 sp<FakeWindowHandle> rightWindow =
3059 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3060
3061 rightWindow->setFrame(Rect(200, 0, 400, 200));
3062
3063 mDispatcher->onWindowInfosChanged(
3064 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3065
3066 const int32_t stylusDeviceId = 1;
3067 const int32_t touchDeviceId = 2;
3068
3069 // Stylus down on the left window
3070 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3071 .deviceId(stylusDeviceId)
3072 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
3073 .build());
3074 leftWindow->consumeMotionEvent(
3075 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3076 spyWindow->consumeMotionEvent(
3077 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3078
3079 // Touch down on the right window
3080 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3081 .deviceId(touchDeviceId)
3082 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3083 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003084 leftWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003085 rightWindow->consumeMotionEvent(
3086 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003087
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003088 // Spy window does not receive touch events, because stylus events take precedence, and it
3089 // already has an active stylus gesture.
3090
3091 // Stylus movements continue. They should be delivered to the left window and to the spy window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003092 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
3093 .deviceId(stylusDeviceId)
3094 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
3095 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003096 leftWindow->consumeMotionEvent(
3097 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
3098 spyWindow->consumeMotionEvent(
3099 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003100
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003101 // Further MOVE events keep going to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003102 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3103 .deviceId(touchDeviceId)
3104 .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110))
3105 .build());
3106 rightWindow->consumeMotionEvent(
3107 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003108
3109 spyWindow->assertNoEvents();
3110 leftWindow->assertNoEvents();
3111 rightWindow->assertNoEvents();
3112}
3113
3114/**
3115 * Three windows: a window on the left, a window on the right, and a spy window positioned above
3116 * both.
3117 * Check hover in left window and touch down in the right window.
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003118 * At first, spy should receive hover. Spy shouldn't receive touch while stylus is hovering.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003119 * At the same time, left and right should be getting independent streams of hovering and touch,
3120 * respectively.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003121 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003122TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003123 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3124
3125 sp<FakeWindowHandle> spyWindow =
3126 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3127 spyWindow->setFrame(Rect(0, 0, 400, 400));
3128 spyWindow->setTrustedOverlay(true);
3129 spyWindow->setSpy(true);
3130
3131 sp<FakeWindowHandle> leftWindow =
3132 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3133 leftWindow->setFrame(Rect(0, 0, 200, 200));
3134
3135 sp<FakeWindowHandle> rightWindow =
3136 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3137 rightWindow->setFrame(Rect(200, 0, 400, 200));
3138
3139 mDispatcher->onWindowInfosChanged(
3140 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3141
3142 const int32_t stylusDeviceId = 1;
3143 const int32_t touchDeviceId = 2;
3144
3145 // Stylus hover on the left window
3146 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3147 .deviceId(stylusDeviceId)
3148 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
3149 .build());
3150 leftWindow->consumeMotionEvent(
3151 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3152 spyWindow->consumeMotionEvent(
3153 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3154
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003155 // Touch down on the right window. Spy doesn't receive this touch because it already has
3156 // stylus hovering there.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003157 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3158 .deviceId(touchDeviceId)
3159 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3160 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003161 leftWindow->assertNoEvents();
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003162 spyWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003163 rightWindow->consumeMotionEvent(
3164 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3165
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003166 // Stylus movements continue. They should be delivered to the left window and the spy.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003167 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3168 .deviceId(stylusDeviceId)
3169 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
3170 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003171 leftWindow->consumeMotionEvent(
3172 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003173 spyWindow->consumeMotionEvent(
3174 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003175
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003176 // Touch movements continue. They should be delivered to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003177 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3178 .deviceId(touchDeviceId)
3179 .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101))
3180 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003181 rightWindow->consumeMotionEvent(
3182 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
3183
3184 spyWindow->assertNoEvents();
3185 leftWindow->assertNoEvents();
3186 rightWindow->assertNoEvents();
3187}
3188
3189/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003190 * On a single window, use two different devices: mouse and touch.
3191 * Touch happens first, with two pointers going down, and then the first pointer leaving.
3192 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
3193 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
3194 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
3195 * represent a new gesture.
3196 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003197TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003198 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3199 sp<FakeWindowHandle> window =
3200 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3201 window->setFrame(Rect(0, 0, 400, 400));
3202
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003203 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003204
3205 const int32_t touchDeviceId = 4;
3206 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003207
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003208 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003209 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3210 .deviceId(touchDeviceId)
3211 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3212 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003213 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003214 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3215 .deviceId(touchDeviceId)
3216 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3217 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3218 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003219 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003220 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
3221 .deviceId(touchDeviceId)
3222 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3223 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3224 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003225 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3226 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3227 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
3228
3229 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00003230 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3231 .deviceId(mouseDeviceId)
3232 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3233 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3234 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003235
3236 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08003237 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003238 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3239
Prabir Pradhan678438e2023-04-13 19:32:51 +00003240 mDispatcher->notifyMotion(
3241 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3242 .deviceId(mouseDeviceId)
3243 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3244 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3245 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3246 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003247 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3248
3249 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003250 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3251 .deviceId(touchDeviceId)
3252 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3253 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3254 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003255 // Since we already canceled this touch gesture, it will be ignored until a completely new
3256 // gesture is started. This is easier to implement than trying to keep track of the new pointer
3257 // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN.
3258 // However, mouse movements should continue to work.
3259 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3260 .deviceId(mouseDeviceId)
3261 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3262 .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110))
3263 .build());
3264 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId)));
3265
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003266 window->assertNoEvents();
3267}
3268
3269/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003270 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
3271 * the injected event.
3272 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003273TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) {
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003274 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3275 sp<FakeWindowHandle> window =
3276 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3277 window->setFrame(Rect(0, 0, 400, 400));
3278
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003279 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003280
3281 const int32_t touchDeviceId = 4;
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003282 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
3283 // completion.
3284 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003285 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003286 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3287 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003288 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003289 .build()));
3290 window->consumeMotionEvent(
3291 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3292
3293 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
3294 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003295 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3296 .deviceId(touchDeviceId)
3297 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3298 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003299
3300 window->consumeMotionEvent(
3301 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3302 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3303}
3304
3305/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003306 * This test is similar to the test above, but the sequence of injected events is different.
3307 *
3308 * Two windows: a window on the left and a window on the right.
3309 * Mouse is hovered over the left window.
3310 * Next, we tap on the left window, where the cursor was last seen.
3311 *
3312 * After that, we inject one finger down onto the right window, and then a second finger down onto
3313 * the left window.
3314 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
3315 * window (first), and then another on the left window (second).
3316 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
3317 * In the buggy implementation, second finger down on the left window would cause a crash.
3318 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003319TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) {
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003320 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3321 sp<FakeWindowHandle> leftWindow =
3322 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3323 leftWindow->setFrame(Rect(0, 0, 200, 200));
3324
3325 sp<FakeWindowHandle> rightWindow =
3326 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3327 rightWindow->setFrame(Rect(200, 0, 400, 200));
3328
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003329 mDispatcher->onWindowInfosChanged(
3330 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003331
3332 const int32_t mouseDeviceId = 6;
3333 const int32_t touchDeviceId = 4;
3334 // Hover over the left window. Keep the cursor there.
3335 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003336 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003337 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3338 AINPUT_SOURCE_MOUSE)
3339 .deviceId(mouseDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003340 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003341 .build()));
3342 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3343
3344 // Tap on left window
3345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003346 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003347 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3348 AINPUT_SOURCE_TOUCHSCREEN)
3349 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003350 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003351 .build()));
3352
3353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003354 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003355 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3356 AINPUT_SOURCE_TOUCHSCREEN)
3357 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003358 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003359 .build()));
3360 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3361 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3362 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
3363
3364 // First finger down on right window
3365 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003366 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003367 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3368 AINPUT_SOURCE_TOUCHSCREEN)
3369 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003370 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003371 .build()));
3372 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3373
3374 // Second finger down on the left window
3375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003376 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003377 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3378 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003379 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3380 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003381 .build()));
3382 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3383 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
3384
3385 // No more events
3386 leftWindow->assertNoEvents();
3387 rightWindow->assertNoEvents();
3388}
3389
3390/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003391 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
3392 * While the touch is down, new hover events from the stylus device should be ignored. After the
3393 * touch is gone, stylus hovering should start working again.
3394 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003395TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) {
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003396 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3397 sp<FakeWindowHandle> window =
3398 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3399 window->setFrame(Rect(0, 0, 200, 200));
3400
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003401 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003402
3403 const int32_t stylusDeviceId = 5;
3404 const int32_t touchDeviceId = 4;
3405 // Start hovering with stylus
3406 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003407 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003408 MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003409 .deviceId(stylusDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003410 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003411 .build()));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003412 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003413
3414 // Finger down on the window
3415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003416 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003417 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003418 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003419 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003420 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003421 // The touch device should be ignored!
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003422
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003423 // Continue hovering with stylus.
3424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003425 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003426 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3427 AINPUT_SOURCE_STYLUS)
3428 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003429 .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003430 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003431 // Hovers continue to work
3432 window->consumeMotionEvent(
3433 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003434
3435 // Lift up the finger
3436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003437 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003438 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3439 AINPUT_SOURCE_TOUCHSCREEN)
3440 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003441 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003442 .build()));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003443
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003444 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003445 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003446 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3447 AINPUT_SOURCE_STYLUS)
3448 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003449 .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003450 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003451 window->consumeMotionEvent(
3452 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003453 window->assertNoEvents();
3454}
3455
3456/**
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07003457 * If stylus is down anywhere on the screen, then touches should not be delivered to windows that
3458 * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH.
3459 *
3460 * Two windows: one on the left and one on the right.
3461 * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config.
3462 * Stylus down on the left window, and then touch down on the right window.
3463 * Check that the right window doesn't get touches while the stylus is down on the left window.
3464 */
3465TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) {
3466 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3467 sp<FakeWindowHandle> leftWindow =
3468 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
3469 ADISPLAY_ID_DEFAULT);
3470 leftWindow->setFrame(Rect(0, 0, 100, 100));
3471
3472 sp<FakeWindowHandle> sbtRightWindow =
3473 sp<FakeWindowHandle>::make(application, mDispatcher,
3474 "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT);
3475 sbtRightWindow->setFrame(Rect(100, 100, 200, 200));
3476 sbtRightWindow->setGlobalStylusBlocksTouch(true);
3477
3478 mDispatcher->onWindowInfosChanged(
3479 {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0});
3480
3481 const int32_t stylusDeviceId = 5;
3482 const int32_t touchDeviceId = 4;
3483
3484 // Stylus down in the left window
3485 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3486 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52))
3487 .deviceId(stylusDeviceId)
3488 .build());
3489 leftWindow->consumeMotionEvent(
3490 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3491
3492 // Finger tap on the right window
3493 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3494 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3495 .deviceId(touchDeviceId)
3496 .build());
3497 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3498 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3499 .deviceId(touchDeviceId)
3500 .build());
3501
3502 // The touch should be blocked, because stylus is down somewhere else on screen!
3503 sbtRightWindow->assertNoEvents();
3504
3505 // Continue stylus motion, and ensure it's not impacted.
3506 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
3507 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3508 .deviceId(stylusDeviceId)
3509 .build());
3510 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3511 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3512 .deviceId(stylusDeviceId)
3513 .build());
3514 leftWindow->consumeMotionEvent(
3515 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
3516 leftWindow->consumeMotionEvent(
3517 AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId)));
3518
3519 // Now that the stylus gesture is done, touches should be getting delivered correctly.
3520 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3521 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153))
3522 .deviceId(touchDeviceId)
3523 .build());
3524 sbtRightWindow->consumeMotionEvent(
3525 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3526}
3527
3528/**
3529 * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows
3530 * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH.
3531 *
3532 * Two windows: one on the left and one on the right.
3533 * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config.
3534 * Stylus hover on the left window, and then touch down on the right window.
3535 * Check that the right window doesn't get touches while the stylus is hovering on the left window.
3536 */
3537TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) {
3538 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3539 sp<FakeWindowHandle> leftWindow =
3540 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
3541 ADISPLAY_ID_DEFAULT);
3542 leftWindow->setFrame(Rect(0, 0, 100, 100));
3543
3544 sp<FakeWindowHandle> sbtRightWindow =
3545 sp<FakeWindowHandle>::make(application, mDispatcher,
3546 "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT);
3547 sbtRightWindow->setFrame(Rect(100, 100, 200, 200));
3548 sbtRightWindow->setGlobalStylusBlocksTouch(true);
3549
3550 mDispatcher->onWindowInfosChanged(
3551 {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0});
3552
3553 const int32_t stylusDeviceId = 5;
3554 const int32_t touchDeviceId = 4;
3555
3556 // Stylus hover in the left window
3557 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3558 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52))
3559 .deviceId(stylusDeviceId)
3560 .build());
3561 leftWindow->consumeMotionEvent(
3562 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3563
3564 // Finger tap on the right window
3565 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3566 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3567 .deviceId(touchDeviceId)
3568 .build());
3569 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3570 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3571 .deviceId(touchDeviceId)
3572 .build());
3573
3574 // The touch should be blocked, because stylus is hovering somewhere else on screen!
3575 sbtRightWindow->assertNoEvents();
3576
3577 // Continue stylus motion, and ensure it's not impacted.
3578 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3579 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3580 .deviceId(stylusDeviceId)
3581 .build());
3582 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3583 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3584 .deviceId(stylusDeviceId)
3585 .build());
3586 leftWindow->consumeMotionEvent(
3587 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
3588 leftWindow->consumeMotionEvent(
3589 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId)));
3590
3591 // Now that the stylus gesture is done, touches should be getting delivered correctly.
3592 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3593 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153))
3594 .deviceId(touchDeviceId)
3595 .build());
3596 sbtRightWindow->consumeMotionEvent(
3597 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3598}
3599
3600/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003601 * A spy window above a window with no input channel.
3602 * Start hovering with a stylus device, and then tap with it.
3603 * Ensure spy window receives the entire sequence.
3604 */
3605TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
3606 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3607 sp<FakeWindowHandle> spyWindow =
3608 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3609 spyWindow->setFrame(Rect(0, 0, 200, 200));
3610 spyWindow->setTrustedOverlay(true);
3611 spyWindow->setSpy(true);
3612 sp<FakeWindowHandle> window =
3613 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3614 window->setNoInputChannel(true);
3615 window->setFrame(Rect(0, 0, 200, 200));
3616
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003617 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003618
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003619 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00003620 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3621 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3622 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003623 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3624 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003625 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3626 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3627 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003628 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3629
3630 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003631 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3632 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3633 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003634 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3635
3636 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00003637 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3638 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3639 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003640 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
3641
3642 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00003643 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3644 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3645 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003646 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3647 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003648 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3649 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3650 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003651 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3652
3653 // No more events
3654 spyWindow->assertNoEvents();
3655 window->assertNoEvents();
3656}
3657
3658/**
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07003659 * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be
3660 * rejected. But since we already have an ongoing gesture, this event should be processed.
3661 * This prevents inconsistent events being handled inside the dispatcher.
3662 */
3663TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) {
3664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3665
3666 sp<FakeWindowHandle> window =
3667 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3668 window->setFrame(Rect(0, 0, 200, 200));
3669
3670 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
3671
3672 // Start hovering with stylus
3673 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3674 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3675 .build());
3676 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3677
3678 NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3679 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3680 .build();
3681 // Make this 'hoverExit' event stale
3682 mFakePolicy->setStaleEventTimeout(100ms);
3683 std::this_thread::sleep_for(100ms);
3684
3685 // It shouldn't be dropped by the dispatcher, even though it's stale.
3686 mDispatcher->notifyMotion(hoverExit);
3687 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3688
3689 // Stylus starts hovering again! There should be no crash.
3690 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3691 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51))
3692 .build());
3693 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3694}
3695
3696/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003697 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
3698 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3699 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3700 * While the mouse is down, new move events from the touch device should be ignored.
3701 */
3702TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3703 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3704 sp<FakeWindowHandle> spyWindow =
3705 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3706 spyWindow->setFrame(Rect(0, 0, 200, 200));
3707 spyWindow->setTrustedOverlay(true);
3708 spyWindow->setSpy(true);
3709 sp<FakeWindowHandle> window =
3710 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3711 window->setFrame(Rect(0, 0, 200, 200));
3712
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003713 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003714
3715 const int32_t mouseDeviceId = 7;
3716 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003717
3718 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003719 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3720 .deviceId(mouseDeviceId)
3721 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3722 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003723 spyWindow->consumeMotionEvent(
3724 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3725 window->consumeMotionEvent(
3726 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3727
3728 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003729 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3730 .deviceId(touchDeviceId)
3731 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3732 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003733 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3734 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3735 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3736 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3737
Prabir Pradhan678438e2023-04-13 19:32:51 +00003738 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3739 .deviceId(touchDeviceId)
3740 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3741 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003742 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3743 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3744
3745 // Pilfer the stream
3746 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3747 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3748
Prabir Pradhan678438e2023-04-13 19:32:51 +00003749 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3750 .deviceId(touchDeviceId)
3751 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3752 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003753 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3754
3755 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003756 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3757 .deviceId(mouseDeviceId)
3758 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3759 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3760 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003761
3762 spyWindow->consumeMotionEvent(
3763 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3764 spyWindow->consumeMotionEvent(
3765 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3766 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3767
Prabir Pradhan678438e2023-04-13 19:32:51 +00003768 mDispatcher->notifyMotion(
3769 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3770 .deviceId(mouseDeviceId)
3771 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3772 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3773 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3774 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003775 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3776 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3777
3778 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3780 .deviceId(mouseDeviceId)
3781 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3782 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3783 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003784 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3785 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3786
3787 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003788 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3789 .deviceId(touchDeviceId)
3790 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3791 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003792
3793 // No more events
3794 spyWindow->assertNoEvents();
3795 window->assertNoEvents();
3796}
3797
3798/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003799 * On the display, have a single window, and also an area where there's no window.
3800 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
3801 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
3802 */
3803TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3804 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3805 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003806 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003807
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003808 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003809
3810 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00003811 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003812
3813 mDispatcher->waitForIdle();
3814 window->assertNoEvents();
3815
3816 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003817 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003818 mDispatcher->waitForIdle();
3819 window->consumeMotionDown();
3820}
3821
3822/**
3823 * Same test as above, but instead of touching the empty space, the first touch goes to
3824 * non-touchable window.
3825 */
3826TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3827 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3828 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003829 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003830 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3831 window1->setTouchable(false);
3832 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003833 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003834 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3835
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003836 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003837
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003838 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003839 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003840
3841 mDispatcher->waitForIdle();
3842 window1->assertNoEvents();
3843 window2->assertNoEvents();
3844
3845 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003846 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003847 mDispatcher->waitForIdle();
3848 window2->consumeMotionDown();
3849}
3850
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003851/**
3852 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3853 * to the event time of the first ACTION_DOWN sent to the particular window.
3854 */
3855TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3856 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3857 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003858 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003859 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3860 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003861 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003862 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3863
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003864 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003865
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003866 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003867 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003868
3869 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003870
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003871 std::unique_ptr<MotionEvent> motionEvent1 = window1->consumeMotionEvent();
3872 ASSERT_NE(nullptr, motionEvent1);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003873 window2->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003874 nsecs_t downTimeForWindow1 = motionEvent1->getDownTime();
3875 ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003876
3877 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003878 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003879 mDispatcher->waitForIdle();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003880 std::unique_ptr<MotionEvent> motionEvent2 = window2->consumeMotionEvent();
3881 ASSERT_NE(nullptr, motionEvent2);
3882 nsecs_t downTimeForWindow2 = motionEvent2->getDownTime();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003883 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003884 ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003885
3886 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003887 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003888 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003889 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003890
3891 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003892 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003893 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003894 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003895
3896 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3897 window1->consumeMotionMove();
3898 window1->assertNoEvents();
3899
3900 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003901 mDispatcher->notifyMotion(
3902 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003903 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003904 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003905
Prabir Pradhan678438e2023-04-13 19:32:51 +00003906 mDispatcher->notifyMotion(
3907 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003908 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003909 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003910}
3911
Garfield Tandf26e862020-07-01 20:18:19 -07003912TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003913 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003914 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003915 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003916 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003917 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003918 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003919 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003920
3921 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3922
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003923 mDispatcher->onWindowInfosChanged(
3924 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07003925
3926 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003928 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003929 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3930 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003931 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003932 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003933 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003934
3935 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003937 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003938 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3939 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003940 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003941 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003942 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3943 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003944
3945 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003947 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003948 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3949 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003950 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003951 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003952 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3953 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003954
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003955 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003956 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003957 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3958 AINPUT_SOURCE_MOUSE)
3959 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3960 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003961 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003962 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003963 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003964
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003966 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003967 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3968 AINPUT_SOURCE_MOUSE)
3969 .buttonState(0)
3970 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003971 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003972 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003973 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003974
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003976 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003977 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3978 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003979 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003980 .build()));
3981 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3982
3983 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003985 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003986 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3987 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003988 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003989 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003990 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003991
3992 // No more events
3993 windowLeft->assertNoEvents();
3994 windowRight->assertNoEvents();
3995}
3996
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003997/**
3998 * Put two fingers down (and don't release them) and click the mouse button.
3999 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
4000 * currently active gesture should be canceled, and the new one should proceed.
4001 */
4002TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
4003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4004 sp<FakeWindowHandle> window =
4005 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4006 window->setFrame(Rect(0, 0, 600, 800));
4007
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004008 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004009
4010 const int32_t touchDeviceId = 4;
4011 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004012
4013 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004014 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4015 .deviceId(touchDeviceId)
4016 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4017 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004018
Prabir Pradhan678438e2023-04-13 19:32:51 +00004019 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4020 .deviceId(touchDeviceId)
4021 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4022 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
4023 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004024 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4025 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4026
4027 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00004028 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4029 .deviceId(mouseDeviceId)
4030 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4031 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4032 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004033 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
4034 WithPointerCount(2u)));
4035 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4036
Prabir Pradhan678438e2023-04-13 19:32:51 +00004037 mDispatcher->notifyMotion(
4038 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4039 .deviceId(mouseDeviceId)
4040 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4041 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4042 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4043 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004044 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4045
4046 // Try to send more touch events while the mouse is down. Since it's a continuation of an
4047 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004048 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4049 .deviceId(touchDeviceId)
4050 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
4051 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
4052 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004053 window->assertNoEvents();
4054}
4055
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004056TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
4057 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4058
4059 sp<FakeWindowHandle> spyWindow =
4060 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4061 spyWindow->setFrame(Rect(0, 0, 600, 800));
4062 spyWindow->setTrustedOverlay(true);
4063 spyWindow->setSpy(true);
4064 sp<FakeWindowHandle> window =
4065 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4066 window->setFrame(Rect(0, 0, 600, 800));
4067
4068 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004069 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004070
4071 // Send mouse cursor to the window
4072 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004073 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004074 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4075 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004076 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004077 .build()));
4078
4079 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4080 WithSource(AINPUT_SOURCE_MOUSE)));
4081 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4082 WithSource(AINPUT_SOURCE_MOUSE)));
4083
4084 window->assertNoEvents();
4085 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004086}
4087
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004088TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
4089 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4090
4091 sp<FakeWindowHandle> spyWindow =
4092 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4093 spyWindow->setFrame(Rect(0, 0, 600, 800));
4094 spyWindow->setTrustedOverlay(true);
4095 spyWindow->setSpy(true);
4096 sp<FakeWindowHandle> window =
4097 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4098 window->setFrame(Rect(0, 0, 600, 800));
4099
4100 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004101 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004102
4103 // Send mouse cursor to the window
4104 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004105 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004106 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4107 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004108 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004109 .build()));
4110
4111 // Move mouse cursor
4112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004113 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004114 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4115 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004116 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004117 .build()));
4118
4119 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4120 WithSource(AINPUT_SOURCE_MOUSE)));
4121 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4122 WithSource(AINPUT_SOURCE_MOUSE)));
4123 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4124 WithSource(AINPUT_SOURCE_MOUSE)));
4125 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4126 WithSource(AINPUT_SOURCE_MOUSE)));
4127 // Touch down on the window
4128 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004129 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004130 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4131 AINPUT_SOURCE_TOUCHSCREEN)
4132 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004133 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004134 .build()));
4135 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4136 WithSource(AINPUT_SOURCE_MOUSE)));
4137 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4138 WithSource(AINPUT_SOURCE_MOUSE)));
4139 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4140 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4141 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4142 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4143
4144 // pilfer the motion, retaining the gesture on the spy window.
4145 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
4146 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4147 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4148
4149 // Touch UP on the window
4150 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004151 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004152 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4153 AINPUT_SOURCE_TOUCHSCREEN)
4154 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004155 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004156 .build()));
4157 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4158 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4159
4160 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
4161 // to send a new gesture. It should again go to both windows (spy and the window below), just
4162 // like the first gesture did, before pilfering. The window configuration has not changed.
4163
4164 // One more tap - DOWN
4165 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004166 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004167 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4168 AINPUT_SOURCE_TOUCHSCREEN)
4169 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004170 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004171 .build()));
4172 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4173 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4174 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4175 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4176
4177 // Touch UP on the window
4178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004179 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004180 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4181 AINPUT_SOURCE_TOUCHSCREEN)
4182 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004183 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004184 .build()));
4185 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4186 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4187 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4188 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4189
4190 window->assertNoEvents();
4191 spyWindow->assertNoEvents();
4192}
4193
Garfield Tandf26e862020-07-01 20:18:19 -07004194// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
4195// directly in this test.
4196TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07004197 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07004198 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004199 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004200 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07004201
4202 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4203
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004204 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07004205
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004206 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004207 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004208 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4209 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004210 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004211 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004212 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004213 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004215 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004216 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4217 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004218 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004219 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004220 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4221 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07004222
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004224 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004225 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
4226 AINPUT_SOURCE_MOUSE)
4227 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4228 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004229 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004230 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004231 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07004232
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004234 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004235 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
4236 AINPUT_SOURCE_MOUSE)
4237 .buttonState(0)
4238 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004239 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004240 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004241 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07004242
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004244 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004245 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
4246 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004247 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004248 .build()));
4249 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4250
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07004251 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
4252 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
4253 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004254 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004255 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
4256 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004257 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004258 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004259 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004260}
4261
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004262/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004263 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
4264 * is generated.
4265 */
4266TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
4267 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4268 sp<FakeWindowHandle> window =
4269 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4270 window->setFrame(Rect(0, 0, 1200, 800));
4271
4272 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4273
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004274 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004275
4276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004277 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004278 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4279 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004280 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004281 .build()));
4282 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4283
4284 // Remove the window, but keep the channel.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004285 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004286 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4287}
4288
4289/**
Daniel Norman7487dfa2023-08-02 16:39:45 -07004290 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
4291 */
Ameer Armalycff4fa52023-10-04 23:45:11 +00004292TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash,
4293 REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags,
4294 a11y_crash_on_inconsistent_event_stream))) {
Daniel Norman7487dfa2023-08-02 16:39:45 -07004295 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4296 sp<FakeWindowHandle> window =
4297 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4298 window->setFrame(Rect(0, 0, 1200, 800));
4299
4300 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4301
4302 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4303
4304 MotionEventBuilder hoverEnterBuilder =
4305 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4306 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4307 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4308 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4309 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4311 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4312 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4313 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4314}
4315
4316/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004317 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
4318 */
4319TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
4320 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4321 sp<FakeWindowHandle> window =
4322 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4323 window->setFrame(Rect(0, 0, 100, 100));
4324
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004325 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004326
4327 const int32_t mouseDeviceId = 7;
4328 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004329
4330 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00004331 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4332 .deviceId(mouseDeviceId)
4333 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4334 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004335 window->consumeMotionEvent(
4336 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4337
4338 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004339 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4340 .deviceId(touchDeviceId)
4341 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4342 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004343
4344 window->consumeMotionEvent(
4345 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4346 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4347}
4348
4349/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004350 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004351 * The tap causes a HOVER_EXIT event to be generated because the current event
4352 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004353 */
4354TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
4355 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4356 sp<FakeWindowHandle> window =
4357 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4358 window->setFrame(Rect(0, 0, 100, 100));
4359
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004360 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004361 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4362 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4363 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004364 ASSERT_NO_FATAL_FAILURE(
4365 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4366 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004367
4368 // Tap on the window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004369 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4370 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4371 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004372 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004373 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4374 WithSource(AINPUT_SOURCE_MOUSE))));
4375
4376 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004377 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4378 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4379
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004380 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4381 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4382 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004383 ASSERT_NO_FATAL_FAILURE(
4384 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4385 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4386}
4387
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004388TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
4389 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4390 sp<FakeWindowHandle> windowDefaultDisplay =
4391 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4392 ADISPLAY_ID_DEFAULT);
4393 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
4394 sp<FakeWindowHandle> windowSecondDisplay =
4395 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
4396 SECOND_DISPLAY_ID);
4397 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
4398
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004399 mDispatcher->onWindowInfosChanged(
4400 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004401
4402 // Set cursor position in window in default display and check that hover enter and move
4403 // events are generated.
4404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004405 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004406 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4407 AINPUT_SOURCE_MOUSE)
4408 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004409 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004410 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004411 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004412
4413 // Remove all windows in secondary display and check that no event happens on window in
4414 // primary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004415 mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0});
4416
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004417 windowDefaultDisplay->assertNoEvents();
4418
4419 // Move cursor position in window in default display and check that only hover move
4420 // event is generated and not hover enter event.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004421 mDispatcher->onWindowInfosChanged(
4422 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004424 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004425 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4426 AINPUT_SOURCE_MOUSE)
4427 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004428 .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004429 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004430 windowDefaultDisplay->consumeMotionEvent(
4431 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4432 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004433 windowDefaultDisplay->assertNoEvents();
4434}
4435
Garfield Tan00f511d2019-06-12 16:55:40 -07004436TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07004437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07004438
4439 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004440 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004441 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004442 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004443 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004444 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004445
4446 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4447
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004448 mDispatcher->onWindowInfosChanged(
4449 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tan00f511d2019-06-12 16:55:40 -07004450
4451 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
4452 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004454 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004455 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004456 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004457 windowRight->assertNoEvents();
4458}
4459
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004460TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004461 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004462 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4463 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07004464 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004465
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004466 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07004467 setFocusedWindow(window);
4468
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004469 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004470
Prabir Pradhan678438e2023-04-13 19:32:51 +00004471 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004472
4473 // Window should receive key down event.
4474 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4475
4476 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
4477 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004478 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00004479 window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004480}
4481
4482TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004483 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004484 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4485 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004486
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004487 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004488
Prabir Pradhan678438e2023-04-13 19:32:51 +00004489 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4490 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004491
4492 // Window should receive motion down event.
4493 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4494
4495 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
4496 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004497 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004498 window->consumeMotionEvent(
4499 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004500}
4501
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004502TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
4503 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4504 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4505 "Fake Window", ADISPLAY_ID_DEFAULT);
4506
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004507 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004508
4509 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4510 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
4511 .build());
4512
4513 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4514
4515 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
4516 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
4517 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4518
4519 // After the device has been reset, a new hovering stream can be sent to the window
4520 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4521 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
4522 .build());
4523 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4524}
4525
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004526TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
4527 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004528 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4529 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004530 window->setFocusable(true);
4531
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004532 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004533 setFocusedWindow(window);
4534
4535 window->consumeFocusEvent(true);
4536
Prabir Pradhan678438e2023-04-13 19:32:51 +00004537 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004538 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
4539 const nsecs_t injectTime = keyArgs.eventTime;
4540 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00004541 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004542 // The dispatching time should be always greater than or equal to intercept key timeout.
4543 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4544 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
4545 std::chrono::nanoseconds(interceptKeyTimeout).count());
4546}
4547
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004548/**
4549 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
4550 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004551TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
4552 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004553 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4554 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004555 window->setFocusable(true);
4556
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004557 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004558 setFocusedWindow(window);
4559
4560 window->consumeFocusEvent(true);
4561
Prabir Pradhan678438e2023-04-13 19:32:51 +00004562 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004563 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004564
4565 // Set a value that's significantly larger than the default consumption timeout. If the
4566 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
4567 mFakePolicy->setInterceptKeyTimeout(600ms);
4568 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
4569 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004570 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4571}
4572
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004573/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004574 * Two windows. First is a regular window. Second does not overlap with the first, and has
4575 * WATCH_OUTSIDE_TOUCH.
4576 * Both windows are owned by the same UID.
4577 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
4578 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
4579 */
4580TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
4581 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004582 sp<FakeWindowHandle> window =
4583 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004584 window->setFrame(Rect{0, 0, 100, 100});
4585
4586 sp<FakeWindowHandle> outsideWindow =
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004587 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004588 ADISPLAY_ID_DEFAULT);
4589 outsideWindow->setFrame(Rect{100, 100, 200, 200});
4590 outsideWindow->setWatchOutsideTouch(true);
4591 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004592 mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004593
4594 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004595 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4596 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4597 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004598 window->consumeMotionDown();
4599 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
4600 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
4601 outsideWindow->consumeMotionEvent(
4602 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
Prabir Pradhan502a7252023-12-01 16:11:24 +00004603
4604 // Ensure outsideWindow doesn't get any more events for the gesture.
4605 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4606 ADISPLAY_ID_DEFAULT, {PointF{51, 51}}));
4607 window->consumeMotionMove();
4608 outsideWindow->assertNoEvents();
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004609}
4610
4611/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004612 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
4613 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
4614 * ACTION_OUTSIDE event is sent per gesture.
4615 */
4616TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
4617 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
4618 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004619 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4620 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004621 window->setWatchOutsideTouch(true);
4622 window->setFrame(Rect{0, 0, 100, 100});
4623 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004624 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4625 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004626 secondWindow->setFrame(Rect{100, 100, 200, 200});
4627 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004628 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
4629 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004630 thirdWindow->setFrame(Rect{200, 200, 300, 300});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004631 mDispatcher->onWindowInfosChanged(
4632 {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0});
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004633
4634 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004635 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4636 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4637 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004638 window->assertNoEvents();
4639 secondWindow->assertNoEvents();
4640
4641 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
4642 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004643 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4644 ADISPLAY_ID_DEFAULT,
4645 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004646 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
4647 window->consumeMotionEvent(
4648 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004649 secondWindow->consumeMotionDown();
4650 thirdWindow->assertNoEvents();
4651
4652 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
4653 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004654 mDispatcher->notifyMotion(
4655 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4656 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004657 window->assertNoEvents();
4658 secondWindow->consumeMotionMove();
4659 thirdWindow->consumeMotionDown();
4660}
4661
Prabir Pradhan814fe082022-07-22 20:22:18 +00004662TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
4663 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004664 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4665 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004666 window->setFocusable(true);
4667
Patrick Williamsd828f302023-04-28 17:52:08 -05004668 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004669 setFocusedWindow(window);
4670
4671 window->consumeFocusEvent(true);
4672
Prabir Pradhan678438e2023-04-13 19:32:51 +00004673 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
4674 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
4675 mDispatcher->notifyKey(keyDown);
4676 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004677
4678 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4679 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4680
4681 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05004682 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004683
4684 window->consumeFocusEvent(false);
4685
Prabir Pradhan678438e2023-04-13 19:32:51 +00004686 mDispatcher->notifyKey(keyDown);
4687 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004688 window->assertNoEvents();
4689}
4690
Arthur Hung96483742022-11-15 03:30:48 +00004691TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
4692 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4693 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4694 "Fake Window", ADISPLAY_ID_DEFAULT);
4695 // Ensure window is non-split and have some transform.
4696 window->setPreventSplitting(true);
4697 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05004698 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00004699
4700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004701 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung96483742022-11-15 03:30:48 +00004702 {50, 50}))
4703 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4704 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4705
4706 const MotionEvent secondFingerDownEvent =
4707 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4708 .displayId(ADISPLAY_ID_DEFAULT)
4709 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004710 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4711 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00004712 .build();
4713 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004714 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung96483742022-11-15 03:30:48 +00004715 InputEventInjectionSync::WAIT_FOR_RESULT))
4716 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4717
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004718 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
4719 ASSERT_NE(nullptr, event);
4720 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4721 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4722 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4723 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4724 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
Arthur Hung96483742022-11-15 03:30:48 +00004725}
4726
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07004727/**
4728 * Two windows: a splittable and a non-splittable.
4729 * The non-splittable window shouldn't receive any "incomplete" gestures.
4730 * Send the first pointer to the splittable window, and then touch the non-splittable window.
4731 * The second pointer should be dropped because the initial window is splittable, so it won't get
4732 * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any
4733 * "incomplete" gestures.
4734 */
4735TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) {
4736 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4737 sp<FakeWindowHandle> leftWindow =
4738 sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window",
4739 ADISPLAY_ID_DEFAULT);
4740 leftWindow->setPreventSplitting(false);
4741 leftWindow->setFrame(Rect(0, 0, 100, 100));
4742 sp<FakeWindowHandle> rightWindow =
4743 sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window",
4744 ADISPLAY_ID_DEFAULT);
4745 rightWindow->setPreventSplitting(true);
4746 rightWindow->setFrame(Rect(100, 100, 200, 200));
4747 mDispatcher->onWindowInfosChanged(
4748 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
4749
4750 // Touch down on left, splittable window
4751 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4752 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4753 .build());
4754 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4755
4756 mDispatcher->notifyMotion(
4757 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4758 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4759 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
4760 .build());
4761 leftWindow->assertNoEvents();
4762 rightWindow->assertNoEvents();
4763}
4764
Harry Cuttsb166c002023-05-09 13:06:05 +00004765TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4766 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4767 sp<FakeWindowHandle> window =
4768 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4769 window->setFrame(Rect(0, 0, 400, 400));
4770 sp<FakeWindowHandle> trustedOverlay =
4771 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4772 ADISPLAY_ID_DEFAULT);
4773 trustedOverlay->setSpy(true);
4774 trustedOverlay->setTrustedOverlay(true);
4775
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004776 mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004777
4778 // Start a three-finger touchpad swipe
4779 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4780 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4781 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4782 .build());
4783 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4784 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4785 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4786 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4787 .build());
4788 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4789 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4790 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4791 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4792 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4793 .build());
4794
4795 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4796 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4797 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4798
4799 // Move the swipe a bit
4800 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4801 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4802 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4803 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4804 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4805 .build());
4806
4807 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4808
4809 // End the swipe
4810 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4811 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4812 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4813 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4814 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4815 .build());
4816 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4817 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4818 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4819 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4820 .build());
4821 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4822 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4823 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4824 .build());
4825
4826 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4827 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4828 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4829
4830 window->assertNoEvents();
4831}
4832
4833TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4834 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4835 sp<FakeWindowHandle> window =
4836 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4837 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004838 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004839
4840 // Start a three-finger touchpad swipe
4841 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4842 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4843 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4844 .build());
4845 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4846 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4847 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4848 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4849 .build());
4850 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4851 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4852 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4853 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4854 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4855 .build());
4856
4857 // Move the swipe a bit
4858 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4859 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4860 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4861 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4862 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4863 .build());
4864
4865 // End the swipe
4866 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4867 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4868 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4869 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4870 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4871 .build());
4872 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4873 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4874 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4875 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4876 .build());
4877 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4878 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4879 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4880 .build());
4881
4882 window->assertNoEvents();
4883}
4884
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004885/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004886 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
4887 * the first pointer.
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004888 * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004889 */
4890TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
4891 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4892 sp<FakeWindowHandle> window =
4893 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4894 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004895 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004896
4897 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
4898 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4899 .downTime(baseTime + 10)
4900 .eventTime(baseTime + 10)
4901 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4902 .build());
4903
4904 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4905
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004906 // Change the transform so that the orientation is now different from original.
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004907 window->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004908
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004909 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004910
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004911 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4912 .downTime(baseTime + 10)
4913 .eventTime(baseTime + 30)
4914 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4915 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4916 .build());
4917
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004918 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4919
4920 // Finish the gesture and start a new one. Ensure all events are sent to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004921 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
4922 .downTime(baseTime + 10)
4923 .eventTime(baseTime + 40)
4924 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4925 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4926 .build());
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004927
4928 window->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4929
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004930 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4931 .downTime(baseTime + 10)
4932 .eventTime(baseTime + 50)
4933 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4934 .build());
4935
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004936 window->consumeMotionEvent(WithMotionAction(ACTION_UP));
4937
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004938 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4939 .downTime(baseTime + 60)
4940 .eventTime(baseTime + 60)
4941 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
4942 .build());
4943
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004944 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004945}
4946
4947/**
Hu Guo771a7692023-09-17 20:51:08 +08004948 * When there are multiple screens, such as screen projection to TV or screen recording, if the
4949 * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and
4950 * its coordinates should be converted by the transform of the windows of target screen.
4951 */
4952TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) {
4953 // This case will create a window and a spy window on the default display and mirror
4954 // window on the second display. cancel event is sent through spy window pilferPointers
4955 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4956
4957 sp<FakeWindowHandle> spyWindowDefaultDisplay =
4958 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4959 spyWindowDefaultDisplay->setTrustedOverlay(true);
4960 spyWindowDefaultDisplay->setSpy(true);
4961
4962 sp<FakeWindowHandle> windowDefaultDisplay =
4963 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4964 ADISPLAY_ID_DEFAULT);
4965 windowDefaultDisplay->setWindowTransform(1, 0, 0, 1);
4966
4967 sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID);
4968 windowSecondDisplay->setWindowTransform(2, 0, 0, 2);
4969
4970 // Add the windows to the dispatcher
4971 mDispatcher->onWindowInfosChanged(
4972 {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(),
4973 *windowSecondDisplay->getInfo()},
4974 {},
4975 0,
4976 0});
4977
4978 // Send down to ADISPLAY_ID_DEFAULT
4979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4980 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4981 {100, 100}))
4982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4983
4984 spyWindowDefaultDisplay->consumeMotionDown();
4985 windowDefaultDisplay->consumeMotionDown();
4986
4987 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken()));
4988
4989 // windowDefaultDisplay gets cancel
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004990 std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent();
4991 ASSERT_NE(nullptr, event);
4992 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction());
Hu Guo771a7692023-09-17 20:51:08 +08004993
4994 // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the
4995 // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y
4996 // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of
4997 // SECOND_DISPLAY_ID, the x and y coordinates are 200
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004998 EXPECT_EQ(100, event->getX(0));
4999 EXPECT_EQ(100, event->getY(0));
Hu Guo771a7692023-09-17 20:51:08 +08005000}
5001
5002/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005003 * Ensure the correct coordinate spaces are used by InputDispatcher.
5004 *
5005 * InputDispatcher works in the display space, so its coordinate system is relative to the display
5006 * panel. Windows get events in the window space, and get raw coordinates in the logical display
5007 * space.
5008 */
5009class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
5010public:
5011 void SetUp() override {
5012 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005013 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005014 }
5015
5016 void addDisplayInfo(int displayId, const ui::Transform& transform) {
5017 gui::DisplayInfo info;
5018 info.displayId = displayId;
5019 info.transform = transform;
5020 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05005021 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005022 }
5023
5024 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
5025 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05005026 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005027 }
5028
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005029 void removeAllWindowsAndDisplays() {
5030 mDisplayInfos.clear();
5031 mWindowInfos.clear();
5032 }
5033
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005034 // Set up a test scenario where the display has a scaled projection and there are two windows
5035 // on the display.
5036 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
5037 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
5038 // respectively.
5039 ui::Transform displayTransform;
5040 displayTransform.set(2, 0, 0, 4);
5041 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5042
5043 std::shared_ptr<FakeApplicationHandle> application =
5044 std::make_shared<FakeApplicationHandle>();
5045
5046 // Add two windows to the display. Their frames are represented in the display space.
5047 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005048 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5049 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005050 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
5051 addWindow(firstWindow);
5052
5053 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005054 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5055 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005056 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
5057 addWindow(secondWindow);
5058 return {std::move(firstWindow), std::move(secondWindow)};
5059 }
5060
5061private:
5062 std::vector<gui::DisplayInfo> mDisplayInfos;
5063 std::vector<gui::WindowInfo> mWindowInfos;
5064};
5065
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005066TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005067 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5068 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005069 // selected so that if the hit test was performed with the point and the bounds being in
5070 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005071 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5072 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5073 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005074
5075 firstWindow->consumeMotionDown();
5076 secondWindow->assertNoEvents();
5077}
5078
5079// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
5080// the event should be treated as being in the logical display space.
5081TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
5082 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5083 // Send down to the first window. The point is represented in the logical display space. The
5084 // point is selected so that if the hit test was done in logical display space, then it would
5085 // end up in the incorrect window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005086 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005087 PointF{75 * 2, 55 * 4});
5088
5089 firstWindow->consumeMotionDown();
5090 secondWindow->assertNoEvents();
5091}
5092
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005093// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
5094// event should be treated as being in the logical display space.
5095TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
5096 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5097
5098 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5099 ui::Transform injectedEventTransform;
5100 injectedEventTransform.set(matrix);
5101 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
5102 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
5103
5104 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5105 .displayId(ADISPLAY_ID_DEFAULT)
5106 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005107 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005108 .x(untransformedPoint.x)
5109 .y(untransformedPoint.y))
5110 .build();
5111 event.transform(matrix);
5112
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005113 injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT,
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005114 InputEventInjectionSync::WAIT_FOR_RESULT);
5115
5116 firstWindow->consumeMotionDown();
5117 secondWindow->assertNoEvents();
5118}
5119
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005120TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
5121 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5122
5123 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005124 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5125 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5126 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005127
5128 firstWindow->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005129 std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent();
5130 ASSERT_NE(nullptr, event);
5131 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005132
5133 // Ensure that the events from the "getRaw" API are in logical display coordinates.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005134 EXPECT_EQ(300, event->getRawX(0));
5135 EXPECT_EQ(880, event->getRawY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005136
5137 // Ensure that the x and y values are in the window's coordinate space.
5138 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
5139 // the logical display space. This will be the origin of the window space.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005140 EXPECT_EQ(100, event->getX(0));
5141 EXPECT_EQ(80, event->getY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005142}
5143
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005144TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) {
5145 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5146 // The monitor will always receive events in the logical display's coordinate space, because
5147 // it does not have a window.
Prabir Pradhanfb549072023-10-05 19:17:36 +00005148 FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005149
5150 // Send down to the first window.
5151 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5152 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
5153 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5154 monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5155
5156 // Second pointer goes down on second window.
5157 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5158 ADISPLAY_ID_DEFAULT,
5159 {PointF{50, 100}, PointF{150, 220}}));
5160 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80)));
5161 const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}},
5162 {1, PointF{300, 880}}};
5163 monitor.consumeMotionEvent(
5164 AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers)));
5165
5166 mDispatcher->cancelCurrentTouch();
5167
5168 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5169 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80)));
5170 monitor.consumeMotionEvent(
5171 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers)));
5172}
5173
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005174TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) {
5175 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5176
5177 // Send down to the first window.
5178 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5179 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
5180 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5181
5182 // The pointer is transferred to the second window, and the second window receives it in the
5183 // correct coordinate space.
Prabir Pradhan367f3432024-02-13 23:05:58 +00005184 mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken());
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005185 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5186 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400)));
5187}
5188
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005189TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) {
5190 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5191
5192 // Send hover move to the second window, and ensure it shows up as hover enter.
5193 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
5194 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5195 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5196 WithCoords(100, 80), WithRawCoords(300, 880)));
5197
5198 // Touch down at the same location and ensure a hover exit is synthesized.
5199 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
5200 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5201 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5202 WithRawCoords(300, 880)));
5203 secondWindow->consumeMotionEvent(
5204 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5205 secondWindow->assertNoEvents();
5206 firstWindow->assertNoEvents();
5207}
5208
Prabir Pradhan453ae732023-10-13 14:30:14 +00005209// Same as above, but while the window is being mirrored.
5210TEST_F(InputDispatcherDisplayProjectionTest,
5211 SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) {
5212 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5213
5214 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5215 ui::Transform secondDisplayTransform;
5216 secondDisplayTransform.set(matrix);
5217 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5218
5219 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5220 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5221 addWindow(secondWindowClone);
5222
5223 // Send hover move to the second window, and ensure it shows up as hover enter.
5224 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
5225 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5226 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5227 WithCoords(100, 80), WithRawCoords(300, 880)));
5228
5229 // Touch down at the same location and ensure a hover exit is synthesized for the correct
5230 // display.
5231 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
5232 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5233 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5234 WithRawCoords(300, 880)));
5235 secondWindow->consumeMotionEvent(
5236 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5237 secondWindow->assertNoEvents();
5238 firstWindow->assertNoEvents();
5239}
5240
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005241TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) {
5242 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5243
5244 // Send hover enter to second window
5245 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
5246 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5247 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5248 WithCoords(100, 80), WithRawCoords(300, 880)));
5249
5250 mDispatcher->cancelCurrentTouch();
5251
5252 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5253 WithRawCoords(300, 880)));
5254 secondWindow->assertNoEvents();
5255 firstWindow->assertNoEvents();
5256}
5257
Prabir Pradhan453ae732023-10-13 14:30:14 +00005258// Same as above, but while the window is being mirrored.
Prabir Pradhan16463382023-10-12 23:03:19 +00005259TEST_F(InputDispatcherDisplayProjectionTest,
5260 SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) {
5261 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5262
5263 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5264 ui::Transform secondDisplayTransform;
5265 secondDisplayTransform.set(matrix);
5266 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5267
5268 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5269 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5270 addWindow(secondWindowClone);
5271
5272 // Send hover enter to second window
5273 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
5274 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5275 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5276 WithCoords(100, 80), WithRawCoords(300, 880),
5277 WithDisplayId(ADISPLAY_ID_DEFAULT)));
5278
5279 mDispatcher->cancelCurrentTouch();
5280
5281 // Ensure the cancelation happens with the correct displayId and the correct coordinates.
5282 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5283 WithRawCoords(300, 880),
5284 WithDisplayId(ADISPLAY_ID_DEFAULT)));
5285 secondWindow->assertNoEvents();
5286 firstWindow->assertNoEvents();
5287}
5288
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005289/** Ensure consistent behavior of InputDispatcher in all orientations. */
5290class InputDispatcherDisplayOrientationFixture
5291 : public InputDispatcherDisplayProjectionTest,
5292 public ::testing::WithParamInterface<ui::Rotation> {};
5293
5294// This test verifies the touchable region of a window for all rotations of the display by tapping
5295// in different locations on the display, specifically points close to the four corners of a
5296// window.
5297TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
5298 constexpr static int32_t displayWidth = 400;
5299 constexpr static int32_t displayHeight = 800;
5300
5301 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5302
5303 const auto rotation = GetParam();
5304
5305 // Set up the display with the specified rotation.
5306 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
5307 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
5308 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
5309 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
5310 logicalDisplayWidth, logicalDisplayHeight);
5311 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5312
5313 // Create a window with its bounds determined in the logical display.
5314 const Rect frameInLogicalDisplay(100, 100, 200, 300);
5315 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
5316 sp<FakeWindowHandle> window =
5317 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5318 window->setFrame(frameInDisplay, displayTransform);
5319 addWindow(window);
5320
5321 // The following points in logical display space should be inside the window.
5322 static const std::array<vec2, 4> insidePoints{
5323 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
5324 for (const auto pointInsideWindow : insidePoints) {
5325 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
5326 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005327 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5328 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5329 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005330 window->consumeMotionDown();
5331
Prabir Pradhan678438e2023-04-13 19:32:51 +00005332 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5333 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5334 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005335 window->consumeMotionUp();
5336 }
5337
5338 // The following points in logical display space should be outside the window.
5339 static const std::array<vec2, 5> outsidePoints{
5340 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
5341 for (const auto pointOutsideWindow : outsidePoints) {
5342 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
5343 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005344 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5345 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5346 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005347
Prabir Pradhan678438e2023-04-13 19:32:51 +00005348 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5349 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5350 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005351 }
5352 window->assertNoEvents();
5353}
5354
5355// Run the precision tests for all rotations.
5356INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
5357 InputDispatcherDisplayOrientationFixture,
5358 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
5359 ui::ROTATION_270),
5360 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
5361 return ftl::enum_string(testParamInfo.param);
5362 });
5363
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005364using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
5365 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005366
5367class TransferTouchFixture : public InputDispatcherTest,
5368 public ::testing::WithParamInterface<TransferFunction> {};
5369
5370TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07005371 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005372
5373 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005374 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005375 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5376 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005377 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005378 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005379 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5380 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005381 sp<FakeWindowHandle> wallpaper =
5382 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
5383 wallpaper->setIsWallpaper(true);
Prabir Pradhan65455c72024-02-13 21:46:41 +00005384 // Add the windows to the dispatcher, and ensure the first window is focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005385 mDispatcher->onWindowInfosChanged(
5386 {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0});
Prabir Pradhan65455c72024-02-13 21:46:41 +00005387 setFocusedWindow(firstWindow);
5388 firstWindow->consumeFocusEvent(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005389
5390 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005391 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5392 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005393
Svet Ganov5d3bc372020-01-26 23:11:07 -08005394 // Only the first window should get the down event
5395 firstWindow->consumeMotionDown();
5396 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005397 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Prabir Pradhan65455c72024-02-13 21:46:41 +00005398 // Dispatcher reports pointer down outside focus for the wallpaper
5399 mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken());
Svet Ganov5d3bc372020-01-26 23:11:07 -08005400
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005401 // Transfer touch to the second window
5402 TransferFunction f = GetParam();
5403 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5404 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005405 // The first window gets cancel and the second gets down
5406 firstWindow->consumeMotionCancel();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005407 secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005408 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Prabir Pradhan65455c72024-02-13 21:46:41 +00005409 // There should not be any changes to the focused window when transferring touch
5410 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled());
Svet Ganov5d3bc372020-01-26 23:11:07 -08005411
5412 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005413 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5414 ADISPLAY_ID_DEFAULT));
Prabir Pradhan65455c72024-02-13 21:46:41 +00005415 // The first window gets no events and the second gets up
Svet Ganov5d3bc372020-01-26 23:11:07 -08005416 firstWindow->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005417 secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005418 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005419}
5420
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005421/**
Prabir Pradhan367f3432024-02-13 23:05:58 +00005422 * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take
5423 * touch from. When we have spy windows, there are several windows to choose from: either spy, or
5424 * the 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005425 * natural to the user.
5426 * In this test, we are sending a pointer to both spy window and first window. We then try to
5427 * transfer touch to the second window. The dispatcher should identify the first window as the
5428 * one that should lose the gesture, and therefore the action should be to move the gesture from
5429 * the first window to the second.
Prabir Pradhan367f3432024-02-13 23:05:58 +00005430 * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid
5431 * to test the other API, as well.
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005432 */
5433TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
5434 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5435
5436 // Create a couple of windows + a spy window
5437 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005438 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005439 spyWindow->setTrustedOverlay(true);
5440 spyWindow->setSpy(true);
5441 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005442 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005443 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005444 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005445
5446 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005447 mDispatcher->onWindowInfosChanged(
5448 {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005449
5450 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005451 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5452 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005453 // Only the first window and spy should get the down event
5454 spyWindow->consumeMotionDown();
5455 firstWindow->consumeMotionDown();
5456
5457 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
Prabir Pradhan367f3432024-02-13 23:05:58 +00005458 // if f === 'transferTouchGesture'.
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005459 TransferFunction f = GetParam();
5460 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5461 ASSERT_TRUE(success);
5462 // The first window gets cancel and the second gets down
5463 firstWindow->consumeMotionCancel();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005464 secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005465
5466 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005467 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5468 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005469 // The first window gets no events and the second+spy get up
5470 firstWindow->assertNoEvents();
5471 spyWindow->consumeMotionUp();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005472 secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005473}
5474
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005475TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005476 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005477
5478 PointF touchPoint = {10, 10};
5479
5480 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005481 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005482 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5483 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005484 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005485 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005486 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5487 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005488 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005489
5490 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005491 mDispatcher->onWindowInfosChanged(
5492 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005493
5494 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005495 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5496 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5497 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005498 // Only the first window should get the down event
5499 firstWindow->consumeMotionDown();
5500 secondWindow->assertNoEvents();
5501
5502 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005503 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5504 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005505 // Only the first window should get the pointer down event
5506 firstWindow->consumeMotionPointerDown(1);
5507 secondWindow->assertNoEvents();
5508
5509 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005510 TransferFunction f = GetParam();
5511 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5512 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005513 // The first window gets cancel and the second gets down and pointer down
5514 firstWindow->consumeMotionCancel();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005515 secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
5516 secondWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT,
5517 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005518
5519 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005520 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5521 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005522 // The first window gets nothing and the second gets pointer up
5523 firstWindow->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005524 secondWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT,
5525 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005526
5527 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005528 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5529 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005530 // The first window gets nothing and the second gets up
5531 firstWindow->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005532 secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005533}
5534
Arthur Hungc539dbb2022-12-08 07:45:36 +00005535TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
5536 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5537
5538 // Create a couple of windows
5539 sp<FakeWindowHandle> firstWindow =
5540 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5541 ADISPLAY_ID_DEFAULT);
5542 firstWindow->setDupTouchToWallpaper(true);
5543 sp<FakeWindowHandle> secondWindow =
5544 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5545 ADISPLAY_ID_DEFAULT);
5546 secondWindow->setDupTouchToWallpaper(true);
5547
5548 sp<FakeWindowHandle> wallpaper1 =
5549 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
5550 wallpaper1->setIsWallpaper(true);
5551
5552 sp<FakeWindowHandle> wallpaper2 =
5553 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
5554 wallpaper2->setIsWallpaper(true);
5555 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005556 mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(),
5557 *secondWindow->getInfo(), *wallpaper2->getInfo()},
5558 {},
5559 0,
5560 0});
Arthur Hungc539dbb2022-12-08 07:45:36 +00005561
5562 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005563 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5564 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005565
5566 // Only the first window should get the down event
5567 firstWindow->consumeMotionDown();
5568 secondWindow->assertNoEvents();
5569 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5570 wallpaper2->assertNoEvents();
5571
5572 // Transfer touch focus to the second window
5573 TransferFunction f = GetParam();
5574 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5575 ASSERT_TRUE(success);
5576
5577 // The first window gets cancel and the second gets down
5578 firstWindow->consumeMotionCancel();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005579 secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005580 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Prabir Pradhan65455c72024-02-13 21:46:41 +00005581 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5582 expectedWallpaperFlags | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005583
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();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005589 secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005590 wallpaper1->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005591 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT,
5592 expectedWallpaperFlags | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005593}
5594
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005595// For the cases of single pointer touch and two pointers non-split touch, the api's
Prabir Pradhan367f3432024-02-13 23:05:58 +00005596// 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005597// for the case where there are multiple pointers split across several windows.
Prabir Pradhan367f3432024-02-13 23:05:58 +00005598INSTANTIATE_TEST_SUITE_P(
5599 InputDispatcherTransferFunctionTests, TransferTouchFixture,
5600 ::testing::Values(
5601 [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/,
5602 sp<IBinder> destChannelToken) {
5603 return dispatcher->transferTouchOnDisplay(destChannelToken,
5604 ADISPLAY_ID_DEFAULT);
5605 },
5606 [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from,
5607 sp<IBinder> to) {
5608 return dispatcher->transferTouchGesture(from, to,
5609 /*isDragAndDrop=*/false);
5610 }));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005611
Prabir Pradhan367f3432024-02-13 23:05:58 +00005612TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005613 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005614
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005615 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005616 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5617 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005618 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005619
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005620 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005621 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5622 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005623 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005624
5625 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005626 mDispatcher->onWindowInfosChanged(
5627 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005628
5629 PointF pointInFirst = {300, 200};
5630 PointF pointInSecond = {300, 600};
5631
5632 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005633 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5634 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5635 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005636 // Only the first window should get the down event
5637 firstWindow->consumeMotionDown();
5638 secondWindow->assertNoEvents();
5639
5640 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005641 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5642 ADISPLAY_ID_DEFAULT,
5643 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005644 // The first window gets a move and the second a down
5645 firstWindow->consumeMotionMove();
5646 secondWindow->consumeMotionDown();
5647
Prabir Pradhan367f3432024-02-13 23:05:58 +00005648 // Transfer touch to the second window
5649 mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken());
Svet Ganov5d3bc372020-01-26 23:11:07 -08005650 // The first window gets cancel and the new gets pointer down (it already saw down)
5651 firstWindow->consumeMotionCancel();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005652 secondWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT,
5653 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005654
5655 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005656 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5657 ADISPLAY_ID_DEFAULT,
5658 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005659 // The first window gets nothing and the second gets pointer up
5660 firstWindow->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005661 secondWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT,
5662 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005663
5664 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005665 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5666 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005667 // The first window gets nothing and the second gets up
5668 firstWindow->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005669 secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005670}
5671
Prabir Pradhan367f3432024-02-13 23:05:58 +00005672// Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api.
5673// Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows
5674// receiving touch is not supported, so the touch should continue on those windows and the
5675// transferred-to window should get nothing.
5676TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005677 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5678
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005679 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005680 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5681 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005682 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005683
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005684 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005685 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5686 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005687 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005688
5689 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005690 mDispatcher->onWindowInfosChanged(
5691 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005692
5693 PointF pointInFirst = {300, 200};
5694 PointF pointInSecond = {300, 600};
5695
5696 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005697 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5698 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5699 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005700 // Only the first window should get the down event
5701 firstWindow->consumeMotionDown();
5702 secondWindow->assertNoEvents();
5703
5704 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005705 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5706 ADISPLAY_ID_DEFAULT,
5707 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005708 // The first window gets a move and the second a down
5709 firstWindow->consumeMotionMove();
5710 secondWindow->consumeMotionDown();
5711
5712 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005713 const bool transferred =
Prabir Pradhan367f3432024-02-13 23:05:58 +00005714 mDispatcher->transferTouchOnDisplay(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
5715 // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005716 ASSERT_FALSE(transferred);
5717 firstWindow->assertNoEvents();
5718 secondWindow->assertNoEvents();
5719
5720 // The rest of the dispatch should proceed as normal
5721 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005722 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5723 ADISPLAY_ID_DEFAULT,
5724 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005725 // The first window gets MOVE and the second gets pointer up
5726 firstWindow->consumeMotionMove();
5727 secondWindow->consumeMotionUp();
5728
5729 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005730 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5731 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005732 // The first window gets nothing and the second gets up
5733 firstWindow->consumeMotionUp();
5734 secondWindow->assertNoEvents();
5735}
5736
Arthur Hungabbb9d82021-09-01 14:52:30 +00005737// This case will create two windows and one mirrored window on the default display and mirror
Prabir Pradhan367f3432024-02-13 23:05:58 +00005738// two windows on the second display. It will test if 'transferTouchGesture' works fine if we put
Arthur Hungabbb9d82021-09-01 14:52:30 +00005739// the windows info of second display before default display.
Prabir Pradhan367f3432024-02-13 23:05:58 +00005740TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00005741 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5742 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005743 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005744 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005745 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005746 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005747 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005748
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005749 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005750 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005751
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005752 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005753 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005754
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005755 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005756 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005757
5758 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005759 mDispatcher->onWindowInfosChanged(
5760 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5761 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5762 *secondWindowInPrimary->getInfo()},
5763 {},
5764 0,
5765 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005766
5767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005768 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005769 {50, 50}))
5770 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5771
5772 // Window should receive motion event.
5773 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5774
Prabir Pradhan367f3432024-02-13 23:05:58 +00005775 // Transfer touch
5776 ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(),
5777 secondWindowInPrimary->getToken()));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005778 // The first window gets cancel.
5779 firstWindowInPrimary->consumeMotionCancel();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005780 secondWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5781 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005782
5783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005784 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005785 ADISPLAY_ID_DEFAULT, {150, 50}))
5786 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5787 firstWindowInPrimary->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005788 secondWindowInPrimary->consumeMotionMove(ADISPLAY_ID_DEFAULT,
5789 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005790
5791 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005792 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005793 {150, 50}))
5794 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5795 firstWindowInPrimary->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005796 secondWindowInPrimary->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005797}
5798
Prabir Pradhan367f3432024-02-13 23:05:58 +00005799// Same as TransferTouch_CloneSurface, but this touch on the secondary display and use
5800// 'transferTouchOnDisplay' api.
5801TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00005802 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5803 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005804 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005805 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005806 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005807 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005808 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005809
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005810 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005811 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005812
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005813 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005814 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005815
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005816 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005817 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005818
5819 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005820 mDispatcher->onWindowInfosChanged(
5821 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5822 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5823 *secondWindowInPrimary->getInfo()},
5824 {},
5825 0,
5826 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005827
5828 // Touch on second display.
5829 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005830 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
5831 {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005832 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5833
5834 // Window should receive motion event.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005835 firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005836
5837 // Transfer touch focus
Prabir Pradhan367f3432024-02-13 23:05:58 +00005838 ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(),
5839 SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005840
5841 // The first window gets cancel.
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005842 firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
Prabir Pradhan65455c72024-02-13 21:46:41 +00005843 secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID,
5844 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005845
5846 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005847 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005848 SECOND_DISPLAY_ID, {150, 50}))
5849 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005850 firstWindowInSecondary->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005851 secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID,
5852 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005853
5854 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005855 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005856 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005857 firstWindowInSecondary->assertNoEvents();
Prabir Pradhan65455c72024-02-13 21:46:41 +00005858 secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005859}
5860
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005861TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005862 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005863 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5864 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005865
Vishnu Nair47074b82020-08-14 11:54:47 -07005866 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005867 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005868 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005869
5870 window->consumeFocusEvent(true);
5871
Prabir Pradhan678438e2023-04-13 19:32:51 +00005872 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005873
5874 // Window should receive key down event.
5875 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00005876
5877 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005878 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005879 mFakePolicy->assertUserActivityPoked();
5880}
5881
5882TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
5883 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5884 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5885 "Fake Window", ADISPLAY_ID_DEFAULT);
5886
5887 window->setDisableUserActivity(true);
5888 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005889 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005890 setFocusedWindow(window);
5891
5892 window->consumeFocusEvent(true);
5893
5894 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5895
5896 // Window should receive key down event.
5897 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5898
5899 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005900 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005901 mFakePolicy->assertUserActivityNotPoked();
5902}
5903
5904TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
5905 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5906 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5907 "Fake Window", ADISPLAY_ID_DEFAULT);
5908
5909 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005910 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005911 setFocusedWindow(window);
5912
5913 window->consumeFocusEvent(true);
5914
5915 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5916 mDispatcher->waitForIdle();
5917
5918 // System key is not passed down
5919 window->assertNoEvents();
5920
5921 // Should have poked user activity
5922 mFakePolicy->assertUserActivityPoked();
5923}
5924
5925TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
5926 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5927 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5928 "Fake Window", ADISPLAY_ID_DEFAULT);
5929
5930 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005931 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005932 setFocusedWindow(window);
5933
5934 window->consumeFocusEvent(true);
5935
5936 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5937 mDispatcher->waitForIdle();
5938
5939 // System key is not passed down
5940 window->assertNoEvents();
5941
5942 // Should have poked user activity
5943 mFakePolicy->assertUserActivityPoked();
5944}
5945
5946TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
5947 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5948 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5949 "Fake Window", ADISPLAY_ID_DEFAULT);
5950
5951 window->setDisableUserActivity(true);
5952 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005953 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005954 setFocusedWindow(window);
5955
5956 window->consumeFocusEvent(true);
5957
5958 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5959 mDispatcher->waitForIdle();
5960
5961 // System key is not passed down
5962 window->assertNoEvents();
5963
5964 // Should have poked user activity
5965 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005966}
5967
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005968TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
5969 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5970 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5971 "Fake Window", ADISPLAY_ID_DEFAULT);
5972
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005973 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005974
5975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005976 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005977 ADISPLAY_ID_DEFAULT, {100, 100}))
5978 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5979
5980 window->consumeMotionEvent(
5981 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
5982
5983 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005984 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005985 mFakePolicy->assertUserActivityPoked();
5986}
5987
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005988TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005989 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005990 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5991 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005992
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005993 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005994
Prabir Pradhan678438e2023-04-13 19:32:51 +00005995 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005996 mDispatcher->waitForIdle();
5997
5998 window->assertNoEvents();
5999}
6000
6001// If a window is touchable, but does not have focus, it should receive motion events, but not keys
6002TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07006003 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006004 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6005 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006006
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006007 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006008
6009 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00006010 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006011 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00006012 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6013 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006014
6015 // Window should receive only the motion event
6016 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6017 window->assertNoEvents(); // Key event or focus event will not be received
6018}
6019
arthurhungea3f4fc2020-12-21 23:18:53 +08006020TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
6021 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6022
arthurhungea3f4fc2020-12-21 23:18:53 +08006023 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006024 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
6025 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006026 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08006027
arthurhungea3f4fc2020-12-21 23:18:53 +08006028 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006029 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
6030 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006031 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08006032
6033 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006034 mDispatcher->onWindowInfosChanged(
6035 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
arthurhungea3f4fc2020-12-21 23:18:53 +08006036
6037 PointF pointInFirst = {300, 200};
6038 PointF pointInSecond = {300, 600};
6039
6040 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006041 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6042 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6043 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006044 // Only the first window should get the down event
6045 firstWindow->consumeMotionDown();
6046 secondWindow->assertNoEvents();
6047
6048 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006049 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6050 ADISPLAY_ID_DEFAULT,
6051 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006052 // The first window gets a move and the second a down
6053 firstWindow->consumeMotionMove();
6054 secondWindow->consumeMotionDown();
6055
6056 // Send pointer cancel to the second window
6057 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006058 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08006059 {pointInFirst, pointInSecond});
6060 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00006061 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08006062 // The first window gets move and the second gets cancel.
6063 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6064 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6065
6066 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006067 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6068 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08006069 // The first window gets up and the second gets nothing.
6070 firstWindow->consumeMotionUp();
6071 secondWindow->assertNoEvents();
6072}
6073
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006074TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
6075 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6076
6077 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006078 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006079 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006080 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
6081 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
6082 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
6083
Harry Cutts33476232023-01-30 19:57:29 +00006084 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006085 window->assertNoEvents();
6086 mDispatcher->waitForIdle();
6087}
6088
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006089using InputDispatcherMonitorTest = InputDispatcherTest;
6090
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006091/**
6092 * Two entities that receive touch: A window, and a global monitor.
6093 * The touch goes to the window, and then the window disappears.
6094 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
6095 * for the monitor, as well.
6096 * 1. foregroundWindow
6097 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
6098 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006099TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006100 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6101 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006102 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006103
Prabir Pradhanfb549072023-10-05 19:17:36 +00006104 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006105
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006106 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006107 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006108 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006109 {100, 200}))
6110 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6111
6112 // Both the foreground window and the global monitor should receive the touch down
6113 window->consumeMotionDown();
6114 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6115
6116 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006117 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006118 ADISPLAY_ID_DEFAULT, {110, 200}))
6119 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6120
6121 window->consumeMotionMove();
6122 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
6123
6124 // Now the foreground window goes away
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006125 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006126 window->consumeMotionCancel();
6127 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
6128
6129 // If more events come in, there will be no more foreground window to send them to. This will
6130 // cause a cancel for the monitor, as well.
6131 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006132 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006133 ADISPLAY_ID_DEFAULT, {120, 200}))
6134 << "Injection should fail because the window was removed";
6135 window->assertNoEvents();
6136 // Global monitor now gets the cancel
6137 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6138}
6139
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006140TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07006141 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006142 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6143 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006144 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006145
Prabir Pradhanfb549072023-10-05 19:17:36 +00006146 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006147
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006148 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006149 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006150 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00006151 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08006152 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006153}
6154
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006155TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
Prabir Pradhanfb549072023-10-05 19:17:36 +00006156 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006157
Chris Yea209fde2020-07-22 13:54:51 -07006158 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006159 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6160 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006161 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006162
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006163 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006164 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006165 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08006166 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006167 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006168
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006169 // Pilfer pointers from the monitor.
6170 // This should not do anything and the window should continue to receive events.
6171 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00006172
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006173 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006174 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006175 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006176 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006177
6178 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
6179 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006180}
6181
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006182TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07006183 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006184 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6185 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006186 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Evan Rosky84f07f02021-04-16 10:42:42 -07006187 window->setWindowOffset(20, 40);
6188 window->setWindowTransform(0, 1, -1, 0);
6189
Prabir Pradhanfb549072023-10-05 19:17:36 +00006190 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07006191
6192 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006193 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Evan Rosky84f07f02021-04-16 10:42:42 -07006194 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6195 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006196 std::unique_ptr<MotionEvent> event = monitor.consumeMotion();
6197 ASSERT_NE(nullptr, event);
Evan Rosky84f07f02021-04-16 10:42:42 -07006198 // Even though window has transform, gesture monitor must not.
6199 ASSERT_EQ(ui::Transform(), event->getTransform());
6200}
6201
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006202TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00006203 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhanfb549072023-10-05 19:17:36 +00006204 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00006205
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006206 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006207 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006208 << "Injection should fail if there is a monitor, but no touchable window";
6209 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00006210}
6211
Linnan Lid8150952024-01-26 18:07:17 +00006212/**
6213 * Two displays
6214 * The first monitor has a foreground window, a monitor
6215 * The second window has only one monitor.
6216 * We first inject a Down event into the first display, this injection should succeed and both
6217 * the foreground window and monitor should receive a down event, then inject a Down event into
6218 * the second display as well, this injection should fail, at this point, the first display
6219 * window and monitor should not receive a cancel or any other event.
6220 * Continue to inject Move and UP events to the first display, the events should be received
6221 * normally by the foreground window and monitor.
6222 */
6223TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) {
6224 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6225 sp<FakeWindowHandle> window =
6226 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6227
6228 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6229 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6230
6231 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6233 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6234 {100, 200}))
6235 << "The down event injected into the first display should succeed";
6236
6237 window->consumeMotionDown();
6238 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006239
6240 ASSERT_EQ(InputEventInjectionResult::FAILED,
6241 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6242 {100, 200}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006243 << "The down event injected into the second display should fail since there's no "
6244 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006245
6246 // Continue to inject event to first display.
6247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6248 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6249 ADISPLAY_ID_DEFAULT, {110, 220}))
6250 << "The move event injected into the first display should succeed";
6251
6252 window->consumeMotionMove();
6253 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006254
6255 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6256 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6257 {110, 220}))
6258 << "The up event injected into the first display should succeed";
6259
6260 window->consumeMotionUp();
6261 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006262
6263 window->assertNoEvents();
6264 monitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00006265 secondMonitor.assertNoEvents();
6266}
6267
6268/**
6269 * Two displays
6270 * There is a monitor and foreground window on each display.
6271 * First, we inject down events into each of the two displays, at this point, the foreground windows
6272 * and monitors on both displays should receive down events.
6273 * At this point, the foreground window of the second display goes away, the gone window should
6274 * receive the cancel event, and the other windows and monitors should not receive any events.
6275 * Inject a move event into the second display. At this point, the injection should fail because
6276 * the second display no longer has a foreground window. At this point, the monitor on the second
6277 * display should receive a cancel event, and any windows or monitors on the first display should
6278 * not receive any events, and any subsequent injection of events into the second display should
6279 * also fail.
6280 * Continue to inject events into the first display, and the events should all be injected
6281 * successfully and received normally.
6282 */
6283TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) {
6284 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6285 sp<FakeWindowHandle> window =
6286 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6287 sp<FakeWindowHandle> secondWindow =
6288 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground",
6289 SECOND_DISPLAY_ID);
6290
6291 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6292 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6293
6294 // There is a foreground window on both displays.
6295 mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
6296 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6297 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6298 {100, 200}))
6299 << "The down event injected into the first display should succeed";
6300
6301 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6302 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006303
6304 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6305 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6306 {100, 200}))
6307 << "The down event injected into the second display should succeed";
6308
Linnan Lid8150952024-01-26 18:07:17 +00006309 secondWindow->consumeMotionDown(SECOND_DISPLAY_ID);
6310 secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID);
6311
6312 // Now second window is gone away.
6313 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6314
6315 // The gone window should receive a cancel, and the monitor on the second display should not
6316 // receive any events.
Linnan Lid8150952024-01-26 18:07:17 +00006317 secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID);
6318 secondMonitor.assertNoEvents();
6319
6320 ASSERT_EQ(InputEventInjectionResult::FAILED,
6321 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6322 SECOND_DISPLAY_ID, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006323 << "The move event injected into the second display should fail because there's no "
6324 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006325 // Now the monitor on the second display should receive a cancel event.
6326 secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID);
Linnan Lid8150952024-01-26 18:07:17 +00006327
6328 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6329 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6330 ADISPLAY_ID_DEFAULT, {110, 200}))
6331 << "The move event injected into the first display should succeed";
6332
6333 window->consumeMotionMove();
6334 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006335
6336 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006337 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6338 {110, 220}))
6339 << "The up event injected into the second display should fail because there's no "
6340 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006341
6342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6343 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6344 {110, 220}))
6345 << "The up event injected into the first display should succeed";
6346
6347 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6348 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006349
Linnan Lid8150952024-01-26 18:07:17 +00006350 window->assertNoEvents();
6351 monitor.assertNoEvents();
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006352 secondWindow->assertNoEvents();
6353 secondMonitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00006354}
6355
6356/**
6357 * One display with transform
6358 * There is a foreground window and a monitor on the display
6359 * Inject down event and move event sequentially, the foreground window and monitor can receive down
6360 * event and move event, then let the foreground window go away, the foreground window receives
6361 * cancel event, inject move event again, the monitor receives cancel event, all the events received
6362 * by the monitor should be with the same transform as the display
6363 */
6364TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) {
6365 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6366 sp<FakeWindowHandle> window =
6367 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6368 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6369
6370 ui::Transform transform;
6371 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6372
6373 gui::DisplayInfo displayInfo;
6374 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6375 displayInfo.transform = transform;
6376
6377 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
6378
6379 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6380 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6381 {100, 200}))
6382 << "The down event injected should succeed";
6383
6384 window->consumeMotionDown();
6385 std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion();
6386 EXPECT_EQ(transform, downMotionEvent->getTransform());
6387 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction());
6388
6389 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6390 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6391 ADISPLAY_ID_DEFAULT, {110, 220}))
6392 << "The move event injected should succeed";
6393
6394 window->consumeMotionMove();
6395 std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion();
6396 EXPECT_EQ(transform, moveMotionEvent->getTransform());
6397 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction());
6398
6399 // Let foreground window gone
6400 mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0});
6401
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006402 // Foreground window should receive a cancel event, but not the monitor.
Linnan Lid8150952024-01-26 18:07:17 +00006403 window->consumeMotionCancel();
Linnan Lid8150952024-01-26 18:07:17 +00006404
6405 ASSERT_EQ(InputEventInjectionResult::FAILED,
6406 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6407 ADISPLAY_ID_DEFAULT, {110, 220}))
6408 << "The move event injected should failed";
6409 // Now foreground should not receive any events, but monitor should receive a cancel event
6410 // with transform that same as display's display.
Linnan Lid8150952024-01-26 18:07:17 +00006411 std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion();
6412 EXPECT_EQ(transform, cancelMotionEvent->getTransform());
6413 EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId());
6414 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction());
6415
6416 // Other event inject to this display should fail.
6417 ASSERT_EQ(InputEventInjectionResult::FAILED,
6418 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6419 ADISPLAY_ID_DEFAULT, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006420 << "The up event injected should fail because the touched window was removed";
Linnan Lid8150952024-01-26 18:07:17 +00006421 window->assertNoEvents();
6422 monitor.assertNoEvents();
6423}
6424
chaviw81e2bb92019-12-18 15:03:51 -08006425TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006426 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006427 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6428 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08006429
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006430 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
chaviw81e2bb92019-12-18 15:03:51 -08006431
6432 NotifyMotionArgs motionArgs =
6433 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6434 ADISPLAY_ID_DEFAULT);
6435
Prabir Pradhan678438e2023-04-13 19:32:51 +00006436 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08006437 // Window should receive motion down event.
6438 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6439
6440 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08006441 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08006442 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6443 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
6444 motionArgs.pointerCoords[0].getX() - 10);
6445
Prabir Pradhan678438e2023-04-13 19:32:51 +00006446 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00006447 window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08006448}
6449
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006450/**
6451 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
6452 * the device default right away. In the test scenario, we check both the default value,
6453 * and the action of enabling / disabling.
6454 */
6455TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07006456 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006457 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6458 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08006459 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006460
6461 // Set focused application.
6462 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006463 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006464
6465 SCOPED_TRACE("Check default value of touch mode");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006466 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006467 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006468 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006469
6470 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07006471 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006472 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00006473 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006474
6475 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08006476 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00006477 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006478 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07006479 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006480 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006481 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006482 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006483
6484 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07006485 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006486 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00006487 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006488
6489 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08006490 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00006491 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006492 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07006493 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006494 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006495 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006496 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006497
6498 window->assertNoEvents();
6499}
6500
Gang Wange9087892020-01-07 12:17:14 -05006501TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006502 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006503 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6504 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05006505
6506 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006507 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05006508
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006509 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006510 setFocusedWindow(window);
6511
Harry Cutts33476232023-01-30 19:57:29 +00006512 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05006513
Prabir Pradhan678438e2023-04-13 19:32:51 +00006514 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
6515 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05006516
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006517 std::unique_ptr<KeyEvent> event = window->consumeKey();
6518 ASSERT_NE(event, nullptr);
6519 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Gang Wange9087892020-01-07 12:17:14 -05006520 ASSERT_NE(verified, nullptr);
6521 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
6522
6523 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
6524 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
6525 ASSERT_EQ(keyArgs.source, verified->source);
6526 ASSERT_EQ(keyArgs.displayId, verified->displayId);
6527
6528 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
6529
6530 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05006531 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006532 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05006533 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
6534 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
6535 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
6536 ASSERT_EQ(0, verifiedKey.repeatCount);
6537}
6538
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006539TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006540 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006541 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6542 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006543
6544 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6545
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006546 ui::Transform transform;
6547 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6548
6549 gui::DisplayInfo displayInfo;
6550 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6551 displayInfo.transform = transform;
6552
Patrick Williamsd828f302023-04-28 17:52:08 -05006553 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006554
Prabir Pradhan678438e2023-04-13 19:32:51 +00006555 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006556 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6557 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006558 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006559
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006560 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
6561 ASSERT_NE(nullptr, event);
6562 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006563 ASSERT_NE(verified, nullptr);
6564 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
6565
6566 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
6567 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
6568 EXPECT_EQ(motionArgs.source, verified->source);
6569 EXPECT_EQ(motionArgs.displayId, verified->displayId);
6570
6571 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
6572
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006573 const vec2 rawXY =
6574 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
6575 motionArgs.pointerCoords[0].getXYValue());
6576 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
6577 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006578 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006579 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006580 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006581 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
6582 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
6583}
6584
chaviw09c8d2d2020-08-24 15:48:26 -07006585/**
6586 * Ensure that separate calls to sign the same data are generating the same key.
6587 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
6588 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
6589 * tests.
6590 */
6591TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
6592 KeyEvent event = getTestKeyEvent();
6593 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6594
6595 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
6596 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
6597 ASSERT_EQ(hmac1, hmac2);
6598}
6599
6600/**
6601 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
6602 */
6603TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
6604 KeyEvent event = getTestKeyEvent();
6605 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6606 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
6607
6608 verifiedEvent.deviceId += 1;
6609 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6610
6611 verifiedEvent.source += 1;
6612 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6613
6614 verifiedEvent.eventTimeNanos += 1;
6615 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6616
6617 verifiedEvent.displayId += 1;
6618 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6619
6620 verifiedEvent.action += 1;
6621 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6622
6623 verifiedEvent.downTimeNanos += 1;
6624 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6625
6626 verifiedEvent.flags += 1;
6627 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6628
6629 verifiedEvent.keyCode += 1;
6630 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6631
6632 verifiedEvent.scanCode += 1;
6633 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6634
6635 verifiedEvent.metaState += 1;
6636 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6637
6638 verifiedEvent.repeatCount += 1;
6639 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6640}
6641
Vishnu Nair958da932020-08-21 17:12:37 -07006642TEST_F(InputDispatcherTest, SetFocusedWindow) {
6643 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6644 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006645 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006646 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006647 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006648 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6649
6650 // Top window is also focusable but is not granted focus.
6651 windowTop->setFocusable(true);
6652 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006653 mDispatcher->onWindowInfosChanged(
6654 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006655 setFocusedWindow(windowSecond);
6656
6657 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006659 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006660
6661 // Focused window should receive event.
6662 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6663 windowTop->assertNoEvents();
6664}
6665
6666TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
6667 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6668 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006669 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006670 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6671
6672 window->setFocusable(true);
6673 // Release channel for window is no longer valid.
6674 window->releaseChannel();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006675 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006676 setFocusedWindow(window);
6677
6678 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006679 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006680
6681 // window channel is invalid, so it should not receive any input event.
6682 window->assertNoEvents();
6683}
6684
6685TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
6686 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6687 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006688 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006689 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07006690 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6691
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006692 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006693 setFocusedWindow(window);
6694
6695 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006696 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006697
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006698 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07006699 window->assertNoEvents();
6700}
6701
6702TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
6703 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6704 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006705 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006706 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006707 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006708 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6709
6710 windowTop->setFocusable(true);
6711 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006712 mDispatcher->onWindowInfosChanged(
6713 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006714 setFocusedWindow(windowTop);
6715 windowTop->consumeFocusEvent(true);
6716
Chavi Weingarten847e8512023-03-29 00:26:09 +00006717 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006718 mDispatcher->onWindowInfosChanged(
6719 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006720 windowSecond->consumeFocusEvent(true);
6721 windowTop->consumeFocusEvent(false);
6722
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006724 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006725
6726 // Focused window should receive event.
6727 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6728}
6729
Chavi Weingarten847e8512023-03-29 00:26:09 +00006730TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07006731 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6732 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006733 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006734 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006735 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006736 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6737
6738 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00006739 windowSecond->setFocusable(false);
6740 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006741 mDispatcher->onWindowInfosChanged(
6742 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Chavi Weingarten847e8512023-03-29 00:26:09 +00006743 setFocusedWindow(windowTop);
6744 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07006745
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Chavi Weingarten847e8512023-03-29 00:26:09 +00006747 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006748
6749 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00006750 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07006751 windowSecond->assertNoEvents();
6752}
6753
6754TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
6755 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6756 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006757 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006758 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006759 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
6760 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006761 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6762
6763 window->setFocusable(true);
6764 previousFocusedWindow->setFocusable(true);
6765 window->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006766 mDispatcher->onWindowInfosChanged(
6767 {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006768 setFocusedWindow(previousFocusedWindow);
6769 previousFocusedWindow->consumeFocusEvent(true);
6770
6771 // Requesting focus on invisible window takes focus from currently focused window.
6772 setFocusedWindow(window);
6773 previousFocusedWindow->consumeFocusEvent(false);
6774
6775 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006776 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006777 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
6778 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006779
6780 // Window does not get focus event or key down.
6781 window->assertNoEvents();
6782
6783 // Window becomes visible.
6784 window->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006785 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006786
6787 // Window receives focus event.
6788 window->consumeFocusEvent(true);
6789 // Focused window receives key down.
6790 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6791}
6792
Vishnu Nair599f1412021-06-21 10:39:58 -07006793TEST_F(InputDispatcherTest, DisplayRemoved) {
6794 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6795 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006796 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07006797 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6798
6799 // window is granted focus.
6800 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006801 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair599f1412021-06-21 10:39:58 -07006802 setFocusedWindow(window);
6803 window->consumeFocusEvent(true);
6804
6805 // When a display is removed window loses focus.
6806 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
6807 window->consumeFocusEvent(false);
6808}
6809
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006810/**
6811 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
6812 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
6813 * of the 'slipperyEnterWindow'.
6814 *
6815 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
6816 * a way so that the touched location is no longer covered by the top window.
6817 *
6818 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
6819 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
6820 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
6821 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
6822 * with ACTION_DOWN).
6823 * Thus, the touch has been transferred from the top window into the bottom window, because the top
6824 * window moved itself away from the touched location and had Flag::SLIPPERY.
6825 *
6826 * Even though the top window moved away from the touched location, it is still obscuring the bottom
6827 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
6828 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
6829 *
6830 * In this test, we ensure that the event received by the bottom window has
6831 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
6832 */
6833TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006834 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006835 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006836
6837 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6838 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6839
6840 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006841 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006842 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006843 // Make sure this one overlaps the bottom window
6844 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
6845 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
6846 // one. Windows with the same owner are not considered to be occluding each other.
6847 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
6848
6849 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006850 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006851 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
6852
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
6856 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00006857 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6858 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6859 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006860 slipperyExitWindow->consumeMotionDown();
6861 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006862 mDispatcher->onWindowInfosChanged(
6863 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006864
Prabir Pradhan678438e2023-04-13 19:32:51 +00006865 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
6866 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6867 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006868
6869 slipperyExitWindow->consumeMotionCancel();
6870
6871 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
6872 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6873}
6874
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006875/**
6876 * Two windows, one on the left and another on the right. The left window is slippery. The right
6877 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
6878 * touch moves from the left window into the right window, the gesture should continue to go to the
6879 * left window. Touch shouldn't slip because the right window can't receive touches. This test
6880 * reproduces a crash.
6881 */
6882TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
6883 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6884
6885 sp<FakeWindowHandle> leftSlipperyWindow =
6886 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6887 leftSlipperyWindow->setSlippery(true);
6888 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
6889
6890 sp<FakeWindowHandle> rightDropTouchesWindow =
6891 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6892 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
6893 rightDropTouchesWindow->setDropInput(true);
6894
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006895 mDispatcher->onWindowInfosChanged(
6896 {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006897
6898 // Start touch in the left window
6899 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6900 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6901 .build());
6902 leftSlipperyWindow->consumeMotionDown();
6903
6904 // And move it into the right window
6905 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6906 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
6907 .build());
6908
6909 // Since the right window isn't eligible to receive input, touch does not slip.
6910 // The left window continues to receive the gesture.
6911 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6912 rightDropTouchesWindow->assertNoEvents();
6913}
6914
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07006915/**
6916 * A single window is on screen first. Touch is injected into that window. Next, a second window
6917 * appears. Since the first window is slippery, touch will move from the first window to the second.
6918 */
6919TEST_F(InputDispatcherTest, InjectedTouchSlips) {
6920 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6921 sp<FakeWindowHandle> originalWindow =
6922 sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT);
6923 originalWindow->setFrame(Rect(0, 0, 200, 200));
6924 originalWindow->setSlippery(true);
6925
6926 sp<FakeWindowHandle> appearingWindow =
6927 sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT);
6928 appearingWindow->setFrame(Rect(0, 0, 200, 200));
6929
6930 mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0});
6931
6932 // Touch down on the original window
6933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6934 injectMotionEvent(*mDispatcher,
6935 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6936 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
6937 .build()));
6938 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6939
6940 // Now, a new window appears. This could be, for example, a notification shade that appears
6941 // after user starts to drag down on the launcher window.
6942 mDispatcher->onWindowInfosChanged(
6943 {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0});
6944 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6945 injectMotionEvent(*mDispatcher,
6946 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6947 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110))
6948 .build()));
6949 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
6950 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6951 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6952 injectMotionEvent(*mDispatcher,
6953 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6954 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
6955 .build()));
6956 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6957
6958 originalWindow->assertNoEvents();
6959 appearingWindow->assertNoEvents();
6960}
6961
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006962TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006963 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006964 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6965
6966 sp<FakeWindowHandle> leftWindow =
6967 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6968 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006969 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006970
6971 sp<FakeWindowHandle> rightSpy =
6972 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
6973 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006974 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006975 rightSpy->setSpy(true);
6976 rightSpy->setTrustedOverlay(true);
6977
6978 sp<FakeWindowHandle> rightWindow =
6979 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6980 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006981 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006982
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006983 mDispatcher->onWindowInfosChanged(
6984 {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006985
6986 // Touch in the left window
6987 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6988 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6989 .build());
6990 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
6991 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006992 ASSERT_NO_FATAL_FAILURE(
6993 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006994
6995 // Touch another finger over the right windows
6996 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6997 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6998 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6999 .build());
7000 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
7001 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
7002 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
7003 mDispatcher->waitForIdle();
7004 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007005 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
7006 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007007
7008 // Release finger over left window. The UP actions are not treated as device interaction.
7009 // The windows that did not receive the UP pointer will receive MOVE events, but since this
7010 // is part of the UP action, we do not treat this as device interaction.
7011 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
7012 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7013 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7014 .build());
7015 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
7016 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
7017 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7018 mDispatcher->waitForIdle();
7019 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7020
7021 // Move remaining finger
7022 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7023 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7024 .build());
7025 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
7026 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7027 mDispatcher->waitForIdle();
7028 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007029 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007030
7031 // Release all fingers
7032 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7033 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7034 .build());
7035 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
7036 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
7037 mDispatcher->waitForIdle();
7038 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7039}
7040
7041TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
7042 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7043
7044 sp<FakeWindowHandle> window =
7045 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
7046 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007047 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007048
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007049 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007050 setFocusedWindow(window);
7051 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
7052
7053 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
7054 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
7055 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007056 ASSERT_NO_FATAL_FAILURE(
7057 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007058
7059 // The UP actions are not treated as device interaction.
7060 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
7061 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
7062 mDispatcher->waitForIdle();
7063 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7064}
7065
Prabir Pradhan5893d362023-11-17 04:30:40 +00007066TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) {
7067 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7068
7069 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
7070 ADISPLAY_ID_DEFAULT);
7071 left->setFrame(Rect(0, 0, 100, 100));
7072 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
7073 "Right Window", ADISPLAY_ID_DEFAULT);
7074 right->setFrame(Rect(100, 0, 200, 100));
7075 sp<FakeWindowHandle> spy =
7076 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
7077 spy->setFrame(Rect(0, 0, 200, 100));
7078 spy->setTrustedOverlay(true);
7079 spy->setSpy(true);
7080
7081 mDispatcher->onWindowInfosChanged(
7082 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
7083
7084 // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId.
7085 NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
7086 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
7087 mDispatcher->notifyMotion(notifyArgs);
7088
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007089 std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00007090 AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)),
7091 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007092 ASSERT_NE(nullptr, leftEnter);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007093 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
7094 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007095 Not(WithEventId(leftEnter->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00007096 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
7097
7098 // Send move to the right window, and ensure hover exit and enter are synthesized with new ids.
7099 notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
7100 {PointF{150, 50}});
7101 mDispatcher->notifyMotion(notifyArgs);
7102
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007103 std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00007104 AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)),
7105 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007106 ASSERT_NE(nullptr, leftExit);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007107 right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
7108 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007109 Not(WithEventId(leftExit->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00007110 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
7111
7112 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id)));
7113}
7114
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00007115class InputDispatcherFallbackKeyTest : public InputDispatcherTest {
7116protected:
7117 std::shared_ptr<FakeApplicationHandle> mApp;
7118 sp<FakeWindowHandle> mWindow;
7119
7120 virtual void SetUp() override {
7121 InputDispatcherTest::SetUp();
7122
7123 mApp = std::make_shared<FakeApplicationHandle>();
7124
7125 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
7126 mWindow->setFrame(Rect(0, 0, 100, 100));
7127
7128 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
7129 setFocusedWindow(mWindow);
7130 ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true));
7131 }
7132
7133 void setFallback(int32_t keycode) {
7134 mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) {
7135 return KeyEventBuilder(event).keyCode(keycode).build();
7136 });
7137 }
7138
7139 void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007140 std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled);
7141 ASSERT_NE(nullptr, event);
7142 ASSERT_THAT(*event, matcher);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00007143 }
7144};
7145
7146TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) {
7147 mDispatcher->notifyKey(
7148 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7149 consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
7150 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7151}
7152
7153TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) {
7154 mDispatcher->notifyKey(
7155 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7156 consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
7157 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7158}
7159
7160TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) {
7161 mDispatcher->notifyKey(
7162 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7163
7164 // Do not handle this key event.
7165 consumeKey(/*handled=*/false,
7166 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7167 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7168
7169 // Since the policy did not request any fallback to be generated, ensure there are no events.
7170 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7171}
7172
7173TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) {
7174 setFallback(AKEYCODE_B);
7175 mDispatcher->notifyKey(
7176 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7177
7178 // Do not handle this key event.
7179 consumeKey(/*handled=*/false,
7180 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7181
7182 // Since the key was not handled, ensure the fallback event was dispatched instead.
7183 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7184 consumeKey(/*handled=*/true,
7185 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7186 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7187
7188 // Release the original key, and ensure the fallback key is also released.
7189 mDispatcher->notifyKey(
7190 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7191 consumeKey(/*handled=*/false,
7192 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7193 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7194 consumeKey(/*handled=*/true,
7195 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7196 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7197
7198 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7199 mWindow->assertNoEvents();
7200}
7201
7202TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) {
7203 setFallback(AKEYCODE_B);
7204 mDispatcher->notifyKey(
7205 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7206
7207 // Do not handle this key event, but handle the fallback.
7208 consumeKey(/*handled=*/false,
7209 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7210 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7211 consumeKey(/*handled=*/true,
7212 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7213 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7214
7215 // Release the original key, and ensure the fallback key is also released.
7216 mDispatcher->notifyKey(
7217 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7218 // But this time, the app handles the original key.
7219 consumeKey(/*handled=*/true,
7220 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7221 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7222 // Ensure the fallback key is canceled.
7223 consumeKey(/*handled=*/true,
7224 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7225 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7226
7227 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7228 mWindow->assertNoEvents();
7229}
7230
7231TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) {
7232 setFallback(AKEYCODE_B);
7233 mDispatcher->notifyKey(
7234 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7235
7236 // Do not handle this key event.
7237 consumeKey(/*handled=*/false,
7238 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7239 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7240 // App does not handle the fallback either, so ensure another fallback is not generated.
7241 setFallback(AKEYCODE_C);
7242 consumeKey(/*handled=*/false,
7243 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7244 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7245
7246 // Release the original key, and ensure the fallback key is also released.
7247 setFallback(AKEYCODE_B);
7248 mDispatcher->notifyKey(
7249 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7250 consumeKey(/*handled=*/false,
7251 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7252 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7253 consumeKey(/*handled=*/false,
7254 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7255 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7256
7257 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7258 mWindow->assertNoEvents();
7259}
7260
7261TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) {
7262 setFallback(AKEYCODE_B);
7263 mDispatcher->notifyKey(
7264 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7265
7266 // Do not handle this key event, so fallback is generated.
7267 consumeKey(/*handled=*/false,
7268 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7269 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7270 consumeKey(/*handled=*/true,
7271 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7272 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7273
7274 // Release the original key, but assume the policy is misbehaving and it
7275 // generates an inconsistent fallback to the one from the DOWN event.
7276 setFallback(AKEYCODE_C);
7277 mDispatcher->notifyKey(
7278 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7279 consumeKey(/*handled=*/false,
7280 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7281 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7282 // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency.
7283 consumeKey(/*handled=*/true,
7284 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7285 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7286
7287 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7288 mWindow->assertNoEvents();
7289}
7290
7291TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) {
7292 setFallback(AKEYCODE_B);
7293 mDispatcher->notifyKey(
7294 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7295
7296 // Do not handle this key event, so fallback is generated.
7297 consumeKey(/*handled=*/false,
7298 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7299 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7300 consumeKey(/*handled=*/true,
7301 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7302 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7303
7304 // The original key is canceled.
7305 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD)
7306 .keyCode(AKEYCODE_A)
7307 .addFlag(AKEY_EVENT_FLAG_CANCELED)
7308 .build());
7309 consumeKey(/*handled=*/false,
7310 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
7311 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7312 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7313 // Ensure the fallback key is also canceled due to the original key being canceled.
7314 consumeKey(/*handled=*/true,
7315 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7316 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7317
7318 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7319 mWindow->assertNoEvents();
7320}
7321
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00007322TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) {
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00007323 setFallback(AKEYCODE_B);
7324 mDispatcher->notifyKey(
7325 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7326
7327 // Do not handle this key event.
7328 consumeKey(/*handled=*/false,
7329 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7330 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7331 consumeKey(/*handled=*/true,
7332 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7333 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7334
7335 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
7336 // When the unhandled key is reported to the policy next, remove the input channel.
7337 mDispatcher->removeInputChannel(mWindow->getToken());
7338 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
7339 });
7340 // Release the original key, and let the app now handle the previously unhandled key.
7341 // This should result in the previously generated fallback key to be cancelled.
7342 // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified
7343 // of the UP event for consistency. The Dispatcher calls into the policy from its own thread
7344 // without holding the lock, because it need to synchronously fetch the fallback key. While in
7345 // the policy call, we will now remove the input channel. Once the policy call returns, the
7346 // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does
7347 // not cause any crashes.
7348 mDispatcher->notifyKey(
7349 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7350 consumeKey(/*handled=*/true,
7351 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7352 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7353}
7354
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00007355TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) {
7356 setFallback(AKEYCODE_B);
7357 mDispatcher->notifyKey(
7358 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7359
7360 // Do not handle this key event.
7361 consumeKey(/*handled=*/false,
7362 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7363 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7364 consumeKey(/*handled=*/true,
7365 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7366 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7367
7368 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
7369 // When the unhandled key is reported to the policy next, remove the window.
7370 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
7371 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
7372 });
7373 // Release the original key, which the app will not handle. When this unhandled key is reported
7374 // to the policy, the window will be removed.
7375 mDispatcher->notifyKey(
7376 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7377 consumeKey(/*handled=*/false,
7378 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7379 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7380
7381 // Since the window was removed, it loses focus, and the channel state will be reset.
7382 consumeKey(/*handled=*/true,
7383 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7384 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7385 mWindow->consumeFocusEvent(false);
7386 mWindow->assertNoEvents();
7387}
7388
7389TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) {
7390 setFallback(AKEYCODE_B);
7391 mDispatcher->notifyKey(
7392 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7393
7394 // Do not handle this key event.
7395 consumeKey(/*handled=*/false,
7396 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7398 const auto [seq, event] = mWindow->receiveEvent();
7399 ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event";
7400 ASSERT_EQ(event->getType(), InputEventType::KEY);
7401 ASSERT_THAT(static_cast<const KeyEvent&>(*event),
7402 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7403 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7404
7405 // Remove the window now, which should generate a cancellations and make the window lose focus.
7406 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
7407 consumeKey(/*handled=*/true,
7408 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
7409 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7410 consumeKey(/*handled=*/true,
7411 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7412 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7413 mWindow->consumeFocusEvent(false);
7414
7415 // Finish the event by reporting it as handled.
7416 mWindow->finishEvent(*seq);
7417 mWindow->assertNoEvents();
7418}
7419
Garfield Tan1c7bc862020-01-28 13:24:04 -08007420class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
7421protected:
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007422 static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms;
7423 static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007424
Chris Yea209fde2020-07-22 13:54:51 -07007425 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007426 sp<FakeWindowHandle> mWindow;
7427
7428 virtual void SetUp() override {
Prabir Pradhandae52792023-12-15 07:36:40 +00007429 InputDispatcherTest::SetUp();
Garfield Tan1c7bc862020-01-28 13:24:04 -08007430
Prabir Pradhandae52792023-12-15 07:36:40 +00007431 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007432 setUpWindow();
7433 }
7434
7435 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07007436 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007437 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007438
Vishnu Nair47074b82020-08-14 11:54:47 -07007439 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007440 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007441 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007442 mWindow->consumeFocusEvent(true);
7443 }
7444
Chris Ye2ad95392020-09-01 13:44:44 -07007445 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08007446 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07007447 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007448 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00007449 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007450
7451 // Window should receive key down event.
7452 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7453 }
7454
7455 void expectKeyRepeatOnce(int32_t repeatCount) {
7456 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007457 mWindow->consumeKeyEvent(
7458 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount)));
Garfield Tan1c7bc862020-01-28 13:24:04 -08007459 }
7460
Chris Ye2ad95392020-09-01 13:44:44 -07007461 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08007462 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07007463 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007464 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00007465 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007466
7467 // Window should receive key down event.
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00007468 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007469 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007470 }
7471};
7472
7473TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00007474 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007475 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7476 expectKeyRepeatOnce(repeatCount);
7477 }
7478}
7479
7480TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00007481 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007482 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7483 expectKeyRepeatOnce(repeatCount);
7484 }
Harry Cutts33476232023-01-30 19:57:29 +00007485 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007486 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08007487 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7488 expectKeyRepeatOnce(repeatCount);
7489 }
7490}
7491
7492TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007493 sendAndConsumeKeyDown(/*deviceId=*/1);
7494 expectKeyRepeatOnce(/*repeatCount=*/1);
7495 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007496 mWindow->assertNoEvents();
7497}
7498
7499TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007500 sendAndConsumeKeyDown(/*deviceId=*/1);
7501 expectKeyRepeatOnce(/*repeatCount=*/1);
7502 sendAndConsumeKeyDown(/*deviceId=*/2);
7503 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007504 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00007505 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007506 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00007507 expectKeyRepeatOnce(/*repeatCount=*/2);
7508 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07007509 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00007510 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007511 mWindow->assertNoEvents();
7512}
7513
7514TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007515 sendAndConsumeKeyDown(/*deviceId=*/1);
7516 expectKeyRepeatOnce(/*repeatCount=*/1);
7517 sendAndConsumeKeyDown(/*deviceId=*/2);
7518 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007519 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00007520 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007521 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08007522 mWindow->assertNoEvents();
7523}
7524
liushenxiang42232912021-05-21 20:24:09 +08007525TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
7526 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00007527 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007528 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08007529 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
7530 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
7531 mWindow->assertNoEvents();
7532}
7533
Garfield Tan1c7bc862020-01-28 13:24:04 -08007534TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00007535 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00007536 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007537 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007538 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
7539 ASSERT_NE(nullptr, repeatEvent);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007540 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007541 IdGenerator::getSource(repeatEvent->getId()));
Garfield Tan1c7bc862020-01-28 13:24:04 -08007542 }
7543}
7544
7545TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00007546 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00007547 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007548
7549 std::unordered_set<int32_t> idSet;
7550 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007551 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
7552 ASSERT_NE(nullptr, repeatEvent);
7553 int32_t id = repeatEvent->getId();
Garfield Tan1c7bc862020-01-28 13:24:04 -08007554 EXPECT_EQ(idSet.end(), idSet.find(id));
7555 idSet.insert(id);
7556 }
7557}
7558
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007559/* Test InputDispatcher for MultiDisplay */
7560class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
7561public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007562 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007563 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08007564
Chris Yea209fde2020-07-22 13:54:51 -07007565 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007566 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007567 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007568
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007569 // Set focus window for primary display, but focused display would be second one.
7570 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07007571 windowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007572 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
7573
Vishnu Nair958da932020-08-21 17:12:37 -07007574 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007575 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08007576
Chris Yea209fde2020-07-22 13:54:51 -07007577 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007578 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007579 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007580 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007581 // Set focus display to second one.
7582 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
7583 // Set focus window for second display.
7584 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07007585 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007586 mDispatcher->onWindowInfosChanged(
7587 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007588 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007589 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007590 }
7591
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007592 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007593 InputDispatcherTest::TearDown();
7594
Chris Yea209fde2020-07-22 13:54:51 -07007595 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007596 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07007597 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007598 windowInSecondary.clear();
7599 }
7600
7601protected:
Chris Yea209fde2020-07-22 13:54:51 -07007602 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007603 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07007604 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007605 sp<FakeWindowHandle> windowInSecondary;
7606};
7607
7608TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
7609 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007610 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007611 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007612 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007613 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08007614 windowInSecondary->assertNoEvents();
7615
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007616 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007618 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007619 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08007620 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007621 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08007622}
7623
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007624TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08007625 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08007626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007627 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007628 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007629 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08007630 windowInSecondary->assertNoEvents();
7631
7632 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007633 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007634 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08007635 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007636 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08007637
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007638 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007639 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08007640
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007641 // Old focus should receive a cancel event.
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00007642 windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08007643
7644 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007645 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08007646 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007647 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08007648 windowInSecondary->assertNoEvents();
7649}
7650
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007651// Test per-display input monitors for motion event.
7652TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08007653 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007654 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007655 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007656 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007657
7658 // Test touch down on primary 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, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007662 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007663 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007664 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007665 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007666
7667 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007669 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007671 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007672 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007673 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08007674 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007675
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007676 // Lift up the touch from the second display
7677 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007678 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007679 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7680 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
7681 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
7682
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007683 // Test inject a non-pointer motion event.
7684 // If specific a display, it will dispatch to the focused window of particular display,
7685 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007687 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007688 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007689 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007690 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007691 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007692 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007693}
7694
7695// Test per-display input monitors for key event.
7696TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007697 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08007698 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007699 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007700 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007701 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007702
7703 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007705 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007706 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007707 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007708 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007709 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007710}
7711
Vishnu Nair958da932020-08-21 17:12:37 -07007712TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
7713 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007714 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007715 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007716 mDispatcher->onWindowInfosChanged(
7717 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
7718 *windowInSecondary->getInfo()},
7719 {},
7720 0,
7721 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007722 setFocusedWindow(secondWindowInPrimary);
7723 windowInPrimary->consumeFocusEvent(false);
7724 secondWindowInPrimary->consumeFocusEvent(true);
7725
7726 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7728 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007729 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007730 windowInPrimary->assertNoEvents();
7731 windowInSecondary->assertNoEvents();
7732 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7733}
7734
Arthur Hungdfd528e2021-12-08 13:23:04 +00007735TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
7736 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007737 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007738 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007739 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007740
7741 // Test touch down on primary display.
7742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007743 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007744 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7745 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7746 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
7747
7748 // Test touch down on second display.
7749 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007750 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007751 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7752 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
7753 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
7754
7755 // Trigger cancel touch.
7756 mDispatcher->cancelCurrentTouch();
7757 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7758 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7759 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
7760 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
7761
7762 // Test inject a move motion event, no window/monitor should receive the event.
7763 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007764 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007765 ADISPLAY_ID_DEFAULT, {110, 200}))
7766 << "Inject motion event should return InputEventInjectionResult::FAILED";
7767 windowInPrimary->assertNoEvents();
7768 monitorInPrimary.assertNoEvents();
7769
7770 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007771 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007772 SECOND_DISPLAY_ID, {110, 200}))
7773 << "Inject motion event should return InputEventInjectionResult::FAILED";
7774 windowInSecondary->assertNoEvents();
7775 monitorInSecondary.assertNoEvents();
7776}
7777
Hu Guocb134f12023-12-23 13:42:44 +00007778/**
7779 * Send a key to the primary display and to the secondary display.
7780 * Then cause the key on the primary display to be canceled by sending in a stale key.
7781 * Ensure that the key on the primary display is canceled, and that the key on the secondary display
7782 * does not get canceled.
7783 */
7784TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) {
7785 // Send a key down on primary display
7786 mDispatcher->notifyKey(
7787 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
7788 .displayId(ADISPLAY_ID_DEFAULT)
7789 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7790 .build());
7791 windowInPrimary->consumeKeyEvent(
7792 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
7793 windowInSecondary->assertNoEvents();
7794
7795 // Send a key down on second display
7796 mDispatcher->notifyKey(
7797 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
7798 .displayId(SECOND_DISPLAY_ID)
7799 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7800 .build());
7801 windowInSecondary->consumeKeyEvent(
7802 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
7803 windowInPrimary->assertNoEvents();
7804
7805 // Send a valid key up event on primary display that will be dropped because it is stale
7806 NotifyKeyArgs staleKeyUp =
7807 KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
7808 .displayId(ADISPLAY_ID_DEFAULT)
7809 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7810 .build();
7811 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
7812 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
7813 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
7814 mDispatcher->notifyKey(staleKeyUp);
7815
7816 // Only the key gesture corresponding to the dropped event should receive the cancel event.
7817 // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not
7818 // receive any events.
7819 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
7820 WithDisplayId(ADISPLAY_ID_DEFAULT),
7821 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7822 windowInSecondary->assertNoEvents();
7823}
7824
7825/**
7826 * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events.
7827 */
7828TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) {
7829 // Send touch down on primary display.
7830 mDispatcher->notifyMotion(
7831 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7832 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7833 .displayId(ADISPLAY_ID_DEFAULT)
7834 .build());
7835 windowInPrimary->consumeMotionEvent(
7836 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
7837 windowInSecondary->assertNoEvents();
7838
7839 // Send touch down on second display.
7840 mDispatcher->notifyMotion(
7841 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7842 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7843 .displayId(SECOND_DISPLAY_ID)
7844 .build());
7845 windowInPrimary->assertNoEvents();
7846 windowInSecondary->consumeMotionEvent(
7847 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
7848
7849 // inject a valid MotionEvent on primary display that will be stale when it arrives.
7850 NotifyMotionArgs staleMotionUp =
7851 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7852 .displayId(ADISPLAY_ID_DEFAULT)
7853 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7854 .build();
7855 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
7856 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
7857 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
7858 mDispatcher->notifyMotion(staleMotionUp);
7859
7860 // For stale motion events, we let the gesture to complete. This behaviour is different from key
7861 // events, where we would cancel the current keys instead.
7862 windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP));
7863 windowInSecondary->assertNoEvents();
7864}
7865
Jackal Guof9696682018-10-05 12:23:23 +08007866class InputFilterTest : public InputDispatcherTest {
7867protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007868 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
7869 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08007870 NotifyMotionArgs motionArgs;
7871
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007872 motionArgs =
7873 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007874 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007875 motionArgs =
7876 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007877 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007878 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007879 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007880 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007881 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08007882 } else {
7883 mFakePolicy->assertFilterInputEventWasNotCalled();
7884 }
7885 }
7886
7887 void testNotifyKey(bool expectToBeFiltered) {
7888 NotifyKeyArgs keyArgs;
7889
7890 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007891 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007892 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007893 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007894 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007895
7896 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08007897 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007898 } else {
7899 mFakePolicy->assertFilterInputEventWasNotCalled();
7900 }
7901 }
7902};
7903
7904// Test InputFilter for MotionEvent
7905TEST_F(InputFilterTest, MotionEvent_InputFilter) {
7906 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007907 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7908 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007909
7910 // Enable InputFilter
7911 mDispatcher->setInputFilterEnabled(true);
7912 // Test touch on both primary and second display, and check if both events are filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007913 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true);
7914 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007915
7916 // Disable InputFilter
7917 mDispatcher->setInputFilterEnabled(false);
7918 // Test touch on both primary and second display, and check if both events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007919 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7920 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007921}
7922
7923// Test InputFilter for KeyEvent
7924TEST_F(InputFilterTest, KeyEvent_InputFilter) {
7925 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007926 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007927
7928 // Enable InputFilter
7929 mDispatcher->setInputFilterEnabled(true);
7930 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007931 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007932
7933 // Disable InputFilter
7934 mDispatcher->setInputFilterEnabled(false);
7935 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007936 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007937}
7938
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007939// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
7940// logical display coordinate space.
7941TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
7942 ui::Transform firstDisplayTransform;
7943 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7944 ui::Transform secondDisplayTransform;
7945 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
7946
7947 std::vector<gui::DisplayInfo> displayInfos(2);
7948 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
7949 displayInfos[0].transform = firstDisplayTransform;
7950 displayInfos[1].displayId = SECOND_DISPLAY_ID;
7951 displayInfos[1].transform = secondDisplayTransform;
7952
Patrick Williamsd828f302023-04-28 17:52:08 -05007953 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007954
7955 // Enable InputFilter
7956 mDispatcher->setInputFilterEnabled(true);
7957
7958 // Ensure the correct transforms are used for the displays.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007959 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform);
7960 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007961}
7962
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007963class InputFilterInjectionPolicyTest : public InputDispatcherTest {
7964protected:
7965 virtual void SetUp() override {
7966 InputDispatcherTest::SetUp();
7967
7968 /**
7969 * We don't need to enable input filter to test the injected event policy, but we enabled it
7970 * here to make the tests more realistic, since this policy only matters when inputfilter is
7971 * on.
7972 */
7973 mDispatcher->setInputFilterEnabled(true);
7974
7975 std::shared_ptr<InputApplicationHandle> application =
7976 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007977 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
7978 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007979
7980 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7981 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007982 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007983 setFocusedWindow(mWindow);
7984 mWindow->consumeFocusEvent(true);
7985 }
7986
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007987 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7988 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007989 KeyEvent event;
7990
7991 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7992 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
7993 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00007994 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007995 const int32_t additionalPolicyFlags =
7996 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
7997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007998 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00007999 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008000 policyFlags | additionalPolicyFlags));
8001
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008002 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008003 }
8004
8005 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
8006 int32_t flags) {
8007 MotionEvent event;
8008 PointerProperties pointerProperties[1];
8009 PointerCoords pointerCoords[1];
8010 pointerProperties[0].clear();
8011 pointerProperties[0].id = 0;
8012 pointerCoords[0].clear();
8013 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
8014 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
8015
8016 ui::Transform identityTransform;
8017 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
8018 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
8019 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
8020 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
8021 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07008022 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07008023 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00008024 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008025
8026 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
8027 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00008028 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00008029 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008030 policyFlags | additionalPolicyFlags));
8031
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008032 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008033 }
8034
8035private:
8036 sp<FakeWindowHandle> mWindow;
8037};
8038
8039TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008040 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
8041 // filter. Without it, the event will no different from a regularly injected event, and the
8042 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00008043 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
8044 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008045}
8046
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008047TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008048 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00008049 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008050 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
8051}
8052
8053TEST_F(InputFilterInjectionPolicyTest,
8054 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
8055 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00008056 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00008057 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008058}
8059
8060TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00008061 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
8062 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00008063}
8064
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08008065class InputDispatcherUserActivityPokeTests : public InputDispatcherTest {
8066protected:
8067 virtual void SetUp() override {
8068 InputDispatcherTest::SetUp();
8069
8070 std::shared_ptr<FakeApplicationHandle> application =
8071 std::make_shared<FakeApplicationHandle>();
8072 application->setDispatchingTimeout(100ms);
8073 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
8074 ADISPLAY_ID_DEFAULT);
Yeabkal Wubshit222d83d2024-01-24 18:00:09 +00008075 mWindow->setFrame(Rect(0, 0, 100, 100));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08008076 mWindow->setDispatchingTimeout(100ms);
8077 mWindow->setFocusable(true);
8078
8079 // Set focused application.
8080 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8081
8082 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
8083 setFocusedWindow(mWindow);
8084 mWindow->consumeFocusEvent(true);
8085 }
8086
8087 void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId,
8088 nsecs_t eventTime) {
8089 mDispatcher->notifyMotion(MotionArgsBuilder(action, source)
8090 .displayId(displayId)
8091 .eventTime(eventTime)
8092 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8093 .build());
8094 mWindow->consumeMotionEvent(WithMotionAction(action));
8095 }
8096
8097private:
8098 sp<FakeWindowHandle> mWindow;
8099};
8100
8101TEST_F_WITH_FLAGS(
8102 InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
8103 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8104 rate_limit_user_activity_poke_in_dispatcher))) {
8105 mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
8106
8107 // First event of type TOUCH. Should poke.
8108 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8109 milliseconds_to_nanoseconds(50));
8110 mFakePolicy->assertUserActivityPoked(
8111 {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8112
8113 // 80ns > 50ns has passed since previous TOUCH event. Should poke.
8114 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8115 milliseconds_to_nanoseconds(130));
8116 mFakePolicy->assertUserActivityPoked(
8117 {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8118
8119 // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
8120 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8121 milliseconds_to_nanoseconds(135));
8122 mFakePolicy->assertUserActivityPoked(
8123 {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}});
8124
8125 // Within 50ns of previous TOUCH event. Should NOT poke.
8126 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8127 milliseconds_to_nanoseconds(140));
8128 mFakePolicy->assertUserActivityNotPoked();
8129
8130 // Within 50ns of previous OTHER event. Should NOT poke.
8131 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8132 milliseconds_to_nanoseconds(150));
8133 mFakePolicy->assertUserActivityNotPoked();
8134
8135 // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
8136 // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
8137 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
8138 milliseconds_to_nanoseconds(160));
8139 mFakePolicy->assertUserActivityNotPoked();
8140
8141 // 65ns > 50ns has passed since previous OTHER event. Should poke.
8142 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8143 milliseconds_to_nanoseconds(200));
8144 mFakePolicy->assertUserActivityPoked(
8145 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}});
8146
8147 // 170ns > 50ns has passed since previous TOUCH event. Should poke.
8148 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
8149 milliseconds_to_nanoseconds(300));
8150 mFakePolicy->assertUserActivityPoked(
8151 {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8152
8153 // Assert that there's no more user activity poke event.
8154 mFakePolicy->assertUserActivityNotPoked();
8155}
8156
8157TEST_F_WITH_FLAGS(
8158 InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
8159 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8160 rate_limit_user_activity_poke_in_dispatcher))) {
8161 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8162 milliseconds_to_nanoseconds(200));
8163 mFakePolicy->assertUserActivityPoked(
8164 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8165
8166 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8167 milliseconds_to_nanoseconds(280));
8168 mFakePolicy->assertUserActivityNotPoked();
8169
8170 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8171 milliseconds_to_nanoseconds(340));
8172 mFakePolicy->assertUserActivityPoked(
8173 {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8174}
8175
8176TEST_F_WITH_FLAGS(
8177 InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
8178 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8179 rate_limit_user_activity_poke_in_dispatcher))) {
8180 mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
8181
8182 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20);
8183 mFakePolicy->assertUserActivityPoked();
8184
8185 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30);
8186 mFakePolicy->assertUserActivityPoked();
8187}
8188
chaviwfd6d3512019-03-25 13:23:49 -07008189class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008190 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07008191 InputDispatcherTest::SetUp();
8192
Chris Yea209fde2020-07-22 13:54:51 -07008193 std::shared_ptr<FakeApplicationHandle> application =
8194 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008195 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008196 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07008197 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07008198
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008199 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008200 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008201 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07008202
8203 // Set focused application.
8204 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07008205 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07008206
8207 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008208 mDispatcher->onWindowInfosChanged(
8209 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008210 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008211 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07008212 }
8213
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008214 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07008215 InputDispatcherTest::TearDown();
8216
8217 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008218 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07008219 }
8220
8221protected:
8222 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008223 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008224 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07008225};
8226
8227// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
8228// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
8229// the onPointerDownOutsideFocus callback.
8230TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008232 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008233 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008234 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008235 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008236
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008237 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07008238 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
8239}
8240
8241// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
8242// DOWN on the window that doesn't have focus. Ensure no window received the
8243// onPointerDownOutsideFocus callback.
8244TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008245 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008246 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT,
8247 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008248 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008249 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008250
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008251 ASSERT_TRUE(mDispatcher->waitForIdle());
8252 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008253}
8254
8255// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
8256// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
8257TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08008258 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008259 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008260 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008261 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07008262
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008263 ASSERT_TRUE(mDispatcher->waitForIdle());
8264 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008265}
8266
8267// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
8268// DOWN on the window that already has focus. Ensure no window received the
8269// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008270TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008271 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008272 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008273 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008274 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008275 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008276
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008277 ASSERT_TRUE(mDispatcher->waitForIdle());
8278 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008279}
8280
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008281// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
8282// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
8283TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
8284 const MotionEvent event =
8285 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8286 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008287 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008288 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
8289 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008291 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8292 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
8293
8294 ASSERT_TRUE(mDispatcher->waitForIdle());
8295 mFakePolicy->assertOnPointerDownWasNotCalled();
8296 // Ensure that the unfocused window did not receive any FOCUS events.
8297 mUnfocusedWindow->assertNoEvents();
8298}
8299
chaviwaf87b3e2019-10-01 16:59:28 -07008300// These tests ensures we can send touch events to a single client when there are multiple input
8301// windows that point to the same client token.
8302class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
8303 virtual void SetUp() override {
8304 InputDispatcherTest::SetUp();
8305
Chris Yea209fde2020-07-22 13:54:51 -07008306 std::shared_ptr<FakeApplicationHandle> application =
8307 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008308 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
8309 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07008310 mWindow1->setFrame(Rect(0, 0, 100, 100));
8311
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00008312 mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07008313 mWindow2->setFrame(Rect(100, 100, 200, 200));
8314
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008315 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07008316 }
8317
8318protected:
8319 sp<FakeWindowHandle> mWindow1;
8320 sp<FakeWindowHandle> mWindow2;
8321
8322 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05008323 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07008324 vec2 vals = windowInfo->transform.transform(point.x, point.y);
8325 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07008326 }
8327
8328 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
8329 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008330 const std::string name = window->getName();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008331 std::unique_ptr<MotionEvent> motionEvent =
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00008332 window->consumeMotionEvent(WithMotionAction(expectedAction));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008333 ASSERT_NE(nullptr, motionEvent);
8334 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07008335
8336 for (size_t i = 0; i < points.size(); i++) {
8337 float expectedX = points[i].x;
8338 float expectedY = points[i].y;
8339
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008340 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07008341 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008342 << ", got " << motionEvent->getX(i);
8343 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07008344 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008345 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07008346 }
8347 }
chaviw9eaa22c2020-07-01 16:21:27 -07008348
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008349 void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action,
8350 const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07008351 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00008352 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
8353 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07008354
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008355 consumeMotionEvent(touchedWindow, action, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008356 }
chaviwaf87b3e2019-10-01 16:59:28 -07008357};
8358
8359TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
8360 // Touch Window 1
8361 PointF touchedPoint = {10, 10};
8362 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008363 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008364
8365 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008366 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008367
8368 // Touch Window 2
8369 touchedPoint = {150, 150};
8370 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008371 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008372}
8373
chaviw9eaa22c2020-07-01 16:21:27 -07008374TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
8375 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07008376 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008377 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07008378
8379 // Touch Window 1
8380 PointF touchedPoint = {10, 10};
8381 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008382 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008383 // Release touch on Window 1
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008384 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008385
8386 // Touch Window 2
8387 touchedPoint = {150, 150};
8388 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008389 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
8390 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008391
chaviw9eaa22c2020-07-01 16:21:27 -07008392 // Update the transform so rotation is set
8393 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008394 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008395 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008396 touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008397}
8398
chaviw9eaa22c2020-07-01 16:21:27 -07008399TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008400 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008401 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008402
8403 // Touch Window 1
8404 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8405 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008406 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008407
8408 // Touch Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008409 // Since this is part of the same touch gesture that has already been dispatched to Window 1,
8410 // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream
8411 // will continue to be dispatched through Window 1.
chaviw9eaa22c2020-07-01 16:21:27 -07008412 touchedPoints.push_back(PointF{150, 150});
8413 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008414 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008415
chaviw9eaa22c2020-07-01 16:21:27 -07008416 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008417 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008418 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008419
chaviw9eaa22c2020-07-01 16:21:27 -07008420 // Update the transform so rotation is set for Window 2
8421 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008422 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008423 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008424 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008425}
8426
chaviw9eaa22c2020-07-01 16:21:27 -07008427TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008428 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008429 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008430
8431 // Touch Window 1
8432 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8433 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008434 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008435
8436 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008437 touchedPoints.push_back(PointF{150, 150});
8438 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008439
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008440 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008441
8442 // Move both windows
8443 touchedPoints = {{20, 20}, {175, 175}};
8444 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8445 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8446
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008447 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008448
chaviw9eaa22c2020-07-01 16:21:27 -07008449 // Release Window 2
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008450 touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008451 expectedPoints.pop_back();
8452
8453 // Touch Window 2
8454 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008455 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008456 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008457 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008458
8459 // Move both windows
8460 touchedPoints = {{20, 20}, {175, 175}};
8461 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8462 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8463
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008464 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008465}
8466
8467TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
8468 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008469 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008470
8471 // Touch Window 1
8472 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8473 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008474 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008475
8476 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008477 touchedPoints.push_back(PointF{150, 150});
8478 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008479
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008480 touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008481
8482 // Move both windows
8483 touchedPoints = {{20, 20}, {175, 175}};
8484 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8485 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8486
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008487 touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008488}
8489
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07008490/**
8491 * When one of the windows is slippery, the touch should not slip into the other window with the
8492 * same input channel.
8493 */
8494TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
8495 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008496 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07008497
8498 // Touch down in window 1
8499 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8500 ADISPLAY_ID_DEFAULT, {{50, 50}}));
8501 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
8502
8503 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
8504 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
8505 // getting generated.
8506 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8507 ADISPLAY_ID_DEFAULT, {{150, 150}}));
8508
8509 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
8510}
8511
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008512/**
8513 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
8514 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
8515 * that the pointer is hovering over may have a different transform.
8516 */
8517TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008518 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008519
8520 // Start hover in window 1
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008521 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN)
8522 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8523 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008524 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
8525 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008526 // Move hover to window 2.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008527 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8528 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
8529 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008530 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00008531 consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER,
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008532 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
8533}
8534
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008535class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
8536 virtual void SetUp() override {
8537 InputDispatcherTest::SetUp();
8538
Chris Yea209fde2020-07-22 13:54:51 -07008539 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008540 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008541 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
8542 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008543 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008544 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07008545 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008546
8547 // Set focused application.
8548 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
8549
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008550 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008551 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008552 mWindow->consumeFocusEvent(true);
8553 }
8554
8555 virtual void TearDown() override {
8556 InputDispatcherTest::TearDown();
8557 mWindow.clear();
8558 }
8559
8560protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008561 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07008562 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008563 sp<FakeWindowHandle> mWindow;
8564 static constexpr PointF WINDOW_LOCATION = {20, 20};
8565
8566 void tapOnWindow() {
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008567 const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER)
8568 .x(WINDOW_LOCATION.x)
8569 .y(WINDOW_LOCATION.y);
8570 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8571 .pointer(touchingPointer)
8572 .build());
8573 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
8574 .pointer(touchingPointer)
8575 .build());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008576 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008577
8578 sp<FakeWindowHandle> addSpyWindow() {
8579 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008580 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008581 spy->setTrustedOverlay(true);
8582 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008583 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008584 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008585 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008586 return spy;
8587 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008588};
8589
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008590// Send a tap and respond, which should not cause an ANR.
8591TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
8592 tapOnWindow();
8593 mWindow->consumeMotionDown();
8594 mWindow->consumeMotionUp();
8595 ASSERT_TRUE(mDispatcher->waitForIdle());
8596 mFakePolicy->assertNotifyAnrWasNotCalled();
8597}
8598
8599// Send a regular key and respond, which should not cause an ANR.
8600TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008601 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008602 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
8603 ASSERT_TRUE(mDispatcher->waitForIdle());
8604 mFakePolicy->assertNotifyAnrWasNotCalled();
8605}
8606
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008607TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
8608 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008609 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008610 mWindow->consumeFocusEvent(false);
8611
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008612 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008613 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
8614 InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00008615 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008617 // Key will not go to window because we have no focused window.
8618 // The 'no focused window' ANR timer should start instead.
8619
8620 // Now, the focused application goes away.
8621 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
8622 // The key should get dropped and there should be no ANR.
8623
8624 ASSERT_TRUE(mDispatcher->waitForIdle());
8625 mFakePolicy->assertNotifyAnrWasNotCalled();
8626}
8627
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008628// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008629// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
8630// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008631TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008633 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008634 WINDOW_LOCATION));
8635
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008636 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008637 ASSERT_TRUE(sequenceNum);
8638 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008639 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008640
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008641 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008642 mWindow->consumeMotionEvent(
8643 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008644 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008645 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008646}
8647
8648// Send a key to the app and have the app not respond right away.
8649TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
8650 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008652 const auto [sequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008653 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008654 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008655 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008656 ASSERT_TRUE(mDispatcher->waitForIdle());
8657}
8658
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008659// We have a focused application, but no focused window
8660TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07008661 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008662 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008663 mWindow->consumeFocusEvent(false);
8664
8665 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008667 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008668 WINDOW_LOCATION));
8669 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
8670 mDispatcher->waitForIdle();
8671 mFakePolicy->assertNotifyAnrWasNotCalled();
8672
8673 // Once a focused event arrives, we get an ANR for this application
8674 // We specify the injection timeout to be smaller than the application timeout, to ensure that
8675 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008676 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008677 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008678 InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008679 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008680 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07008681 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008682 ASSERT_TRUE(mDispatcher->waitForIdle());
8683}
8684
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008685/**
8686 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
8687 * there will not be an ANR.
8688 */
8689TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
8690 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008691 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008692 mWindow->consumeFocusEvent(false);
8693
8694 KeyEvent event;
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07008695 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
8696 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008697 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
8698 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
8699
8700 // Define a valid key down event that is stale (too old).
8701 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Harry Cutts101ee9b2023-07-06 18:04:14 +00008702 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00008703 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008704
8705 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
8706
8707 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00008708 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008709 InputEventInjectionSync::WAIT_FOR_RESULT,
8710 INJECT_EVENT_TIMEOUT, policyFlags);
8711 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
8712 << "Injection should fail because the event is stale";
8713
8714 ASSERT_TRUE(mDispatcher->waitForIdle());
8715 mFakePolicy->assertNotifyAnrWasNotCalled();
8716 mWindow->assertNoEvents();
8717}
8718
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008719// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008720// Make sure that we don't notify policy twice about the same ANR.
8721TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008722 const std::chrono::duration appTimeout = 400ms;
8723 mApplication->setDispatchingTimeout(appTimeout);
8724 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
8725
Vishnu Nair47074b82020-08-14 11:54:47 -07008726 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008727 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008728 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008729
8730 // Once a focused event arrives, we get an ANR for this application
8731 // We specify the injection timeout to be smaller than the application timeout, to ensure that
8732 // injection times out (instead of failing).
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008733 const std::chrono::duration eventInjectionTimeout = 100ms;
8734 ASSERT_LT(eventInjectionTimeout, appTimeout);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008735 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008736 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008737 InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout,
8738 /*allowKeyRepeat=*/false);
8739 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
8740 << "result=" << ftl::enum_string(result);
8741 // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
8742 // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
8743 std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
8744 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008745
Vishnu Naire4df8752022-09-08 09:17:55 -07008746 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008747 // ANR should not be raised again. It is up to policy to do that if it desires.
8748 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008749
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008750 // If we now get a focused window, the ANR should stop, but the policy handles that via
8751 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008752 ASSERT_TRUE(mDispatcher->waitForIdle());
8753}
8754
8755// We have a focused application, but no focused window
8756TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07008757 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008758 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008759 mWindow->consumeFocusEvent(false);
8760
8761 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008762 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008763
Vishnu Naire4df8752022-09-08 09:17:55 -07008764 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
8765 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008766
8767 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008768 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008769 ASSERT_TRUE(mDispatcher->waitForIdle());
8770 mWindow->assertNoEvents();
8771}
8772
8773/**
8774 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
8775 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
8776 * If we process 1 of the events, but ANR on the second event with the same timestamp,
8777 * the ANR mechanism should still work.
8778 *
8779 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
8780 * DOWN event, while not responding on the second one.
8781 */
8782TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
8783 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008784 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008785 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
8786 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8787 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008788 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008789
8790 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008791 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008792 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
8793 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8794 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008795 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008796
8797 // We have now sent down and up. Let's consume first event and then ANR on the second.
8798 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8799 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008800 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008801}
8802
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008803// A spy window can receive an ANR
8804TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
8805 sp<FakeWindowHandle> spy = addSpyWindow();
8806
8807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008808 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008809 WINDOW_LOCATION));
8810 mWindow->consumeMotionDown();
8811
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008812 const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008813 ASSERT_TRUE(sequenceNum);
8814 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008815 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008816
8817 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008818 spy->consumeMotionEvent(
8819 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008820 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008821 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008822}
8823
8824// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008825// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008826TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
8827 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008828
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008829 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008830 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008831 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008833
8834 // Stuck on the ACTION_UP
8835 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008836 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008837
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008838 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008839 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008840 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8841 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008842
8843 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
8844 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008845 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008846 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008847 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008848}
8849
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008850// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008851// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008852TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
8853 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008854
8855 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008856 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8857 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008858
8859 mWindow->consumeMotionDown();
8860 // Stuck on the ACTION_UP
8861 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008862 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008863
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008864 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008865 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008866 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8867 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008868
8869 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
8870 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008871 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008872 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008873 spy->assertNoEvents();
8874}
8875
8876TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008877 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008878
Prabir Pradhanfb549072023-10-05 19:17:36 +00008879 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008880
8881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008882 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008883 WINDOW_LOCATION));
8884
8885 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8886 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
8887 ASSERT_TRUE(consumeSeq);
8888
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008889 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
8890 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008891
8892 monitor.finishEvent(*consumeSeq);
8893 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
8894
8895 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008896 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008897}
8898
8899// If a window is unresponsive, then you get anr. if the window later catches up and starts to
8900// process events, you don't get an anr. When the window later becomes unresponsive again, you
8901// get an ANR again.
8902// 1. tap -> block on ACTION_UP -> receive ANR
8903// 2. consume all pending events (= queue becomes healthy again)
8904// 3. tap again -> block on ACTION_UP again -> receive ANR second time
8905TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
8906 tapOnWindow();
8907
8908 mWindow->consumeMotionDown();
8909 // Block on ACTION_UP
8910 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008911 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008912 mWindow->consumeMotionUp(); // Now the connection should be healthy again
8913 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008914 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008915 mWindow->assertNoEvents();
8916
8917 tapOnWindow();
8918 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008919 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008920 mWindow->consumeMotionUp();
8921
8922 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008923 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008924 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008925 mWindow->assertNoEvents();
8926}
8927
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008928// If a connection remains unresponsive for a while, make sure policy is only notified once about
8929// it.
8930TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008931 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008932 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008933 WINDOW_LOCATION));
8934
8935 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008936 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008937 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008938 // 'notifyConnectionUnresponsive' should only be called once per connection
8939 mFakePolicy->assertNotifyAnrWasNotCalled();
8940 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008941 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008942 mWindow->consumeMotionEvent(
8943 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008944 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008945 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008946 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008947 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008948}
8949
8950/**
8951 * 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 -07008952 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008953 */
8954TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008955 // The timeouts in this test are established by relying on the fact that the "key waiting for
8956 // events timeout" is equal to 500ms.
8957 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008958 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008959 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008960
8961 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008962 const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008963 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008964 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008965 ASSERT_TRUE(upSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008966
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008967 // Don't finish the events yet, and send a key
8968 mDispatcher->notifyKey(
8969 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8970 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8971 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008972 // Key will not be sent to the window, yet, because the window is still processing events
8973 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008974 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008975 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008976
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008977 std::this_thread::sleep_for(400ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008978 // if we wait long enough though, dispatcher will give up, and still send the key
8979 // to the focused window, even though we have not yet finished the motion event
8980 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8981 mWindow->finishEvent(*downSequenceNum);
8982 mWindow->finishEvent(*upSequenceNum);
8983}
8984
8985/**
8986 * If a window is processing a motion event, and then a key event comes in, the key event should
8987 * not go to the focused window until the motion is processed.
8988 * If then a new motion comes in, then the pending key event should be going to the currently
8989 * focused window right away.
8990 */
8991TEST_F(InputDispatcherSingleWindowAnr,
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08008992 PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) {
8993 // The timeouts in this test are established by relying on the fact that the "key waiting for
8994 // events timeout" is equal to 500ms.
8995 ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008996 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008997 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008998
8999 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009000 const auto& [downSequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009001 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009002 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009003 ASSERT_TRUE(upSequenceNum);
9004 // Don't finish the events yet, and send a key
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08009005 mDispatcher->notifyKey(
9006 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
9007 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
9008 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009009 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08009010 mWindow->assertNoEvents(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009011
9012 // Now tap down again. It should cause the pending key to go to the focused window right away.
9013 tapOnWindow();
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08009014 // Now that we tapped, we should receive the key immediately.
9015 // Since there's still room for slowness, we use 200ms, which is much less than
9016 // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration.
9017 std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms);
9018 ASSERT_NE(nullptr, keyEvent);
9019 ASSERT_EQ(InputEventType::KEY, keyEvent->getType());
9020 ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN));
9021 // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any
9022 // order.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009023 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
9024 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08009025 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9026 mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009027 mWindow->assertNoEvents();
9028}
9029
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07009030/**
9031 * Send an event to the app and have the app not respond right away.
9032 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
9033 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
9034 * At some point, the window becomes responsive again.
9035 * Ensure that subsequent events get dropped, and the next gesture is delivered.
9036 */
9037TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
9038 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9039 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
9040 .build());
9041
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009042 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07009043 ASSERT_TRUE(sequenceNum);
9044 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9045 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
9046
9047 mWindow->finishEvent(*sequenceNum);
9048 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
9049 ASSERT_TRUE(mDispatcher->waitForIdle());
9050 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
9051
9052 // Now that the window is responsive, let's continue the gesture.
9053 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
9054 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9055 .build());
9056
9057 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9058 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9059 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
9060 .build());
9061
9062 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
9063 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9064 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
9065 .build());
9066 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9067 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
9068 .build());
9069 // We already canceled this pointer, so the window shouldn't get any new events.
9070 mWindow->assertNoEvents();
9071
9072 // Start another one.
9073 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9074 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
9075 .build());
9076 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9077}
9078
Prabir Pradhanfc364722024-02-08 17:51:20 +00009079// Send an event to the app and have the app not respond right away. Then remove the app window.
9080// When the window is removed, the dispatcher will cancel the events for that window.
9081// So InputDispatcher will enqueue ACTION_CANCEL event as well.
9082TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) {
9083 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9084 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9085 {WINDOW_LOCATION}));
9086
9087 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
9088 ASSERT_TRUE(sequenceNum);
9089
9090 // Remove the window, but the input channel should remain alive.
9091 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
9092
9093 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9094 // Since the window was removed, Dispatcher does not know the PID associated with the window
9095 // anymore, so the policy is notified without the PID.
9096 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(),
9097 /*pid=*/std::nullopt);
9098
9099 mWindow->finishEvent(*sequenceNum);
9100 // The cancellation was generated when the window was removed, along with the focus event.
9101 mWindow->consumeMotionEvent(
9102 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
9103 mWindow->consumeFocusEvent(false);
9104 ASSERT_TRUE(mDispatcher->waitForIdle());
9105 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
9106}
9107
9108// Send an event to the app and have the app not respond right away. Wait for the policy to be
9109// notified of the unresponsive window, then remove the app window.
9110TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) {
9111 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9112 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9113 {WINDOW_LOCATION}));
9114
9115 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
9116 ASSERT_TRUE(sequenceNum);
9117 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
9118 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
9119
9120 // Remove the window, but the input channel should remain alive.
9121 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
9122
9123 mWindow->finishEvent(*sequenceNum);
9124 // The cancellation was generated during the ANR, and the window lost focus when it was removed.
9125 mWindow->consumeMotionEvent(
9126 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
9127 mWindow->consumeFocusEvent(false);
9128 ASSERT_TRUE(mDispatcher->waitForIdle());
9129 // Since the window was removed, Dispatcher does not know the PID associated with the window
9130 // becoming responsive, so the policy is notified without the PID.
9131 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt);
9132}
9133
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009134class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
9135 virtual void SetUp() override {
9136 InputDispatcherTest::SetUp();
9137
Chris Yea209fde2020-07-22 13:54:51 -07009138 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009139 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009140 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
9141 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009142 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009143 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009144 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009145
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009146 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
9147 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009148 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009149 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009150
9151 // Set focused application.
9152 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07009153 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009154
9155 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009156 mDispatcher->onWindowInfosChanged(
9157 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009158 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009159 mFocusedWindow->consumeFocusEvent(true);
9160 }
9161
9162 virtual void TearDown() override {
9163 InputDispatcherTest::TearDown();
9164
9165 mUnfocusedWindow.clear();
9166 mFocusedWindow.clear();
9167 }
9168
9169protected:
Chris Yea209fde2020-07-22 13:54:51 -07009170 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009171 sp<FakeWindowHandle> mUnfocusedWindow;
9172 sp<FakeWindowHandle> mFocusedWindow;
9173 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
9174 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
9175 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
9176
9177 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
9178
9179 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
9180
9181private:
9182 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009183 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009184 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009185 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009186 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009187 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009188 location));
9189 }
9190};
9191
9192// If we have 2 windows that are both unresponsive, the one with the shortest timeout
9193// should be ANR'd first.
9194TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009196 injectMotionEvent(*mDispatcher,
9197 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9198 AINPUT_SOURCE_TOUCHSCREEN)
9199 .pointer(PointerBuilder(0, ToolType::FINGER)
9200 .x(FOCUSED_WINDOW_LOCATION.x)
9201 .y(FOCUSED_WINDOW_LOCATION.y))
9202 .build()));
9203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9204 injectMotionEvent(*mDispatcher,
9205 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
9206 AINPUT_SOURCE_TOUCHSCREEN)
9207 .pointer(PointerBuilder(0, ToolType::FINGER)
9208 .x(FOCUSED_WINDOW_LOCATION.x)
9209 .y(FOCUSED_WINDOW_LOCATION.y))
9210 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009211 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009212 mFocusedWindow->consumeMotionUp();
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009213 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009214 // We consumed all events, so no ANR
9215 ASSERT_TRUE(mDispatcher->waitForIdle());
9216 mFakePolicy->assertNotifyAnrWasNotCalled();
9217
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009219 injectMotionEvent(*mDispatcher,
9220 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9221 AINPUT_SOURCE_TOUCHSCREEN)
9222 .pointer(PointerBuilder(0, ToolType::FINGER)
9223 .x(FOCUSED_WINDOW_LOCATION.x)
9224 .y(FOCUSED_WINDOW_LOCATION.y))
9225 .build()));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009226 const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009227 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009228
9229 const std::chrono::duration timeout =
9230 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009231 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009232
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009233 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009234 mFocusedWindow->consumeMotionDown();
9235 // This cancel is generated because the connection was unresponsive
9236 mFocusedWindow->consumeMotionCancel();
9237 mFocusedWindow->assertNoEvents();
9238 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009239 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009240 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9241 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009242 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009243}
9244
9245// If we have 2 windows with identical timeouts that are both unresponsive,
9246// it doesn't matter which order they should have ANR.
9247// But we should receive ANR for both.
9248TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
9249 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009250 mUnfocusedWindow->setDispatchingTimeout(
9251 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009252 mDispatcher->onWindowInfosChanged(
9253 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009254
9255 tapOnFocusedWindow();
9256 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009257 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009258 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
9259 mFocusedWindow->getDispatchingTimeout(
9260 DISPATCHING_TIMEOUT)),
9261 mFakePolicy->getUnresponsiveWindowToken(0ms)};
9262
9263 ASSERT_THAT(anrConnectionTokens,
9264 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
9265 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009266
9267 ASSERT_TRUE(mDispatcher->waitForIdle());
9268 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009269
9270 mFocusedWindow->consumeMotionDown();
9271 mFocusedWindow->consumeMotionUp();
9272 mUnfocusedWindow->consumeMotionOutside();
9273
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009274 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
9275 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009276
9277 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009278 ASSERT_THAT(responsiveTokens,
9279 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
9280 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009281 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009282}
9283
9284// If a window is already not responding, the second tap on the same window should be ignored.
9285// We should also log an error to account for the dropped event (not tested here).
9286// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
9287TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
9288 tapOnFocusedWindow();
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009289 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009290 // Receive the events, but don't respond
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009291 const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009292 ASSERT_TRUE(downEventSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009293 const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009294 ASSERT_TRUE(upEventSequenceNum);
9295 const std::chrono::duration timeout =
9296 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009297 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009298
9299 // Tap once again
9300 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009301 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009302 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009303 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009304 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009305 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009306 FOCUSED_WINDOW_LOCATION));
9307 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
9308 // valid touch target
9309 mUnfocusedWindow->assertNoEvents();
9310
9311 // Consume the first tap
9312 mFocusedWindow->finishEvent(*downEventSequenceNum);
9313 mFocusedWindow->finishEvent(*upEventSequenceNum);
9314 ASSERT_TRUE(mDispatcher->waitForIdle());
9315 // The second tap did not go to the focused window
9316 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009317 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08009318 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9319 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009320 mFakePolicy->assertNotifyAnrWasNotCalled();
9321}
9322
9323// If you tap outside of all windows, there will not be ANR
9324TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009325 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009326 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009327 LOCATION_OUTSIDE_ALL_WINDOWS));
9328 ASSERT_TRUE(mDispatcher->waitForIdle());
9329 mFakePolicy->assertNotifyAnrWasNotCalled();
9330}
9331
9332// Since the focused window is paused, tapping on it should not produce any events
9333TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
9334 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009335 mDispatcher->onWindowInfosChanged(
9336 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009337
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009338 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009339 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009340 FOCUSED_WINDOW_LOCATION));
9341
9342 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
9343 ASSERT_TRUE(mDispatcher->waitForIdle());
9344 // Should not ANR because the window is paused, and touches shouldn't go to it
9345 mFakePolicy->assertNotifyAnrWasNotCalled();
9346
9347 mFocusedWindow->assertNoEvents();
9348 mUnfocusedWindow->assertNoEvents();
9349}
9350
9351/**
9352 * 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 -07009353 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009354 * If a different window becomes focused at this time, the key should go to that window instead.
9355 *
9356 * Warning!!!
9357 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
9358 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009359 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009360 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
9361 *
9362 * If that value changes, this test should also change.
9363 */
9364TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
9365 // Set a long ANR timeout to prevent it from triggering
9366 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009367 mDispatcher->onWindowInfosChanged(
9368 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009369
9370 tapOnUnfocusedWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009371 const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009372 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009373 const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009374 ASSERT_TRUE(upSequenceNum);
9375 // Don't finish the events yet, and send a key
9376 // Injection will succeed because we will eventually give up and send the key to the focused
9377 // window even if motions are still being processed.
9378
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009379 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009380 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9381 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009382 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009383 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009384 // and the key remains pending, waiting for the touch events to be processed.
9385 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
9386 // under the hood.
9387 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
9388 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009389
9390 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07009391 mFocusedWindow->setFocusable(false);
9392 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009393 mDispatcher->onWindowInfosChanged(
9394 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009395 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009396
9397 // Focus events should precede the key events
9398 mUnfocusedWindow->consumeFocusEvent(true);
9399 mFocusedWindow->consumeFocusEvent(false);
9400
9401 // Finish the tap events, which should unblock dispatcher
9402 mUnfocusedWindow->finishEvent(*downSequenceNum);
9403 mUnfocusedWindow->finishEvent(*upSequenceNum);
9404
9405 // Now that all queues are cleared and no backlog in the connections, the key event
9406 // can finally go to the newly focused "mUnfocusedWindow".
9407 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9408 mFocusedWindow->assertNoEvents();
9409 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009410 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009411}
9412
9413// When the touch stream is split across 2 windows, and one of them does not respond,
9414// then ANR should be raised and the touch should be canceled for the unresponsive window.
9415// The other window should not be affected by that.
9416TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
9417 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00009418 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9419 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9420 {FOCUSED_WINDOW_LOCATION}));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009421 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009422
9423 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00009424 mDispatcher->notifyMotion(
9425 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9426 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009427
9428 const std::chrono::duration timeout =
9429 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009430 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009431
9432 mUnfocusedWindow->consumeMotionDown();
9433 mFocusedWindow->consumeMotionDown();
9434 // Focused window may or may not receive ACTION_MOVE
9435 // But it should definitely receive ACTION_CANCEL due to the ANR
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009436 const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009437 ASSERT_TRUE(moveOrCancelSequenceNum);
9438 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
9439 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07009440 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009441 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
9442 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
9443 mFocusedWindow->consumeMotionCancel();
9444 } else {
9445 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
9446 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009447 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009448 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9449 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009450
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009451 mUnfocusedWindow->assertNoEvents();
9452 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009453 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009454}
9455
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009456/**
9457 * If we have no focused window, and a key comes in, we start the ANR timer.
9458 * The focused application should add a focused window before the timer runs out to prevent ANR.
9459 *
9460 * If the user touches another application during this time, the key should be dropped.
9461 * Next, if a new focused window comes in, without toggling the focused application,
9462 * then no ANR should occur.
9463 *
9464 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
9465 * but in some cases the policy may not update the focused application.
9466 */
9467TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
9468 std::shared_ptr<FakeApplicationHandle> focusedApplication =
9469 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07009470 focusedApplication->setDispatchingTimeout(300ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009471 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
9472 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
9473 mFocusedWindow->setFocusable(false);
9474
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009475 mDispatcher->onWindowInfosChanged(
9476 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009477 mFocusedWindow->consumeFocusEvent(false);
9478
9479 // Send a key. The ANR timer should start because there is no focused window.
9480 // 'focusedApplication' will get blamed if this timer completes.
9481 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009482 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009483 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9484 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +00009485 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009486 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009487
9488 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
9489 // then the injected touches won't cause the focused event to get dropped.
9490 // The dispatcher only checks for whether the queue should be pruned upon queueing.
9491 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
9492 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
9493 // For this test, it means that the key would get delivered to the window once it becomes
9494 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009495 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009496
9497 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00009498 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9499 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9500 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009501
9502 // We do not consume the motion right away, because that would require dispatcher to first
9503 // process (== drop) the key event, and by that time, ANR will be raised.
9504 // Set the focused window first.
9505 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009506 mDispatcher->onWindowInfosChanged(
9507 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009508 setFocusedWindow(mFocusedWindow);
9509 mFocusedWindow->consumeFocusEvent(true);
9510 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
9511 // to another application. This could be a bug / behaviour in the policy.
9512
9513 mUnfocusedWindow->consumeMotionDown();
9514
9515 ASSERT_TRUE(mDispatcher->waitForIdle());
9516 // Should not ANR because we actually have a focused window. It was just added too slowly.
9517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
9518}
9519
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08009520/**
9521 * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having
9522 * an inconsistent event stream inside the dispatcher. In this test, we make sure that the
9523 * dispatcher doesn't prune pointer events incorrectly.
9524 *
9525 * This test reproduces a crash in InputDispatcher.
9526 * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur.
9527 *
9528 * Keep the currently focused application (mApplication), and have no focused window.
9529 * We set up two additional windows:
9530 * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the
9531 * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application
9532 * window. This window is not focusable, but is touchable.
9533 *
9534 * We first touch the navigation bar, which causes it to inject a key. Since there's no focused
9535 * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now
9536 * blocked. The dispatcher is waiting for 'mApplication' to add a focused window.
9537 *
9538 * Now, we touch "Another window". This window is owned by a different application than
9539 * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused
9540 * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start
9541 * dropping the events from its queue. Ensure that no crash occurs.
9542 *
9543 * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale.
9544 * This does not affect the test running time.
9545 */
9546TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) {
9547 std::shared_ptr<FakeApplicationHandle> systemUiApplication =
9548 std::make_shared<FakeApplicationHandle>();
9549 systemUiApplication->setDispatchingTimeout(3000ms);
9550 mFakePolicy->setStaleEventTimeout(3000ms);
9551 sp<FakeWindowHandle> navigationBar =
9552 sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar",
9553 ADISPLAY_ID_DEFAULT);
9554 navigationBar->setFocusable(false);
9555 navigationBar->setWatchOutsideTouch(true);
9556 navigationBar->setFrame(Rect(0, 0, 100, 100));
9557
9558 mApplication->setDispatchingTimeout(3000ms);
9559 // 'mApplication' is already focused, but we call it again here to make it explicit.
9560 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
9561
9562 std::shared_ptr<FakeApplicationHandle> anotherApplication =
9563 std::make_shared<FakeApplicationHandle>();
9564 sp<FakeWindowHandle> appWindow =
9565 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window",
9566 ADISPLAY_ID_DEFAULT);
9567 appWindow->setFocusable(false);
9568 appWindow->setFrame(Rect(100, 100, 200, 200));
9569
9570 mDispatcher->onWindowInfosChanged(
9571 {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0});
9572 // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus
9573 mFocusedWindow->consumeFocusEvent(false);
9574
9575 // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher
9576 // in response.
9577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9578 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9579 .build());
9580 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9581
9582 // Key will not be sent anywhere because we have no focused window. It will remain pending.
9583 // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is.
9584 InputEventInjectionResult result =
9585 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9586 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
9587 /*allowKeyRepeat=*/false);
9588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
9589
9590 // Finish the gesture - lift up finger and inject ACTION_UP key event
9591 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9592 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9593 .build());
9594 result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9595 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
9596 /*allowKeyRepeat=*/false);
9597 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
9598 // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be
9599 // getting any events yet.
9600 navigationBar->assertNoEvents();
9601
9602 // Now touch "Another window". This touch is going to a different application than the one we
9603 // are waiting for (which is 'mApplication').
9604 // This should cause the dispatcher to drop the pending focus-dispatched events (like the key
9605 // trying to be injected) and to continue processing the rest of the events in the original
9606 // order.
9607 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9608 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
9609 .build());
9610 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP));
9611 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE));
9612 appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9613
9614 appWindow->assertNoEvents();
9615 navigationBar->assertNoEvents();
9616}
9617
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009618// These tests ensure we cannot send touch events to a window that's positioned behind a window
9619// that has feature NO_INPUT_CHANNEL.
9620// Layout:
9621// Top (closest to user)
9622// mNoInputWindow (above all windows)
9623// mBottomWindow
9624// Bottom (furthest from user)
9625class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
9626 virtual void SetUp() override {
9627 InputDispatcherTest::SetUp();
9628
9629 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009630 mNoInputWindow =
9631 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
9632 "Window without input channel", ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00009633 /*createInputChannel=*/false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009634 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009635 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
9636 // It's perfectly valid for this window to not have an associated input channel
9637
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009638 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
9639 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009640 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
9641
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009642 mDispatcher->onWindowInfosChanged(
9643 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009644 }
9645
9646protected:
9647 std::shared_ptr<FakeApplicationHandle> mApplication;
9648 sp<FakeWindowHandle> mNoInputWindow;
9649 sp<FakeWindowHandle> mBottomWindow;
9650};
9651
9652TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
9653 PointF touchedPoint = {10, 10};
9654
Prabir Pradhan678438e2023-04-13 19:32:51 +00009655 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9656 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9657 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009658
9659 mNoInputWindow->assertNoEvents();
9660 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
9661 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
9662 // and therefore should prevent mBottomWindow from receiving touches
9663 mBottomWindow->assertNoEvents();
9664}
9665
9666/**
9667 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
9668 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
9669 */
9670TEST_F(InputDispatcherMultiWindowOcclusionTests,
9671 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009672 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
9673 "Window with input channel and NO_INPUT_CHANNEL",
9674 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009675
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009676 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009677 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009678 mDispatcher->onWindowInfosChanged(
9679 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009680
9681 PointF touchedPoint = {10, 10};
9682
Prabir Pradhan678438e2023-04-13 19:32:51 +00009683 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9684 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9685 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009686
9687 mNoInputWindow->assertNoEvents();
9688 mBottomWindow->assertNoEvents();
9689}
9690
Vishnu Nair958da932020-08-21 17:12:37 -07009691class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
9692protected:
9693 std::shared_ptr<FakeApplicationHandle> mApp;
9694 sp<FakeWindowHandle> mWindow;
9695 sp<FakeWindowHandle> mMirror;
9696
9697 virtual void SetUp() override {
9698 InputDispatcherTest::SetUp();
9699 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009700 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00009701 mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07009702 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
9703 mWindow->setFocusable(true);
9704 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009705 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009706 }
9707};
9708
9709TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
9710 // Request focus on a mirrored window
9711 setFocusedWindow(mMirror);
9712
9713 // window gets focused
9714 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009715 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009716 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009717 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
9718}
9719
9720// A focused & mirrored window remains focused only if the window and its mirror are both
9721// focusable.
9722TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
9723 setFocusedWindow(mMirror);
9724
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009725 // window gets focused because it is above the mirror
Vishnu Nair958da932020-08-21 17:12:37 -07009726 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009728 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009729 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009731 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009732 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9733
9734 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009735 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009736
9737 // window loses focus since one of the windows associated with the token in not focusable
9738 mWindow->consumeFocusEvent(false);
9739
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009740 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009741 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009742 mWindow->assertNoEvents();
9743}
9744
9745// A focused & mirrored window remains focused until the window and its mirror both become
9746// invisible.
9747TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
9748 setFocusedWindow(mMirror);
9749
9750 // window gets focused
9751 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009752 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009753 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009754 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009755 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009756 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009757 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9758
9759 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009760 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009761
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009762 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009763 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009764 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009765 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009766 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009767 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9768
9769 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009770 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009771
9772 // window loses focus only after all windows associated with the token become invisible.
9773 mWindow->consumeFocusEvent(false);
9774
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009775 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009776 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009777 mWindow->assertNoEvents();
9778}
9779
9780// A focused & mirrored window remains focused until both windows are removed.
9781TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
9782 setFocusedWindow(mMirror);
9783
9784 // window gets focused
9785 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009787 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009788 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009789 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009790 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009791 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9792
9793 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009794 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009795
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009797 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009798 mMirror->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009800 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00009801 mMirror->consumeKeyUp(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07009802
9803 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009804 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009805 mWindow->consumeFocusEvent(false);
9806
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009807 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009808 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009809 mWindow->assertNoEvents();
9810}
9811
9812// Focus request can be pending until one window becomes visible.
9813TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
9814 // Request focus on an invisible mirror.
9815 mWindow->setVisible(false);
9816 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009817 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009818 setFocusedWindow(mMirror);
9819
9820 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009821 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009822 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9823 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07009824
9825 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009826 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009827
9828 // window gets focused
9829 mWindow->consumeFocusEvent(true);
9830 // window gets the pending key event
9831 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9832}
Prabir Pradhan99987712020-11-10 18:43:05 -08009833
9834class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
9835protected:
9836 std::shared_ptr<FakeApplicationHandle> mApp;
9837 sp<FakeWindowHandle> mWindow;
9838 sp<FakeWindowHandle> mSecondWindow;
9839
9840 void SetUp() override {
9841 InputDispatcherTest::SetUp();
9842 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009843 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08009844 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009845 mSecondWindow =
9846 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08009847 mSecondWindow->setFocusable(true);
9848
9849 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009850 mDispatcher->onWindowInfosChanged(
9851 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -08009852
9853 setFocusedWindow(mWindow);
9854 mWindow->consumeFocusEvent(true);
9855 }
9856
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009857 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009858 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08009859 }
9860
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009861 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
9862 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08009863 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009864 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
9865 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08009866 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009867 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08009868 }
9869};
9870
9871TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
9872 // Ensure that capture cannot be obtained for unfocused windows.
9873 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
9874 mFakePolicy->assertSetPointerCaptureNotCalled();
9875 mSecondWindow->assertNoEvents();
9876
9877 // Ensure that capture can be enabled from the focus window.
9878 requestAndVerifyPointerCapture(mWindow, true);
9879
9880 // Ensure that capture cannot be disabled from a window that does not have capture.
9881 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
9882 mFakePolicy->assertSetPointerCaptureNotCalled();
9883
9884 // Ensure that capture can be disabled from the window with capture.
9885 requestAndVerifyPointerCapture(mWindow, false);
9886}
9887
9888TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009889 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08009890
9891 setFocusedWindow(mSecondWindow);
9892
9893 // Ensure that the capture disabled event was sent first.
9894 mWindow->consumeCaptureEvent(false);
9895 mWindow->consumeFocusEvent(false);
9896 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009897 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08009898
9899 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009900 notifyPointerCaptureChanged({});
9901 notifyPointerCaptureChanged(request);
9902 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08009903 mWindow->assertNoEvents();
9904 mSecondWindow->assertNoEvents();
9905 mFakePolicy->assertSetPointerCaptureNotCalled();
9906}
9907
9908TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009909 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08009910
9911 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009912 notifyPointerCaptureChanged({});
9913 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08009914
9915 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009916 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08009917 mWindow->consumeCaptureEvent(false);
9918 mWindow->assertNoEvents();
9919}
9920
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009921TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
9922 requestAndVerifyPointerCapture(mWindow, true);
9923
9924 // The first window loses focus.
9925 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009926 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009927 mWindow->consumeCaptureEvent(false);
9928
9929 // Request Pointer Capture from the second window before the notification from InputReader
9930 // arrives.
9931 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009932 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009933
9934 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009935 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009936
9937 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009938 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009939
9940 mSecondWindow->consumeFocusEvent(true);
9941 mSecondWindow->consumeCaptureEvent(true);
9942}
9943
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009944TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
9945 // App repeatedly enables and disables capture.
9946 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9947 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9948 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
9949 mFakePolicy->assertSetPointerCaptureCalled(false);
9950 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9951 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9952
9953 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
9954 // first request is now stale, this should do nothing.
9955 notifyPointerCaptureChanged(firstRequest);
9956 mWindow->assertNoEvents();
9957
9958 // InputReader notifies that the second request was enabled.
9959 notifyPointerCaptureChanged(secondRequest);
9960 mWindow->consumeCaptureEvent(true);
9961}
9962
Prabir Pradhan7092e262022-05-03 16:51:09 +00009963TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
9964 requestAndVerifyPointerCapture(mWindow, true);
9965
9966 // App toggles pointer capture off and on.
9967 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
9968 mFakePolicy->assertSetPointerCaptureCalled(false);
9969
9970 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9971 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9972
9973 // InputReader notifies that the latest "enable" request was processed, while skipping over the
9974 // preceding "disable" request.
9975 notifyPointerCaptureChanged(enableRequest);
9976
9977 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
9978 // any notifications.
9979 mWindow->assertNoEvents();
9980}
9981
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009982/**
9983 * One window. Hover mouse in the window, and then start capture. Make sure that the relative
9984 * mouse movements don't affect the previous mouse hovering state.
9985 * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no
9986 * HOVER_MOVE events).
9987 */
9988TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) {
9989 // Mouse hover on the window
9990 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
9991 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
9992 .build());
9993 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
9994 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
9995 .build());
9996
9997 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER)));
9998 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE)));
9999
10000 // Start pointer capture
10001 requestAndVerifyPointerCapture(mWindow, true);
10002
10003 // Send some relative mouse movements and receive them in the window.
10004 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE)
10005 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11))
10006 .build());
10007 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11),
10008 WithSource(AINPUT_SOURCE_MOUSE_RELATIVE)));
10009
10010 // Stop pointer capture
10011 requestAndVerifyPointerCapture(mWindow, false);
10012
10013 // Continue hovering on the window
10014 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
10015 .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115))
10016 .build());
10017 mWindow->consumeMotionEvent(
10018 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
10019
10020 mWindow->assertNoEvents();
10021}
10022
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010023class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
10024protected:
10025 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +000010026
10027 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
10028 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
10029
10030 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
10031 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
10032
10033 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
10034 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
10035 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
10036 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
10037 MAXIMUM_OBSCURING_OPACITY);
10038
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010039 static constexpr gui::Uid TOUCHED_APP_UID{10001};
10040 static constexpr gui::Uid APP_B_UID{10002};
10041 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010042
10043 sp<FakeWindowHandle> mTouchWindow;
10044
10045 virtual void SetUp() override {
10046 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010047 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010048 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
10049 }
10050
10051 virtual void TearDown() override {
10052 InputDispatcherTest::TearDown();
10053 mTouchWindow.clear();
10054 }
10055
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010056 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -050010057 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010058 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010059 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010060 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010061 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010062 return window;
10063 }
10064
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010065 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010066 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
10067 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010068 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010069 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010070 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010071 return window;
10072 }
10073
10074 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000010075 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10076 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10077 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010078 }
10079};
10080
10081TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010082 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010083 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010084 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010085
10086 touch();
10087
10088 mTouchWindow->assertNoEvents();
10089}
10090
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010091TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +000010092 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
10093 const sp<FakeWindowHandle>& w =
10094 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010095 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010096
10097 touch();
10098
10099 mTouchWindow->assertNoEvents();
10100}
10101
10102TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010103 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
10104 const sp<FakeWindowHandle>& w =
10105 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010106 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010107
10108 touch();
10109
10110 w->assertNoEvents();
10111}
10112
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010113TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010114 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010115 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010116
10117 touch();
10118
10119 mTouchWindow->consumeAnyMotionDown();
10120}
10121
10122TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010123 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010124 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010125 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010126 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010127
10128 touch({PointF{100, 100}});
10129
10130 mTouchWindow->consumeAnyMotionDown();
10131}
10132
10133TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010134 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010135 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010136 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010137
10138 touch();
10139
10140 mTouchWindow->consumeAnyMotionDown();
10141}
10142
10143TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
10144 const sp<FakeWindowHandle>& w =
10145 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010146 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010147
10148 touch();
10149
10150 mTouchWindow->consumeAnyMotionDown();
10151}
10152
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010153TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
10154 const sp<FakeWindowHandle>& w =
10155 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010156 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010157
10158 touch();
10159
10160 w->assertNoEvents();
10161}
10162
10163/**
10164 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
10165 * inside) while letting them pass-through. Note that even though touch passes through the occluding
10166 * window, the occluding window will still receive ACTION_OUTSIDE event.
10167 */
10168TEST_F(InputDispatcherUntrustedTouchesTest,
10169 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
10170 const sp<FakeWindowHandle>& w =
10171 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010172 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010173 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010174
10175 touch();
10176
10177 w->consumeMotionOutside();
10178}
10179
10180TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
10181 const sp<FakeWindowHandle>& w =
10182 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010183 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010184 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010185
10186 touch();
10187
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080010188 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010189}
10190
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010191TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010192 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010193 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10194 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010195 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010196
10197 touch();
10198
10199 mTouchWindow->consumeAnyMotionDown();
10200}
10201
10202TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
10203 const sp<FakeWindowHandle>& w =
10204 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10205 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010206 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010207
10208 touch();
10209
10210 mTouchWindow->consumeAnyMotionDown();
10211}
10212
10213TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010214 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010215 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10216 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010217 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010218
10219 touch();
10220
10221 mTouchWindow->assertNoEvents();
10222}
10223
10224TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
10225 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
10226 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010227 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
10228 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010229 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010230 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
10231 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010232 mDispatcher->onWindowInfosChanged(
10233 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010234
10235 touch();
10236
10237 mTouchWindow->assertNoEvents();
10238}
10239
10240TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
10241 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
10242 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010243 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
10244 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010245 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010246 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
10247 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010248 mDispatcher->onWindowInfosChanged(
10249 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010250
10251 touch();
10252
10253 mTouchWindow->consumeAnyMotionDown();
10254}
10255
10256TEST_F(InputDispatcherUntrustedTouchesTest,
10257 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
10258 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010259 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10260 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010261 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010262 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10263 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010264 mDispatcher->onWindowInfosChanged(
10265 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010266
10267 touch();
10268
10269 mTouchWindow->consumeAnyMotionDown();
10270}
10271
10272TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
10273 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010274 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10275 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010276 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010277 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10278 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010279 mDispatcher->onWindowInfosChanged(
10280 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010281
10282 touch();
10283
10284 mTouchWindow->assertNoEvents();
10285}
10286
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010287TEST_F(InputDispatcherUntrustedTouchesTest,
10288 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
10289 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010290 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10291 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010292 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010293 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10294 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010295 mDispatcher->onWindowInfosChanged(
10296 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010297
10298 touch();
10299
10300 mTouchWindow->assertNoEvents();
10301}
10302
10303TEST_F(InputDispatcherUntrustedTouchesTest,
10304 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
10305 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010306 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10307 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010308 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010309 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10310 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010311 mDispatcher->onWindowInfosChanged(
10312 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010313
10314 touch();
10315
10316 mTouchWindow->consumeAnyMotionDown();
10317}
10318
10319TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
10320 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010321 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10322 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010323 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010324
10325 touch();
10326
10327 mTouchWindow->consumeAnyMotionDown();
10328}
10329
10330TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
10331 const sp<FakeWindowHandle>& w =
10332 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010333 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010334
10335 touch();
10336
10337 mTouchWindow->consumeAnyMotionDown();
10338}
10339
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010340TEST_F(InputDispatcherUntrustedTouchesTest,
10341 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
10342 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
10343 const sp<FakeWindowHandle>& w =
10344 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010345 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010346
10347 touch();
10348
10349 mTouchWindow->assertNoEvents();
10350}
10351
10352TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
10353 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
10354 const sp<FakeWindowHandle>& w =
10355 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010356 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010357
10358 touch();
10359
10360 mTouchWindow->consumeAnyMotionDown();
10361}
10362
10363TEST_F(InputDispatcherUntrustedTouchesTest,
10364 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
10365 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
10366 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010367 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10368 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010369 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010370
10371 touch();
10372
10373 mTouchWindow->consumeAnyMotionDown();
10374}
10375
10376TEST_F(InputDispatcherUntrustedTouchesTest,
10377 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
10378 const sp<FakeWindowHandle>& w1 =
10379 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
10380 OPACITY_BELOW_THRESHOLD);
10381 const sp<FakeWindowHandle>& w2 =
10382 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10383 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010384 mDispatcher->onWindowInfosChanged(
10385 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010386
10387 touch();
10388
10389 mTouchWindow->assertNoEvents();
10390}
10391
10392/**
10393 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
10394 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
10395 * (which alone would result in allowing touches) does not affect the blocking behavior.
10396 */
10397TEST_F(InputDispatcherUntrustedTouchesTest,
10398 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
10399 const sp<FakeWindowHandle>& wB =
10400 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
10401 OPACITY_BELOW_THRESHOLD);
10402 const sp<FakeWindowHandle>& wC =
10403 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10404 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010405 mDispatcher->onWindowInfosChanged(
10406 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010407
10408 touch();
10409
10410 mTouchWindow->assertNoEvents();
10411}
10412
10413/**
10414 * This test is testing that a window from a different UID but with same application token doesn't
10415 * block the touch. Apps can share the application token for close UI collaboration for example.
10416 */
10417TEST_F(InputDispatcherUntrustedTouchesTest,
10418 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
10419 const sp<FakeWindowHandle>& w =
10420 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
10421 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010422 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010423
10424 touch();
10425
10426 mTouchWindow->consumeAnyMotionDown();
10427}
10428
arthurhungb89ccb02020-12-30 16:19:01 +080010429class InputDispatcherDragTests : public InputDispatcherTest {
10430protected:
10431 std::shared_ptr<FakeApplicationHandle> mApp;
10432 sp<FakeWindowHandle> mWindow;
10433 sp<FakeWindowHandle> mSecondWindow;
10434 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010435 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010436 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
10437 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +080010438
10439 void SetUp() override {
10440 InputDispatcherTest::SetUp();
10441 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010442 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080010443 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080010444
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010445 mSecondWindow =
10446 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080010447 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080010448
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010449 mSpyWindow =
10450 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010451 mSpyWindow->setSpy(true);
10452 mSpyWindow->setTrustedOverlay(true);
10453 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
10454
arthurhungb89ccb02020-12-30 16:19:01 +080010455 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010456 mDispatcher->onWindowInfosChanged(
10457 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
10458 {},
10459 0,
10460 0});
arthurhungb89ccb02020-12-30 16:19:01 +080010461 }
10462
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010463 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
10464 switch (fromSource) {
10465 case AINPUT_SOURCE_TOUCHSCREEN:
10466 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010467 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010468 ADISPLAY_ID_DEFAULT, {50, 50}))
10469 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10470 break;
10471 case AINPUT_SOURCE_STYLUS:
10472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010473 injectMotionEvent(*mDispatcher,
10474 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10475 AINPUT_SOURCE_STYLUS)
10476 .buttonState(
10477 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
10478 .pointer(PointerBuilder(0, ToolType::STYLUS)
10479 .x(50)
10480 .y(50))
10481 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010482 break;
10483 case AINPUT_SOURCE_MOUSE:
10484 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010485 injectMotionEvent(*mDispatcher,
10486 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10487 AINPUT_SOURCE_MOUSE)
10488 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
10489 .pointer(PointerBuilder(MOUSE_POINTER_ID,
10490 ToolType::MOUSE)
10491 .x(50)
10492 .y(50))
10493 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010494 break;
10495 default:
10496 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
10497 }
arthurhungb89ccb02020-12-30 16:19:01 +080010498
10499 // Window should receive motion event.
10500 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010501 // Spy window should also receive motion event
10502 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000010503 }
10504
10505 // Start performing drag, we will create a drag window and transfer touch to it.
10506 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
10507 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010508 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +000010509 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010510 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +000010511 }
arthurhungb89ccb02020-12-30 16:19:01 +080010512
10513 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010514 mDragWindow =
10515 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010516 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010517 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
10518 *mWindow->getInfo(), *mSecondWindow->getInfo()},
10519 {},
10520 0,
10521 0});
arthurhungb89ccb02020-12-30 16:19:01 +080010522
10523 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +000010524 bool transferred =
Prabir Pradhan367f3432024-02-13 23:05:58 +000010525 mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(),
10526 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +000010527 if (transferred) {
10528 mWindow->consumeMotionCancel();
Prabir Pradhan65455c72024-02-13 21:46:41 +000010529 mDragWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung54745652022-04-20 07:17:41 +000010530 }
10531 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +080010532 }
10533};
10534
10535TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010536 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +080010537
10538 // Move on window.
10539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010540 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010541 ADISPLAY_ID_DEFAULT, {50, 50}))
10542 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010543 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080010544 mWindow->consumeDragEvent(false, 50, 50);
10545 mSecondWindow->assertNoEvents();
10546
10547 // Move to another 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, {150, 50}))
10551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010552 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080010553 mWindow->consumeDragEvent(true, 150, 50);
10554 mSecondWindow->consumeDragEvent(false, 50, 50);
10555
10556 // Move back to original window.
10557 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010558 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010559 ADISPLAY_ID_DEFAULT, {50, 50}))
10560 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010561 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080010562 mWindow->consumeDragEvent(false, 50, 50);
10563 mSecondWindow->consumeDragEvent(true, -50, 50);
10564
10565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010566 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10567 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080010568 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010569 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungb89ccb02020-12-30 16:19:01 +080010570 mWindow->assertNoEvents();
10571 mSecondWindow->assertNoEvents();
10572}
10573
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010574TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010575 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010576
10577 // No cancel event after drag start
10578 mSpyWindow->assertNoEvents();
10579
10580 const MotionEvent secondFingerDownEvent =
10581 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10582 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010583 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10584 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010585 .build();
10586 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010587 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010588 InputEventInjectionSync::WAIT_FOR_RESULT))
10589 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10590
10591 // Receives cancel for first pointer after next pointer down
10592 mSpyWindow->consumeMotionCancel();
10593 mSpyWindow->consumeMotionDown();
10594
10595 mSpyWindow->assertNoEvents();
10596}
10597
arthurhungf452d0b2021-01-06 00:19:52 +080010598TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010599 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +080010600
10601 // Move on 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, {50, 50}))
10605 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010606 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungf452d0b2021-01-06 00:19:52 +080010607 mWindow->consumeDragEvent(false, 50, 50);
10608 mSecondWindow->assertNoEvents();
10609
10610 // Move to another window.
10611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010612 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +080010613 ADISPLAY_ID_DEFAULT, {150, 50}))
10614 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010615 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhungf452d0b2021-01-06 00:19:52 +080010616 mWindow->consumeDragEvent(true, 150, 50);
10617 mSecondWindow->consumeDragEvent(false, 50, 50);
10618
10619 // drop to another window.
10620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010621 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +080010622 {150, 50}))
10623 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010624 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010625 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +080010626 mWindow->assertNoEvents();
10627 mSecondWindow->assertNoEvents();
10628}
10629
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010630TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) {
10631 startDrag();
10632
10633 // No cancel event after drag start
10634 mSpyWindow->assertNoEvents();
10635
10636 const MotionEvent secondFingerDownEvent =
10637 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10638 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
10639 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10640 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
10641 .build();
10642 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10643 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
10644 InputEventInjectionSync::WAIT_FOR_RESULT))
10645 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10646
10647 // Receives cancel for first pointer after next pointer down
10648 mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -080010649 mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1})));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010650 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
10651
10652 mSpyWindow->assertNoEvents();
10653
10654 // Spy window calls pilfer pointers
10655 EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken()));
10656 mDragWindow->assertNoEvents();
10657
10658 const MotionEvent firstFingerMoveEvent =
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080010659 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010660 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
10661 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60))
10662 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
10663 .build();
10664 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080010665 injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010666 InputEventInjectionSync::WAIT_FOR_RESULT))
10667 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10668
10669 // Drag window should still receive the new event
Prabir Pradhan65455c72024-02-13 21:46:41 +000010670 mDragWindow->consumeMotionEvent(
10671 AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010672 mDragWindow->assertNoEvents();
10673}
10674
arthurhung6d4bed92021-03-17 11:59:33 +080010675TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010676 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +080010677
10678 // Move on window and keep button pressed.
10679 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010680 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010681 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10682 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010683 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010684 .build()))
10685 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010686 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080010687 mWindow->consumeDragEvent(false, 50, 50);
10688 mSecondWindow->assertNoEvents();
10689
10690 // Move to another window and release button, expect to drop item.
10691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010692 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010693 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10694 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010695 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010696 .build()))
10697 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010698 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080010699 mWindow->assertNoEvents();
10700 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010701 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +080010702
10703 // nothing to the window.
10704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010705 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010706 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
10707 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010708 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010709 .build()))
10710 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010711 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
arthurhung6d4bed92021-03-17 11:59:33 +080010712 mWindow->assertNoEvents();
10713 mSecondWindow->assertNoEvents();
10714}
10715
Arthur Hung54745652022-04-20 07:17:41 +000010716TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010717 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +080010718
10719 // Set second window invisible.
10720 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010721 mDispatcher->onWindowInfosChanged(
10722 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +080010723
10724 // Move on window.
10725 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010726 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010727 ADISPLAY_ID_DEFAULT, {50, 50}))
10728 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010729 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung6d0571e2021-04-09 20:18:16 +080010730 mWindow->consumeDragEvent(false, 50, 50);
10731 mSecondWindow->assertNoEvents();
10732
10733 // Move to another window.
10734 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010735 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010736 ADISPLAY_ID_DEFAULT, {150, 50}))
10737 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010738 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung6d0571e2021-04-09 20:18:16 +080010739 mWindow->consumeDragEvent(true, 150, 50);
10740 mSecondWindow->assertNoEvents();
10741
10742 // drop to another window.
10743 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010744 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010745 {150, 50}))
10746 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010747 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010748 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +080010749 mWindow->assertNoEvents();
10750 mSecondWindow->assertNoEvents();
10751}
10752
Arthur Hung54745652022-04-20 07:17:41 +000010753TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010754 // Ensure window could track pointerIds if it didn't support split touch.
10755 mWindow->setPreventSplitting(true);
10756
Arthur Hung54745652022-04-20 07:17:41 +000010757 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010758 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +000010759 {50, 50}))
10760 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10761 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10762
10763 const MotionEvent secondFingerDownEvent =
10764 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10765 .displayId(ADISPLAY_ID_DEFAULT)
10766 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010767 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10768 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010769 .build();
10770 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010771 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010772 InputEventInjectionSync::WAIT_FOR_RESULT))
10773 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000010774 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +000010775
10776 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010777 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000010778}
10779
10780TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
10781 // First down on second window.
10782 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010783 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +000010784 {150, 50}))
10785 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10786
10787 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10788
10789 // Second down on first window.
10790 const MotionEvent secondFingerDownEvent =
10791 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10792 .displayId(ADISPLAY_ID_DEFAULT)
10793 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010794 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10795 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010796 .build();
10797 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010798 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010799 InputEventInjectionSync::WAIT_FOR_RESULT))
10800 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10801 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10802
10803 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010804 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000010805
10806 // Move on window.
10807 const MotionEvent secondFingerMoveEvent =
10808 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10809 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010810 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10811 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010812 .build();
10813 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010814 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010815 InputEventInjectionSync::WAIT_FOR_RESULT));
Prabir Pradhan65455c72024-02-13 21:46:41 +000010816 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung54745652022-04-20 07:17:41 +000010817 mWindow->consumeDragEvent(false, 50, 50);
10818 mSecondWindow->consumeMotionMove();
10819
10820 // Release the drag pointer should perform drop.
10821 const MotionEvent secondFingerUpEvent =
10822 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
10823 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010824 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10825 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010826 .build();
10827 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010828 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010829 InputEventInjectionSync::WAIT_FOR_RESULT));
Prabir Pradhan65455c72024-02-13 21:46:41 +000010830 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010831 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +000010832 mWindow->assertNoEvents();
10833 mSecondWindow->consumeMotionMove();
10834}
10835
Arthur Hung3915c1f2022-05-31 07:17:17 +000010836TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010837 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +000010838
10839 // Update window of second display.
10840 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010841 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010842 mDispatcher->onWindowInfosChanged(
10843 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
10844 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
10845 {},
10846 0,
10847 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000010848
10849 // Let second display has a touch state.
10850 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010851 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010852 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10853 AINPUT_SOURCE_TOUCHSCREEN)
10854 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010855 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +000010856 .build()));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +000010857 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +000010858 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010859 mDispatcher->onWindowInfosChanged(
10860 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
10861 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
10862 {},
10863 0,
10864 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000010865
10866 // Move on 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, {50, 50}))
10870 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010871 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung3915c1f2022-05-31 07:17:17 +000010872 mWindow->consumeDragEvent(false, 50, 50);
10873 mSecondWindow->assertNoEvents();
10874
10875 // Move to another window.
10876 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010877 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010878 ADISPLAY_ID_DEFAULT, {150, 50}))
10879 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010880 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hung3915c1f2022-05-31 07:17:17 +000010881 mWindow->consumeDragEvent(true, 150, 50);
10882 mSecondWindow->consumeDragEvent(false, 50, 50);
10883
10884 // drop to another window.
10885 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010886 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010887 {150, 50}))
10888 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010889 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010890 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +000010891 mWindow->assertNoEvents();
10892 mSecondWindow->assertNoEvents();
10893}
10894
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010895TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
10896 startDrag(true, AINPUT_SOURCE_MOUSE);
10897 // Move on window.
10898 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010899 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010900 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
10901 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010902 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010903 .x(50)
10904 .y(50))
10905 .build()))
10906 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010907 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010908 mWindow->consumeDragEvent(false, 50, 50);
10909 mSecondWindow->assertNoEvents();
10910
10911 // Move to another window.
10912 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010913 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010914 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
10915 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010916 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010917 .x(150)
10918 .y(50))
10919 .build()))
10920 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010921 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010922 mWindow->consumeDragEvent(true, 150, 50);
10923 mSecondWindow->consumeDragEvent(false, 50, 50);
10924
10925 // drop to another window.
10926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010927 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010928 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
10929 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010930 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010931 .x(150)
10932 .y(50))
10933 .build()))
10934 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan65455c72024-02-13 21:46:41 +000010935 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010936 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010937 mWindow->assertNoEvents();
10938 mSecondWindow->assertNoEvents();
10939}
10940
Linnan Li5af92f92023-07-14 14:36:22 +080010941/**
10942 * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag
10943 * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash.
10944 */
10945TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) {
10946 // Down on second window
10947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10948 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10949 {150, 50}))
10950 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10951
10952 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown());
10953 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown());
10954
10955 // Down on first window
10956 const MotionEvent secondFingerDownEvent =
10957 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10958 .displayId(ADISPLAY_ID_DEFAULT)
10959 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10960 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
10961 .build();
10962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10963 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
10964 InputEventInjectionSync::WAIT_FOR_RESULT))
10965 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10966 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
10967 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove());
10968 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1));
10969
10970 // Start drag on first window
10971 ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN));
10972
10973 // Trigger cancel
10974 mDispatcher->cancelCurrentTouch();
10975 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel());
Prabir Pradhan65455c72024-02-13 21:46:41 +000010976 ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT,
10977 AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE));
Linnan Li5af92f92023-07-14 14:36:22 +080010978 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel());
10979
10980 ASSERT_TRUE(mDispatcher->waitForIdle());
10981 // The D&D finished with nullptr
10982 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
10983
10984 // Remove drag window
10985 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
10986
10987 // Inject a simple gesture, ensure dispatcher not crashed
10988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10989 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10990 PointF{50, 50}))
10991 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10992 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
10993
10994 const MotionEvent moveEvent =
10995 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10996 .displayId(ADISPLAY_ID_DEFAULT)
10997 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10998 .build();
10999 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11000 injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT,
11001 InputEventInjectionSync::WAIT_FOR_RESULT))
11002 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11003 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove());
11004
11005 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11006 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
11007 {50, 50}))
11008 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11009 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp());
11010}
11011
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000011012TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) {
11013 // Start hovering over the window.
11014 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11015 injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE,
11016 ADISPLAY_ID_DEFAULT, {50, 50}));
11017
11018 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
11019 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
11020
11021 ASSERT_FALSE(startDrag(/*sendDown=*/false))
11022 << "Drag and drop should not work with a hovering pointer";
11023}
11024
Vishnu Nair062a8672021-09-03 16:07:44 -070011025class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
11026
11027TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
11028 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011029 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11030 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011031 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011032 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11033 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011034 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011035 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011036 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011037
11038 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011039 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011040 window->assertNoEvents();
11041
Prabir Pradhan678438e2023-04-13 19:32:51 +000011042 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11043 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011044 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11045 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -080011046 mDispatcher->waitForIdle();
Vishnu Nair062a8672021-09-03 16:07:44 -070011047 window->assertNoEvents();
11048
11049 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011050 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011051 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011052
Prabir Pradhan678438e2023-04-13 19:32:51 +000011053 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011054 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11055
Prabir Pradhan678438e2023-04-13 19:32:51 +000011056 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11057 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011058 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11059 window->assertNoEvents();
11060}
11061
11062TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
11063 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
11064 std::make_shared<FakeApplicationHandle>();
11065 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011066 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
11067 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070011068 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011069 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011070 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070011071 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011072 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11073 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011074 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011075 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070011076 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11077 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011078 mDispatcher->onWindowInfosChanged(
11079 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011080 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011081 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011082
11083 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011084 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011085 window->assertNoEvents();
11086
Prabir Pradhan678438e2023-04-13 19:32:51 +000011087 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11088 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011089 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11090 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011091 window->assertNoEvents();
11092
11093 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011094 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011095 mDispatcher->onWindowInfosChanged(
11096 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011097
Prabir Pradhan678438e2023-04-13 19:32:51 +000011098 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011099 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11100
Prabir Pradhan678438e2023-04-13 19:32:51 +000011101 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11102 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011103 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
11104 window->assertNoEvents();
11105}
11106
11107TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
11108 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
11109 std::make_shared<FakeApplicationHandle>();
11110 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011111 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
11112 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070011113 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011114 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011115 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070011116 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011117 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
11118 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011119 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011120 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070011121 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
11122 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011123 mDispatcher->onWindowInfosChanged(
11124 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011125 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011126 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070011127
11128 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000011129 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011130 window->assertNoEvents();
11131
Prabir Pradhan678438e2023-04-13 19:32:51 +000011132 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11133 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070011134 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
11135 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011136 window->assertNoEvents();
11137
11138 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011139 mDispatcher->onWindowInfosChanged(
11140 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011141
Prabir Pradhan678438e2023-04-13 19:32:51 +000011142 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011143 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11144
Prabir Pradhan678438e2023-04-13 19:32:51 +000011145 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11146 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011147 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11148 window->assertNoEvents();
11149}
11150
Antonio Kantekf16f2832021-09-28 04:39:20 +000011151class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
11152protected:
11153 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +000011154 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +000011155 sp<FakeWindowHandle> mWindow;
11156 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +000011157 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +000011158
11159 void SetUp() override {
11160 InputDispatcherTest::SetUp();
11161
11162 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +000011163 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011164 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011165 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011166 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011167 mSecondWindow =
11168 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011169 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +000011170 mThirdWindow =
11171 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
11172 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
11173 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011174
11175 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011176 mDispatcher->onWindowInfosChanged(
11177 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
11178 {},
11179 0,
11180 0});
Antonio Kantek15beb512022-06-13 22:35:41 +000011181 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011182 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011183
Antonio Kantek15beb512022-06-13 22:35:41 +000011184 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +000011185 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000011186 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -070011187 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
11188 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000011189 mThirdWindow->assertNoEvents();
11190 }
11191
11192 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
11193 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000011194 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +000011195 SECOND_DISPLAY_ID)) {
11196 mWindow->assertNoEvents();
11197 mSecondWindow->assertNoEvents();
11198 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -070011199 }
Antonio Kantekf16f2832021-09-28 04:39:20 +000011200 }
11201
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011202 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +000011203 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -070011204 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
11205 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000011206 mWindow->consumeTouchModeEvent(inTouchMode);
11207 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000011208 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +000011209 }
11210};
11211
Antonio Kantek26defcf2022-02-08 01:12:27 +000011212TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080011213 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +000011214 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
11215 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011216 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011217}
11218
Antonio Kantek26defcf2022-02-08 01:12:27 +000011219TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
11220 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011221 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011222 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011223 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011224 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000011225 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -070011226 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +000011227 mWindow->assertNoEvents();
11228 mSecondWindow->assertNoEvents();
11229}
11230
11231TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
11232 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011233 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011234 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011235 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +000011236 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000011237 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011238}
11239
Antonio Kantekf16f2832021-09-28 04:39:20 +000011240TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080011241 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +000011242 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
11243 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011244 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000011245 mWindow->assertNoEvents();
11246 mSecondWindow->assertNoEvents();
11247}
11248
Antonio Kantek15beb512022-06-13 22:35:41 +000011249TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
11250 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
11251 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
11252 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011253 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +000011254 mWindow->assertNoEvents();
11255 mSecondWindow->assertNoEvents();
11256 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
11257}
11258
Antonio Kantek48710e42022-03-24 14:19:30 -070011259TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
11260 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011261 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11262 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -070011263 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11264 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
11265
11266 // Then remove focus.
11267 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011268 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -070011269
11270 // Assert that caller can switch touch mode by owning one of the last interacted window.
11271 const WindowInfo& windowInfo = *mWindow->getInfo();
11272 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
11273 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011274 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -070011275}
11276
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011277class InputDispatcherSpyWindowTest : public InputDispatcherTest {
11278public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011279 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011280 std::shared_ptr<FakeApplicationHandle> application =
11281 std::make_shared<FakeApplicationHandle>();
11282 std::string name = "Fake Spy ";
11283 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011284 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
11285 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011286 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011287 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011288 return spy;
11289 }
11290
11291 sp<FakeWindowHandle> createForeground() {
11292 std::shared_ptr<FakeApplicationHandle> application =
11293 std::make_shared<FakeApplicationHandle>();
11294 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011295 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
11296 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011297 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011298 return window;
11299 }
11300
11301private:
11302 int mSpyCount{0};
11303};
11304
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011305using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011306/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011307 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
11308 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011309TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -070011310 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011311 ScopedSilentDeath _silentDeath;
11312
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011313 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011314 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011315 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011316 ".* not a trusted overlay");
11317}
11318
11319/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011320 * Input injection into a display with a spy window but no foreground windows should succeed.
11321 */
11322TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011323 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011324 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011325
11326 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011327 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011328 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11329 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11330}
11331
11332/**
11333 * Verify the order in which different input windows receive events. The touched foreground window
11334 * (if there is one) should always receive the event first. When there are multiple spy windows, the
11335 * spy windows will receive the event according to their Z-order, where the top-most spy window will
11336 * receive events before ones belows it.
11337 *
11338 * Here, we set up a scenario with four windows in the following Z order from the top:
11339 * spy1, spy2, window, spy3.
11340 * We then inject an event and verify that the foreground "window" receives it first, followed by
11341 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
11342 * window.
11343 */
11344TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
11345 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011346 auto spy1 = createSpy();
11347 auto spy2 = createSpy();
11348 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011349 mDispatcher->onWindowInfosChanged(
11350 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011351 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
11352 const size_t numChannels = channels.size();
11353
Michael Wright8e9a8562022-02-09 13:44:29 +000011354 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011355 if (!epollFd.ok()) {
11356 FAIL() << "Failed to create epoll fd";
11357 }
11358
11359 for (size_t i = 0; i < numChannels; i++) {
11360 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
11361 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
11362 FAIL() << "Failed to add fd to epoll";
11363 }
11364 }
11365
11366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011367 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011368 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11369
11370 std::vector<size_t> eventOrder;
11371 std::vector<struct epoll_event> events(numChannels);
11372 for (;;) {
11373 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
11374 (100ms).count());
11375 if (nFds < 0) {
11376 FAIL() << "Failed to call epoll_wait";
11377 }
11378 if (nFds == 0) {
11379 break; // epoll_wait timed out
11380 }
11381 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -070011382 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -070011383 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011384 channels[i]->consumeMotionDown();
11385 }
11386 }
11387
11388 // Verify the order in which the events were received.
11389 EXPECT_EQ(3u, eventOrder.size());
11390 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
11391 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
11392 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
11393}
11394
11395/**
11396 * A spy window using the NOT_TOUCHABLE flag does not receive events.
11397 */
11398TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
11399 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011400 auto spy = createSpy();
11401 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011402 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011403
11404 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011405 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011406 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11407 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11408 spy->assertNoEvents();
11409}
11410
11411/**
11412 * A spy window will only receive gestures that originate within its touchable region. Gestures that
11413 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
11414 * to the window.
11415 */
11416TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
11417 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011418 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011419 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011420 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011421
11422 // Inject an event outside the spy window's touchable region.
11423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011424 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011425 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11426 window->consumeMotionDown();
11427 spy->assertNoEvents();
11428 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011429 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011430 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11431 window->consumeMotionUp();
11432 spy->assertNoEvents();
11433
11434 // Inject an event inside the spy window's touchable region.
11435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011436 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011437 {5, 10}))
11438 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11439 window->consumeMotionDown();
11440 spy->consumeMotionDown();
11441}
11442
11443/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011444 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011445 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011446 */
11447TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
11448 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011449 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011450 auto spy = createSpy();
11451 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011452 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011453 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011454 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011455
11456 // Inject an event outside the spy window's frame and touchable region.
11457 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011458 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011459 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011460 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11461 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011462 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011463}
11464
11465/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011466 * Even when a spy window spans over multiple foreground windows, the spy should receive all
11467 * pointers that are down within its bounds.
11468 */
11469TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
11470 auto windowLeft = createForeground();
11471 windowLeft->setFrame({0, 0, 100, 200});
11472 auto windowRight = createForeground();
11473 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011474 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011475 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011476 mDispatcher->onWindowInfosChanged(
11477 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011478
11479 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011480 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011481 {50, 50}))
11482 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11483 windowLeft->consumeMotionDown();
11484 spy->consumeMotionDown();
11485
11486 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011487 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011488 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011489 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11490 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011491 .build();
11492 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011493 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011494 InputEventInjectionSync::WAIT_FOR_RESULT))
11495 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11496 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +000011497 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011498}
11499
11500/**
11501 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
11502 * the spy should receive the second pointer with ACTION_DOWN.
11503 */
11504TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
11505 auto window = createForeground();
11506 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011507 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011508 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011509 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011510
11511 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011512 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011513 {50, 50}))
11514 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11515 window->consumeMotionDown();
11516 spyRight->assertNoEvents();
11517
11518 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011519 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011520 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011521 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11522 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011523 .build();
11524 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011525 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011526 InputEventInjectionSync::WAIT_FOR_RESULT))
11527 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000011528 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011529 spyRight->consumeMotionDown();
11530}
11531
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011532/**
11533 * The spy window should not be able to affect whether or not touches are split. Only the foreground
11534 * windows should be allowed to control split touch.
11535 */
11536TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080011537 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011538 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011539 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080011540 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011541
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011542 auto window = createForeground();
11543 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011544
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011545 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011546
11547 // First finger down, no window touched.
11548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011549 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011550 {100, 200}))
11551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11552 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11553 window->assertNoEvents();
11554
11555 // Second finger down on window, the window should receive touch down.
11556 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011557 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011558 .displayId(ADISPLAY_ID_DEFAULT)
11559 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011560 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11561 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011562 .build();
11563 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011564 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011565 InputEventInjectionSync::WAIT_FOR_RESULT))
11566 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11567
11568 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +000011569 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011570}
11571
11572/**
11573 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
11574 * do not receive key events.
11575 */
11576TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011577 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011578 spy->setFocusable(false);
11579
11580 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011581 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011582 setFocusedWindow(window);
11583 window->consumeFocusEvent(true);
11584
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011585 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011586 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11587 window->consumeKeyDown(ADISPLAY_ID_NONE);
11588
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011589 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011590 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11591 window->consumeKeyUp(ADISPLAY_ID_NONE);
11592
11593 spy->assertNoEvents();
11594}
11595
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011596using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
11597
11598/**
11599 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
11600 * are currently sent to any other windows - including other spy windows - will also be cancelled.
11601 */
11602TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
11603 auto window = createForeground();
11604 auto spy1 = createSpy();
11605 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011606 mDispatcher->onWindowInfosChanged(
11607 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011608
11609 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011610 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011611 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11612 window->consumeMotionDown();
11613 spy1->consumeMotionDown();
11614 spy2->consumeMotionDown();
11615
11616 // Pilfer pointers from the second spy window.
11617 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
11618 spy2->assertNoEvents();
11619 spy1->consumeMotionCancel();
11620 window->consumeMotionCancel();
11621
11622 // The rest of the gesture should only be sent to the second spy window.
11623 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011624 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011625 ADISPLAY_ID_DEFAULT))
11626 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11627 spy2->consumeMotionMove();
11628 spy1->assertNoEvents();
11629 window->assertNoEvents();
11630}
11631
11632/**
11633 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
11634 * in the middle of the gesture.
11635 */
11636TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
11637 auto window = createForeground();
11638 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011639 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011640
11641 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011642 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11644 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11645 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11646
11647 window->releaseChannel();
11648
11649 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11650
11651 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011652 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011653 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11654 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
11655}
11656
11657/**
11658 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
11659 * the spy, but not to any other windows.
11660 */
11661TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
11662 auto spy = createSpy();
11663 auto window = createForeground();
11664
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011665 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011666
11667 // First finger down on the window and the spy.
11668 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011669 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011670 {100, 200}))
11671 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11672 spy->consumeMotionDown();
11673 window->consumeMotionDown();
11674
11675 // Spy window pilfers the pointers.
11676 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11677 window->consumeMotionCancel();
11678
11679 // Second finger down on the window and spy, but the window should not receive the pointer down.
11680 const MotionEvent secondFingerDownEvent =
11681 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11682 .displayId(ADISPLAY_ID_DEFAULT)
11683 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011684 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11685 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011686 .build();
11687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011688 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011689 InputEventInjectionSync::WAIT_FOR_RESULT))
11690 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11691
Harry Cutts33476232023-01-30 19:57:29 +000011692 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011693
11694 // Third finger goes down outside all windows, so injection should fail.
11695 const MotionEvent thirdFingerDownEvent =
11696 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11697 .displayId(ADISPLAY_ID_DEFAULT)
11698 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011699 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11700 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
11701 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011702 .build();
11703 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011704 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011705 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080011706 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011707
11708 spy->assertNoEvents();
11709 window->assertNoEvents();
11710}
11711
11712/**
11713 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
11714 */
11715TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
11716 auto spy = createSpy();
11717 spy->setFrame(Rect(0, 0, 100, 100));
11718 auto window = createForeground();
11719 window->setFrame(Rect(0, 0, 200, 200));
11720
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011721 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011722
11723 // First finger down on the window only
11724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011725 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011726 {150, 150}))
11727 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11728 window->consumeMotionDown();
11729
11730 // Second finger down on the spy and window
11731 const MotionEvent secondFingerDownEvent =
11732 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11733 .displayId(ADISPLAY_ID_DEFAULT)
11734 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011735 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
11736 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011737 .build();
11738 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011739 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011740 InputEventInjectionSync::WAIT_FOR_RESULT))
11741 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11742 spy->consumeMotionDown();
11743 window->consumeMotionPointerDown(1);
11744
11745 // Third finger down on the spy and window
11746 const MotionEvent thirdFingerDownEvent =
11747 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11748 .displayId(ADISPLAY_ID_DEFAULT)
11749 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011750 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
11751 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
11752 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011753 .build();
11754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011755 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011756 InputEventInjectionSync::WAIT_FOR_RESULT))
11757 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11758 spy->consumeMotionPointerDown(1);
11759 window->consumeMotionPointerDown(2);
11760
11761 // Spy window pilfers the pointers.
11762 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Harry Cutts101ee9b2023-07-06 18:04:14 +000011763 window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
11764 window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011765
11766 spy->assertNoEvents();
11767 window->assertNoEvents();
11768}
11769
11770/**
11771 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
11772 * other windows should be canceled. If this results in the cancellation of all pointers for some
11773 * window, then that window should receive ACTION_CANCEL.
11774 */
11775TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
11776 auto spy = createSpy();
11777 spy->setFrame(Rect(0, 0, 100, 100));
11778 auto window = createForeground();
11779 window->setFrame(Rect(0, 0, 200, 200));
11780
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011781 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011782
11783 // First finger down on both spy and window
11784 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011785 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011786 {10, 10}))
11787 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11788 window->consumeMotionDown();
11789 spy->consumeMotionDown();
11790
11791 // Second finger down on the spy and window
11792 const MotionEvent secondFingerDownEvent =
11793 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11794 .displayId(ADISPLAY_ID_DEFAULT)
11795 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011796 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
11797 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011798 .build();
11799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011800 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011801 InputEventInjectionSync::WAIT_FOR_RESULT))
11802 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11803 spy->consumeMotionPointerDown(1);
11804 window->consumeMotionPointerDown(1);
11805
11806 // Spy window pilfers the pointers.
11807 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11808 window->consumeMotionCancel();
11809
11810 spy->assertNoEvents();
11811 window->assertNoEvents();
11812}
11813
11814/**
11815 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
11816 * be sent to other windows
11817 */
11818TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
11819 auto spy = createSpy();
11820 spy->setFrame(Rect(0, 0, 100, 100));
11821 auto window = createForeground();
11822 window->setFrame(Rect(0, 0, 200, 200));
11823
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011824 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011825
11826 // First finger down on both window and spy
11827 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011828 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011829 {10, 10}))
11830 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11831 window->consumeMotionDown();
11832 spy->consumeMotionDown();
11833
11834 // Spy window pilfers the pointers.
11835 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11836 window->consumeMotionCancel();
11837
11838 // Second finger down on the window only
11839 const MotionEvent secondFingerDownEvent =
11840 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11841 .displayId(ADISPLAY_ID_DEFAULT)
11842 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011843 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
11844 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011845 .build();
11846 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011847 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011848 InputEventInjectionSync::WAIT_FOR_RESULT))
11849 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11850 window->consumeMotionDown();
11851 window->assertNoEvents();
11852
11853 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
11854 spy->consumeMotionMove();
11855 spy->assertNoEvents();
11856}
11857
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011858/**
11859 * A window on the left and a window on the right. Also, a spy window that's above all of the
11860 * windows, and spanning both left and right windows.
11861 * Send simultaneous motion streams from two different devices, one to the left window, and another
11862 * to the right window.
11863 * Pilfer from spy window.
11864 * Check that the pilfering only affects the pointers that are actually being received by the spy.
11865 */
11866TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) {
11867 sp<FakeWindowHandle> spy = createSpy();
11868 spy->setFrame(Rect(0, 0, 200, 200));
11869 sp<FakeWindowHandle> leftWindow = createForeground();
11870 leftWindow->setFrame(Rect(0, 0, 100, 100));
11871
11872 sp<FakeWindowHandle> rightWindow = createForeground();
11873 rightWindow->setFrame(Rect(100, 0, 200, 100));
11874
11875 constexpr int32_t stylusDeviceId = 1;
11876 constexpr int32_t touchDeviceId = 2;
11877
11878 mDispatcher->onWindowInfosChanged(
11879 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
11880
11881 // Stylus down on left window and spy
11882 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
11883 .deviceId(stylusDeviceId)
11884 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
11885 .build());
11886 leftWindow->consumeMotionEvent(
11887 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
11888 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
11889
11890 // Finger down on right window and spy - but spy already has stylus
11891 mDispatcher->notifyMotion(
11892 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11893 .deviceId(touchDeviceId)
11894 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
11895 .build());
11896 rightWindow->consumeMotionEvent(
11897 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011898 spy->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011899
11900 // Act: pilfer from spy. Spy is currently receiving touch events.
11901 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011902 leftWindow->consumeMotionEvent(
11903 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011904 rightWindow->consumeMotionEvent(
11905 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
11906
11907 // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus
11908 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11909 .deviceId(stylusDeviceId)
11910 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
11911 .build());
11912 mDispatcher->notifyMotion(
11913 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
11914 .deviceId(touchDeviceId)
11915 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
11916 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011917 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011918
11919 spy->assertNoEvents();
11920 leftWindow->assertNoEvents();
11921 rightWindow->assertNoEvents();
11922}
11923
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000011924TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) {
11925 auto window = createForeground();
11926 auto spy = createSpy();
11927 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
11928
11929 mDispatcher->notifyMotion(
11930 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
11931 .deviceId(1)
11932 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200))
11933 .build());
11934 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
11935 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
11936
11937 // Pilfer pointers from the spy window should fail.
11938 EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken()));
11939 spy->assertNoEvents();
11940 window->assertNoEvents();
11941}
11942
Prabir Pradhand65552b2021-10-07 11:23:50 -070011943class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
11944public:
11945 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
11946 std::shared_ptr<FakeApplicationHandle> overlayApplication =
11947 std::make_shared<FakeApplicationHandle>();
11948 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011949 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
11950 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011951 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011952 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011953 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011954 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011955 overlay->setTrustedOverlay(true);
11956
11957 std::shared_ptr<FakeApplicationHandle> application =
11958 std::make_shared<FakeApplicationHandle>();
11959 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011960 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
11961 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011962 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011963 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011964
11965 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011966 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011967 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011968 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011969 return {std::move(overlay), std::move(window)};
11970 }
11971
11972 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000011973 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -070011974 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +000011975 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -070011976 }
11977
11978 void sendStylusEvent(int32_t action) {
11979 NotifyMotionArgs motionArgs =
11980 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
11981 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011982 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +000011983 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011984 }
11985};
11986
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011987using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
11988
11989TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -070011990 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011991 ScopedSilentDeath _silentDeath;
11992
Prabir Pradhand65552b2021-10-07 11:23:50 -070011993 auto [overlay, window] = setupStylusOverlayScenario();
11994 overlay->setTrustedOverlay(false);
11995 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011996 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
11997 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -070011998 ".* not a trusted overlay");
11999}
12000
12001TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
12002 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012003 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070012004
12005 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
12006 overlay->consumeMotionDown();
12007 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
12008 overlay->consumeMotionUp();
12009
12010 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
12011 window->consumeMotionDown();
12012 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
12013 window->consumeMotionUp();
12014
12015 overlay->assertNoEvents();
12016 window->assertNoEvents();
12017}
12018
12019TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
12020 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -080012021 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012022 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070012023
12024 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
12025 overlay->consumeMotionDown();
12026 window->consumeMotionDown();
12027 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
12028 overlay->consumeMotionUp();
12029 window->consumeMotionUp();
12030
12031 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
12032 window->consumeMotionDown();
12033 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
12034 window->consumeMotionUp();
12035
12036 overlay->assertNoEvents();
12037 window->assertNoEvents();
12038}
12039
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012040/**
12041 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
12042 * The scenario is as follows:
12043 * - The stylus interceptor overlay is configured as a spy window.
12044 * - The stylus interceptor spy receives the start of a new stylus gesture.
12045 * - It pilfers pointers and then configures itself to no longer be a spy.
12046 * - The stylus interceptor continues to receive the rest of the gesture.
12047 */
12048TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
12049 auto [overlay, window] = setupStylusOverlayScenario();
12050 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012051 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012052
12053 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
12054 overlay->consumeMotionDown();
12055 window->consumeMotionDown();
12056
12057 // The interceptor pilfers the pointers.
12058 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
12059 window->consumeMotionCancel();
12060
12061 // The interceptor configures itself so that it is no longer a spy.
12062 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012063 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000012064
12065 // It continues to receive the rest of the stylus gesture.
12066 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
12067 overlay->consumeMotionMove();
12068 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
12069 overlay->consumeMotionUp();
12070
12071 window->assertNoEvents();
12072}
12073
Prabir Pradhan5735a322022-04-11 17:23:34 +000012074struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012075 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000012076 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +000012077 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
12078 std::unique_ptr<InputDispatcher>& mDispatcher;
12079
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012080 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +000012081 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
12082
12083 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012084 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012085 ADISPLAY_ID_DEFAULT, {100, 200},
12086 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
12087 AMOTION_EVENT_INVALID_CURSOR_POSITION},
12088 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
12089 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
12090 }
12091
12092 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012093 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012094 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +000012095 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +000012096 mPolicyFlags);
12097 }
12098
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012099 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +000012100 std::shared_ptr<FakeApplicationHandle> overlayApplication =
12101 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012102 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
12103 name, ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +000012104 window->setOwnerInfo(mPid, mUid);
12105 return window;
12106 }
12107};
12108
12109using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
12110
12111TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012112 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012113 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012114 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012115
12116 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12117 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12118 window->consumeMotionDown();
12119
12120 setFocusedWindow(window);
12121 window->consumeFocusEvent(true);
12122
12123 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12124 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
12125 window->consumeKeyDown(ADISPLAY_ID_NONE);
12126}
12127
12128TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012129 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012130 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012131 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012132
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012133 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012134 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
12135 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12136
12137 setFocusedWindow(window);
12138 window->consumeFocusEvent(true);
12139
12140 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
12141 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
12142 window->assertNoEvents();
12143}
12144
12145TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012146 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012147 auto window = owner.createWindow("Owned window");
12148 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012149 spy->setSpy(true);
12150 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012151 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012152
12153 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12154 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12155 spy->consumeMotionDown();
12156 window->consumeMotionDown();
12157}
12158
12159TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012160 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012161 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012162
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012163 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012164 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012165 randosSpy->setSpy(true);
12166 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012167 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012168
12169 // The event is targeted at owner's window, so injection should succeed, but the spy should
12170 // not receive the event.
12171 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12172 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12173 randosSpy->assertNoEvents();
12174 window->consumeMotionDown();
12175}
12176
12177TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012178 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012179 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012180
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012181 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012182 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012183 randosSpy->setSpy(true);
12184 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012185 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012186
12187 // A user that has injection permission can inject into any window.
12188 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012189 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012190 ADISPLAY_ID_DEFAULT));
12191 randosSpy->consumeMotionDown();
12192 window->consumeMotionDown();
12193
12194 setFocusedWindow(randosSpy);
12195 randosSpy->consumeFocusEvent(true);
12196
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012197 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Prabir Pradhan5735a322022-04-11 17:23:34 +000012198 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
12199 window->assertNoEvents();
12200}
12201
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012202TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012203 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012204 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012205
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012206 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012207 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012208 randosWindow->setFrame(Rect{-10, -10, -5, -5});
12209 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012210 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012211
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012212 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +000012213 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12214 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12215 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012216 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +000012217}
12218
Prabir Pradhan64f21d22023-11-28 21:19:42 +000012219using InputDispatcherPointerInWindowTest = InputDispatcherTest;
12220
12221TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) {
12222 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12223
12224 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12225 ADISPLAY_ID_DEFAULT);
12226 left->setFrame(Rect(0, 0, 100, 100));
12227 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12228 "Right Window", ADISPLAY_ID_DEFAULT);
12229 right->setFrame(Rect(100, 0, 200, 100));
12230 sp<FakeWindowHandle> spy =
12231 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
12232 spy->setFrame(Rect(0, 0, 200, 100));
12233 spy->setTrustedOverlay(true);
12234 spy->setSpy(true);
12235
12236 mDispatcher->onWindowInfosChanged(
12237 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
12238
12239 // Hover into the left window.
12240 mDispatcher->notifyMotion(
12241 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
12242 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50))
12243 .build());
12244
12245 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12246 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12247
12248 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12249 /*pointerId=*/0));
12250 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12251 /*pointerId=*/0));
12252 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12253 /*pointerId=*/0));
12254
12255 // Hover move to the right window.
12256 mDispatcher->notifyMotion(
12257 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
12258 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
12259 .build());
12260
12261 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12262 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12263 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
12264
12265 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12266 /*pointerId=*/0));
12267 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12268 /*pointerId=*/0));
12269 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12270 /*pointerId=*/0));
12271
12272 // Stop hovering.
12273 mDispatcher->notifyMotion(
12274 MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
12275 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
12276 .build());
12277
12278 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12279 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12280
12281 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12282 /*pointerId=*/0));
12283 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12284 /*pointerId=*/0));
12285 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12286 /*pointerId=*/0));
12287}
12288
12289TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) {
12290 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12291
12292 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12293 ADISPLAY_ID_DEFAULT);
12294 left->setFrame(Rect(0, 0, 100, 100));
12295 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12296 "Right Window", ADISPLAY_ID_DEFAULT);
12297 right->setFrame(Rect(100, 0, 200, 100));
12298 sp<FakeWindowHandle> spy =
12299 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
12300 spy->setFrame(Rect(0, 0, 200, 100));
12301 spy->setTrustedOverlay(true);
12302 spy->setSpy(true);
12303
12304 mDispatcher->onWindowInfosChanged(
12305 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
12306
12307 // First pointer down on left window.
12308 mDispatcher->notifyMotion(
12309 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
12310 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12311 .build());
12312
12313 left->consumeMotionDown();
12314 spy->consumeMotionDown();
12315
12316 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12317 /*pointerId=*/0));
12318 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12319 /*pointerId=*/0));
12320 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12321 /*pointerId=*/0));
12322
12323 // Second pointer down on right window.
12324 mDispatcher->notifyMotion(
12325 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
12326 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12327 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
12328 .build());
12329
12330 left->consumeMotionMove();
12331 right->consumeMotionDown();
12332 spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
12333
12334 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12335 /*pointerId=*/0));
12336 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12337 /*pointerId=*/0));
12338 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12339 /*pointerId=*/0));
12340 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12341 /*pointerId=*/1));
12342 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12343 /*pointerId=*/1));
12344 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12345 /*pointerId=*/1));
12346
12347 // Second pointer up.
12348 mDispatcher->notifyMotion(
12349 MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
12350 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12351 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
12352 .build());
12353
12354 left->consumeMotionMove();
12355 right->consumeMotionUp();
12356 spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
12357
12358 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12359 /*pointerId=*/0));
12360 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12361 /*pointerId=*/0));
12362 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12363 /*pointerId=*/0));
12364 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12365 /*pointerId=*/1));
12366 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12367 /*pointerId=*/1));
12368 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12369 /*pointerId=*/1));
12370
12371 // First pointer up.
12372 mDispatcher->notifyMotion(
12373 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
12374 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12375 .build());
12376
12377 left->consumeMotionUp();
12378 spy->consumeMotionUp();
12379
12380 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12381 /*pointerId=*/0));
12382 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12383 /*pointerId=*/0));
12384 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12385 /*pointerId=*/0));
12386}
12387
12388TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) {
12389 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12390
12391 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12392 ADISPLAY_ID_DEFAULT);
12393 left->setFrame(Rect(0, 0, 100, 100));
12394 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12395 "Right Window", ADISPLAY_ID_DEFAULT);
12396 right->setFrame(Rect(100, 0, 200, 100));
12397
12398 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
12399
12400 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12401 /*pointerId=*/0));
12402 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12403 /*pointerId=*/0));
12404
12405 // Hover move into the window.
12406 mDispatcher->notifyMotion(
12407 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12408 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
12409 .rawXCursorPosition(50)
12410 .rawYCursorPosition(50)
12411 .deviceId(DEVICE_ID)
12412 .build());
12413
12414 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12415
12416 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12417 /*pointerId=*/0));
12418
12419 // Move the mouse with another device. This cancels the hovering pointer from the first device.
12420 mDispatcher->notifyMotion(
12421 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12422 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
12423 .rawXCursorPosition(51)
12424 .rawYCursorPosition(50)
12425 .deviceId(SECOND_DEVICE_ID)
12426 .build());
12427
12428 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12429 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12430
12431 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
12432 // a HOVER_EXIT from the first device.
12433 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12434 /*pointerId=*/0));
12435 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT,
12436 SECOND_DEVICE_ID,
12437 /*pointerId=*/0));
12438
12439 // Move the mouse outside the window. Document the current behavior, where the window does not
12440 // receive HOVER_EXIT even though the mouse left the window.
12441 mDispatcher->notifyMotion(
12442 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12443 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
12444 .rawXCursorPosition(150)
12445 .rawYCursorPosition(50)
12446 .deviceId(SECOND_DEVICE_ID)
12447 .build());
12448
12449 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12450 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12451 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12452 /*pointerId=*/0));
12453 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT,
12454 SECOND_DEVICE_ID,
12455 /*pointerId=*/0));
12456}
12457
Garfield Tane84e6f92019-08-29 17:28:41 -070012458} // namespace android::inputdispatcher