blob: e826ddc8d826d16da16d7f6d05932c551ac675c7 [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 Pradhand3fb6272023-12-15 01:21:47 +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{};
158 gui::Pid pid{gui::Pid::INVALID};
159 };
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 Pradhanaeebeb42023-06-13 19:53:03 +0000263 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 Pradhanaeebeb42023-06-13 19:53:03 +0000283 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 Vishniakoua7333112023-10-27 13:33:29 -0700367 void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; }
368
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800369 void assertUserActivityNotPoked() {
370 std::unique_lock lock(mLock);
371 base::ScopedLockAssertion assumeLocked(mLock);
372
373 std::optional<UserActivityPokeEvent> pokeEvent =
374 getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock,
375 mNotifyUserActivity);
376
377 ASSERT_FALSE(pokeEvent) << "Expected user activity not to have been poked";
Josep del Riob3981622023-04-18 15:49:45 +0000378 }
379
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800380 /**
381 * Asserts that a user activity poke has happened. The earliest recorded poke event will be
382 * cleared after this call.
383 *
384 * If an expected UserActivityPokeEvent is provided, asserts that the given event is the
385 * earliest recorded poke event.
386 */
387 void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {}) {
388 std::unique_lock lock(mLock);
389 base::ScopedLockAssertion assumeLocked(mLock);
390
391 std::optional<UserActivityPokeEvent> pokeEvent =
392 getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock,
393 mNotifyUserActivity);
394 ASSERT_TRUE(pokeEvent) << "Expected a user poke event";
395
396 if (expectedPokeEvent) {
397 ASSERT_EQ(expectedPokeEvent, *pokeEvent);
398 }
Josep del Riob3981622023-04-18 15:49:45 +0000399 }
400
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000401 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000402 ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
403 }
404
405 void assertNotifyDeviceInteractionWasNotCalled() {
406 ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
407 }
408
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000409 void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) {
410 std::scoped_lock lock(mLock);
411 mUnhandledKeyHandler = handler;
412 }
413
414 void assertUnhandledKeyReported(int32_t keycode) {
415 std::unique_lock lock(mLock);
416 base::ScopedLockAssertion assumeLocked(mLock);
417 std::optional<int32_t> unhandledKeycode =
418 getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock,
419 mNotifyUnhandledKey);
420 ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported";
421 ASSERT_EQ(unhandledKeycode, keycode);
422 }
423
424 void assertUnhandledKeyNotReported() {
425 std::unique_lock lock(mLock);
426 base::ScopedLockAssertion assumeLocked(mLock);
427 std::optional<int32_t> unhandledKeycode =
428 getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock,
429 mNotifyUnhandledKey);
430 ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported";
431 }
432
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700434 std::mutex mLock;
435 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
436 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
437 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
438 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800439
Prabir Pradhan99987712020-11-10 18:43:05 -0800440 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000441
442 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800443
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700444 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700445 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800446 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
447 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700448 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800449 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
450 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700451
arthurhungf452d0b2021-01-06 00:19:52 +0800452 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800453 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800454
455 std::condition_variable mNotifyUserActivity;
456 std::queue<UserActivityPokeEvent> mUserActivityPokeEvents;
arthurhungf452d0b2021-01-06 00:19:52 +0800457
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800458 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
459
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700460 std::chrono::nanoseconds mStaleEventTimeout = 1000ms;
461
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000462 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000463
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000464 std::condition_variable mNotifyUnhandledKey;
465 std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock);
466 std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock);
467
Prabir Pradhanedd96402022-02-15 01:46:16 -0800468 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
469 // for a specific container to become non-empty. When the container is non-empty, return the
470 // first entry from the container and erase it.
471 template <class T>
472 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
473 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
474 // If there is an ANR, Dispatcher won't be idle because there are still events
475 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
476 // before checking if ANR was called.
477 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
478 // to provide it some time to act. 100ms seems reasonable.
479 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
480 const std::chrono::time_point start = std::chrono::steady_clock::now();
481 std::optional<T> token =
482 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
483 if (!token.has_value()) {
484 ADD_FAILURE() << "Did not receive the ANR callback";
485 return {};
486 }
487
488 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
489 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
490 // the dispatcher started counting before this function was called
491 if (std::chrono::abs(timeout - waited) > 100ms) {
492 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
493 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
494 << "ms, but waited "
495 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
496 << "ms instead";
497 }
498 return *token;
499 }
500
501 template <class T>
502 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
503 std::queue<T>& storage,
504 std::unique_lock<std::mutex>& lock,
505 std::condition_variable& condition)
506 REQUIRES(mLock) {
507 condition.wait_for(lock, timeout,
508 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
509 if (storage.empty()) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800510 return std::nullopt;
511 }
512 T item = storage.front();
513 storage.pop();
514 return std::make_optional(item);
515 }
516
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600517 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700518 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800519 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520 }
521
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000522 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800523 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700524 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800525 ASSERT_TRUE(pid.has_value());
526 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700527 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500528 }
529
Prabir Pradhanedd96402022-02-15 01:46:16 -0800530 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000531 std::optional<gui::Pid> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500532 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800533 ASSERT_TRUE(pid.has_value());
534 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 Pradhand3fb6272023-12-15 01:21:47 +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 Pradhand3fb6272023-12-15 01:21:47 +0000673 gVerifyingTrace->reset();
Prabir Pradhana41d2442023-04-20 21:30:40 +0000674 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Prabir Pradhand3fb6272023-12-15 01:21:47 +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 Pradhan3608aad2019-10-02 17:08:26 -0700685 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000686 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700687 mDispatcher.reset();
Prabir Pradhand3fb6272023-12-15 01:21:47 +0000688 ASSERT_NO_FATAL_FAILURE(gVerifyingTrace->verifyExpectedEventsTraced());
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
chaviwd1c23182019-12-20 18:44:56 -08001096 void assertNoEvents() {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001097 std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
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 Pradhand3fb6272023-12-15 01:21:47 +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
Arthur Hungb92218b2018-08-14 12:00:21 +08001421 void assertNoEvents() {
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";
chaviwd1c23182019-12-20 18:44:56 -08001428 mInputReceiver->assertNoEvents();
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
chaviwd1c23182019-12-20 18:44:56 -08001446private:
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001447 FakeWindowHandle(std::string name) : mName(name){};
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001448 const std::string mName;
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001449 std::shared_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001450 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001451 friend class sp<FakeWindowHandle>;
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001452
Prabir Pradhand3fb6272023-12-15 01:21:47 +00001453 // FakeWindowHandle uses this consume method to ensure received events are added to the trace.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001454 std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) {
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001455 if (mInputReceiver == nullptr) {
1456 LOG(FATAL) << "Cannot consume event from a window with no input event receiver";
1457 }
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001458 std::unique_ptr<InputEvent> event = mInputReceiver->consume(timeout, handled);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001459 if (event == nullptr) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001460 ADD_FAILURE() << "Consume failed: no event";
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001461 }
Prabir Pradhand3fb6272023-12-15 01:21:47 +00001462 expectReceivedEventTraced(event);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001463 return event;
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00001464 }
Prabir Pradhand3fb6272023-12-15 01:21:47 +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: {
1485 gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event));
1486 break;
1487 }
1488 case InputEventType::MOTION: {
1489 gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event));
1490 break;
1491 }
1492 default:
1493 break;
1494 }
1495 }
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001496};
1497
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001498std::atomic<int32_t> FakeWindowHandle::sId{1};
1499
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001500class FakeMonitorReceiver {
1501public:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001502 FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId)
1503 : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {}
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001504
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001505 sp<IBinder> getToken() { return mInputReceiver.getToken(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001506
1507 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001508 mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1509 expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001510 }
1511
1512 std::optional<int32_t> receiveEvent() {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001513 const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
1514 return sequenceNum;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001515 }
1516
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001517 void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001518
1519 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001520 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
1521 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001522 }
1523
1524 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001525 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
1526 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001527 }
1528
1529 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001530 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
1531 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001532 }
1533
1534 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001535 mInputReceiver.consumeMotionEvent(
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001536 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
1537 WithDisplayId(expectedDisplayId),
1538 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
1539 }
1540
1541 void consumeMotionPointerDown(int32_t pointerIdx) {
1542 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1543 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001544 mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
1545 /*expectedFlags=*/0);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001546 }
1547
1548 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001549 mInputReceiver.consumeMotionEvent(matcher);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001550 }
1551
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08001552 std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001553
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001554 void assertNoEvents() { mInputReceiver.assertNoEvents(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001555
1556private:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001557 FakeInputReceiver mInputReceiver;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001558};
1559
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001560static InputEventInjectionResult injectKey(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001561 InputDispatcher& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001562 int32_t displayId = ADISPLAY_ID_NONE,
1563 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001564 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001565 bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {},
Prabir Pradhan5735a322022-04-11 17:23:34 +00001566 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001567 KeyEvent event;
1568 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1569
1570 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001571 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001572 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1573 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001574
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001575 if (!allowKeyRepeat) {
1576 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1577 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001578 // Inject event until dispatch out.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001579 return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001580}
1581
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001582static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) {
1583 InputEventInjectionResult result =
1584 injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE,
1585 InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
1586 if (result != InputEventInjectionResult::TIMED_OUT) {
1587 FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result);
1588 }
1589}
1590
1591static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001592 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001593 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001594}
1595
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001596// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1597// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1598// has to be woken up to process the repeating key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001599static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher,
1600 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001601 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001602 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001603 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001604}
1605
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001606static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001607 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001608 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001609}
1610
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001611static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001612 InputDispatcher& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001613 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001614 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001615 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001616 return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1617 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001618}
1619
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001620static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001621 InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId,
1622 const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001623 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001624 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1625 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001626 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001627 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001628 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001629 MotionEventBuilder motionBuilder =
1630 MotionEventBuilder(action, source)
1631 .displayId(displayId)
1632 .eventTime(eventTime)
1633 .rawXCursorPosition(cursorPosition.x)
1634 .rawYCursorPosition(cursorPosition.y)
1635 .pointer(
1636 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1637 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1638 motionBuilder.downTime(eventTime);
1639 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001640
1641 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001642 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1643 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001644}
1645
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001646static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source,
1647 int32_t displayId,
1648 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001649 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001650}
1651
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001652static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source,
1653 int32_t displayId,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001654 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001655 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001656}
1657
Jackal Guof9696682018-10-05 12:23:23 +08001658static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1659 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1660 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001661 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1662 AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action,
1663 /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001664
1665 return args;
1666}
1667
Josep del Riob3981622023-04-18 15:49:45 +00001668static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1669 int32_t displayId = ADISPLAY_ID_NONE) {
1670 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1671 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001672 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1673 AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C,
1674 AMETA_META_ON, currentTime);
Josep del Riob3981622023-04-18 15:49:45 +00001675
1676 return args;
1677}
1678
1679static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1680 int32_t displayId = ADISPLAY_ID_NONE) {
1681 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1682 // Define a valid key event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001683 NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
1684 AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST,
1685 KEY_ASSISTANT, AMETA_NONE, currentTime);
Josep del Riob3981622023-04-18 15:49:45 +00001686
1687 return args;
1688}
1689
Prabir Pradhan678438e2023-04-13 19:32:51 +00001690[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1691 int32_t displayId,
1692 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001693 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001694 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1695 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1696 }
1697
chaviwd1c23182019-12-20 18:44:56 -08001698 PointerProperties pointerProperties[pointerCount];
1699 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001700
chaviwd1c23182019-12-20 18:44:56 -08001701 for (size_t i = 0; i < pointerCount; i++) {
1702 pointerProperties[i].clear();
1703 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001704 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001705
chaviwd1c23182019-12-20 18:44:56 -08001706 pointerCoords[i].clear();
1707 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1708 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1709 }
Jackal Guof9696682018-10-05 12:23:23 +08001710
1711 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1712 // Define a valid motion event.
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001713 NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source,
1714 displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0,
1715 /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001716 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001717 pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001718 AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001719 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{});
Jackal Guof9696682018-10-05 12:23:23 +08001720
1721 return args;
1722}
1723
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001724static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1725 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1726}
1727
chaviwd1c23182019-12-20 18:44:56 -08001728static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1729 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1730}
1731
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001732static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1733 const PointerCaptureRequest& request) {
Prabir Pradhana8fe7c52023-12-14 22:07:23 +00001734 return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC),
1735 request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001736}
1737
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001738} // namespace
1739
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001740/**
1741 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1742 * broken channel.
1743 */
1744TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1745 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1746 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001747 sp<FakeWindowHandle>::make(application, mDispatcher,
1748 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001749
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001750 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001751
1752 // Window closes its channel, but the window remains.
1753 window->destroyReceiver();
1754 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1755}
1756
Arthur Hungb92218b2018-08-14 12:00:21 +08001757TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001758 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001759 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1760 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001761
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001762 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001763 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001764 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001765 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001766
1767 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001768 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001769}
1770
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -08001771using InputDispatcherDeathTest = InputDispatcherTest;
1772
1773/**
1774 * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher
1775 * should crash.
1776 */
1777TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) {
1778 testing::GTEST_FLAG(death_test_style) = "threadsafe";
1779 ScopedSilentDeath _silentDeath;
1780
1781 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1782 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1783 "Fake Window", ADISPLAY_ID_DEFAULT);
1784 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
1785 {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}),
1786 "Incorrect WindowInfosUpdate provided");
1787}
1788
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001789TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1790 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001791 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1792 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001793
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001794 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001795 // Inject a MotionEvent to an unknown display.
1796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001797 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001798 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1799
1800 // Window should receive motion event.
1801 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1802}
1803
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001804/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001805 * Calling onWindowInfosChanged once should not cause any issues.
1806 * This test serves as a sanity check for the next test, where onWindowInfosChanged is
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001807 * called twice.
1808 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001809TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001810 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001811 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1812 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001813 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001814
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001815 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001817 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001818 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001819 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001820
1821 // Window should receive motion event.
1822 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1823}
1824
1825/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001826 * Calling onWindowInfosChanged twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001827 */
1828TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001829 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001830 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1831 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001832 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001833
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001834 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1835 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001836 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001837 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001838 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001839 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001840
1841 // Window should receive motion event.
1842 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1843}
1844
Arthur Hungb92218b2018-08-14 12:00:21 +08001845// The foreground window should receive the first touch down event.
1846TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001847 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001848 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001849 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001850 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001851 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001852
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001853 mDispatcher->onWindowInfosChanged(
1854 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001855 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001856 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001857 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001858
1859 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001860 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001861 windowSecond->assertNoEvents();
1862}
1863
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001864/**
1865 * Two windows: A top window, and a wallpaper behind the window.
1866 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1867 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001868 * 1. foregroundWindow <-- dup touch to wallpaper
1869 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001870 */
1871TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1872 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1873 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001874 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001875 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001876 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001877 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001878 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001879
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001880 mDispatcher->onWindowInfosChanged(
1881 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001882 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001883 injectMotionEvent(*mDispatcher,
1884 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1885 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200))
1886 .build()))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001887 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1888
1889 // Both foreground window and its wallpaper should receive the touch down
1890 foregroundWindow->consumeMotionDown();
1891 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1892
1893 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001894 injectMotionEvent(*mDispatcher,
1895 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
1896 .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200))
1897 .build()))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001898 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1899
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -08001900 foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001901 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1902
1903 // Now the foreground window goes away, but the wallpaper stays
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001904 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001905 foregroundWindow->consumeMotionCancel();
1906 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1907 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1908}
1909
1910/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001911 * Two fingers down on the window, and lift off the first finger.
1912 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1913 * contains a single pointer.
1914 */
1915TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1916 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1917 sp<FakeWindowHandle> window =
1918 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1919
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001920 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001921 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001922 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1923 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1924 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001925 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001926 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1927 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1928 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1929 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001930 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001931 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1932 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1933 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1934 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001935 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1936 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1937 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1938
1939 // Remove the window. The gesture should be canceled
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001940 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001941 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1942 window->consumeMotionEvent(
1943 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1944}
1945
1946/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001947 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1948 * with the following differences:
1949 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1950 * clean up the connection.
1951 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1952 * Ensure that there's no crash in the dispatcher.
1953 */
1954TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1955 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1956 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001957 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001958 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001959 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001960 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001961 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001962
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001963 mDispatcher->onWindowInfosChanged(
1964 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001966 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001967 {100, 200}))
1968 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1969
1970 // Both foreground window and its wallpaper should receive the touch down
1971 foregroundWindow->consumeMotionDown();
1972 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1973
1974 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001975 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001976 ADISPLAY_ID_DEFAULT, {110, 200}))
1977 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1978
1979 foregroundWindow->consumeMotionMove();
1980 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1981
1982 // Wallpaper closes its channel, but the window remains.
1983 wallpaperWindow->destroyReceiver();
1984 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1985
1986 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1987 // is no longer valid.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001988 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001989 foregroundWindow->consumeMotionCancel();
1990}
1991
Arthur Hungc539dbb2022-12-08 07:45:36 +00001992class ShouldSplitTouchFixture : public InputDispatcherTest,
1993 public ::testing::WithParamInterface<bool> {};
1994INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1995 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001996/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001997 * A single window that receives touch (on top), and a wallpaper window underneath it.
1998 * The top window gets a multitouch gesture.
1999 * Ensure that wallpaper gets the same gesture.
2000 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002001TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002002 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002003 sp<FakeWindowHandle> foregroundWindow =
2004 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2005 foregroundWindow->setDupTouchToWallpaper(true);
2006 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002007
2008 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002009 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002010 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002011
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002012 mDispatcher->onWindowInfosChanged(
2013 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002014
2015 // Touch down on top window
2016 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002017 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002018 {100, 100}))
2019 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2020
2021 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002022 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002023 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2024
2025 // Second finger down on the top window
2026 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002027 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002028 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002029 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2030 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002031 .build();
2032 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002033 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002034 InputEventInjectionSync::WAIT_FOR_RESULT))
2035 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2036
Harry Cutts33476232023-01-30 19:57:29 +00002037 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2038 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002039 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002040
2041 const MotionEvent secondFingerUpEvent =
2042 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2043 .displayId(ADISPLAY_ID_DEFAULT)
2044 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002045 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2046 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002047 .build();
2048 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002049 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002050 InputEventInjectionSync::WAIT_FOR_RESULT))
2051 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2052 foregroundWindow->consumeMotionPointerUp(0);
2053 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2054
2055 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002056 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002057 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2058 AINPUT_SOURCE_TOUCHSCREEN)
2059 .displayId(ADISPLAY_ID_DEFAULT)
2060 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts101ee9b2023-07-06 18:04:14 +00002061 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002062 .x(100)
2063 .y(100))
2064 .build(),
2065 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002066 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2067 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2068 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002069}
2070
2071/**
2072 * Two windows: a window on the left and window on the right.
2073 * A third window, wallpaper, is behind both windows, and spans both top windows.
2074 * The first touch down goes to the left window. A second pointer touches down on the right window.
2075 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2076 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2077 * ACTION_POINTER_DOWN(1).
2078 */
2079TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2080 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2081 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002082 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002083 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002084 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002085
2086 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002087 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002088 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002089 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002090
2091 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002092 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002093 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002094 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002095
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002096 mDispatcher->onWindowInfosChanged(
2097 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
2098 {},
2099 0,
2100 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002101
2102 // Touch down on left window
2103 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002104 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002105 {100, 100}))
2106 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2107
2108 // Both foreground window and its wallpaper should receive the touch down
2109 leftWindow->consumeMotionDown();
2110 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2111
2112 // Second finger down on the right window
2113 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002114 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002115 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2117 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002118 .build();
2119 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002120 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002121 InputEventInjectionSync::WAIT_FOR_RESULT))
2122 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2123
2124 leftWindow->consumeMotionMove();
2125 // Since the touch is split, right window gets ACTION_DOWN
2126 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002127 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002128 expectedWallpaperFlags);
2129
2130 // Now, leftWindow, which received the first finger, disappears.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002131 mDispatcher->onWindowInfosChanged(
2132 {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002133 leftWindow->consumeMotionCancel();
2134 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2135 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2136
2137 // The pointer that's still down on the right window moves, and goes to the right window only.
2138 // As far as the dispatcher's concerned though, both pointers are still present.
2139 const MotionEvent secondFingerMoveEvent =
2140 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2141 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002142 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2143 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002144 .build();
2145 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002146 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002147 InputEventInjectionSync::WAIT_FOR_RESULT));
2148 rightWindow->consumeMotionMove();
2149
2150 leftWindow->assertNoEvents();
2151 rightWindow->assertNoEvents();
2152 wallpaperWindow->assertNoEvents();
2153}
2154
Arthur Hungc539dbb2022-12-08 07:45:36 +00002155/**
2156 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2157 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2158 * The right window should receive ACTION_DOWN.
2159 */
2160TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002161 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002162 sp<FakeWindowHandle> leftWindow =
2163 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2164 leftWindow->setFrame(Rect(0, 0, 200, 200));
2165 leftWindow->setDupTouchToWallpaper(true);
2166 leftWindow->setSlippery(true);
2167
2168 sp<FakeWindowHandle> rightWindow =
2169 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2170 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002171
2172 sp<FakeWindowHandle> wallpaperWindow =
2173 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2174 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002175
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002176 mDispatcher->onWindowInfosChanged(
2177 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
2178 {},
2179 0,
2180 0});
Arthur Hung74c248d2022-11-23 07:09:59 +00002181
Arthur Hungc539dbb2022-12-08 07:45:36 +00002182 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002183 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002184 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002185 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002186 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002187
2188 // Both foreground window and its wallpaper should receive the touch down
2189 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002190 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2191
Arthur Hungc539dbb2022-12-08 07:45:36 +00002192 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002193 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002194 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002195 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002196 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2197
Arthur Hungc539dbb2022-12-08 07:45:36 +00002198 leftWindow->consumeMotionCancel();
2199 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2200 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002201}
2202
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002203/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002204 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2205 * interactive, it might stop sending this flag.
2206 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2207 * to have a consistent input stream.
2208 *
2209 * Test procedure:
2210 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2211 * DOWN (new gesture).
2212 *
2213 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2214 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2215 *
2216 * We technically just need a single window here, but we are using two windows (spy on top and a
2217 * regular window below) to emulate the actual situation where it happens on the device.
2218 */
2219TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2220 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2221 sp<FakeWindowHandle> spyWindow =
2222 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2223 spyWindow->setFrame(Rect(0, 0, 200, 200));
2224 spyWindow->setTrustedOverlay(true);
2225 spyWindow->setSpy(true);
2226
2227 sp<FakeWindowHandle> window =
2228 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2229 window->setFrame(Rect(0, 0, 200, 200));
2230
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002231 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002232 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002233
2234 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002235 mDispatcher->notifyMotion(
2236 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2237 .deviceId(touchDeviceId)
2238 .policyFlags(DEFAULT_POLICY_FLAGS)
2239 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2240 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002241
Prabir Pradhan678438e2023-04-13 19:32:51 +00002242 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2243 .deviceId(touchDeviceId)
2244 .policyFlags(DEFAULT_POLICY_FLAGS)
2245 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2246 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2247 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002248 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2249 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2250 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2251 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2252
2253 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002254 mDispatcher->notifyMotion(
2255 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2256 .deviceId(touchDeviceId)
2257 .policyFlags(0)
2258 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2259 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2260 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002261 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2262 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2263
2264 // We don't need to reset the device to reproduce the issue, but the reset event typically
2265 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002266 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002267
2268 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002269 mDispatcher->notifyMotion(
2270 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2271 .deviceId(touchDeviceId)
2272 .policyFlags(DEFAULT_POLICY_FLAGS)
2273 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2274 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002275 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2276 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2277
2278 // No more events
2279 spyWindow->assertNoEvents();
2280 window->assertNoEvents();
2281}
2282
2283/**
Linnan Li907ae732023-09-05 17:14:21 +08002284 * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers.
2285 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2286 * interactive, it might stop sending this flag.
2287 * We've already ensured the consistency of the touch event in this case, and we should also ensure
2288 * the consistency of the hover event in this case.
2289 *
2290 * Test procedure:
2291 * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT
2292 * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT
2293 *
2294 * We expect to receive two full streams of hover events.
2295 */
2296TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) {
2297 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2298
2299 sp<FakeWindowHandle> window =
2300 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2301 window->setFrame(Rect(0, 0, 300, 300));
2302
2303 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2304
2305 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2306 .policyFlags(DEFAULT_POLICY_FLAGS)
2307 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2308 .build());
2309 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2310
2311 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2312 .policyFlags(DEFAULT_POLICY_FLAGS)
2313 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2314 .build());
2315 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2316
2317 // Send hover exit without the default policy flags.
2318 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2319 .policyFlags(0)
2320 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2321 .build());
2322
2323 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2324
2325 // Send a simple hover event stream, ensure dispatcher not crashed and window can receive
2326 // right event.
2327 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2328 .policyFlags(DEFAULT_POLICY_FLAGS)
2329 .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201))
2330 .build());
2331 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2332
2333 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2334 .policyFlags(DEFAULT_POLICY_FLAGS)
2335 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
2336 .build());
2337 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2338
2339 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2340 .policyFlags(DEFAULT_POLICY_FLAGS)
2341 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
2342 .build());
2343 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2344}
2345
2346/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002347 * Two windows: a window on the left and a window on the right.
2348 * Mouse is hovered from the right window into the left window.
2349 * Next, we tap on the left window, where the cursor was last seen.
2350 * The second tap is done onto the right window.
2351 * The mouse and tap are from two different devices.
2352 * We technically don't need to set the downtime / eventtime for these events, but setting these
2353 * explicitly helps during debugging.
2354 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2355 * In the buggy implementation, a tap on the right window would cause a crash.
2356 */
2357TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2358 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2359 sp<FakeWindowHandle> leftWindow =
2360 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2361 leftWindow->setFrame(Rect(0, 0, 200, 200));
2362
2363 sp<FakeWindowHandle> rightWindow =
2364 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2365 rightWindow->setFrame(Rect(200, 0, 400, 200));
2366
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002367 mDispatcher->onWindowInfosChanged(
2368 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002369 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2370 // stale.
2371 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2372 const int32_t mouseDeviceId = 6;
2373 const int32_t touchDeviceId = 4;
2374 // Move the cursor from right
2375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002376 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002377 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2378 AINPUT_SOURCE_MOUSE)
2379 .deviceId(mouseDeviceId)
2380 .downTime(baseTime + 10)
2381 .eventTime(baseTime + 20)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002382 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002383 .build()));
2384 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2385
2386 // .. to the left window
2387 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002388 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002389 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2390 AINPUT_SOURCE_MOUSE)
2391 .deviceId(mouseDeviceId)
2392 .downTime(baseTime + 10)
2393 .eventTime(baseTime + 30)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002394 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002395 .build()));
2396 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2397 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2398 // Now tap the left window
2399 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002400 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002401 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2402 AINPUT_SOURCE_TOUCHSCREEN)
2403 .deviceId(touchDeviceId)
2404 .downTime(baseTime + 40)
2405 .eventTime(baseTime + 40)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002406 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002407 .build()));
2408 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2409 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2410
2411 // release tap
2412 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002413 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002414 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2415 AINPUT_SOURCE_TOUCHSCREEN)
2416 .deviceId(touchDeviceId)
2417 .downTime(baseTime + 40)
2418 .eventTime(baseTime + 50)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002419 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002420 .build()));
2421 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2422
2423 // Tap the window on the right
2424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002425 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002426 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2427 AINPUT_SOURCE_TOUCHSCREEN)
2428 .deviceId(touchDeviceId)
2429 .downTime(baseTime + 60)
2430 .eventTime(baseTime + 60)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002431 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002432 .build()));
2433 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2434
2435 // release tap
2436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002437 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002438 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2439 AINPUT_SOURCE_TOUCHSCREEN)
2440 .deviceId(touchDeviceId)
2441 .downTime(baseTime + 60)
2442 .eventTime(baseTime + 70)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002443 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002444 .build()));
2445 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2446
2447 // No more events
2448 leftWindow->assertNoEvents();
2449 rightWindow->assertNoEvents();
2450}
2451
2452/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002453 * Start hovering in a window. While this hover is still active, make another window appear on top.
2454 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2455 * While the top window is present, the hovering is stopped.
2456 * Later, hovering gets resumed again.
2457 * Ensure that new hover gesture is handled correctly.
2458 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2459 * to the window that's currently being hovered over.
2460 */
2461TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2462 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2463 sp<FakeWindowHandle> window =
2464 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2465 window->setFrame(Rect(0, 0, 200, 200));
2466
2467 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002468 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002469
2470 // Start hovering in the window
2471 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2472 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2473 .build());
2474 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2475
2476 // Now, an obscuring window appears!
2477 sp<FakeWindowHandle> obscuringWindow =
2478 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2479 ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00002480 /*createInputChannel=*/false);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002481 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2482 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2483 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2484 obscuringWindow->setNoInputChannel(true);
2485 obscuringWindow->setFocusable(false);
2486 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002487 mDispatcher->onWindowInfosChanged(
2488 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002489
2490 // While this new obscuring window is present, the hovering is stopped
2491 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2492 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2493 .build());
2494 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2495
2496 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002497 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002498
2499 // And a new hover gesture starts.
2500 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2501 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2502 .build());
2503 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2504}
2505
2506/**
2507 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2508 * the obscuring window.
2509 */
2510TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2511 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2512 sp<FakeWindowHandle> window =
2513 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2514 window->setFrame(Rect(0, 0, 200, 200));
2515
2516 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002517 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002518
2519 // Start hovering in the window
2520 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2521 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2522 .build());
2523 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2524
2525 // Now, an obscuring window appears!
2526 sp<FakeWindowHandle> obscuringWindow =
2527 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2528 ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00002529 /*createInputChannel=*/false);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002530 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2531 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2532 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2533 obscuringWindow->setNoInputChannel(true);
2534 obscuringWindow->setFocusable(false);
2535 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002536 mDispatcher->onWindowInfosChanged(
2537 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002538
2539 // While this new obscuring window is present, the hovering continues. The event can't go to the
2540 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2541 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2542 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2543 .build());
2544 obscuringWindow->assertNoEvents();
2545 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2546
2547 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002548 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002549
2550 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2551 // so it should generate a HOVER_ENTER
2552 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2553 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2554 .build());
2555 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2556
2557 // Now the MOVE should be getting dispatched normally
2558 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2559 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2560 .build());
2561 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2562}
2563
2564/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002565 * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll
2566 * events are delivered to the window.
2567 */
2568TEST_F(InputDispatcherTest, HoverMoveAndScroll) {
2569 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2570 sp<FakeWindowHandle> window =
2571 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2572 window->setFrame(Rect(0, 0, 200, 200));
2573 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2574
2575 // Start hovering in the window
2576 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2577 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
2578 .build());
2579 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2580
2581 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
2582 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2583 .build());
2584 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2585
2586 // Scroll with the mouse
2587 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE)
2588 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2589 .build());
2590 window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL));
2591}
2592
2593using InputDispatcherMultiDeviceTest = InputDispatcherTest;
2594
2595/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002596 * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that
2597 * touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002598 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002599TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002600 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2601 sp<FakeWindowHandle> window =
2602 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2603 window->setFrame(Rect(0, 0, 200, 200));
2604
2605 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2606
2607 constexpr int32_t touchDeviceId = 4;
2608 constexpr int32_t stylusDeviceId = 2;
2609
2610 // Stylus down
2611 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2612 .deviceId(stylusDeviceId)
2613 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2614 .build());
2615 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2616
2617 // Touch down
2618 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2619 .deviceId(touchDeviceId)
2620 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2621 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002622
2623 // Touch move
2624 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2625 .deviceId(touchDeviceId)
2626 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2627 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002628 // Touch is ignored because stylus is already down
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002629
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002630 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002631 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2632 .deviceId(stylusDeviceId)
2633 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2634 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002635 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2636 WithCoords(101, 111)));
2637
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002638 window->assertNoEvents();
2639}
2640
2641/**
2642 * One window and one spy window. Stylus down on the window. Next, touch from another device goes
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002643 * down. Ensure that touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002644 * Similar test as above, but with added SPY window.
2645 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002646TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002647 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2648 sp<FakeWindowHandle> window =
2649 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2650 sp<FakeWindowHandle> spyWindow =
2651 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2652 spyWindow->setFrame(Rect(0, 0, 200, 200));
2653 spyWindow->setTrustedOverlay(true);
2654 spyWindow->setSpy(true);
2655 window->setFrame(Rect(0, 0, 200, 200));
2656
2657 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
2658
2659 constexpr int32_t touchDeviceId = 4;
2660 constexpr int32_t stylusDeviceId = 2;
2661
2662 // Stylus down
2663 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2664 .deviceId(stylusDeviceId)
2665 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2666 .build());
2667 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2668 spyWindow->consumeMotionEvent(
2669 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2670
2671 // Touch down
2672 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2673 .deviceId(touchDeviceId)
2674 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2675 .build());
2676
2677 // Touch move
2678 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2679 .deviceId(touchDeviceId)
2680 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2681 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002682
2683 // Touch is ignored because stylus is already down
2684
2685 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002686 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2687 .deviceId(stylusDeviceId)
2688 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2689 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002690 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2691 WithCoords(101, 111)));
2692 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2693 WithCoords(101, 111)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002694
2695 window->assertNoEvents();
2696 spyWindow->assertNoEvents();
2697}
2698
2699/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002700 * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002701 * touch is dropped, because stylus hover takes precedence.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002702 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002703TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002704 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2705 sp<FakeWindowHandle> window =
2706 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2707 window->setFrame(Rect(0, 0, 200, 200));
2708
2709 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2710
2711 constexpr int32_t touchDeviceId = 4;
2712 constexpr int32_t stylusDeviceId = 2;
2713
2714 // Stylus down on the window
2715 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2716 .deviceId(stylusDeviceId)
2717 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2718 .build());
2719 window->consumeMotionEvent(
2720 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2721
2722 // Touch down on window
2723 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2724 .deviceId(touchDeviceId)
2725 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2726 .build());
2727 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2728 .deviceId(touchDeviceId)
2729 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2730 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002731
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002732 // Touch is ignored because stylus is hovering
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002733
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002734 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002735 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2736 .deviceId(stylusDeviceId)
2737 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2738 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002739 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2740 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002741
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002742 // and subsequent touches continue to be ignored
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002743 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2744 .deviceId(touchDeviceId)
2745 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2746 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002747 window->assertNoEvents();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002748}
2749
2750/**
2751 * One window. Touch down on the window. Then, stylus hover on the window from another device.
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002752 * Ensure that touch is canceled, because stylus hover should take precedence.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002753 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002754TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002755 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2756 sp<FakeWindowHandle> window =
2757 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2758 window->setFrame(Rect(0, 0, 200, 200));
2759
2760 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2761
2762 constexpr int32_t touchDeviceId = 4;
2763 constexpr int32_t stylusDeviceId = 2;
2764
2765 // Touch down on window
2766 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2767 .deviceId(touchDeviceId)
2768 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2769 .build());
2770 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2771 .deviceId(touchDeviceId)
2772 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2773 .build());
2774 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2775 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2776
2777 // Stylus hover on the window
2778 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2779 .deviceId(stylusDeviceId)
2780 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2781 .build());
2782 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2783 .deviceId(stylusDeviceId)
2784 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2785 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002786 // Stylus hover movement causes touch to be canceled
2787 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
2788 WithCoords(141, 146)));
2789 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
2790 WithDeviceId(stylusDeviceId), WithCoords(100, 110)));
2791 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE),
2792 WithDeviceId(stylusDeviceId), WithCoords(101, 111)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002793
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002794 // Subsequent touch movements are ignored
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002795 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2796 .deviceId(touchDeviceId)
2797 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2798 .build());
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07002799
2800 window->assertNoEvents();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002801}
2802
2803/**
2804 * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that
2805 * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should
2806 * become active.
2807 */
2808TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) {
2809 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2810 sp<FakeWindowHandle> window =
2811 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2812 window->setFrame(Rect(0, 0, 200, 200));
2813
2814 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2815
2816 constexpr int32_t stylusDeviceId1 = 3;
2817 constexpr int32_t stylusDeviceId2 = 5;
2818
2819 // Touch down on window
2820 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2821 .deviceId(stylusDeviceId1)
2822 .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100))
2823 .build());
2824 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2825 .deviceId(stylusDeviceId1)
2826 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2827 .build());
2828 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1)));
2829 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1)));
2830
2831 // Second stylus down
2832 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2833 .deviceId(stylusDeviceId2)
2834 .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10))
2835 .build());
2836 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2837 .deviceId(stylusDeviceId2)
2838 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11))
2839 .build());
2840
2841 // First stylus is canceled, second one takes over.
2842 window->consumeMotionEvent(
2843 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1)));
2844 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2)));
2845 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2)));
2846
2847 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2848 .deviceId(stylusDeviceId1)
2849 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2850 .build());
2851 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002852 window->assertNoEvents();
2853}
2854
2855/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002856 * One window. Touch down on the window. Then, stylus down on the window from another device.
2857 * Ensure that is canceled, because stylus down should be preferred over touch.
2858 */
2859TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) {
2860 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2861 sp<FakeWindowHandle> window =
2862 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2863 window->setFrame(Rect(0, 0, 200, 200));
2864
2865 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2866
2867 constexpr int32_t touchDeviceId = 4;
2868 constexpr int32_t stylusDeviceId = 2;
2869
2870 // Touch down on window
2871 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2872 .deviceId(touchDeviceId)
2873 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2874 .build());
2875 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2876 .deviceId(touchDeviceId)
2877 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2878 .build());
2879 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2880 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2881
2882 // Stylus down on the window
2883 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2884 .deviceId(stylusDeviceId)
2885 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2886 .build());
2887 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2888 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2889
2890 // Subsequent stylus movements are delivered correctly
2891 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2892 .deviceId(stylusDeviceId)
2893 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2894 .build());
2895 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2896 WithCoords(101, 111)));
2897}
2898
2899/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002900 * Two windows: a window on the left and a window on the right.
2901 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2902 * down. Then, on the left window, also place second touch pointer down.
2903 * This test tries to reproduce a crash.
2904 * In the buggy implementation, second pointer down on the left window would cause a crash.
2905 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002906TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002907 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2908 sp<FakeWindowHandle> leftWindow =
2909 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2910 leftWindow->setFrame(Rect(0, 0, 200, 200));
2911
2912 sp<FakeWindowHandle> rightWindow =
2913 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2914 rightWindow->setFrame(Rect(200, 0, 400, 200));
2915
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002916 mDispatcher->onWindowInfosChanged(
2917 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002918
2919 const int32_t touchDeviceId = 4;
2920 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002921
2922 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002923 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2924 .deviceId(mouseDeviceId)
2925 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2926 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002927 leftWindow->consumeMotionEvent(
2928 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2929
2930 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002931 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2932 .deviceId(mouseDeviceId)
2933 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2934 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2935 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002936
2937 leftWindow->consumeMotionEvent(
2938 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2939 leftWindow->consumeMotionEvent(
2940 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2941
Prabir Pradhan678438e2023-04-13 19:32:51 +00002942 mDispatcher->notifyMotion(
2943 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2944 .deviceId(mouseDeviceId)
2945 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2946 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2947 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2948 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002949 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2950
2951 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002952 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2953 .deviceId(touchDeviceId)
2954 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2955 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002956 leftWindow->assertNoEvents();
2957
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002958 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2959
2960 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002961 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2962 .deviceId(touchDeviceId)
2963 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2964 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2965 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002966 // Since this is now a new splittable pointer going down on the left window, and it's coming
2967 // from a different device, the current gesture in the left window (pointer down) should first
2968 // be canceled.
2969 leftWindow->consumeMotionEvent(
2970 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002971 leftWindow->consumeMotionEvent(
2972 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2973 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2974 // current implementation.
2975 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2976 rightWindow->consumeMotionEvent(
2977 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2978
2979 leftWindow->assertNoEvents();
2980 rightWindow->assertNoEvents();
2981}
2982
2983/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002984 * Two windows: a window on the left and a window on the right.
2985 * Mouse is hovered on the left window and stylus is hovered on the right window.
2986 */
2987TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) {
2988 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2989 sp<FakeWindowHandle> leftWindow =
2990 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2991 leftWindow->setFrame(Rect(0, 0, 200, 200));
2992
2993 sp<FakeWindowHandle> rightWindow =
2994 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2995 rightWindow->setFrame(Rect(200, 0, 400, 200));
2996
2997 mDispatcher->onWindowInfosChanged(
2998 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
2999
3000 const int32_t stylusDeviceId = 3;
3001 const int32_t mouseDeviceId = 6;
3002
3003 // Start hovering over the left window
3004 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3005 .deviceId(mouseDeviceId)
3006 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
3007 .build());
3008 leftWindow->consumeMotionEvent(
3009 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3010
3011 // Stylus hovered on right window
3012 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3013 .deviceId(stylusDeviceId)
3014 .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100))
3015 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003016 rightWindow->consumeMotionEvent(
3017 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3018
3019 // Subsequent HOVER_MOVE events are dispatched correctly.
3020 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
3021 .deviceId(mouseDeviceId)
3022 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
3023 .build());
3024 leftWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003025 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003026
3027 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3028 .deviceId(stylusDeviceId)
3029 .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110))
3030 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003031 rightWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003032 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003033
3034 leftWindow->assertNoEvents();
3035 rightWindow->assertNoEvents();
3036}
3037
3038/**
3039 * Three windows: a window on the left and a window on the right.
3040 * And a spy window that's positioned above all of them.
3041 * Stylus down on the left window and remains down. Touch goes down on the right and remains down.
3042 * Check the stream that's received by the spy.
3043 */
3044TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) {
3045 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3046
3047 sp<FakeWindowHandle> spyWindow =
3048 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3049 spyWindow->setFrame(Rect(0, 0, 400, 400));
3050 spyWindow->setTrustedOverlay(true);
3051 spyWindow->setSpy(true);
3052
3053 sp<FakeWindowHandle> leftWindow =
3054 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3055 leftWindow->setFrame(Rect(0, 0, 200, 200));
3056
3057 sp<FakeWindowHandle> rightWindow =
3058 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3059
3060 rightWindow->setFrame(Rect(200, 0, 400, 200));
3061
3062 mDispatcher->onWindowInfosChanged(
3063 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3064
3065 const int32_t stylusDeviceId = 1;
3066 const int32_t touchDeviceId = 2;
3067
3068 // Stylus down on the left window
3069 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3070 .deviceId(stylusDeviceId)
3071 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
3072 .build());
3073 leftWindow->consumeMotionEvent(
3074 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3075 spyWindow->consumeMotionEvent(
3076 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3077
3078 // Touch down on the right window
3079 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3080 .deviceId(touchDeviceId)
3081 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3082 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003083 leftWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003084 rightWindow->consumeMotionEvent(
3085 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003086
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003087 // Spy window does not receive touch events, because stylus events take precedence, and it
3088 // already has an active stylus gesture.
3089
3090 // Stylus movements continue. They should be delivered to the left window and to the spy window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003091 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
3092 .deviceId(stylusDeviceId)
3093 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
3094 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003095 leftWindow->consumeMotionEvent(
3096 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
3097 spyWindow->consumeMotionEvent(
3098 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003099
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003100 // Further MOVE events keep going to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003101 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3102 .deviceId(touchDeviceId)
3103 .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110))
3104 .build());
3105 rightWindow->consumeMotionEvent(
3106 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003107
3108 spyWindow->assertNoEvents();
3109 leftWindow->assertNoEvents();
3110 rightWindow->assertNoEvents();
3111}
3112
3113/**
3114 * Three windows: a window on the left, a window on the right, and a spy window positioned above
3115 * both.
3116 * Check hover in left window and touch down in the right window.
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003117 * At first, spy should receive hover. Spy shouldn't receive touch while stylus is hovering.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003118 * At the same time, left and right should be getting independent streams of hovering and touch,
3119 * respectively.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003120 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003121TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003122 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3123
3124 sp<FakeWindowHandle> spyWindow =
3125 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3126 spyWindow->setFrame(Rect(0, 0, 400, 400));
3127 spyWindow->setTrustedOverlay(true);
3128 spyWindow->setSpy(true);
3129
3130 sp<FakeWindowHandle> leftWindow =
3131 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3132 leftWindow->setFrame(Rect(0, 0, 200, 200));
3133
3134 sp<FakeWindowHandle> rightWindow =
3135 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3136 rightWindow->setFrame(Rect(200, 0, 400, 200));
3137
3138 mDispatcher->onWindowInfosChanged(
3139 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3140
3141 const int32_t stylusDeviceId = 1;
3142 const int32_t touchDeviceId = 2;
3143
3144 // Stylus hover on the left window
3145 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3146 .deviceId(stylusDeviceId)
3147 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
3148 .build());
3149 leftWindow->consumeMotionEvent(
3150 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3151 spyWindow->consumeMotionEvent(
3152 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3153
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003154 // Touch down on the right window. Spy doesn't receive this touch because it already has
3155 // stylus hovering there.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003156 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3157 .deviceId(touchDeviceId)
3158 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3159 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003160 leftWindow->assertNoEvents();
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003161 spyWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003162 rightWindow->consumeMotionEvent(
3163 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3164
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003165 // Stylus movements continue. They should be delivered to the left window and the spy.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003166 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3167 .deviceId(stylusDeviceId)
3168 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
3169 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003170 leftWindow->consumeMotionEvent(
3171 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003172 spyWindow->consumeMotionEvent(
3173 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003174
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003175 // Touch movements continue. They should be delivered to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003176 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3177 .deviceId(touchDeviceId)
3178 .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101))
3179 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003180 rightWindow->consumeMotionEvent(
3181 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
3182
3183 spyWindow->assertNoEvents();
3184 leftWindow->assertNoEvents();
3185 rightWindow->assertNoEvents();
3186}
3187
3188/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003189 * On a single window, use two different devices: mouse and touch.
3190 * Touch happens first, with two pointers going down, and then the first pointer leaving.
3191 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
3192 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
3193 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
3194 * represent a new gesture.
3195 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003196TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003197 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3198 sp<FakeWindowHandle> window =
3199 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3200 window->setFrame(Rect(0, 0, 400, 400));
3201
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003202 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003203
3204 const int32_t touchDeviceId = 4;
3205 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003206
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003207 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003208 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3209 .deviceId(touchDeviceId)
3210 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3211 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003212 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003213 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3214 .deviceId(touchDeviceId)
3215 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3216 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3217 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003218 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003219 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
3220 .deviceId(touchDeviceId)
3221 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3222 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3223 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003224 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3225 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3226 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
3227
3228 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00003229 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3230 .deviceId(mouseDeviceId)
3231 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3232 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3233 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003234
3235 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08003236 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003237 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3238
Prabir Pradhan678438e2023-04-13 19:32:51 +00003239 mDispatcher->notifyMotion(
3240 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3241 .deviceId(mouseDeviceId)
3242 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3243 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3244 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3245 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003246 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3247
3248 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003249 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3250 .deviceId(touchDeviceId)
3251 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3252 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3253 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003254 // Since we already canceled this touch gesture, it will be ignored until a completely new
3255 // gesture is started. This is easier to implement than trying to keep track of the new pointer
3256 // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN.
3257 // However, mouse movements should continue to work.
3258 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3259 .deviceId(mouseDeviceId)
3260 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3261 .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110))
3262 .build());
3263 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId)));
3264
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003265 window->assertNoEvents();
3266}
3267
3268/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003269 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
3270 * the injected event.
3271 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003272TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) {
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003273 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3274 sp<FakeWindowHandle> window =
3275 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3276 window->setFrame(Rect(0, 0, 400, 400));
3277
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003278 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003279
3280 const int32_t touchDeviceId = 4;
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003281 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
3282 // completion.
3283 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003284 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003285 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3286 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003287 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003288 .build()));
3289 window->consumeMotionEvent(
3290 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3291
3292 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
3293 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003294 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3295 .deviceId(touchDeviceId)
3296 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3297 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003298
3299 window->consumeMotionEvent(
3300 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3301 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3302}
3303
3304/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003305 * This test is similar to the test above, but the sequence of injected events is different.
3306 *
3307 * Two windows: a window on the left and a window on the right.
3308 * Mouse is hovered over the left window.
3309 * Next, we tap on the left window, where the cursor was last seen.
3310 *
3311 * After that, we inject one finger down onto the right window, and then a second finger down onto
3312 * the left window.
3313 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
3314 * window (first), and then another on the left window (second).
3315 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
3316 * In the buggy implementation, second finger down on the left window would cause a crash.
3317 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003318TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) {
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003319 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3320 sp<FakeWindowHandle> leftWindow =
3321 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3322 leftWindow->setFrame(Rect(0, 0, 200, 200));
3323
3324 sp<FakeWindowHandle> rightWindow =
3325 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3326 rightWindow->setFrame(Rect(200, 0, 400, 200));
3327
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003328 mDispatcher->onWindowInfosChanged(
3329 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003330
3331 const int32_t mouseDeviceId = 6;
3332 const int32_t touchDeviceId = 4;
3333 // Hover over the left window. Keep the cursor there.
3334 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003335 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003336 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3337 AINPUT_SOURCE_MOUSE)
3338 .deviceId(mouseDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003339 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003340 .build()));
3341 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3342
3343 // Tap on left window
3344 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003345 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003346 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3347 AINPUT_SOURCE_TOUCHSCREEN)
3348 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003349 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003350 .build()));
3351
3352 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003353 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003354 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3355 AINPUT_SOURCE_TOUCHSCREEN)
3356 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003357 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003358 .build()));
3359 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3360 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3361 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
3362
3363 // First finger down on right window
3364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003365 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003366 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3367 AINPUT_SOURCE_TOUCHSCREEN)
3368 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003369 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003370 .build()));
3371 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3372
3373 // Second finger down on the left window
3374 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003375 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003376 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3377 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003378 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3379 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003380 .build()));
3381 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3382 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
3383
3384 // No more events
3385 leftWindow->assertNoEvents();
3386 rightWindow->assertNoEvents();
3387}
3388
3389/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003390 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
3391 * While the touch is down, new hover events from the stylus device should be ignored. After the
3392 * touch is gone, stylus hovering should start working again.
3393 */
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003394TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) {
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003395 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3396 sp<FakeWindowHandle> window =
3397 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3398 window->setFrame(Rect(0, 0, 200, 200));
3399
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003400 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003401
3402 const int32_t stylusDeviceId = 5;
3403 const int32_t touchDeviceId = 4;
3404 // Start hovering with stylus
3405 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003406 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003407 MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003408 .deviceId(stylusDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003409 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003410 .build()));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003411 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003412
3413 // Finger down on the window
3414 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003415 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003416 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003417 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003418 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003419 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003420 // The touch device should be ignored!
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003421
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003422 // Continue hovering with stylus.
3423 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003424 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003425 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3426 AINPUT_SOURCE_STYLUS)
3427 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003428 .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003429 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003430 // Hovers continue to work
3431 window->consumeMotionEvent(
3432 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003433
3434 // Lift up the finger
3435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003436 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003437 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3438 AINPUT_SOURCE_TOUCHSCREEN)
3439 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003440 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003441 .build()));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003442
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003444 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003445 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3446 AINPUT_SOURCE_STYLUS)
3447 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003448 .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003449 .build()));
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -07003450 window->consumeMotionEvent(
3451 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003452 window->assertNoEvents();
3453}
3454
3455/**
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07003456 * If stylus is down anywhere on the screen, then touches should not be delivered to windows that
3457 * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH.
3458 *
3459 * Two windows: one on the left and one on the right.
3460 * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config.
3461 * Stylus down on the left window, and then touch down on the right window.
3462 * Check that the right window doesn't get touches while the stylus is down on the left window.
3463 */
3464TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) {
3465 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3466 sp<FakeWindowHandle> leftWindow =
3467 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
3468 ADISPLAY_ID_DEFAULT);
3469 leftWindow->setFrame(Rect(0, 0, 100, 100));
3470
3471 sp<FakeWindowHandle> sbtRightWindow =
3472 sp<FakeWindowHandle>::make(application, mDispatcher,
3473 "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT);
3474 sbtRightWindow->setFrame(Rect(100, 100, 200, 200));
3475 sbtRightWindow->setGlobalStylusBlocksTouch(true);
3476
3477 mDispatcher->onWindowInfosChanged(
3478 {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0});
3479
3480 const int32_t stylusDeviceId = 5;
3481 const int32_t touchDeviceId = 4;
3482
3483 // Stylus down in the left window
3484 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3485 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52))
3486 .deviceId(stylusDeviceId)
3487 .build());
3488 leftWindow->consumeMotionEvent(
3489 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
3490
3491 // Finger tap on the right window
3492 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3493 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3494 .deviceId(touchDeviceId)
3495 .build());
3496 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3497 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3498 .deviceId(touchDeviceId)
3499 .build());
3500
3501 // The touch should be blocked, because stylus is down somewhere else on screen!
3502 sbtRightWindow->assertNoEvents();
3503
3504 // Continue stylus motion, and ensure it's not impacted.
3505 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
3506 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3507 .deviceId(stylusDeviceId)
3508 .build());
3509 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3510 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3511 .deviceId(stylusDeviceId)
3512 .build());
3513 leftWindow->consumeMotionEvent(
3514 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
3515 leftWindow->consumeMotionEvent(
3516 AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId)));
3517
3518 // Now that the stylus gesture is done, touches should be getting delivered correctly.
3519 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3520 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153))
3521 .deviceId(touchDeviceId)
3522 .build());
3523 sbtRightWindow->consumeMotionEvent(
3524 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3525}
3526
3527/**
3528 * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows
3529 * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH.
3530 *
3531 * Two windows: one on the left and one on the right.
3532 * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config.
3533 * Stylus hover on the left window, and then touch down on the right window.
3534 * Check that the right window doesn't get touches while the stylus is hovering on the left window.
3535 */
3536TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) {
3537 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3538 sp<FakeWindowHandle> leftWindow =
3539 sp<FakeWindowHandle>::make(application, mDispatcher, "Left window",
3540 ADISPLAY_ID_DEFAULT);
3541 leftWindow->setFrame(Rect(0, 0, 100, 100));
3542
3543 sp<FakeWindowHandle> sbtRightWindow =
3544 sp<FakeWindowHandle>::make(application, mDispatcher,
3545 "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT);
3546 sbtRightWindow->setFrame(Rect(100, 100, 200, 200));
3547 sbtRightWindow->setGlobalStylusBlocksTouch(true);
3548
3549 mDispatcher->onWindowInfosChanged(
3550 {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0});
3551
3552 const int32_t stylusDeviceId = 5;
3553 const int32_t touchDeviceId = 4;
3554
3555 // Stylus hover in the left window
3556 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3557 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52))
3558 .deviceId(stylusDeviceId)
3559 .build());
3560 leftWindow->consumeMotionEvent(
3561 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3562
3563 // Finger tap on the right window
3564 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3565 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3566 .deviceId(touchDeviceId)
3567 .build());
3568 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
3569 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151))
3570 .deviceId(touchDeviceId)
3571 .build());
3572
3573 // The touch should be blocked, because stylus is hovering somewhere else on screen!
3574 sbtRightWindow->assertNoEvents();
3575
3576 // Continue stylus motion, and ensure it's not impacted.
3577 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3578 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3579 .deviceId(stylusDeviceId)
3580 .build());
3581 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3582 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53))
3583 .deviceId(stylusDeviceId)
3584 .build());
3585 leftWindow->consumeMotionEvent(
3586 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
3587 leftWindow->consumeMotionEvent(
3588 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId)));
3589
3590 // Now that the stylus gesture is done, touches should be getting delivered correctly.
3591 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3592 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153))
3593 .deviceId(touchDeviceId)
3594 .build());
3595 sbtRightWindow->consumeMotionEvent(
3596 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3597}
3598
3599/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003600 * A spy window above a window with no input channel.
3601 * Start hovering with a stylus device, and then tap with it.
3602 * Ensure spy window receives the entire sequence.
3603 */
3604TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
3605 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3606 sp<FakeWindowHandle> spyWindow =
3607 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3608 spyWindow->setFrame(Rect(0, 0, 200, 200));
3609 spyWindow->setTrustedOverlay(true);
3610 spyWindow->setSpy(true);
3611 sp<FakeWindowHandle> window =
3612 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3613 window->setNoInputChannel(true);
3614 window->setFrame(Rect(0, 0, 200, 200));
3615
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003616 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003617
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003618 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00003619 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3620 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3621 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003622 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3623 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003624 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3625 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3626 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003627 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3628
3629 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003630 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3631 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3632 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003633 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3634
3635 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00003636 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3637 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3638 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003639 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
3640
3641 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00003642 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3643 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3644 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003645 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3646 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003647 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3648 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3649 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003650 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3651
3652 // No more events
3653 spyWindow->assertNoEvents();
3654 window->assertNoEvents();
3655}
3656
3657/**
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07003658 * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be
3659 * rejected. But since we already have an ongoing gesture, this event should be processed.
3660 * This prevents inconsistent events being handled inside the dispatcher.
3661 */
3662TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) {
3663 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3664
3665 sp<FakeWindowHandle> window =
3666 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3667 window->setFrame(Rect(0, 0, 200, 200));
3668
3669 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
3670
3671 // Start hovering with stylus
3672 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3673 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3674 .build());
3675 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3676
3677 NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3678 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3679 .build();
3680 // Make this 'hoverExit' event stale
3681 mFakePolicy->setStaleEventTimeout(100ms);
3682 std::this_thread::sleep_for(100ms);
3683
3684 // It shouldn't be dropped by the dispatcher, even though it's stale.
3685 mDispatcher->notifyMotion(hoverExit);
3686 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3687
3688 // Stylus starts hovering again! There should be no crash.
3689 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3690 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51))
3691 .build());
3692 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3693}
3694
3695/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003696 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
3697 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3698 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3699 * While the mouse is down, new move events from the touch device should be ignored.
3700 */
3701TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3702 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3703 sp<FakeWindowHandle> spyWindow =
3704 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3705 spyWindow->setFrame(Rect(0, 0, 200, 200));
3706 spyWindow->setTrustedOverlay(true);
3707 spyWindow->setSpy(true);
3708 sp<FakeWindowHandle> window =
3709 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3710 window->setFrame(Rect(0, 0, 200, 200));
3711
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003712 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003713
3714 const int32_t mouseDeviceId = 7;
3715 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003716
3717 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003718 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3719 .deviceId(mouseDeviceId)
3720 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3721 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003722 spyWindow->consumeMotionEvent(
3723 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3724 window->consumeMotionEvent(
3725 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3726
3727 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003728 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3729 .deviceId(touchDeviceId)
3730 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3731 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003732 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3733 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3734 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3735 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3736
Prabir Pradhan678438e2023-04-13 19:32:51 +00003737 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3738 .deviceId(touchDeviceId)
3739 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3740 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003741 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3742 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3743
3744 // Pilfer the stream
3745 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3746 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3747
Prabir Pradhan678438e2023-04-13 19:32:51 +00003748 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3749 .deviceId(touchDeviceId)
3750 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3751 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003752 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3753
3754 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003755 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3756 .deviceId(mouseDeviceId)
3757 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3758 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3759 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003760
3761 spyWindow->consumeMotionEvent(
3762 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3763 spyWindow->consumeMotionEvent(
3764 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3765 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3766
Prabir Pradhan678438e2023-04-13 19:32:51 +00003767 mDispatcher->notifyMotion(
3768 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3769 .deviceId(mouseDeviceId)
3770 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3771 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3772 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3773 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003774 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3775 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3776
3777 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003778 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3779 .deviceId(mouseDeviceId)
3780 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3781 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3782 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003783 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3784 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3785
3786 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003787 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3788 .deviceId(touchDeviceId)
3789 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3790 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003791
3792 // No more events
3793 spyWindow->assertNoEvents();
3794 window->assertNoEvents();
3795}
3796
3797/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003798 * On the display, have a single window, and also an area where there's no window.
3799 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
3800 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
3801 */
3802TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3803 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3804 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003805 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003806
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003807 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003808
3809 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00003810 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003811
3812 mDispatcher->waitForIdle();
3813 window->assertNoEvents();
3814
3815 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003816 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003817 mDispatcher->waitForIdle();
3818 window->consumeMotionDown();
3819}
3820
3821/**
3822 * Same test as above, but instead of touching the empty space, the first touch goes to
3823 * non-touchable window.
3824 */
3825TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3826 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3827 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003828 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003829 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3830 window1->setTouchable(false);
3831 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003832 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003833 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3834
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003835 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003836
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003837 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003838 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003839
3840 mDispatcher->waitForIdle();
3841 window1->assertNoEvents();
3842 window2->assertNoEvents();
3843
3844 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003845 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003846 mDispatcher->waitForIdle();
3847 window2->consumeMotionDown();
3848}
3849
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003850/**
3851 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3852 * to the event time of the first ACTION_DOWN sent to the particular window.
3853 */
3854TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3855 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3856 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003857 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003858 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3859 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003860 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003861 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3862
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003863 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003864
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003865 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003866 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003867
3868 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003869
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003870 std::unique_ptr<MotionEvent> motionEvent1 = window1->consumeMotionEvent();
3871 ASSERT_NE(nullptr, motionEvent1);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003872 window2->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003873 nsecs_t downTimeForWindow1 = motionEvent1->getDownTime();
3874 ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003875
3876 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003877 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003878 mDispatcher->waitForIdle();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003879 std::unique_ptr<MotionEvent> motionEvent2 = window2->consumeMotionEvent();
3880 ASSERT_NE(nullptr, motionEvent2);
3881 nsecs_t downTimeForWindow2 = motionEvent2->getDownTime();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003882 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08003883 ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003884
3885 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003886 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003887 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003888 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003889
3890 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003891 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003892 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003893 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003894
3895 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3896 window1->consumeMotionMove();
3897 window1->assertNoEvents();
3898
3899 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003900 mDispatcher->notifyMotion(
3901 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003902 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003903 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003904
Prabir Pradhan678438e2023-04-13 19:32:51 +00003905 mDispatcher->notifyMotion(
3906 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003907 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003908 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003909}
3910
Garfield Tandf26e862020-07-01 20:18:19 -07003911TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003912 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003913 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003914 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003915 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003916 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003917 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003918 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003919
3920 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3921
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003922 mDispatcher->onWindowInfosChanged(
3923 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07003924
3925 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003927 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003928 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3929 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003930 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003931 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003932 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003933
3934 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003936 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003937 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3938 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003939 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003940 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003941 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3942 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003943
3944 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003945 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003946 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003947 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3948 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003949 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003950 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003951 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3952 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003953
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003954 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003955 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003956 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3957 AINPUT_SOURCE_MOUSE)
3958 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3959 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003960 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003961 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003962 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003963
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003964 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003965 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003966 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3967 AINPUT_SOURCE_MOUSE)
3968 .buttonState(0)
3969 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003970 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003971 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003972 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003973
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003974 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003975 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003976 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3977 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003978 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003979 .build()));
3980 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3981
3982 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003984 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003985 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3986 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003987 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003988 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003989 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003990
3991 // No more events
3992 windowLeft->assertNoEvents();
3993 windowRight->assertNoEvents();
3994}
3995
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003996/**
3997 * Put two fingers down (and don't release them) and click the mouse button.
3998 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3999 * currently active gesture should be canceled, and the new one should proceed.
4000 */
4001TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
4002 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4003 sp<FakeWindowHandle> window =
4004 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4005 window->setFrame(Rect(0, 0, 600, 800));
4006
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004007 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004008
4009 const int32_t touchDeviceId = 4;
4010 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004011
4012 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004013 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4014 .deviceId(touchDeviceId)
4015 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4016 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004017
Prabir Pradhan678438e2023-04-13 19:32:51 +00004018 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4019 .deviceId(touchDeviceId)
4020 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4021 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
4022 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004023 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4024 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4025
4026 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00004027 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4028 .deviceId(mouseDeviceId)
4029 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4030 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4031 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004032 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
4033 WithPointerCount(2u)));
4034 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
4035
Prabir Pradhan678438e2023-04-13 19:32:51 +00004036 mDispatcher->notifyMotion(
4037 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
4038 .deviceId(mouseDeviceId)
4039 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4040 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
4041 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4042 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004043 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
4044
4045 // Try to send more touch events while the mouse is down. Since it's a continuation of an
4046 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004047 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
4048 .deviceId(touchDeviceId)
4049 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
4050 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
4051 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004052 window->assertNoEvents();
4053}
4054
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004055TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
4056 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4057
4058 sp<FakeWindowHandle> spyWindow =
4059 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4060 spyWindow->setFrame(Rect(0, 0, 600, 800));
4061 spyWindow->setTrustedOverlay(true);
4062 spyWindow->setSpy(true);
4063 sp<FakeWindowHandle> window =
4064 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4065 window->setFrame(Rect(0, 0, 600, 800));
4066
4067 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004068 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004069
4070 // Send mouse cursor to the window
4071 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004072 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004073 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4074 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004075 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004076 .build()));
4077
4078 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4079 WithSource(AINPUT_SOURCE_MOUSE)));
4080 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4081 WithSource(AINPUT_SOURCE_MOUSE)));
4082
4083 window->assertNoEvents();
4084 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004085}
4086
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004087TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
4088 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4089
4090 sp<FakeWindowHandle> spyWindow =
4091 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4092 spyWindow->setFrame(Rect(0, 0, 600, 800));
4093 spyWindow->setTrustedOverlay(true);
4094 spyWindow->setSpy(true);
4095 sp<FakeWindowHandle> window =
4096 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4097 window->setFrame(Rect(0, 0, 600, 800));
4098
4099 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004100 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004101
4102 // Send mouse cursor to the window
4103 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004104 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004105 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4106 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004107 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004108 .build()));
4109
4110 // Move mouse cursor
4111 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004112 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004113 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4114 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004115 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004116 .build()));
4117
4118 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4119 WithSource(AINPUT_SOURCE_MOUSE)));
4120 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4121 WithSource(AINPUT_SOURCE_MOUSE)));
4122 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4123 WithSource(AINPUT_SOURCE_MOUSE)));
4124 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4125 WithSource(AINPUT_SOURCE_MOUSE)));
4126 // Touch down on the window
4127 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004128 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004129 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4130 AINPUT_SOURCE_TOUCHSCREEN)
4131 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004132 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004133 .build()));
4134 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4135 WithSource(AINPUT_SOURCE_MOUSE)));
4136 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4137 WithSource(AINPUT_SOURCE_MOUSE)));
4138 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4139 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4140 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4141 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4142
4143 // pilfer the motion, retaining the gesture on the spy window.
4144 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
4145 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
4146 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4147
4148 // Touch UP on the window
4149 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004150 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004151 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4152 AINPUT_SOURCE_TOUCHSCREEN)
4153 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004154 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004155 .build()));
4156 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4157 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4158
4159 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
4160 // to send a new gesture. It should again go to both windows (spy and the window below), just
4161 // like the first gesture did, before pilfering. The window configuration has not changed.
4162
4163 // One more tap - DOWN
4164 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004165 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004166 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
4167 AINPUT_SOURCE_TOUCHSCREEN)
4168 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004169 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004170 .build()));
4171 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4172 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4173 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4174 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4175
4176 // Touch UP on the window
4177 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004178 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004179 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
4180 AINPUT_SOURCE_TOUCHSCREEN)
4181 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004182 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08004183 .build()));
4184 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4185 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4186 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4187 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
4188
4189 window->assertNoEvents();
4190 spyWindow->assertNoEvents();
4191}
4192
Garfield Tandf26e862020-07-01 20:18:19 -07004193// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
4194// directly in this test.
4195TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07004196 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07004197 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004198 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07004199 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07004200
4201 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4202
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004203 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07004204
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004205 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004206 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004207 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4208 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004209 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004210 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004211 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07004212 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004214 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004215 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4216 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004217 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004218 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004219 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4220 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07004221
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004222 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004223 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004224 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
4225 AINPUT_SOURCE_MOUSE)
4226 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
4227 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004228 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004229 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004230 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07004231
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004233 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004234 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
4235 AINPUT_SOURCE_MOUSE)
4236 .buttonState(0)
4237 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004238 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004239 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004240 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07004241
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004242 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004243 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004244 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
4245 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004246 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004247 .build()));
4248 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
4249
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07004250 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
4251 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
4252 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004253 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07004254 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
4255 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004256 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07004257 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08004258 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07004259}
4260
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004261/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004262 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
4263 * is generated.
4264 */
4265TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
4266 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4267 sp<FakeWindowHandle> window =
4268 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4269 window->setFrame(Rect(0, 0, 1200, 800));
4270
4271 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4272
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004273 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004274
4275 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004276 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004277 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
4278 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004279 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004280 .build()));
4281 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4282
4283 // Remove the window, but keep the channel.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004284 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004285 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4286}
4287
4288/**
Daniel Norman7487dfa2023-08-02 16:39:45 -07004289 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
4290 */
Ameer Armalycff4fa52023-10-04 23:45:11 +00004291TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash,
4292 REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags,
4293 a11y_crash_on_inconsistent_event_stream))) {
Daniel Norman7487dfa2023-08-02 16:39:45 -07004294 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4295 sp<FakeWindowHandle> window =
4296 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4297 window->setFrame(Rect(0, 0, 1200, 800));
4298
4299 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4300
4301 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4302
4303 MotionEventBuilder hoverEnterBuilder =
4304 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4305 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4306 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4308 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4309 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4310 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4311 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4312 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4313}
4314
4315/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004316 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
4317 */
4318TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
4319 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4320 sp<FakeWindowHandle> window =
4321 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4322 window->setFrame(Rect(0, 0, 100, 100));
4323
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004324 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004325
4326 const int32_t mouseDeviceId = 7;
4327 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004328
4329 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00004330 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4331 .deviceId(mouseDeviceId)
4332 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4333 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004334 window->consumeMotionEvent(
4335 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4336
4337 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004338 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4339 .deviceId(touchDeviceId)
4340 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4341 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004342
4343 window->consumeMotionEvent(
4344 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4345 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4346}
4347
4348/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004349 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004350 * The tap causes a HOVER_EXIT event to be generated because the current event
4351 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004352 */
4353TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
4354 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4355 sp<FakeWindowHandle> window =
4356 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4357 window->setFrame(Rect(0, 0, 100, 100));
4358
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004359 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004360 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4361 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4362 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004363 ASSERT_NO_FATAL_FAILURE(
4364 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4365 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004366
4367 // Tap on the window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004368 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4369 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4370 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004371 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004372 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4373 WithSource(AINPUT_SOURCE_MOUSE))));
4374
4375 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004376 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4377 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4378
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004379 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4380 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4381 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004382 ASSERT_NO_FATAL_FAILURE(
4383 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4384 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4385}
4386
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004387TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
4388 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4389 sp<FakeWindowHandle> windowDefaultDisplay =
4390 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4391 ADISPLAY_ID_DEFAULT);
4392 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
4393 sp<FakeWindowHandle> windowSecondDisplay =
4394 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
4395 SECOND_DISPLAY_ID);
4396 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
4397
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004398 mDispatcher->onWindowInfosChanged(
4399 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004400
4401 // Set cursor position in window in default display and check that hover enter and move
4402 // events are generated.
4403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004404 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004405 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4406 AINPUT_SOURCE_MOUSE)
4407 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004408 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004409 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004410 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004411
4412 // Remove all windows in secondary display and check that no event happens on window in
4413 // primary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004414 mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0});
4415
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004416 windowDefaultDisplay->assertNoEvents();
4417
4418 // Move cursor position in window in default display and check that only hover move
4419 // event is generated and not hover enter event.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004420 mDispatcher->onWindowInfosChanged(
4421 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004423 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004424 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4425 AINPUT_SOURCE_MOUSE)
4426 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004427 .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004428 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004429 windowDefaultDisplay->consumeMotionEvent(
4430 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4431 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004432 windowDefaultDisplay->assertNoEvents();
4433}
4434
Garfield Tan00f511d2019-06-12 16:55:40 -07004435TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07004436 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07004437
4438 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004439 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004440 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004441 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004442 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004443 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004444
4445 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4446
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004447 mDispatcher->onWindowInfosChanged(
4448 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tan00f511d2019-06-12 16:55:40 -07004449
4450 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
4451 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004452 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004453 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004454 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004455 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004456 windowRight->assertNoEvents();
4457}
4458
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004459TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004460 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004461 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4462 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07004463 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004464
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004465 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07004466 setFocusedWindow(window);
4467
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004468 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004469
Prabir Pradhan678438e2023-04-13 19:32:51 +00004470 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004471
4472 // Window should receive key down event.
4473 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4474
4475 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
4476 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004477 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00004478 window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004479}
4480
4481TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004482 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004483 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4484 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004485
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004486 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004487
Prabir Pradhan678438e2023-04-13 19:32:51 +00004488 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4489 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004490
4491 // Window should receive motion down event.
4492 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4493
4494 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
4495 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004496 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004497 window->consumeMotionEvent(
4498 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004499}
4500
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004501TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
4502 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4503 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4504 "Fake Window", ADISPLAY_ID_DEFAULT);
4505
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004506 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004507
4508 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4509 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
4510 .build());
4511
4512 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4513
4514 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
4515 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
4516 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4517
4518 // After the device has been reset, a new hovering stream can be sent to the window
4519 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4520 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
4521 .build());
4522 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4523}
4524
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004525TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
4526 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004527 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4528 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004529 window->setFocusable(true);
4530
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004531 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004532 setFocusedWindow(window);
4533
4534 window->consumeFocusEvent(true);
4535
Prabir Pradhan678438e2023-04-13 19:32:51 +00004536 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004537 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
4538 const nsecs_t injectTime = keyArgs.eventTime;
4539 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00004540 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004541 // The dispatching time should be always greater than or equal to intercept key timeout.
4542 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4543 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
4544 std::chrono::nanoseconds(interceptKeyTimeout).count());
4545}
4546
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004547/**
4548 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
4549 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004550TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
4551 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004552 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4553 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004554 window->setFocusable(true);
4555
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004556 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004557 setFocusedWindow(window);
4558
4559 window->consumeFocusEvent(true);
4560
Prabir Pradhan678438e2023-04-13 19:32:51 +00004561 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004562 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004563
4564 // Set a value that's significantly larger than the default consumption timeout. If the
4565 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
4566 mFakePolicy->setInterceptKeyTimeout(600ms);
4567 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
4568 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004569 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4570}
4571
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004572/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004573 * Two windows. First is a regular window. Second does not overlap with the first, and has
4574 * WATCH_OUTSIDE_TOUCH.
4575 * Both windows are owned by the same UID.
4576 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
4577 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
4578 */
4579TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
4580 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004581 sp<FakeWindowHandle> window =
4582 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004583 window->setFrame(Rect{0, 0, 100, 100});
4584
4585 sp<FakeWindowHandle> outsideWindow =
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004586 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004587 ADISPLAY_ID_DEFAULT);
4588 outsideWindow->setFrame(Rect{100, 100, 200, 200});
4589 outsideWindow->setWatchOutsideTouch(true);
4590 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004591 mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004592
4593 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004594 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4595 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4596 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004597 window->consumeMotionDown();
4598 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
4599 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
4600 outsideWindow->consumeMotionEvent(
4601 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
Prabir Pradhan502a7252023-12-01 16:11:24 +00004602
4603 // Ensure outsideWindow doesn't get any more events for the gesture.
4604 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4605 ADISPLAY_ID_DEFAULT, {PointF{51, 51}}));
4606 window->consumeMotionMove();
4607 outsideWindow->assertNoEvents();
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004608}
4609
4610/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004611 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
4612 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
4613 * ACTION_OUTSIDE event is sent per gesture.
4614 */
4615TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
4616 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
4617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4619 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004620 window->setWatchOutsideTouch(true);
4621 window->setFrame(Rect{0, 0, 100, 100});
4622 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004623 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4624 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004625 secondWindow->setFrame(Rect{100, 100, 200, 200});
4626 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004627 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
4628 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004629 thirdWindow->setFrame(Rect{200, 200, 300, 300});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004630 mDispatcher->onWindowInfosChanged(
4631 {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0});
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004632
4633 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004634 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4635 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4636 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004637 window->assertNoEvents();
4638 secondWindow->assertNoEvents();
4639
4640 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
4641 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004642 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4643 ADISPLAY_ID_DEFAULT,
4644 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004645 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
4646 window->consumeMotionEvent(
4647 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004648 secondWindow->consumeMotionDown();
4649 thirdWindow->assertNoEvents();
4650
4651 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
4652 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004653 mDispatcher->notifyMotion(
4654 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4655 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004656 window->assertNoEvents();
4657 secondWindow->consumeMotionMove();
4658 thirdWindow->consumeMotionDown();
4659}
4660
Prabir Pradhan814fe082022-07-22 20:22:18 +00004661TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
4662 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004663 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4664 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004665 window->setFocusable(true);
4666
Patrick Williamsd828f302023-04-28 17:52:08 -05004667 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004668 setFocusedWindow(window);
4669
4670 window->consumeFocusEvent(true);
4671
Prabir Pradhan678438e2023-04-13 19:32:51 +00004672 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
4673 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
4674 mDispatcher->notifyKey(keyDown);
4675 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004676
4677 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4678 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4679
4680 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05004681 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004682
4683 window->consumeFocusEvent(false);
4684
Prabir Pradhan678438e2023-04-13 19:32:51 +00004685 mDispatcher->notifyKey(keyDown);
4686 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004687 window->assertNoEvents();
4688}
4689
Arthur Hung96483742022-11-15 03:30:48 +00004690TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
4691 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4692 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4693 "Fake Window", ADISPLAY_ID_DEFAULT);
4694 // Ensure window is non-split and have some transform.
4695 window->setPreventSplitting(true);
4696 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05004697 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00004698
4699 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004700 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung96483742022-11-15 03:30:48 +00004701 {50, 50}))
4702 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4703 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4704
4705 const MotionEvent secondFingerDownEvent =
4706 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4707 .displayId(ADISPLAY_ID_DEFAULT)
4708 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004709 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4710 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00004711 .build();
4712 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004713 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung96483742022-11-15 03:30:48 +00004714 InputEventInjectionSync::WAIT_FOR_RESULT))
4715 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4716
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004717 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
4718 ASSERT_NE(nullptr, event);
4719 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4720 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4721 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4722 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4723 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
Arthur Hung96483742022-11-15 03:30:48 +00004724}
4725
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07004726/**
4727 * Two windows: a splittable and a non-splittable.
4728 * The non-splittable window shouldn't receive any "incomplete" gestures.
4729 * Send the first pointer to the splittable window, and then touch the non-splittable window.
4730 * The second pointer should be dropped because the initial window is splittable, so it won't get
4731 * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any
4732 * "incomplete" gestures.
4733 */
4734TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) {
4735 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4736 sp<FakeWindowHandle> leftWindow =
4737 sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window",
4738 ADISPLAY_ID_DEFAULT);
4739 leftWindow->setPreventSplitting(false);
4740 leftWindow->setFrame(Rect(0, 0, 100, 100));
4741 sp<FakeWindowHandle> rightWindow =
4742 sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window",
4743 ADISPLAY_ID_DEFAULT);
4744 rightWindow->setPreventSplitting(true);
4745 rightWindow->setFrame(Rect(100, 100, 200, 200));
4746 mDispatcher->onWindowInfosChanged(
4747 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
4748
4749 // Touch down on left, splittable window
4750 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4751 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4752 .build());
4753 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4754
4755 mDispatcher->notifyMotion(
4756 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4757 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4758 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
4759 .build());
4760 leftWindow->assertNoEvents();
4761 rightWindow->assertNoEvents();
4762}
4763
Harry Cuttsb166c002023-05-09 13:06:05 +00004764TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4765 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4766 sp<FakeWindowHandle> window =
4767 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4768 window->setFrame(Rect(0, 0, 400, 400));
4769 sp<FakeWindowHandle> trustedOverlay =
4770 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4771 ADISPLAY_ID_DEFAULT);
4772 trustedOverlay->setSpy(true);
4773 trustedOverlay->setTrustedOverlay(true);
4774
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004775 mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004776
4777 // Start a three-finger touchpad swipe
4778 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4779 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4780 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4781 .build());
4782 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4783 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4784 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4785 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4786 .build());
4787 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4788 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4789 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4790 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4791 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4792 .build());
4793
4794 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4795 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4796 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4797
4798 // Move the swipe a bit
4799 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4800 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4801 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4802 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4803 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4804 .build());
4805
4806 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4807
4808 // End the swipe
4809 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4810 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4811 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4812 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4813 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4814 .build());
4815 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4816 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4817 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4818 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4819 .build());
4820 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4821 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4822 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4823 .build());
4824
4825 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4826 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4827 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4828
4829 window->assertNoEvents();
4830}
4831
4832TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4833 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4834 sp<FakeWindowHandle> window =
4835 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4836 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004837 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004838
4839 // Start a three-finger touchpad swipe
4840 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4841 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4842 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4843 .build());
4844 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4845 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4846 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4847 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4848 .build());
4849 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4850 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4851 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4852 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4853 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4854 .build());
4855
4856 // Move the swipe a bit
4857 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4858 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4859 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4860 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4861 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4862 .build());
4863
4864 // End the swipe
4865 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4866 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4867 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4868 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4869 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4870 .build());
4871 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4872 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4873 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4874 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4875 .build());
4876 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4877 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4878 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4879 .build());
4880
4881 window->assertNoEvents();
4882}
4883
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004884/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004885 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
4886 * the first pointer.
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004887 * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004888 */
4889TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
4890 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4891 sp<FakeWindowHandle> window =
4892 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4893 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004894 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004895
4896 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
4897 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4898 .downTime(baseTime + 10)
4899 .eventTime(baseTime + 10)
4900 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4901 .build());
4902
4903 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4904
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004905 // Change the transform so that the orientation is now different from original.
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004906 window->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004907
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004908 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004909
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004910 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4911 .downTime(baseTime + 10)
4912 .eventTime(baseTime + 30)
4913 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4914 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4915 .build());
4916
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004917 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4918
4919 // Finish the gesture and start a new one. Ensure all events are sent to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004920 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
4921 .downTime(baseTime + 10)
4922 .eventTime(baseTime + 40)
4923 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4924 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4925 .build());
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004926
4927 window->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4928
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004929 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4930 .downTime(baseTime + 10)
4931 .eventTime(baseTime + 50)
4932 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4933 .build());
4934
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004935 window->consumeMotionEvent(WithMotionAction(ACTION_UP));
4936
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004937 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4938 .downTime(baseTime + 60)
4939 .eventTime(baseTime + 60)
4940 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
4941 .build());
4942
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004943 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004944}
4945
4946/**
Hu Guo771a7692023-09-17 20:51:08 +08004947 * When there are multiple screens, such as screen projection to TV or screen recording, if the
4948 * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and
4949 * its coordinates should be converted by the transform of the windows of target screen.
4950 */
4951TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) {
4952 // This case will create a window and a spy window on the default display and mirror
4953 // window on the second display. cancel event is sent through spy window pilferPointers
4954 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4955
4956 sp<FakeWindowHandle> spyWindowDefaultDisplay =
4957 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4958 spyWindowDefaultDisplay->setTrustedOverlay(true);
4959 spyWindowDefaultDisplay->setSpy(true);
4960
4961 sp<FakeWindowHandle> windowDefaultDisplay =
4962 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4963 ADISPLAY_ID_DEFAULT);
4964 windowDefaultDisplay->setWindowTransform(1, 0, 0, 1);
4965
4966 sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID);
4967 windowSecondDisplay->setWindowTransform(2, 0, 0, 2);
4968
4969 // Add the windows to the dispatcher
4970 mDispatcher->onWindowInfosChanged(
4971 {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(),
4972 *windowSecondDisplay->getInfo()},
4973 {},
4974 0,
4975 0});
4976
4977 // Send down to ADISPLAY_ID_DEFAULT
4978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4979 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4980 {100, 100}))
4981 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4982
4983 spyWindowDefaultDisplay->consumeMotionDown();
4984 windowDefaultDisplay->consumeMotionDown();
4985
4986 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken()));
4987
4988 // windowDefaultDisplay gets cancel
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004989 std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent();
4990 ASSERT_NE(nullptr, event);
4991 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction());
Hu Guo771a7692023-09-17 20:51:08 +08004992
4993 // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the
4994 // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y
4995 // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of
4996 // SECOND_DISPLAY_ID, the x and y coordinates are 200
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08004997 EXPECT_EQ(100, event->getX(0));
4998 EXPECT_EQ(100, event->getY(0));
Hu Guo771a7692023-09-17 20:51:08 +08004999}
5000
5001/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005002 * Ensure the correct coordinate spaces are used by InputDispatcher.
5003 *
5004 * InputDispatcher works in the display space, so its coordinate system is relative to the display
5005 * panel. Windows get events in the window space, and get raw coordinates in the logical display
5006 * space.
5007 */
5008class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
5009public:
5010 void SetUp() override {
5011 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005012 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005013 }
5014
5015 void addDisplayInfo(int displayId, const ui::Transform& transform) {
5016 gui::DisplayInfo info;
5017 info.displayId = displayId;
5018 info.transform = transform;
5019 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05005020 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005021 }
5022
5023 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
5024 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05005025 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005026 }
5027
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005028 void removeAllWindowsAndDisplays() {
5029 mDisplayInfos.clear();
5030 mWindowInfos.clear();
5031 }
5032
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005033 // Set up a test scenario where the display has a scaled projection and there are two windows
5034 // on the display.
5035 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
5036 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
5037 // respectively.
5038 ui::Transform displayTransform;
5039 displayTransform.set(2, 0, 0, 4);
5040 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5041
5042 std::shared_ptr<FakeApplicationHandle> application =
5043 std::make_shared<FakeApplicationHandle>();
5044
5045 // Add two windows to the display. Their frames are represented in the display space.
5046 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005047 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5048 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005049 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
5050 addWindow(firstWindow);
5051
5052 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005053 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5054 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005055 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
5056 addWindow(secondWindow);
5057 return {std::move(firstWindow), std::move(secondWindow)};
5058 }
5059
5060private:
5061 std::vector<gui::DisplayInfo> mDisplayInfos;
5062 std::vector<gui::WindowInfo> mWindowInfos;
5063};
5064
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005065TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005066 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5067 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005068 // selected so that if the hit test was performed with the point and the bounds being in
5069 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005070 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5071 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5072 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005073
5074 firstWindow->consumeMotionDown();
5075 secondWindow->assertNoEvents();
5076}
5077
5078// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
5079// the event should be treated as being in the logical display space.
5080TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
5081 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5082 // Send down to the first window. The point is represented in the logical display space. The
5083 // point is selected so that if the hit test was done in logical display space, then it would
5084 // end up in the incorrect window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005085 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005086 PointF{75 * 2, 55 * 4});
5087
5088 firstWindow->consumeMotionDown();
5089 secondWindow->assertNoEvents();
5090}
5091
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005092// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
5093// event should be treated as being in the logical display space.
5094TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
5095 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5096
5097 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5098 ui::Transform injectedEventTransform;
5099 injectedEventTransform.set(matrix);
5100 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
5101 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
5102
5103 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5104 .displayId(ADISPLAY_ID_DEFAULT)
5105 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005106 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005107 .x(untransformedPoint.x)
5108 .y(untransformedPoint.y))
5109 .build();
5110 event.transform(matrix);
5111
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005112 injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT,
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005113 InputEventInjectionSync::WAIT_FOR_RESULT);
5114
5115 firstWindow->consumeMotionDown();
5116 secondWindow->assertNoEvents();
5117}
5118
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005119TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
5120 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5121
5122 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005123 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5124 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5125 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005126
5127 firstWindow->assertNoEvents();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005128 std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent();
5129 ASSERT_NE(nullptr, event);
5130 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005131
5132 // Ensure that the events from the "getRaw" API are in logical display coordinates.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005133 EXPECT_EQ(300, event->getRawX(0));
5134 EXPECT_EQ(880, event->getRawY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005135
5136 // Ensure that the x and y values are in the window's coordinate space.
5137 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
5138 // the logical display space. This will be the origin of the window space.
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08005139 EXPECT_EQ(100, event->getX(0));
5140 EXPECT_EQ(80, event->getY(0));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07005141}
5142
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005143TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) {
5144 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5145 // The monitor will always receive events in the logical display's coordinate space, because
5146 // it does not have a window.
Prabir Pradhanfb549072023-10-05 19:17:36 +00005147 FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00005148
5149 // Send down to the first window.
5150 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5151 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
5152 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5153 monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5154
5155 // Second pointer goes down on second window.
5156 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5157 ADISPLAY_ID_DEFAULT,
5158 {PointF{50, 100}, PointF{150, 220}}));
5159 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80)));
5160 const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}},
5161 {1, PointF{300, 880}}};
5162 monitor.consumeMotionEvent(
5163 AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers)));
5164
5165 mDispatcher->cancelCurrentTouch();
5166
5167 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5168 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80)));
5169 monitor.consumeMotionEvent(
5170 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers)));
5171}
5172
Prabir Pradhan1c29a092023-09-21 10:29:29 +00005173TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) {
5174 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5175
5176 // Send down to the first window.
5177 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5178 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
5179 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
5180
5181 // The pointer is transferred to the second window, and the second window receives it in the
5182 // correct coordinate space.
5183 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
5184 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
5185 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400)));
5186}
5187
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005188TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) {
5189 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5190
5191 // Send hover move to the second window, and ensure it shows up as hover enter.
5192 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
5193 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5194 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5195 WithCoords(100, 80), WithRawCoords(300, 880)));
5196
5197 // Touch down at the same location and ensure a hover exit is synthesized.
5198 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
5199 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5200 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5201 WithRawCoords(300, 880)));
5202 secondWindow->consumeMotionEvent(
5203 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5204 secondWindow->assertNoEvents();
5205 firstWindow->assertNoEvents();
5206}
5207
Prabir Pradhan453ae732023-10-13 14:30:14 +00005208// Same as above, but while the window is being mirrored.
5209TEST_F(InputDispatcherDisplayProjectionTest,
5210 SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) {
5211 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5212
5213 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5214 ui::Transform secondDisplayTransform;
5215 secondDisplayTransform.set(matrix);
5216 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5217
5218 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5219 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5220 addWindow(secondWindowClone);
5221
5222 // Send hover move to the second window, and ensure it shows up as hover enter.
5223 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
5224 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5225 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5226 WithCoords(100, 80), WithRawCoords(300, 880)));
5227
5228 // Touch down at the same location and ensure a hover exit is synthesized for the correct
5229 // display.
5230 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
5231 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5232 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5233 WithRawCoords(300, 880)));
5234 secondWindow->consumeMotionEvent(
5235 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
5236 secondWindow->assertNoEvents();
5237 firstWindow->assertNoEvents();
5238}
5239
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00005240TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) {
5241 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5242
5243 // Send hover enter to second window
5244 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
5245 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5246 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5247 WithCoords(100, 80), WithRawCoords(300, 880)));
5248
5249 mDispatcher->cancelCurrentTouch();
5250
5251 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5252 WithRawCoords(300, 880)));
5253 secondWindow->assertNoEvents();
5254 firstWindow->assertNoEvents();
5255}
5256
Prabir Pradhan453ae732023-10-13 14:30:14 +00005257// Same as above, but while the window is being mirrored.
Prabir Pradhan16463382023-10-12 23:03:19 +00005258TEST_F(InputDispatcherDisplayProjectionTest,
5259 SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) {
5260 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
5261
5262 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
5263 ui::Transform secondDisplayTransform;
5264 secondDisplayTransform.set(matrix);
5265 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
5266
5267 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
5268 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
5269 addWindow(secondWindowClone);
5270
5271 // Send hover enter to second window
5272 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
5273 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
5274 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
5275 WithCoords(100, 80), WithRawCoords(300, 880),
5276 WithDisplayId(ADISPLAY_ID_DEFAULT)));
5277
5278 mDispatcher->cancelCurrentTouch();
5279
5280 // Ensure the cancelation happens with the correct displayId and the correct coordinates.
5281 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
5282 WithRawCoords(300, 880),
5283 WithDisplayId(ADISPLAY_ID_DEFAULT)));
5284 secondWindow->assertNoEvents();
5285 firstWindow->assertNoEvents();
5286}
5287
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005288/** Ensure consistent behavior of InputDispatcher in all orientations. */
5289class InputDispatcherDisplayOrientationFixture
5290 : public InputDispatcherDisplayProjectionTest,
5291 public ::testing::WithParamInterface<ui::Rotation> {};
5292
5293// This test verifies the touchable region of a window for all rotations of the display by tapping
5294// in different locations on the display, specifically points close to the four corners of a
5295// window.
5296TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
5297 constexpr static int32_t displayWidth = 400;
5298 constexpr static int32_t displayHeight = 800;
5299
5300 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5301
5302 const auto rotation = GetParam();
5303
5304 // Set up the display with the specified rotation.
5305 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
5306 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
5307 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
5308 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
5309 logicalDisplayWidth, logicalDisplayHeight);
5310 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5311
5312 // Create a window with its bounds determined in the logical display.
5313 const Rect frameInLogicalDisplay(100, 100, 200, 300);
5314 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
5315 sp<FakeWindowHandle> window =
5316 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5317 window->setFrame(frameInDisplay, displayTransform);
5318 addWindow(window);
5319
5320 // The following points in logical display space should be inside the window.
5321 static const std::array<vec2, 4> insidePoints{
5322 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
5323 for (const auto pointInsideWindow : insidePoints) {
5324 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
5325 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005326 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5327 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5328 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005329 window->consumeMotionDown();
5330
Prabir Pradhan678438e2023-04-13 19:32:51 +00005331 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5332 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5333 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005334 window->consumeMotionUp();
5335 }
5336
5337 // The following points in logical display space should be outside the window.
5338 static const std::array<vec2, 5> outsidePoints{
5339 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
5340 for (const auto pointOutsideWindow : outsidePoints) {
5341 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
5342 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005343 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5344 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5345 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005346
Prabir Pradhan678438e2023-04-13 19:32:51 +00005347 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5348 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5349 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005350 }
5351 window->assertNoEvents();
5352}
5353
5354// Run the precision tests for all rotations.
5355INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
5356 InputDispatcherDisplayOrientationFixture,
5357 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
5358 ui::ROTATION_270),
5359 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
5360 return ftl::enum_string(testParamInfo.param);
5361 });
5362
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005363using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
5364 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005365
5366class TransferTouchFixture : public InputDispatcherTest,
5367 public ::testing::WithParamInterface<TransferFunction> {};
5368
5369TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07005370 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005371
5372 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005373 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005374 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5375 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005376 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005377 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005378 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5379 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005380 sp<FakeWindowHandle> wallpaper =
5381 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
5382 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005383 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005384 mDispatcher->onWindowInfosChanged(
5385 {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005386
5387 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005388 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5389 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005390
Svet Ganov5d3bc372020-01-26 23:11:07 -08005391 // Only the first window should get the down event
5392 firstWindow->consumeMotionDown();
5393 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005394 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005395
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005396 // Transfer touch to the second window
5397 TransferFunction f = GetParam();
5398 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5399 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005400 // The first window gets cancel and the second gets down
5401 firstWindow->consumeMotionCancel();
5402 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005403 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005404
5405 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005406 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5407 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005408 // The first window gets no events and the second gets up
5409 firstWindow->assertNoEvents();
5410 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005411 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005412}
5413
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005414/**
5415 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
5416 * from. When we have spy windows, there are several windows to choose from: either spy, or the
5417 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
5418 * natural to the user.
5419 * In this test, we are sending a pointer to both spy window and first window. We then try to
5420 * transfer touch to the second window. The dispatcher should identify the first window as the
5421 * one that should lose the gesture, and therefore the action should be to move the gesture from
5422 * the first window to the second.
5423 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
5424 * the other API, as well.
5425 */
5426TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
5427 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5428
5429 // Create a couple of windows + a spy window
5430 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005431 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005432 spyWindow->setTrustedOverlay(true);
5433 spyWindow->setSpy(true);
5434 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005435 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005436 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005437 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005438
5439 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005440 mDispatcher->onWindowInfosChanged(
5441 {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005442
5443 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005444 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5445 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005446 // Only the first window and spy should get the down event
5447 spyWindow->consumeMotionDown();
5448 firstWindow->consumeMotionDown();
5449
5450 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
5451 // if f === 'transferTouch'.
5452 TransferFunction f = GetParam();
5453 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5454 ASSERT_TRUE(success);
5455 // The first window gets cancel and the second gets down
5456 firstWindow->consumeMotionCancel();
5457 secondWindow->consumeMotionDown();
5458
5459 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005460 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5461 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005462 // The first window gets no events and the second+spy get up
5463 firstWindow->assertNoEvents();
5464 spyWindow->consumeMotionUp();
5465 secondWindow->consumeMotionUp();
5466}
5467
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005468TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005469 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005470
5471 PointF touchPoint = {10, 10};
5472
5473 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005474 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005475 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5476 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005477 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005478 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005479 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5480 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005481 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005482
5483 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005484 mDispatcher->onWindowInfosChanged(
5485 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005486
5487 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005488 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5489 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5490 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005491 // Only the first window should get the down event
5492 firstWindow->consumeMotionDown();
5493 secondWindow->assertNoEvents();
5494
5495 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005496 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5497 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005498 // Only the first window should get the pointer down event
5499 firstWindow->consumeMotionPointerDown(1);
5500 secondWindow->assertNoEvents();
5501
5502 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005503 TransferFunction f = GetParam();
5504 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5505 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005506 // The first window gets cancel and the second gets down and pointer down
5507 firstWindow->consumeMotionCancel();
5508 secondWindow->consumeMotionDown();
5509 secondWindow->consumeMotionPointerDown(1);
5510
5511 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005512 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5513 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005514 // The first window gets nothing and the second gets pointer up
5515 firstWindow->assertNoEvents();
5516 secondWindow->consumeMotionPointerUp(1);
5517
5518 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005519 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5520 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005521 // The first window gets nothing and the second gets up
5522 firstWindow->assertNoEvents();
5523 secondWindow->consumeMotionUp();
5524}
5525
Arthur Hungc539dbb2022-12-08 07:45:36 +00005526TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
5527 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5528
5529 // Create a couple of windows
5530 sp<FakeWindowHandle> firstWindow =
5531 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5532 ADISPLAY_ID_DEFAULT);
5533 firstWindow->setDupTouchToWallpaper(true);
5534 sp<FakeWindowHandle> secondWindow =
5535 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5536 ADISPLAY_ID_DEFAULT);
5537 secondWindow->setDupTouchToWallpaper(true);
5538
5539 sp<FakeWindowHandle> wallpaper1 =
5540 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
5541 wallpaper1->setIsWallpaper(true);
5542
5543 sp<FakeWindowHandle> wallpaper2 =
5544 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
5545 wallpaper2->setIsWallpaper(true);
5546 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005547 mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(),
5548 *secondWindow->getInfo(), *wallpaper2->getInfo()},
5549 {},
5550 0,
5551 0});
Arthur Hungc539dbb2022-12-08 07:45:36 +00005552
5553 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005554 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5555 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005556
5557 // Only the first window should get the down event
5558 firstWindow->consumeMotionDown();
5559 secondWindow->assertNoEvents();
5560 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5561 wallpaper2->assertNoEvents();
5562
5563 // Transfer touch focus to the second window
5564 TransferFunction f = GetParam();
5565 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5566 ASSERT_TRUE(success);
5567
5568 // The first window gets cancel and the second gets down
5569 firstWindow->consumeMotionCancel();
5570 secondWindow->consumeMotionDown();
5571 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5572 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5573
5574 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005575 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5576 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005577 // The first window gets no events and the second gets up
5578 firstWindow->assertNoEvents();
5579 secondWindow->consumeMotionUp();
5580 wallpaper1->assertNoEvents();
5581 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5582}
5583
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005584// For the cases of single pointer touch and two pointers non-split touch, the api's
5585// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
5586// for the case where there are multiple pointers split across several windows.
5587INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
5588 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005589 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
5590 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005591 return dispatcher->transferTouch(destChannelToken,
5592 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005593 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005594 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
5595 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005596 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00005597 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005598 }));
5599
Svet Ganov5d3bc372020-01-26 23:11:07 -08005600TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005601 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005602
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005603 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005604 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5605 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005606 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005607
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005608 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005609 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5610 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005611 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005612
5613 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005614 mDispatcher->onWindowInfosChanged(
5615 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005616
5617 PointF pointInFirst = {300, 200};
5618 PointF pointInSecond = {300, 600};
5619
5620 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005621 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5622 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5623 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005624 // Only the first window should get the down event
5625 firstWindow->consumeMotionDown();
5626 secondWindow->assertNoEvents();
5627
5628 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005629 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5630 ADISPLAY_ID_DEFAULT,
5631 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005632 // The first window gets a move and the second a down
5633 firstWindow->consumeMotionMove();
5634 secondWindow->consumeMotionDown();
5635
5636 // Transfer touch focus to the second window
5637 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
5638 // The first window gets cancel and the new gets pointer down (it already saw down)
5639 firstWindow->consumeMotionCancel();
5640 secondWindow->consumeMotionPointerDown(1);
5641
5642 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005643 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5644 ADISPLAY_ID_DEFAULT,
5645 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005646 // The first window gets nothing and the second gets pointer up
5647 firstWindow->assertNoEvents();
5648 secondWindow->consumeMotionPointerUp(1);
5649
5650 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005651 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5652 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005653 // The first window gets nothing and the second gets up
5654 firstWindow->assertNoEvents();
5655 secondWindow->consumeMotionUp();
5656}
5657
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005658// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
5659// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
5660// touch is not supported, so the touch should continue on those windows and the transferred-to
5661// window should get nothing.
5662TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
5663 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5664
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005665 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005666 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5667 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005668 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005669
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005670 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005671 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5672 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005673 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005674
5675 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005676 mDispatcher->onWindowInfosChanged(
5677 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005678
5679 PointF pointInFirst = {300, 200};
5680 PointF pointInSecond = {300, 600};
5681
5682 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005683 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5684 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5685 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005686 // Only the first window should get the down event
5687 firstWindow->consumeMotionDown();
5688 secondWindow->assertNoEvents();
5689
5690 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005691 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5692 ADISPLAY_ID_DEFAULT,
5693 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005694 // The first window gets a move and the second a down
5695 firstWindow->consumeMotionMove();
5696 secondWindow->consumeMotionDown();
5697
5698 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005699 const bool transferred =
5700 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005701 // The 'transferTouch' call should not succeed, because there are 2 touched windows
5702 ASSERT_FALSE(transferred);
5703 firstWindow->assertNoEvents();
5704 secondWindow->assertNoEvents();
5705
5706 // The rest of the dispatch should proceed as normal
5707 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005708 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5709 ADISPLAY_ID_DEFAULT,
5710 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005711 // The first window gets MOVE and the second gets pointer up
5712 firstWindow->consumeMotionMove();
5713 secondWindow->consumeMotionUp();
5714
5715 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005716 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5717 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005718 // The first window gets nothing and the second gets up
5719 firstWindow->consumeMotionUp();
5720 secondWindow->assertNoEvents();
5721}
5722
Arthur Hungabbb9d82021-09-01 14:52:30 +00005723// This case will create two windows and one mirrored window on the default display and mirror
5724// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
5725// the windows info of second display before default display.
5726TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
5727 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5728 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005729 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005730 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005731 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005732 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005733 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005734
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005735 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005736 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005737
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005738 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005739 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005740
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005741 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005742 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005743
5744 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005745 mDispatcher->onWindowInfosChanged(
5746 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5747 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5748 *secondWindowInPrimary->getInfo()},
5749 {},
5750 0,
5751 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005752
5753 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005754 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005755 {50, 50}))
5756 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5757
5758 // Window should receive motion event.
5759 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5760
5761 // Transfer touch focus
5762 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
5763 secondWindowInPrimary->getToken()));
5764 // The first window gets cancel.
5765 firstWindowInPrimary->consumeMotionCancel();
5766 secondWindowInPrimary->consumeMotionDown();
5767
5768 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005769 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005770 ADISPLAY_ID_DEFAULT, {150, 50}))
5771 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5772 firstWindowInPrimary->assertNoEvents();
5773 secondWindowInPrimary->consumeMotionMove();
5774
5775 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005776 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005777 {150, 50}))
5778 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5779 firstWindowInPrimary->assertNoEvents();
5780 secondWindowInPrimary->consumeMotionUp();
5781}
5782
5783// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
5784// 'transferTouch' api.
5785TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
5786 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5787 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005788 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005789 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005790 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005791 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005792 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005793
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005794 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005795 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005796
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005797 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005798 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005799
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005800 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005801 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005802
5803 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005804 mDispatcher->onWindowInfosChanged(
5805 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5806 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5807 *secondWindowInPrimary->getInfo()},
5808 {},
5809 0,
5810 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005811
5812 // Touch on second display.
5813 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005814 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
5815 {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005816 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5817
5818 // Window should receive motion event.
5819 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
5820
5821 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005822 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005823
5824 // The first window gets cancel.
5825 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
5826 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
5827
5828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005829 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005830 SECOND_DISPLAY_ID, {150, 50}))
5831 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5832 firstWindowInPrimary->assertNoEvents();
5833 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
5834
5835 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005836 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005837 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5838 firstWindowInPrimary->assertNoEvents();
5839 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
5840}
5841
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005842TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005843 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005844 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5845 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005846
Vishnu Nair47074b82020-08-14 11:54:47 -07005847 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005848 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005849 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005850
5851 window->consumeFocusEvent(true);
5852
Prabir Pradhan678438e2023-04-13 19:32:51 +00005853 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005854
5855 // Window should receive key down event.
5856 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00005857
5858 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005859 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005860 mFakePolicy->assertUserActivityPoked();
5861}
5862
5863TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
5864 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5865 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5866 "Fake Window", ADISPLAY_ID_DEFAULT);
5867
5868 window->setDisableUserActivity(true);
5869 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005870 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005871 setFocusedWindow(window);
5872
5873 window->consumeFocusEvent(true);
5874
5875 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5876
5877 // Window should receive key down event.
5878 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5879
5880 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005881 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005882 mFakePolicy->assertUserActivityNotPoked();
5883}
5884
5885TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
5886 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5887 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5888 "Fake Window", ADISPLAY_ID_DEFAULT);
5889
5890 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005891 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005892 setFocusedWindow(window);
5893
5894 window->consumeFocusEvent(true);
5895
5896 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5897 mDispatcher->waitForIdle();
5898
5899 // System key is not passed down
5900 window->assertNoEvents();
5901
5902 // Should have poked user activity
5903 mFakePolicy->assertUserActivityPoked();
5904}
5905
5906TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
5907 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5908 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5909 "Fake Window", ADISPLAY_ID_DEFAULT);
5910
5911 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005912 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005913 setFocusedWindow(window);
5914
5915 window->consumeFocusEvent(true);
5916
5917 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5918 mDispatcher->waitForIdle();
5919
5920 // System key is not passed down
5921 window->assertNoEvents();
5922
5923 // Should have poked user activity
5924 mFakePolicy->assertUserActivityPoked();
5925}
5926
5927TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
5928 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5929 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5930 "Fake Window", ADISPLAY_ID_DEFAULT);
5931
5932 window->setDisableUserActivity(true);
5933 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005934 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005935 setFocusedWindow(window);
5936
5937 window->consumeFocusEvent(true);
5938
5939 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5940 mDispatcher->waitForIdle();
5941
5942 // System key is not passed down
5943 window->assertNoEvents();
5944
5945 // Should have poked user activity
5946 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005947}
5948
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005949TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
5950 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5951 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5952 "Fake Window", ADISPLAY_ID_DEFAULT);
5953
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005954 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005955
5956 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005957 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005958 ADISPLAY_ID_DEFAULT, {100, 100}))
5959 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5960
5961 window->consumeMotionEvent(
5962 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
5963
5964 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005965 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005966 mFakePolicy->assertUserActivityPoked();
5967}
5968
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005969TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005970 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005971 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5972 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005973
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005974 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005975
Prabir Pradhan678438e2023-04-13 19:32:51 +00005976 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005977 mDispatcher->waitForIdle();
5978
5979 window->assertNoEvents();
5980}
5981
5982// If a window is touchable, but does not have focus, it should receive motion events, but not keys
5983TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07005984 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005985 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5986 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005987
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005988 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005989
5990 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00005991 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005992 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00005993 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5994 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005995
5996 // Window should receive only the motion event
5997 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5998 window->assertNoEvents(); // Key event or focus event will not be received
5999}
6000
arthurhungea3f4fc2020-12-21 23:18:53 +08006001TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
6002 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6003
arthurhungea3f4fc2020-12-21 23:18:53 +08006004 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006005 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
6006 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006007 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08006008
arthurhungea3f4fc2020-12-21 23:18:53 +08006009 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006010 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
6011 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08006012 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08006013
6014 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006015 mDispatcher->onWindowInfosChanged(
6016 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
arthurhungea3f4fc2020-12-21 23:18:53 +08006017
6018 PointF pointInFirst = {300, 200};
6019 PointF pointInSecond = {300, 600};
6020
6021 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006022 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6023 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6024 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006025 // Only the first window should get the down event
6026 firstWindow->consumeMotionDown();
6027 secondWindow->assertNoEvents();
6028
6029 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00006030 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6031 ADISPLAY_ID_DEFAULT,
6032 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08006033 // The first window gets a move and the second a down
6034 firstWindow->consumeMotionMove();
6035 secondWindow->consumeMotionDown();
6036
6037 // Send pointer cancel to the second window
6038 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006039 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08006040 {pointInFirst, pointInSecond});
6041 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00006042 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08006043 // The first window gets move and the second gets cancel.
6044 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6045 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
6046
6047 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00006048 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6049 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08006050 // The first window gets up and the second gets nothing.
6051 firstWindow->consumeMotionUp();
6052 secondWindow->assertNoEvents();
6053}
6054
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006055TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
6056 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6057
6058 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006059 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006060 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006061 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
6062 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
6063 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
6064
Harry Cutts33476232023-01-30 19:57:29 +00006065 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00006066 window->assertNoEvents();
6067 mDispatcher->waitForIdle();
6068}
6069
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006070using InputDispatcherMonitorTest = InputDispatcherTest;
6071
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006072/**
6073 * Two entities that receive touch: A window, and a global monitor.
6074 * The touch goes to the window, and then the window disappears.
6075 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
6076 * for the monitor, as well.
6077 * 1. foregroundWindow
6078 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
6079 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006080TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006081 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6082 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006083 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006084
Prabir Pradhanfb549072023-10-05 19:17:36 +00006085 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006086
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006087 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006088 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006089 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006090 {100, 200}))
6091 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6092
6093 // Both the foreground window and the global monitor should receive the touch down
6094 window->consumeMotionDown();
6095 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6096
6097 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006098 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006099 ADISPLAY_ID_DEFAULT, {110, 200}))
6100 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6101
6102 window->consumeMotionMove();
6103 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
6104
6105 // Now the foreground window goes away
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006106 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006107 window->consumeMotionCancel();
6108 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
6109
6110 // If more events come in, there will be no more foreground window to send them to. This will
6111 // cause a cancel for the monitor, as well.
6112 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006113 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006114 ADISPLAY_ID_DEFAULT, {120, 200}))
6115 << "Injection should fail because the window was removed";
6116 window->assertNoEvents();
6117 // Global monitor now gets the cancel
6118 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6119}
6120
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006121TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07006122 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006123 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6124 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006125 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006126
Prabir Pradhanfb549072023-10-05 19:17:36 +00006127 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006128
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006130 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006131 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00006132 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08006133 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006134}
6135
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006136TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
Prabir Pradhanfb549072023-10-05 19:17:36 +00006137 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006138
Chris Yea209fde2020-07-22 13:54:51 -07006139 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006140 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6141 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006142 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00006143
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006144 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006145 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006146 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08006147 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006148 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006149
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006150 // Pilfer pointers from the monitor.
6151 // This should not do anything and the window should continue to receive events.
6152 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00006153
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006154 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006155 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006156 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006157 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006158
6159 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
6160 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00006161}
6162
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006163TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07006164 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006165 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6166 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006167 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Evan Rosky84f07f02021-04-16 10:42:42 -07006168 window->setWindowOffset(20, 40);
6169 window->setWindowTransform(0, 1, -1, 0);
6170
Prabir Pradhanfb549072023-10-05 19:17:36 +00006171 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07006172
6173 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006174 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Evan Rosky84f07f02021-04-16 10:42:42 -07006175 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6176 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006177 std::unique_ptr<MotionEvent> event = monitor.consumeMotion();
6178 ASSERT_NE(nullptr, event);
Evan Rosky84f07f02021-04-16 10:42:42 -07006179 // Even though window has transform, gesture monitor must not.
6180 ASSERT_EQ(ui::Transform(), event->getTransform());
6181}
6182
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006183TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00006184 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhanfb549072023-10-05 19:17:36 +00006185 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00006186
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006187 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006188 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006189 << "Injection should fail if there is a monitor, but no touchable window";
6190 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00006191}
6192
Linnan Lid8150952024-01-26 18:07:17 +00006193/**
6194 * Two displays
6195 * The first monitor has a foreground window, a monitor
6196 * The second window has only one monitor.
6197 * We first inject a Down event into the first display, this injection should succeed and both
6198 * the foreground window and monitor should receive a down event, then inject a Down event into
6199 * the second display as well, this injection should fail, at this point, the first display
6200 * window and monitor should not receive a cancel or any other event.
6201 * Continue to inject Move and UP events to the first display, the events should be received
6202 * normally by the foreground window and monitor.
6203 */
6204TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) {
6205 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6206 sp<FakeWindowHandle> window =
6207 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6208
6209 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6210 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6211
6212 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6214 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6215 {100, 200}))
6216 << "The down event injected into the first display should succeed";
6217
6218 window->consumeMotionDown();
6219 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006220
6221 ASSERT_EQ(InputEventInjectionResult::FAILED,
6222 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6223 {100, 200}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006224 << "The down event injected into the second display should fail since there's no "
6225 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006226
6227 // Continue to inject event to first display.
6228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6229 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6230 ADISPLAY_ID_DEFAULT, {110, 220}))
6231 << "The move event injected into the first display should succeed";
6232
6233 window->consumeMotionMove();
6234 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006235
6236 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6237 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6238 {110, 220}))
6239 << "The up event injected into the first display should succeed";
6240
6241 window->consumeMotionUp();
6242 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006243
6244 window->assertNoEvents();
6245 monitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00006246 secondMonitor.assertNoEvents();
6247}
6248
6249/**
6250 * Two displays
6251 * There is a monitor and foreground window on each display.
6252 * First, we inject down events into each of the two displays, at this point, the foreground windows
6253 * and monitors on both displays should receive down events.
6254 * At this point, the foreground window of the second display goes away, the gone window should
6255 * receive the cancel event, and the other windows and monitors should not receive any events.
6256 * Inject a move event into the second display. At this point, the injection should fail because
6257 * the second display no longer has a foreground window. At this point, the monitor on the second
6258 * display should receive a cancel event, and any windows or monitors on the first display should
6259 * not receive any events, and any subsequent injection of events into the second display should
6260 * also fail.
6261 * Continue to inject events into the first display, and the events should all be injected
6262 * successfully and received normally.
6263 */
6264TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) {
6265 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6266 sp<FakeWindowHandle> window =
6267 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6268 sp<FakeWindowHandle> secondWindow =
6269 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground",
6270 SECOND_DISPLAY_ID);
6271
6272 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6273 FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
6274
6275 // There is a foreground window on both displays.
6276 mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
6277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6278 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6279 {100, 200}))
6280 << "The down event injected into the first display should succeed";
6281
6282 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6283 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006284
6285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6286 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6287 {100, 200}))
6288 << "The down event injected into the second display should succeed";
6289
Linnan Lid8150952024-01-26 18:07:17 +00006290 secondWindow->consumeMotionDown(SECOND_DISPLAY_ID);
6291 secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID);
6292
6293 // Now second window is gone away.
6294 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
6295
6296 // The gone window should receive a cancel, and the monitor on the second display should not
6297 // receive any events.
Linnan Lid8150952024-01-26 18:07:17 +00006298 secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID);
6299 secondMonitor.assertNoEvents();
6300
6301 ASSERT_EQ(InputEventInjectionResult::FAILED,
6302 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6303 SECOND_DISPLAY_ID, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006304 << "The move event injected into the second display should fail because there's no "
6305 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006306 // Now the monitor on the second display should receive a cancel event.
6307 secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID);
Linnan Lid8150952024-01-26 18:07:17 +00006308
6309 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6310 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6311 ADISPLAY_ID_DEFAULT, {110, 200}))
6312 << "The move event injected into the first display should succeed";
6313
6314 window->consumeMotionMove();
6315 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
Linnan Lid8150952024-01-26 18:07:17 +00006316
6317 ASSERT_EQ(InputEventInjectionResult::FAILED,
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006318 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
6319 {110, 220}))
6320 << "The up event injected into the second display should fail because there's no "
6321 "touchable window";
Linnan Lid8150952024-01-26 18:07:17 +00006322
6323 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6324 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6325 {110, 220}))
6326 << "The up event injected into the first display should succeed";
6327
6328 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
6329 monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT);
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006330
Linnan Lid8150952024-01-26 18:07:17 +00006331 window->assertNoEvents();
6332 monitor.assertNoEvents();
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006333 secondWindow->assertNoEvents();
6334 secondMonitor.assertNoEvents();
Linnan Lid8150952024-01-26 18:07:17 +00006335}
6336
6337/**
6338 * One display with transform
6339 * There is a foreground window and a monitor on the display
6340 * Inject down event and move event sequentially, the foreground window and monitor can receive down
6341 * event and move event, then let the foreground window go away, the foreground window receives
6342 * cancel event, inject move event again, the monitor receives cancel event, all the events received
6343 * by the monitor should be with the same transform as the display
6344 */
6345TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) {
6346 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6347 sp<FakeWindowHandle> window =
6348 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
6349 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6350
6351 ui::Transform transform;
6352 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6353
6354 gui::DisplayInfo displayInfo;
6355 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6356 displayInfo.transform = transform;
6357
6358 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
6359
6360 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6361 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6362 {100, 200}))
6363 << "The down event injected should succeed";
6364
6365 window->consumeMotionDown();
6366 std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion();
6367 EXPECT_EQ(transform, downMotionEvent->getTransform());
6368 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction());
6369
6370 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6371 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6372 ADISPLAY_ID_DEFAULT, {110, 220}))
6373 << "The move event injected should succeed";
6374
6375 window->consumeMotionMove();
6376 std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion();
6377 EXPECT_EQ(transform, moveMotionEvent->getTransform());
6378 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction());
6379
6380 // Let foreground window gone
6381 mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0});
6382
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006383 // Foreground window should receive a cancel event, but not the monitor.
Linnan Lid8150952024-01-26 18:07:17 +00006384 window->consumeMotionCancel();
Linnan Lid8150952024-01-26 18:07:17 +00006385
6386 ASSERT_EQ(InputEventInjectionResult::FAILED,
6387 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6388 ADISPLAY_ID_DEFAULT, {110, 220}))
6389 << "The move event injected should failed";
6390 // Now foreground should not receive any events, but monitor should receive a cancel event
6391 // with transform that same as display's display.
Linnan Lid8150952024-01-26 18:07:17 +00006392 std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion();
6393 EXPECT_EQ(transform, cancelMotionEvent->getTransform());
6394 EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId());
6395 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction());
6396
6397 // Other event inject to this display should fail.
6398 ASSERT_EQ(InputEventInjectionResult::FAILED,
6399 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6400 ADISPLAY_ID_DEFAULT, {110, 220}))
Prabir Pradhana0d43d42024-01-30 00:27:08 +00006401 << "The up event injected should fail because the touched window was removed";
Linnan Lid8150952024-01-26 18:07:17 +00006402 window->assertNoEvents();
6403 monitor.assertNoEvents();
6404}
6405
chaviw81e2bb92019-12-18 15:03:51 -08006406TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006407 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006408 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6409 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08006410
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006411 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
chaviw81e2bb92019-12-18 15:03:51 -08006412
6413 NotifyMotionArgs motionArgs =
6414 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6415 ADISPLAY_ID_DEFAULT);
6416
Prabir Pradhan678438e2023-04-13 19:32:51 +00006417 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08006418 // Window should receive motion down event.
6419 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6420
6421 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08006422 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08006423 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6424 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
6425 motionArgs.pointerCoords[0].getX() - 10);
6426
Prabir Pradhan678438e2023-04-13 19:32:51 +00006427 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00006428 window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08006429}
6430
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006431/**
6432 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
6433 * the device default right away. In the test scenario, we check both the default value,
6434 * and the action of enabling / disabling.
6435 */
6436TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07006437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006438 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6439 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08006440 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006441
6442 // Set focused application.
6443 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006444 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006445
6446 SCOPED_TRACE("Check default value of touch mode");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006447 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006448 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006449 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006450
6451 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07006452 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006453 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00006454 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006455
6456 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08006457 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00006458 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006459 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07006460 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006461 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006462 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006463 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006464
6465 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07006466 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006467 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00006468 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006469
6470 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08006471 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00006472 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00006473 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07006474 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006475 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006476 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00006477 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006478
6479 window->assertNoEvents();
6480}
6481
Gang Wange9087892020-01-07 12:17:14 -05006482TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006483 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006484 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6485 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05006486
6487 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006488 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05006489
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006490 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006491 setFocusedWindow(window);
6492
Harry Cutts33476232023-01-30 19:57:29 +00006493 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05006494
Prabir Pradhan678438e2023-04-13 19:32:51 +00006495 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
6496 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05006497
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006498 std::unique_ptr<KeyEvent> event = window->consumeKey();
6499 ASSERT_NE(event, nullptr);
6500 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Gang Wange9087892020-01-07 12:17:14 -05006501 ASSERT_NE(verified, nullptr);
6502 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
6503
6504 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
6505 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
6506 ASSERT_EQ(keyArgs.source, verified->source);
6507 ASSERT_EQ(keyArgs.displayId, verified->displayId);
6508
6509 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
6510
6511 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05006512 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006513 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05006514 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
6515 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
6516 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
6517 ASSERT_EQ(0, verifiedKey.repeatCount);
6518}
6519
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006520TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006521 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006522 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6523 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006524
6525 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6526
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006527 ui::Transform transform;
6528 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6529
6530 gui::DisplayInfo displayInfo;
6531 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6532 displayInfo.transform = transform;
6533
Patrick Williamsd828f302023-04-28 17:52:08 -05006534 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006535
Prabir Pradhan678438e2023-04-13 19:32:51 +00006536 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006537 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6538 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006539 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006540
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08006541 std::unique_ptr<MotionEvent> event = window->consumeMotionEvent();
6542 ASSERT_NE(nullptr, event);
6543 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006544 ASSERT_NE(verified, nullptr);
6545 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
6546
6547 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
6548 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
6549 EXPECT_EQ(motionArgs.source, verified->source);
6550 EXPECT_EQ(motionArgs.displayId, verified->displayId);
6551
6552 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
6553
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006554 const vec2 rawXY =
6555 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
6556 motionArgs.pointerCoords[0].getXYValue());
6557 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
6558 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006559 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006560 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006561 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006562 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
6563 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
6564}
6565
chaviw09c8d2d2020-08-24 15:48:26 -07006566/**
6567 * Ensure that separate calls to sign the same data are generating the same key.
6568 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
6569 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
6570 * tests.
6571 */
6572TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
6573 KeyEvent event = getTestKeyEvent();
6574 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6575
6576 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
6577 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
6578 ASSERT_EQ(hmac1, hmac2);
6579}
6580
6581/**
6582 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
6583 */
6584TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
6585 KeyEvent event = getTestKeyEvent();
6586 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6587 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
6588
6589 verifiedEvent.deviceId += 1;
6590 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6591
6592 verifiedEvent.source += 1;
6593 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6594
6595 verifiedEvent.eventTimeNanos += 1;
6596 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6597
6598 verifiedEvent.displayId += 1;
6599 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6600
6601 verifiedEvent.action += 1;
6602 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6603
6604 verifiedEvent.downTimeNanos += 1;
6605 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6606
6607 verifiedEvent.flags += 1;
6608 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6609
6610 verifiedEvent.keyCode += 1;
6611 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6612
6613 verifiedEvent.scanCode += 1;
6614 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6615
6616 verifiedEvent.metaState += 1;
6617 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6618
6619 verifiedEvent.repeatCount += 1;
6620 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6621}
6622
Vishnu Nair958da932020-08-21 17:12:37 -07006623TEST_F(InputDispatcherTest, SetFocusedWindow) {
6624 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6625 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006626 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006627 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006628 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006629 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6630
6631 // Top window is also focusable but is not granted focus.
6632 windowTop->setFocusable(true);
6633 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006634 mDispatcher->onWindowInfosChanged(
6635 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006636 setFocusedWindow(windowSecond);
6637
6638 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006640 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006641
6642 // Focused window should receive event.
6643 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6644 windowTop->assertNoEvents();
6645}
6646
6647TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
6648 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6649 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006650 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006651 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6652
6653 window->setFocusable(true);
6654 // Release channel for window is no longer valid.
6655 window->releaseChannel();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006656 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006657 setFocusedWindow(window);
6658
6659 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006660 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006661
6662 // window channel is invalid, so it should not receive any input event.
6663 window->assertNoEvents();
6664}
6665
6666TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
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);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006670 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07006671 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6672
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006673 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006674 setFocusedWindow(window);
6675
6676 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006677 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006678
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006679 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07006680 window->assertNoEvents();
6681}
6682
6683TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
6684 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6685 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006686 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006687 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006688 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006689 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6690
6691 windowTop->setFocusable(true);
6692 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006693 mDispatcher->onWindowInfosChanged(
6694 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006695 setFocusedWindow(windowTop);
6696 windowTop->consumeFocusEvent(true);
6697
Chavi Weingarten847e8512023-03-29 00:26:09 +00006698 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006699 mDispatcher->onWindowInfosChanged(
6700 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006701 windowSecond->consumeFocusEvent(true);
6702 windowTop->consumeFocusEvent(false);
6703
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006704 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006705 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006706
6707 // Focused window should receive event.
6708 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6709}
6710
Chavi Weingarten847e8512023-03-29 00:26:09 +00006711TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07006712 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6713 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006714 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006715 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006716 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006717 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6718
6719 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00006720 windowSecond->setFocusable(false);
6721 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006722 mDispatcher->onWindowInfosChanged(
6723 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Chavi Weingarten847e8512023-03-29 00:26:09 +00006724 setFocusedWindow(windowTop);
6725 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07006726
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Chavi Weingarten847e8512023-03-29 00:26:09 +00006728 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006729
6730 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00006731 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07006732 windowSecond->assertNoEvents();
6733}
6734
6735TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
6736 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6737 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006738 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006739 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006740 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
6741 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006742 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6743
6744 window->setFocusable(true);
6745 previousFocusedWindow->setFocusable(true);
6746 window->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006747 mDispatcher->onWindowInfosChanged(
6748 {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006749 setFocusedWindow(previousFocusedWindow);
6750 previousFocusedWindow->consumeFocusEvent(true);
6751
6752 // Requesting focus on invisible window takes focus from currently focused window.
6753 setFocusedWindow(window);
6754 previousFocusedWindow->consumeFocusEvent(false);
6755
6756 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006757 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006758 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
6759 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006760
6761 // Window does not get focus event or key down.
6762 window->assertNoEvents();
6763
6764 // Window becomes visible.
6765 window->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006766 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006767
6768 // Window receives focus event.
6769 window->consumeFocusEvent(true);
6770 // Focused window receives key down.
6771 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6772}
6773
Vishnu Nair599f1412021-06-21 10:39:58 -07006774TEST_F(InputDispatcherTest, DisplayRemoved) {
6775 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6776 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006777 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07006778 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6779
6780 // window is granted focus.
6781 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006782 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair599f1412021-06-21 10:39:58 -07006783 setFocusedWindow(window);
6784 window->consumeFocusEvent(true);
6785
6786 // When a display is removed window loses focus.
6787 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
6788 window->consumeFocusEvent(false);
6789}
6790
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006791/**
6792 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
6793 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
6794 * of the 'slipperyEnterWindow'.
6795 *
6796 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
6797 * a way so that the touched location is no longer covered by the top window.
6798 *
6799 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
6800 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
6801 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
6802 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
6803 * with ACTION_DOWN).
6804 * Thus, the touch has been transferred from the top window into the bottom window, because the top
6805 * window moved itself away from the touched location and had Flag::SLIPPERY.
6806 *
6807 * Even though the top window moved away from the touched location, it is still obscuring the bottom
6808 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
6809 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
6810 *
6811 * In this test, we ensure that the event received by the bottom window has
6812 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
6813 */
6814TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006815 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006816 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006817
6818 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6819 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6820
6821 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006822 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006823 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006824 // Make sure this one overlaps the bottom window
6825 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
6826 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
6827 // one. Windows with the same owner are not considered to be occluding each other.
6828 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
6829
6830 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006831 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006832 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
6833
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006834 mDispatcher->onWindowInfosChanged(
6835 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006836
6837 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00006838 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6839 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6840 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006841 slipperyExitWindow->consumeMotionDown();
6842 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006843 mDispatcher->onWindowInfosChanged(
6844 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006845
Prabir Pradhan678438e2023-04-13 19:32:51 +00006846 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
6847 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6848 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006849
6850 slipperyExitWindow->consumeMotionCancel();
6851
6852 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
6853 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6854}
6855
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006856/**
6857 * Two windows, one on the left and another on the right. The left window is slippery. The right
6858 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
6859 * touch moves from the left window into the right window, the gesture should continue to go to the
6860 * left window. Touch shouldn't slip because the right window can't receive touches. This test
6861 * reproduces a crash.
6862 */
6863TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
6864 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6865
6866 sp<FakeWindowHandle> leftSlipperyWindow =
6867 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6868 leftSlipperyWindow->setSlippery(true);
6869 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
6870
6871 sp<FakeWindowHandle> rightDropTouchesWindow =
6872 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6873 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
6874 rightDropTouchesWindow->setDropInput(true);
6875
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006876 mDispatcher->onWindowInfosChanged(
6877 {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006878
6879 // Start touch in the left window
6880 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6881 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6882 .build());
6883 leftSlipperyWindow->consumeMotionDown();
6884
6885 // And move it into the right window
6886 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6887 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
6888 .build());
6889
6890 // Since the right window isn't eligible to receive input, touch does not slip.
6891 // The left window continues to receive the gesture.
6892 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6893 rightDropTouchesWindow->assertNoEvents();
6894}
6895
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07006896/**
6897 * A single window is on screen first. Touch is injected into that window. Next, a second window
6898 * appears. Since the first window is slippery, touch will move from the first window to the second.
6899 */
6900TEST_F(InputDispatcherTest, InjectedTouchSlips) {
6901 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6902 sp<FakeWindowHandle> originalWindow =
6903 sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT);
6904 originalWindow->setFrame(Rect(0, 0, 200, 200));
6905 originalWindow->setSlippery(true);
6906
6907 sp<FakeWindowHandle> appearingWindow =
6908 sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT);
6909 appearingWindow->setFrame(Rect(0, 0, 200, 200));
6910
6911 mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0});
6912
6913 // Touch down on the original window
6914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6915 injectMotionEvent(*mDispatcher,
6916 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6917 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
6918 .build()));
6919 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6920
6921 // Now, a new window appears. This could be, for example, a notification shade that appears
6922 // after user starts to drag down on the launcher window.
6923 mDispatcher->onWindowInfosChanged(
6924 {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0});
6925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6926 injectMotionEvent(*mDispatcher,
6927 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6928 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110))
6929 .build()));
6930 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
6931 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6933 injectMotionEvent(*mDispatcher,
6934 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6935 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
6936 .build()));
6937 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6938
6939 originalWindow->assertNoEvents();
6940 appearingWindow->assertNoEvents();
6941}
6942
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006943TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006944 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006945 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6946
6947 sp<FakeWindowHandle> leftWindow =
6948 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6949 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006950 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006951
6952 sp<FakeWindowHandle> rightSpy =
6953 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
6954 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006955 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006956 rightSpy->setSpy(true);
6957 rightSpy->setTrustedOverlay(true);
6958
6959 sp<FakeWindowHandle> rightWindow =
6960 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6961 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006962 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006963
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006964 mDispatcher->onWindowInfosChanged(
6965 {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006966
6967 // Touch in the left window
6968 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6969 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6970 .build());
6971 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
6972 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006973 ASSERT_NO_FATAL_FAILURE(
6974 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006975
6976 // Touch another finger over the right windows
6977 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6978 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6979 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6980 .build());
6981 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
6982 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
6983 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
6984 mDispatcher->waitForIdle();
6985 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006986 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
6987 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006988
6989 // Release finger over left window. The UP actions are not treated as device interaction.
6990 // The windows that did not receive the UP pointer will receive MOVE events, but since this
6991 // is part of the UP action, we do not treat this as device interaction.
6992 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
6993 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6994 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6995 .build());
6996 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
6997 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
6998 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
6999 mDispatcher->waitForIdle();
7000 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7001
7002 // Move remaining finger
7003 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7004 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7005 .build());
7006 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
7007 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
7008 mDispatcher->waitForIdle();
7009 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007010 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007011
7012 // Release all fingers
7013 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7014 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
7015 .build());
7016 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
7017 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
7018 mDispatcher->waitForIdle();
7019 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7020}
7021
7022TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
7023 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7024
7025 sp<FakeWindowHandle> window =
7026 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
7027 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00007028 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007029
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007030 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007031 setFocusedWindow(window);
7032 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
7033
7034 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
7035 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
7036 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00007037 ASSERT_NO_FATAL_FAILURE(
7038 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00007039
7040 // The UP actions are not treated as device interaction.
7041 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
7042 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
7043 mDispatcher->waitForIdle();
7044 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
7045}
7046
Prabir Pradhan5893d362023-11-17 04:30:40 +00007047TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) {
7048 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
7049
7050 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
7051 ADISPLAY_ID_DEFAULT);
7052 left->setFrame(Rect(0, 0, 100, 100));
7053 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
7054 "Right Window", ADISPLAY_ID_DEFAULT);
7055 right->setFrame(Rect(100, 0, 200, 100));
7056 sp<FakeWindowHandle> spy =
7057 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
7058 spy->setFrame(Rect(0, 0, 200, 100));
7059 spy->setTrustedOverlay(true);
7060 spy->setSpy(true);
7061
7062 mDispatcher->onWindowInfosChanged(
7063 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
7064
7065 // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId.
7066 NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
7067 ADISPLAY_ID_DEFAULT, {PointF{50, 50}});
7068 mDispatcher->notifyMotion(notifyArgs);
7069
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007070 std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00007071 AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)),
7072 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007073 ASSERT_NE(nullptr, leftEnter);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007074 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
7075 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007076 Not(WithEventId(leftEnter->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00007077 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
7078
7079 // Send move to the right window, and ensure hover exit and enter are synthesized with new ids.
7080 notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
7081 {PointF{150, 50}});
7082 mDispatcher->notifyMotion(notifyArgs);
7083
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007084 std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent(
Prabir Pradhan5893d362023-11-17 04:30:40 +00007085 AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)),
7086 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007087 ASSERT_NE(nullptr, leftExit);
Prabir Pradhan5893d362023-11-17 04:30:40 +00007088 right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
7089 Not(WithEventId(notifyArgs.id)),
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007090 Not(WithEventId(leftExit->getId())),
Prabir Pradhan5893d362023-11-17 04:30:40 +00007091 WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER)));
7092
7093 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id)));
7094}
7095
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00007096class InputDispatcherFallbackKeyTest : public InputDispatcherTest {
7097protected:
7098 std::shared_ptr<FakeApplicationHandle> mApp;
7099 sp<FakeWindowHandle> mWindow;
7100
7101 virtual void SetUp() override {
7102 InputDispatcherTest::SetUp();
7103
7104 mApp = std::make_shared<FakeApplicationHandle>();
7105
7106 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
7107 mWindow->setFrame(Rect(0, 0, 100, 100));
7108
7109 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
7110 setFocusedWindow(mWindow);
7111 ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true));
7112 }
7113
7114 void setFallback(int32_t keycode) {
7115 mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) {
7116 return KeyEventBuilder(event).keyCode(keycode).build();
7117 });
7118 }
7119
7120 void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007121 std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled);
7122 ASSERT_NE(nullptr, event);
7123 ASSERT_THAT(*event, matcher);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00007124 }
7125};
7126
7127TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) {
7128 mDispatcher->notifyKey(
7129 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7130 consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
7131 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7132}
7133
7134TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) {
7135 mDispatcher->notifyKey(
7136 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7137 consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
7138 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7139}
7140
7141TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) {
7142 mDispatcher->notifyKey(
7143 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7144
7145 // Do not handle this key event.
7146 consumeKey(/*handled=*/false,
7147 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7148 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7149
7150 // Since the policy did not request any fallback to be generated, ensure there are no events.
7151 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7152}
7153
7154TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) {
7155 setFallback(AKEYCODE_B);
7156 mDispatcher->notifyKey(
7157 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7158
7159 // Do not handle this key event.
7160 consumeKey(/*handled=*/false,
7161 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7162
7163 // Since the key was not handled, ensure the fallback event was dispatched instead.
7164 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7165 consumeKey(/*handled=*/true,
7166 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7167 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7168
7169 // Release the original key, and ensure the fallback key is also released.
7170 mDispatcher->notifyKey(
7171 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7172 consumeKey(/*handled=*/false,
7173 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7174 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7175 consumeKey(/*handled=*/true,
7176 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7177 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7178
7179 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7180 mWindow->assertNoEvents();
7181}
7182
7183TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) {
7184 setFallback(AKEYCODE_B);
7185 mDispatcher->notifyKey(
7186 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7187
7188 // Do not handle this key event, but handle the fallback.
7189 consumeKey(/*handled=*/false,
7190 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7191 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7192 consumeKey(/*handled=*/true,
7193 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7194 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7195
7196 // Release the original key, and ensure the fallback key is also released.
7197 mDispatcher->notifyKey(
7198 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7199 // But this time, the app handles the original key.
7200 consumeKey(/*handled=*/true,
7201 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7202 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7203 // Ensure the fallback key is canceled.
7204 consumeKey(/*handled=*/true,
7205 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7206 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7207
7208 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7209 mWindow->assertNoEvents();
7210}
7211
7212TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) {
7213 setFallback(AKEYCODE_B);
7214 mDispatcher->notifyKey(
7215 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7216
7217 // Do not handle this key event.
7218 consumeKey(/*handled=*/false,
7219 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7220 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7221 // App does not handle the fallback either, so ensure another fallback is not generated.
7222 setFallback(AKEYCODE_C);
7223 consumeKey(/*handled=*/false,
7224 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7225 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7226
7227 // Release the original key, and ensure the fallback key is also released.
7228 setFallback(AKEYCODE_B);
7229 mDispatcher->notifyKey(
7230 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7231 consumeKey(/*handled=*/false,
7232 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7233 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7234 consumeKey(/*handled=*/false,
7235 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7236 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7237
7238 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7239 mWindow->assertNoEvents();
7240}
7241
7242TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) {
7243 setFallback(AKEYCODE_B);
7244 mDispatcher->notifyKey(
7245 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7246
7247 // Do not handle this key event, so fallback is generated.
7248 consumeKey(/*handled=*/false,
7249 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7250 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7251 consumeKey(/*handled=*/true,
7252 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7253 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7254
7255 // Release the original key, but assume the policy is misbehaving and it
7256 // generates an inconsistent fallback to the one from the DOWN event.
7257 setFallback(AKEYCODE_C);
7258 mDispatcher->notifyKey(
7259 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7260 consumeKey(/*handled=*/false,
7261 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7262 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7263 // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency.
7264 consumeKey(/*handled=*/true,
7265 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7266 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7267
7268 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7269 mWindow->assertNoEvents();
7270}
7271
7272TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) {
7273 setFallback(AKEYCODE_B);
7274 mDispatcher->notifyKey(
7275 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7276
7277 // Do not handle this key event, so fallback is generated.
7278 consumeKey(/*handled=*/false,
7279 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7280 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7281 consumeKey(/*handled=*/true,
7282 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7283 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7284
7285 // The original key is canceled.
7286 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD)
7287 .keyCode(AKEYCODE_A)
7288 .addFlag(AKEY_EVENT_FLAG_CANCELED)
7289 .build());
7290 consumeKey(/*handled=*/false,
7291 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
7292 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7293 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7294 // Ensure the fallback key is also canceled due to the original key being canceled.
7295 consumeKey(/*handled=*/true,
7296 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
7297 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
7298
7299 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
7300 mWindow->assertNoEvents();
7301}
7302
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00007303TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) {
7304 setFallback(AKEYCODE_B);
7305 mDispatcher->notifyKey(
7306 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7307
7308 // Do not handle this key event.
7309 consumeKey(/*handled=*/false,
7310 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7311 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7312 consumeKey(/*handled=*/true,
7313 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
7314 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
7315
7316 mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) {
7317 // When the unhandled key is reported to the policy next, remove the input channel.
7318 mDispatcher->removeInputChannel(mWindow->getToken());
7319 return KeyEventBuilder(event).keyCode(AKEYCODE_B).build();
7320 });
7321 // Release the original key, and let the app now handle the previously unhandled key.
7322 // This should result in the previously generated fallback key to be cancelled.
7323 // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified
7324 // of the UP event for consistency. The Dispatcher calls into the policy from its own thread
7325 // without holding the lock, because it need to synchronously fetch the fallback key. While in
7326 // the policy call, we will now remove the input channel. Once the policy call returns, the
7327 // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does
7328 // not cause any crashes.
7329 mDispatcher->notifyKey(
7330 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
7331 consumeKey(/*handled=*/true,
7332 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
7333 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
7334}
7335
Garfield Tan1c7bc862020-01-28 13:24:04 -08007336class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
7337protected:
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007338 static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms;
7339 static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007340
Chris Yea209fde2020-07-22 13:54:51 -07007341 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007342 sp<FakeWindowHandle> mWindow;
7343
7344 virtual void SetUp() override {
Prabir Pradhandae52792023-12-15 07:36:40 +00007345 InputDispatcherTest::SetUp();
Garfield Tan1c7bc862020-01-28 13:24:04 -08007346
Prabir Pradhandae52792023-12-15 07:36:40 +00007347 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007348 setUpWindow();
7349 }
7350
7351 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07007352 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007353 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007354
Vishnu Nair47074b82020-08-14 11:54:47 -07007355 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007356 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007357 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007358 mWindow->consumeFocusEvent(true);
7359 }
7360
Chris Ye2ad95392020-09-01 13:44:44 -07007361 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08007362 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07007363 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007364 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00007365 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007366
7367 // Window should receive key down event.
7368 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7369 }
7370
7371 void expectKeyRepeatOnce(int32_t repeatCount) {
7372 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007373 mWindow->consumeKeyEvent(
7374 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount)));
Garfield Tan1c7bc862020-01-28 13:24:04 -08007375 }
7376
Chris Ye2ad95392020-09-01 13:44:44 -07007377 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08007378 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07007379 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08007380 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00007381 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007382
7383 // Window should receive key down event.
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00007384 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007385 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007386 }
7387};
7388
7389TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00007390 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007391 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7392 expectKeyRepeatOnce(repeatCount);
7393 }
7394}
7395
7396TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00007397 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007398 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7399 expectKeyRepeatOnce(repeatCount);
7400 }
Harry Cutts33476232023-01-30 19:57:29 +00007401 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007402 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08007403 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
7404 expectKeyRepeatOnce(repeatCount);
7405 }
7406}
7407
7408TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007409 sendAndConsumeKeyDown(/*deviceId=*/1);
7410 expectKeyRepeatOnce(/*repeatCount=*/1);
7411 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007412 mWindow->assertNoEvents();
7413}
7414
7415TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007416 sendAndConsumeKeyDown(/*deviceId=*/1);
7417 expectKeyRepeatOnce(/*repeatCount=*/1);
7418 sendAndConsumeKeyDown(/*deviceId=*/2);
7419 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007420 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00007421 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007422 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00007423 expectKeyRepeatOnce(/*repeatCount=*/2);
7424 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07007425 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00007426 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007427 mWindow->assertNoEvents();
7428}
7429
7430TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00007431 sendAndConsumeKeyDown(/*deviceId=*/1);
7432 expectKeyRepeatOnce(/*repeatCount=*/1);
7433 sendAndConsumeKeyDown(/*deviceId=*/2);
7434 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07007435 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00007436 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07007437 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08007438 mWindow->assertNoEvents();
7439}
7440
liushenxiang42232912021-05-21 20:24:09 +08007441TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
7442 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00007443 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007444 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08007445 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
7446 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
7447 mWindow->assertNoEvents();
7448}
7449
Garfield Tan1c7bc862020-01-28 13:24:04 -08007450TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00007451 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00007452 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007453 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007454 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
7455 ASSERT_NE(nullptr, repeatEvent);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007456 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007457 IdGenerator::getSource(repeatEvent->getId()));
Garfield Tan1c7bc862020-01-28 13:24:04 -08007458 }
7459}
7460
7461TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00007462 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00007463 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08007464
7465 std::unordered_set<int32_t> idSet;
7466 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08007467 std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey();
7468 ASSERT_NE(nullptr, repeatEvent);
7469 int32_t id = repeatEvent->getId();
Garfield Tan1c7bc862020-01-28 13:24:04 -08007470 EXPECT_EQ(idSet.end(), idSet.find(id));
7471 idSet.insert(id);
7472 }
7473}
7474
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007475/* Test InputDispatcher for MultiDisplay */
7476class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
7477public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007478 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007479 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08007480
Chris Yea209fde2020-07-22 13:54:51 -07007481 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007482 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007483 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007484
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007485 // Set focus window for primary display, but focused display would be second one.
7486 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07007487 windowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007488 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
7489
Vishnu Nair958da932020-08-21 17:12:37 -07007490 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007491 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08007492
Chris Yea209fde2020-07-22 13:54:51 -07007493 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007494 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007495 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007496 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007497 // Set focus display to second one.
7498 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
7499 // Set focus window for second display.
7500 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07007501 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007502 mDispatcher->onWindowInfosChanged(
7503 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007504 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007505 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007506 }
7507
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007508 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007509 InputDispatcherTest::TearDown();
7510
Chris Yea209fde2020-07-22 13:54:51 -07007511 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007512 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07007513 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007514 windowInSecondary.clear();
7515 }
7516
7517protected:
Chris Yea209fde2020-07-22 13:54:51 -07007518 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007519 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07007520 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007521 sp<FakeWindowHandle> windowInSecondary;
7522};
7523
7524TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
7525 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007527 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007528 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007529 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08007530 windowInSecondary->assertNoEvents();
7531
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007532 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007534 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007535 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08007536 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007537 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08007538}
7539
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007540TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08007541 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08007542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007543 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007544 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007545 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08007546 windowInSecondary->assertNoEvents();
7547
7548 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007549 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007550 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08007551 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007552 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08007553
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007554 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007555 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08007556
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007557 // Old focus should receive a cancel event.
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00007558 windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08007559
7560 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007561 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08007562 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007563 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08007564 windowInSecondary->assertNoEvents();
7565}
7566
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007567// Test per-display input monitors for motion event.
7568TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08007569 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007570 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007571 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007572 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007573
7574 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007575 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007576 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007577 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007578 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007579 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007580 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007581 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007582
7583 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007585 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007586 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007587 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007588 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007589 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08007590 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007591
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007592 // Lift up the touch from the second display
7593 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007594 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007595 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7596 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
7597 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
7598
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007599 // Test inject a non-pointer motion event.
7600 // If specific a display, it will dispatch to the focused window of particular display,
7601 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007602 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007603 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007604 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007605 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007606 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007607 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007608 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007609}
7610
7611// Test per-display input monitors for key event.
7612TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007613 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08007614 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007615 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007616 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007617 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007618
7619 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007620 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007621 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007622 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007623 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007624 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007625 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007626}
7627
Vishnu Nair958da932020-08-21 17:12:37 -07007628TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
7629 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007630 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007631 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007632 mDispatcher->onWindowInfosChanged(
7633 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
7634 *windowInSecondary->getInfo()},
7635 {},
7636 0,
7637 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007638 setFocusedWindow(secondWindowInPrimary);
7639 windowInPrimary->consumeFocusEvent(false);
7640 secondWindowInPrimary->consumeFocusEvent(true);
7641
7642 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7644 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007645 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007646 windowInPrimary->assertNoEvents();
7647 windowInSecondary->assertNoEvents();
7648 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7649}
7650
Arthur Hungdfd528e2021-12-08 13:23:04 +00007651TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
7652 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007653 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007654 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007655 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007656
7657 // Test touch down on primary display.
7658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007659 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007660 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7661 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7662 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
7663
7664 // Test touch down on second display.
7665 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007666 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007667 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7668 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
7669 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
7670
7671 // Trigger cancel touch.
7672 mDispatcher->cancelCurrentTouch();
7673 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7674 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7675 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
7676 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
7677
7678 // Test inject a move motion event, no window/monitor should receive the event.
7679 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007680 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007681 ADISPLAY_ID_DEFAULT, {110, 200}))
7682 << "Inject motion event should return InputEventInjectionResult::FAILED";
7683 windowInPrimary->assertNoEvents();
7684 monitorInPrimary.assertNoEvents();
7685
7686 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007687 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007688 SECOND_DISPLAY_ID, {110, 200}))
7689 << "Inject motion event should return InputEventInjectionResult::FAILED";
7690 windowInSecondary->assertNoEvents();
7691 monitorInSecondary.assertNoEvents();
7692}
7693
Hu Guocb134f12023-12-23 13:42:44 +00007694/**
7695 * Send a key to the primary display and to the secondary display.
7696 * Then cause the key on the primary display to be canceled by sending in a stale key.
7697 * Ensure that the key on the primary display is canceled, and that the key on the secondary display
7698 * does not get canceled.
7699 */
7700TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) {
7701 // Send a key down on primary display
7702 mDispatcher->notifyKey(
7703 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
7704 .displayId(ADISPLAY_ID_DEFAULT)
7705 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7706 .build());
7707 windowInPrimary->consumeKeyEvent(
7708 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
7709 windowInSecondary->assertNoEvents();
7710
7711 // Send a key down on second display
7712 mDispatcher->notifyKey(
7713 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
7714 .displayId(SECOND_DISPLAY_ID)
7715 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7716 .build());
7717 windowInSecondary->consumeKeyEvent(
7718 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
7719 windowInPrimary->assertNoEvents();
7720
7721 // Send a valid key up event on primary display that will be dropped because it is stale
7722 NotifyKeyArgs staleKeyUp =
7723 KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
7724 .displayId(ADISPLAY_ID_DEFAULT)
7725 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
7726 .build();
7727 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
7728 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
7729 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
7730 mDispatcher->notifyKey(staleKeyUp);
7731
7732 // Only the key gesture corresponding to the dropped event should receive the cancel event.
7733 // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not
7734 // receive any events.
7735 windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
7736 WithDisplayId(ADISPLAY_ID_DEFAULT),
7737 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
7738 windowInSecondary->assertNoEvents();
7739}
7740
7741/**
7742 * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events.
7743 */
7744TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) {
7745 // Send touch down on primary display.
7746 mDispatcher->notifyMotion(
7747 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7748 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7749 .displayId(ADISPLAY_ID_DEFAULT)
7750 .build());
7751 windowInPrimary->consumeMotionEvent(
7752 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
7753 windowInSecondary->assertNoEvents();
7754
7755 // Send touch down on second display.
7756 mDispatcher->notifyMotion(
7757 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
7758 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7759 .displayId(SECOND_DISPLAY_ID)
7760 .build());
7761 windowInPrimary->assertNoEvents();
7762 windowInSecondary->consumeMotionEvent(
7763 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID)));
7764
7765 // inject a valid MotionEvent on primary display that will be stale when it arrives.
7766 NotifyMotionArgs staleMotionUp =
7767 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
7768 .displayId(ADISPLAY_ID_DEFAULT)
7769 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
7770 .build();
7771 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms;
7772 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
7773 std::this_thread::sleep_for(STALE_EVENT_TIMEOUT);
7774 mDispatcher->notifyMotion(staleMotionUp);
7775
7776 // For stale motion events, we let the gesture to complete. This behaviour is different from key
7777 // events, where we would cancel the current keys instead.
7778 windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP));
7779 windowInSecondary->assertNoEvents();
7780}
7781
Jackal Guof9696682018-10-05 12:23:23 +08007782class InputFilterTest : public InputDispatcherTest {
7783protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007784 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
7785 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08007786 NotifyMotionArgs motionArgs;
7787
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007788 motionArgs =
7789 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007790 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007791 motionArgs =
7792 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007793 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007794 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007795 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007796 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007797 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08007798 } else {
7799 mFakePolicy->assertFilterInputEventWasNotCalled();
7800 }
7801 }
7802
7803 void testNotifyKey(bool expectToBeFiltered) {
7804 NotifyKeyArgs keyArgs;
7805
7806 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007807 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007808 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007809 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007810 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007811
7812 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08007813 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007814 } else {
7815 mFakePolicy->assertFilterInputEventWasNotCalled();
7816 }
7817 }
7818};
7819
7820// Test InputFilter for MotionEvent
7821TEST_F(InputFilterTest, MotionEvent_InputFilter) {
7822 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007823 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7824 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007825
7826 // Enable InputFilter
7827 mDispatcher->setInputFilterEnabled(true);
7828 // Test touch on both primary and second display, and check if both events are filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007829 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true);
7830 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007831
7832 // Disable InputFilter
7833 mDispatcher->setInputFilterEnabled(false);
7834 // Test touch on both primary and second display, and check if both events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007835 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7836 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007837}
7838
7839// Test InputFilter for KeyEvent
7840TEST_F(InputFilterTest, KeyEvent_InputFilter) {
7841 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007842 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007843
7844 // Enable InputFilter
7845 mDispatcher->setInputFilterEnabled(true);
7846 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007847 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007848
7849 // Disable InputFilter
7850 mDispatcher->setInputFilterEnabled(false);
7851 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007852 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007853}
7854
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007855// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
7856// logical display coordinate space.
7857TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
7858 ui::Transform firstDisplayTransform;
7859 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7860 ui::Transform secondDisplayTransform;
7861 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
7862
7863 std::vector<gui::DisplayInfo> displayInfos(2);
7864 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
7865 displayInfos[0].transform = firstDisplayTransform;
7866 displayInfos[1].displayId = SECOND_DISPLAY_ID;
7867 displayInfos[1].transform = secondDisplayTransform;
7868
Patrick Williamsd828f302023-04-28 17:52:08 -05007869 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007870
7871 // Enable InputFilter
7872 mDispatcher->setInputFilterEnabled(true);
7873
7874 // Ensure the correct transforms are used for the displays.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007875 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform);
7876 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007877}
7878
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007879class InputFilterInjectionPolicyTest : public InputDispatcherTest {
7880protected:
7881 virtual void SetUp() override {
7882 InputDispatcherTest::SetUp();
7883
7884 /**
7885 * We don't need to enable input filter to test the injected event policy, but we enabled it
7886 * here to make the tests more realistic, since this policy only matters when inputfilter is
7887 * on.
7888 */
7889 mDispatcher->setInputFilterEnabled(true);
7890
7891 std::shared_ptr<InputApplicationHandle> application =
7892 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007893 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
7894 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007895
7896 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7897 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007898 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007899 setFocusedWindow(mWindow);
7900 mWindow->consumeFocusEvent(true);
7901 }
7902
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007903 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7904 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007905 KeyEvent event;
7906
7907 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7908 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
7909 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00007910 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007911 const int32_t additionalPolicyFlags =
7912 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
7913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007914 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00007915 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007916 policyFlags | additionalPolicyFlags));
7917
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007918 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007919 }
7920
7921 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7922 int32_t flags) {
7923 MotionEvent event;
7924 PointerProperties pointerProperties[1];
7925 PointerCoords pointerCoords[1];
7926 pointerProperties[0].clear();
7927 pointerProperties[0].id = 0;
7928 pointerCoords[0].clear();
7929 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
7930 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
7931
7932 ui::Transform identityTransform;
7933 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7934 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
7935 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
7936 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
7937 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07007938 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07007939 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00007940 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007941
7942 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
7943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007944 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00007945 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007946 policyFlags | additionalPolicyFlags));
7947
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007948 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007949 }
7950
7951private:
7952 sp<FakeWindowHandle> mWindow;
7953};
7954
7955TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007956 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
7957 // filter. Without it, the event will no different from a regularly injected event, and the
7958 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00007959 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
7960 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007961}
7962
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007963TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007964 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00007965 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007966 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
7967}
7968
7969TEST_F(InputFilterInjectionPolicyTest,
7970 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
7971 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00007972 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007973 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007974}
7975
7976TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00007977 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
7978 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007979}
7980
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08007981class InputDispatcherUserActivityPokeTests : public InputDispatcherTest {
7982protected:
7983 virtual void SetUp() override {
7984 InputDispatcherTest::SetUp();
7985
7986 std::shared_ptr<FakeApplicationHandle> application =
7987 std::make_shared<FakeApplicationHandle>();
7988 application->setDispatchingTimeout(100ms);
7989 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow",
7990 ADISPLAY_ID_DEFAULT);
Yeabkal Wubshit222d83d2024-01-24 18:00:09 +00007991 mWindow->setFrame(Rect(0, 0, 100, 100));
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08007992 mWindow->setDispatchingTimeout(100ms);
7993 mWindow->setFocusable(true);
7994
7995 // Set focused application.
7996 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7997
7998 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
7999 setFocusedWindow(mWindow);
8000 mWindow->consumeFocusEvent(true);
8001 }
8002
8003 void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId,
8004 nsecs_t eventTime) {
8005 mDispatcher->notifyMotion(MotionArgsBuilder(action, source)
8006 .displayId(displayId)
8007 .eventTime(eventTime)
8008 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8009 .build());
8010 mWindow->consumeMotionEvent(WithMotionAction(action));
8011 }
8012
8013private:
8014 sp<FakeWindowHandle> mWindow;
8015};
8016
8017TEST_F_WITH_FLAGS(
8018 InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
8019 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8020 rate_limit_user_activity_poke_in_dispatcher))) {
8021 mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
8022
8023 // First event of type TOUCH. Should poke.
8024 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8025 milliseconds_to_nanoseconds(50));
8026 mFakePolicy->assertUserActivityPoked(
8027 {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8028
8029 // 80ns > 50ns has passed since previous TOUCH event. Should poke.
8030 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8031 milliseconds_to_nanoseconds(130));
8032 mFakePolicy->assertUserActivityPoked(
8033 {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8034
8035 // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
8036 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8037 milliseconds_to_nanoseconds(135));
8038 mFakePolicy->assertUserActivityPoked(
8039 {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}});
8040
8041 // Within 50ns of previous TOUCH event. Should NOT poke.
8042 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8043 milliseconds_to_nanoseconds(140));
8044 mFakePolicy->assertUserActivityNotPoked();
8045
8046 // Within 50ns of previous OTHER event. Should NOT poke.
8047 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8048 milliseconds_to_nanoseconds(150));
8049 mFakePolicy->assertUserActivityNotPoked();
8050
8051 // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
8052 // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
8053 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
8054 milliseconds_to_nanoseconds(160));
8055 mFakePolicy->assertUserActivityNotPoked();
8056
8057 // 65ns > 50ns has passed since previous OTHER event. Should poke.
8058 notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT,
8059 milliseconds_to_nanoseconds(200));
8060 mFakePolicy->assertUserActivityPoked(
8061 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}});
8062
8063 // 170ns > 50ns has passed since previous TOUCH event. Should poke.
8064 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT,
8065 milliseconds_to_nanoseconds(300));
8066 mFakePolicy->assertUserActivityPoked(
8067 {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8068
8069 // Assert that there's no more user activity poke event.
8070 mFakePolicy->assertUserActivityNotPoked();
8071}
8072
8073TEST_F_WITH_FLAGS(
8074 InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
8075 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8076 rate_limit_user_activity_poke_in_dispatcher))) {
8077 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8078 milliseconds_to_nanoseconds(200));
8079 mFakePolicy->assertUserActivityPoked(
8080 {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8081
8082 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8083 milliseconds_to_nanoseconds(280));
8084 mFakePolicy->assertUserActivityNotPoked();
8085
8086 notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8087 milliseconds_to_nanoseconds(340));
8088 mFakePolicy->assertUserActivityPoked(
8089 {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}});
8090}
8091
8092TEST_F_WITH_FLAGS(
8093 InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
8094 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
8095 rate_limit_user_activity_poke_in_dispatcher))) {
8096 mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
8097
8098 notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20);
8099 mFakePolicy->assertUserActivityPoked();
8100
8101 notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30);
8102 mFakePolicy->assertUserActivityPoked();
8103}
8104
chaviwfd6d3512019-03-25 13:23:49 -07008105class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008106 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07008107 InputDispatcherTest::SetUp();
8108
Chris Yea209fde2020-07-22 13:54:51 -07008109 std::shared_ptr<FakeApplicationHandle> application =
8110 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008111 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008112 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07008113 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07008114
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008115 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008116 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008117 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07008118
8119 // Set focused application.
8120 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07008121 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07008122
8123 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008124 mDispatcher->onWindowInfosChanged(
8125 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008126 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008127 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07008128 }
8129
Prabir Pradhan3608aad2019-10-02 17:08:26 -07008130 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07008131 InputDispatcherTest::TearDown();
8132
8133 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008134 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07008135 }
8136
8137protected:
8138 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08008139 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008140 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07008141};
8142
8143// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
8144// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
8145// the onPointerDownOutsideFocus callback.
8146TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008147 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008148 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008149 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008150 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008151 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008152
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008153 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07008154 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
8155}
8156
8157// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
8158// DOWN on the window that doesn't have focus. Ensure no window received the
8159// onPointerDownOutsideFocus callback.
8160TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008162 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT,
8163 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008164 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008165 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008166
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008167 ASSERT_TRUE(mDispatcher->waitForIdle());
8168 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008169}
8170
8171// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
8172// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
8173TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08008174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008175 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008176 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008177 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07008178
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008179 ASSERT_TRUE(mDispatcher->waitForIdle());
8180 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008181}
8182
8183// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
8184// DOWN on the window that already has focus. Ensure no window received the
8185// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10008186TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008188 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07008189 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008190 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07008191 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07008192
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08008193 ASSERT_TRUE(mDispatcher->waitForIdle());
8194 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07008195}
8196
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008197// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
8198// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
8199TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
8200 const MotionEvent event =
8201 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8202 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008203 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008204 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
8205 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008206 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08008207 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8208 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
8209
8210 ASSERT_TRUE(mDispatcher->waitForIdle());
8211 mFakePolicy->assertOnPointerDownWasNotCalled();
8212 // Ensure that the unfocused window did not receive any FOCUS events.
8213 mUnfocusedWindow->assertNoEvents();
8214}
8215
chaviwaf87b3e2019-10-01 16:59:28 -07008216// These tests ensures we can send touch events to a single client when there are multiple input
8217// windows that point to the same client token.
8218class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
8219 virtual void SetUp() override {
8220 InputDispatcherTest::SetUp();
8221
Chris Yea209fde2020-07-22 13:54:51 -07008222 std::shared_ptr<FakeApplicationHandle> application =
8223 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008224 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
8225 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07008226 mWindow1->setFrame(Rect(0, 0, 100, 100));
8227
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00008228 mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07008229 mWindow2->setFrame(Rect(100, 100, 200, 200));
8230
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008231 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07008232 }
8233
8234protected:
8235 sp<FakeWindowHandle> mWindow1;
8236 sp<FakeWindowHandle> mWindow2;
8237
8238 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05008239 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07008240 vec2 vals = windowInfo->transform.transform(point.x, point.y);
8241 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07008242 }
8243
8244 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
8245 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01008246 const std::string name = window->getName();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008247 std::unique_ptr<MotionEvent> motionEvent =
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00008248 window->consumeMotionEvent(WithMotionAction(expectedAction));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008249 ASSERT_NE(nullptr, motionEvent);
8250 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07008251
8252 for (size_t i = 0; i < points.size(); i++) {
8253 float expectedX = points[i].x;
8254 float expectedY = points[i].y;
8255
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008256 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07008257 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008258 << ", got " << motionEvent->getX(i);
8259 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07008260 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008261 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07008262 }
8263 }
chaviw9eaa22c2020-07-01 16:21:27 -07008264
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08008265 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07008266 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00008267 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
8268 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07008269
8270 // Always consume from window1 since it's the window that has the InputReceiver
8271 consumeMotionEvent(mWindow1, action, expectedPoints);
8272 }
chaviwaf87b3e2019-10-01 16:59:28 -07008273};
8274
8275TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
8276 // Touch Window 1
8277 PointF touchedPoint = {10, 10};
8278 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07008279 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008280
8281 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07008282 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008283
8284 // Touch Window 2
8285 touchedPoint = {150, 150};
8286 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07008287 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008288}
8289
chaviw9eaa22c2020-07-01 16:21:27 -07008290TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
8291 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07008292 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008293 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07008294
8295 // Touch Window 1
8296 PointF touchedPoint = {10, 10};
8297 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07008298 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008299 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07008300 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008301
8302 // Touch Window 2
8303 touchedPoint = {150, 150};
8304 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07008305 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
8306 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008307
chaviw9eaa22c2020-07-01 16:21:27 -07008308 // Update the transform so rotation is set
8309 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008310 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008311 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
8312 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07008313}
8314
chaviw9eaa22c2020-07-01 16:21:27 -07008315TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008316 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008317 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008318
8319 // Touch Window 1
8320 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8321 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07008322 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008323
8324 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008325 touchedPoints.push_back(PointF{150, 150});
8326 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008327 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008328
chaviw9eaa22c2020-07-01 16:21:27 -07008329 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008330 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008331 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008332
chaviw9eaa22c2020-07-01 16:21:27 -07008333 // Update the transform so rotation is set for Window 2
8334 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008335 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008336 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008337 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008338}
8339
chaviw9eaa22c2020-07-01 16:21:27 -07008340TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008341 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008342 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008343
8344 // Touch Window 1
8345 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8346 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07008347 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008348
8349 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008350 touchedPoints.push_back(PointF{150, 150});
8351 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008352
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008353 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008354
8355 // Move both windows
8356 touchedPoints = {{20, 20}, {175, 175}};
8357 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8358 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8359
chaviw9eaa22c2020-07-01 16:21:27 -07008360 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008361
chaviw9eaa22c2020-07-01 16:21:27 -07008362 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008363 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008364 expectedPoints.pop_back();
8365
8366 // Touch Window 2
8367 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008368 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07008369 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008370 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07008371
8372 // Move both windows
8373 touchedPoints = {{20, 20}, {175, 175}};
8374 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8375 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8376
8377 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008378}
8379
8380TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
8381 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008382 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008383
8384 // Touch Window 1
8385 std::vector<PointF> touchedPoints = {PointF{10, 10}};
8386 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07008387 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008388
8389 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07008390 touchedPoints.push_back(PointF{150, 150});
8391 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008392
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08008393 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008394
8395 // Move both windows
8396 touchedPoints = {{20, 20}, {175, 175}};
8397 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
8398 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
8399
chaviw9eaa22c2020-07-01 16:21:27 -07008400 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00008401}
8402
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07008403/**
8404 * When one of the windows is slippery, the touch should not slip into the other window with the
8405 * same input channel.
8406 */
8407TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
8408 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008409 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07008410
8411 // Touch down in window 1
8412 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
8413 ADISPLAY_ID_DEFAULT, {{50, 50}}));
8414 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
8415
8416 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
8417 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
8418 // getting generated.
8419 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8420 ADISPLAY_ID_DEFAULT, {{150, 150}}));
8421
8422 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
8423}
8424
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008425/**
8426 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
8427 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
8428 * that the pointer is hovering over may have a different transform.
8429 */
8430TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008431 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008432
8433 // Start hover in window 1
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008434 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN)
8435 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
8436 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008437 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
8438 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008439 // Move hover to window 2.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008440 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8441 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
8442 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07008443 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
8444 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
8445 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
8446}
8447
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008448class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
8449 virtual void SetUp() override {
8450 InputDispatcherTest::SetUp();
8451
Chris Yea209fde2020-07-22 13:54:51 -07008452 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008453 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008454 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
8455 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008456 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008457 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07008458 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008459
8460 // Set focused application.
8461 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
8462
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008463 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008464 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008465 mWindow->consumeFocusEvent(true);
8466 }
8467
8468 virtual void TearDown() override {
8469 InputDispatcherTest::TearDown();
8470 mWindow.clear();
8471 }
8472
8473protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008474 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07008475 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008476 sp<FakeWindowHandle> mWindow;
8477 static constexpr PointF WINDOW_LOCATION = {20, 20};
8478
8479 void tapOnWindow() {
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008480 const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER)
8481 .x(WINDOW_LOCATION.x)
8482 .y(WINDOW_LOCATION.y);
8483 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8484 .pointer(touchingPointer)
8485 .build());
8486 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
8487 .pointer(touchingPointer)
8488 .build());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008489 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008490
8491 sp<FakeWindowHandle> addSpyWindow() {
8492 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008493 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008494 spy->setTrustedOverlay(true);
8495 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008496 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008497 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008498 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008499 return spy;
8500 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008501};
8502
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008503// Send a tap and respond, which should not cause an ANR.
8504TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
8505 tapOnWindow();
8506 mWindow->consumeMotionDown();
8507 mWindow->consumeMotionUp();
8508 ASSERT_TRUE(mDispatcher->waitForIdle());
8509 mFakePolicy->assertNotifyAnrWasNotCalled();
8510}
8511
8512// Send a regular key and respond, which should not cause an ANR.
8513TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008514 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008515 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
8516 ASSERT_TRUE(mDispatcher->waitForIdle());
8517 mFakePolicy->assertNotifyAnrWasNotCalled();
8518}
8519
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008520TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
8521 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008522 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008523 mWindow->consumeFocusEvent(false);
8524
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008525 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008526 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
8527 InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00008528 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008529 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05008530 // Key will not go to window because we have no focused window.
8531 // The 'no focused window' ANR timer should start instead.
8532
8533 // Now, the focused application goes away.
8534 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
8535 // The key should get dropped and there should be no ANR.
8536
8537 ASSERT_TRUE(mDispatcher->waitForIdle());
8538 mFakePolicy->assertNotifyAnrWasNotCalled();
8539}
8540
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008541// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008542// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
8543// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008544TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008545 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008546 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008547 WINDOW_LOCATION));
8548
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008549 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008550 ASSERT_TRUE(sequenceNum);
8551 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008552 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008553
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008554 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008555 mWindow->consumeMotionEvent(
8556 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008557 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008558 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008559}
8560
8561// Send a key to the app and have the app not respond right away.
8562TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
8563 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008565 const auto [sequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008566 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008567 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008568 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07008569 ASSERT_TRUE(mDispatcher->waitForIdle());
8570}
8571
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008572// We have a focused application, but no focused window
8573TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07008574 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008575 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008576 mWindow->consumeFocusEvent(false);
8577
8578 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008580 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008581 WINDOW_LOCATION));
8582 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
8583 mDispatcher->waitForIdle();
8584 mFakePolicy->assertNotifyAnrWasNotCalled();
8585
8586 // Once a focused event arrives, we get an ANR for this application
8587 // We specify the injection timeout to be smaller than the application timeout, to ensure that
8588 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008589 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008590 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008591 InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008592 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008593 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07008594 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008595 ASSERT_TRUE(mDispatcher->waitForIdle());
8596}
8597
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008598/**
8599 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
8600 * there will not be an ANR.
8601 */
8602TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
8603 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008604 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008605 mWindow->consumeFocusEvent(false);
8606
8607 KeyEvent event;
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07008608 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
8609 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008610 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
8611 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
8612
8613 // Define a valid key down event that is stale (too old).
8614 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Harry Cutts101ee9b2023-07-06 18:04:14 +00008615 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00008616 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008617
8618 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
8619
8620 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00008621 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08008622 InputEventInjectionSync::WAIT_FOR_RESULT,
8623 INJECT_EVENT_TIMEOUT, policyFlags);
8624 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
8625 << "Injection should fail because the event is stale";
8626
8627 ASSERT_TRUE(mDispatcher->waitForIdle());
8628 mFakePolicy->assertNotifyAnrWasNotCalled();
8629 mWindow->assertNoEvents();
8630}
8631
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008632// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008633// Make sure that we don't notify policy twice about the same ANR.
8634TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008635 const std::chrono::duration appTimeout = 400ms;
8636 mApplication->setDispatchingTimeout(appTimeout);
8637 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
8638
Vishnu Nair47074b82020-08-14 11:54:47 -07008639 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008640 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008641 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008642
8643 // Once a focused event arrives, we get an ANR for this application
8644 // We specify the injection timeout to be smaller than the application timeout, to ensure that
8645 // injection times out (instead of failing).
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008646 const std::chrono::duration eventInjectionTimeout = 100ms;
8647 ASSERT_LT(eventInjectionTimeout, appTimeout);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008648 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008649 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07008650 InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout,
8651 /*allowKeyRepeat=*/false);
8652 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
8653 << "result=" << ftl::enum_string(result);
8654 // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
8655 // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
8656 std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
8657 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008658
Vishnu Naire4df8752022-09-08 09:17:55 -07008659 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008660 // ANR should not be raised again. It is up to policy to do that if it desires.
8661 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008662
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008663 // If we now get a focused window, the ANR should stop, but the policy handles that via
8664 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008665 ASSERT_TRUE(mDispatcher->waitForIdle());
8666}
8667
8668// We have a focused application, but no focused window
8669TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07008670 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008671 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008672 mWindow->consumeFocusEvent(false);
8673
8674 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008675 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008676
Vishnu Naire4df8752022-09-08 09:17:55 -07008677 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
8678 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008679
8680 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008681 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008682 ASSERT_TRUE(mDispatcher->waitForIdle());
8683 mWindow->assertNoEvents();
8684}
8685
8686/**
8687 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
8688 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
8689 * If we process 1 of the events, but ANR on the second event with the same timestamp,
8690 * the ANR mechanism should still work.
8691 *
8692 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
8693 * DOWN event, while not responding on the second one.
8694 */
8695TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
8696 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008697 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008698 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
8699 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8700 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008701 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008702
8703 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008704 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008705 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
8706 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
8707 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008708 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008709
8710 // We have now sent down and up. Let's consume first event and then ANR on the second.
8711 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8712 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008713 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008714}
8715
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008716// A spy window can receive an ANR
8717TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
8718 sp<FakeWindowHandle> spy = addSpyWindow();
8719
8720 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008721 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008722 WINDOW_LOCATION));
8723 mWindow->consumeMotionDown();
8724
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008725 const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008726 ASSERT_TRUE(sequenceNum);
8727 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008728 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008729
8730 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008731 spy->consumeMotionEvent(
8732 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008733 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008734 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008735}
8736
8737// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008738// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008739TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
8740 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008741
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008743 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008744 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008746
8747 // Stuck on the ACTION_UP
8748 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008749 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008750
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008751 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008752 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008753 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8754 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008755
8756 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
8757 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008758 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008759 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008760 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008761}
8762
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008763// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008764// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008765TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
8766 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008767
8768 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008769 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8770 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008771
8772 mWindow->consumeMotionDown();
8773 // Stuck on the ACTION_UP
8774 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008775 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008776
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008777 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008778 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008779 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8780 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008781
8782 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
8783 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008784 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008785 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008786 spy->assertNoEvents();
8787}
8788
8789TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008790 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008791
Prabir Pradhanfb549072023-10-05 19:17:36 +00008792 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008793
8794 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008795 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008796 WINDOW_LOCATION));
8797
8798 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8799 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
8800 ASSERT_TRUE(consumeSeq);
8801
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008802 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
8803 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008804
8805 monitor.finishEvent(*consumeSeq);
8806 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
8807
8808 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008809 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008810}
8811
8812// If a window is unresponsive, then you get anr. if the window later catches up and starts to
8813// process events, you don't get an anr. When the window later becomes unresponsive again, you
8814// get an ANR again.
8815// 1. tap -> block on ACTION_UP -> receive ANR
8816// 2. consume all pending events (= queue becomes healthy again)
8817// 3. tap again -> block on ACTION_UP again -> receive ANR second time
8818TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
8819 tapOnWindow();
8820
8821 mWindow->consumeMotionDown();
8822 // Block on ACTION_UP
8823 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008824 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008825 mWindow->consumeMotionUp(); // Now the connection should be healthy again
8826 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008827 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008828 mWindow->assertNoEvents();
8829
8830 tapOnWindow();
8831 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008832 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008833 mWindow->consumeMotionUp();
8834
8835 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008836 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008837 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008838 mWindow->assertNoEvents();
8839}
8840
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008841// If a connection remains unresponsive for a while, make sure policy is only notified once about
8842// it.
8843TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008844 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008845 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008846 WINDOW_LOCATION));
8847
8848 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008849 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008850 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008851 // 'notifyConnectionUnresponsive' should only be called once per connection
8852 mFakePolicy->assertNotifyAnrWasNotCalled();
8853 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008854 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008855 mWindow->consumeMotionEvent(
8856 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008857 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008858 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008859 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008860 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008861}
8862
8863/**
8864 * 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 -07008865 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008866 *
8867 * Warning!!!
8868 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
8869 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008870 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008871 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
8872 *
8873 * If that value changes, this test should also change.
8874 */
8875TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
8876 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008877 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008878
8879 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008880 const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008881 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008882 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008883 ASSERT_TRUE(upSequenceNum);
8884 // Don't finish the events yet, and send a key
8885 // Injection will "succeed" because we will eventually give up and send the key to the focused
8886 // window even if motions are still being processed. But because the injection timeout is short,
8887 // we will receive INJECTION_TIMED_OUT as the result.
8888
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008889 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008890 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
8891 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008892 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008893 // Key will not be sent to the window, yet, because the window is still processing events
8894 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008895 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
8896 // Rely here on the fact that it uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood.
8897 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
8898 mWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008899
8900 std::this_thread::sleep_for(500ms);
8901 // if we wait long enough though, dispatcher will give up, and still send the key
8902 // to the focused window, even though we have not yet finished the motion event
8903 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8904 mWindow->finishEvent(*downSequenceNum);
8905 mWindow->finishEvent(*upSequenceNum);
8906}
8907
8908/**
8909 * If a window is processing a motion event, and then a key event comes in, the key event should
8910 * not go to the focused window until the motion is processed.
8911 * If then a new motion comes in, then the pending key event should be going to the currently
8912 * focused window right away.
8913 */
8914TEST_F(InputDispatcherSingleWindowAnr,
8915 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
8916 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008917 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008918
8919 tapOnWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008920 const auto& [downSequenceNum, _] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008921 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008922 const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008923 ASSERT_TRUE(upSequenceNum);
8924 // Don't finish the events yet, and send a key
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008925 mDispatcher->notifyKey(
8926 KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
8927 .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT)
8928 .build());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008929 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008930 // Make sure the `assertNoEvents` check doesn't take too long. It uses
8931 // CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood.
8932 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
8933 mWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008934
8935 // Now tap down again. It should cause the pending key to go to the focused window right away.
8936 tapOnWindow();
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008937 mWindow->consumeKeyEvent(WithKeyAction(AKEY_EVENT_ACTION_DOWN)); // it doesn't matter that we
8938 // haven't ack'd the other events yet. We can finish events in any order.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008939 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
8940 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
Siarhei Vishniakou67bf2162023-11-16 13:29:50 -08008941 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
8942 mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008943 mWindow->assertNoEvents();
8944}
8945
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07008946/**
8947 * Send an event to the app and have the app not respond right away.
8948 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
8949 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
8950 * At some point, the window becomes responsive again.
8951 * Ensure that subsequent events get dropped, and the next gesture is delivered.
8952 */
8953TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
8954 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8955 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
8956 .build());
8957
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08008958 const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07008959 ASSERT_TRUE(sequenceNum);
8960 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
8961 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
8962
8963 mWindow->finishEvent(*sequenceNum);
8964 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
8965 ASSERT_TRUE(mDispatcher->waitForIdle());
8966 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
8967
8968 // Now that the window is responsive, let's continue the gesture.
8969 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8970 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
8971 .build());
8972
8973 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8974 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
8975 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
8976 .build());
8977
8978 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8979 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
8980 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
8981 .build());
8982 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
8983 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
8984 .build());
8985 // We already canceled this pointer, so the window shouldn't get any new events.
8986 mWindow->assertNoEvents();
8987
8988 // Start another one.
8989 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8990 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
8991 .build());
8992 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
8993}
8994
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008995class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
8996 virtual void SetUp() override {
8997 InputDispatcherTest::SetUp();
8998
Chris Yea209fde2020-07-22 13:54:51 -07008999 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009000 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009001 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
9002 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009003 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009004 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009005 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009006
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009007 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
9008 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009009 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009010 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009011
9012 // Set focused application.
9013 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07009014 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009015
9016 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009017 mDispatcher->onWindowInfosChanged(
9018 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009019 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009020 mFocusedWindow->consumeFocusEvent(true);
9021 }
9022
9023 virtual void TearDown() override {
9024 InputDispatcherTest::TearDown();
9025
9026 mUnfocusedWindow.clear();
9027 mFocusedWindow.clear();
9028 }
9029
9030protected:
Chris Yea209fde2020-07-22 13:54:51 -07009031 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009032 sp<FakeWindowHandle> mUnfocusedWindow;
9033 sp<FakeWindowHandle> mFocusedWindow;
9034 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
9035 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
9036 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
9037
9038 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
9039
9040 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
9041
9042private:
9043 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009044 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009045 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009046 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009048 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009049 location));
9050 }
9051};
9052
9053// If we have 2 windows that are both unresponsive, the one with the shortest timeout
9054// should be ANR'd first.
9055TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009056 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009057 injectMotionEvent(*mDispatcher,
9058 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9059 AINPUT_SOURCE_TOUCHSCREEN)
9060 .pointer(PointerBuilder(0, ToolType::FINGER)
9061 .x(FOCUSED_WINDOW_LOCATION.x)
9062 .y(FOCUSED_WINDOW_LOCATION.y))
9063 .build()));
9064 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9065 injectMotionEvent(*mDispatcher,
9066 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
9067 AINPUT_SOURCE_TOUCHSCREEN)
9068 .pointer(PointerBuilder(0, ToolType::FINGER)
9069 .x(FOCUSED_WINDOW_LOCATION.x)
9070 .y(FOCUSED_WINDOW_LOCATION.y))
9071 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009072 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009073 mFocusedWindow->consumeMotionUp();
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009074 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009075 // We consumed all events, so no ANR
9076 ASSERT_TRUE(mDispatcher->waitForIdle());
9077 mFakePolicy->assertNotifyAnrWasNotCalled();
9078
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009080 injectMotionEvent(*mDispatcher,
9081 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9082 AINPUT_SOURCE_TOUCHSCREEN)
9083 .pointer(PointerBuilder(0, ToolType::FINGER)
9084 .x(FOCUSED_WINDOW_LOCATION.x)
9085 .y(FOCUSED_WINDOW_LOCATION.y))
9086 .build()));
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009087 const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009088 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009089
9090 const std::chrono::duration timeout =
9091 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009092 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07009093
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009094 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009095 mFocusedWindow->consumeMotionDown();
9096 // This cancel is generated because the connection was unresponsive
9097 mFocusedWindow->consumeMotionCancel();
9098 mFocusedWindow->assertNoEvents();
9099 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009100 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009101 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9102 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009103 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009104}
9105
9106// If we have 2 windows with identical timeouts that are both unresponsive,
9107// it doesn't matter which order they should have ANR.
9108// But we should receive ANR for both.
9109TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
9110 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009111 mUnfocusedWindow->setDispatchingTimeout(
9112 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009113 mDispatcher->onWindowInfosChanged(
9114 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009115
9116 tapOnFocusedWindow();
9117 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009118 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009119 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
9120 mFocusedWindow->getDispatchingTimeout(
9121 DISPATCHING_TIMEOUT)),
9122 mFakePolicy->getUnresponsiveWindowToken(0ms)};
9123
9124 ASSERT_THAT(anrConnectionTokens,
9125 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
9126 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009127
9128 ASSERT_TRUE(mDispatcher->waitForIdle());
9129 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009130
9131 mFocusedWindow->consumeMotionDown();
9132 mFocusedWindow->consumeMotionUp();
9133 mUnfocusedWindow->consumeMotionOutside();
9134
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009135 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
9136 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009137
9138 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07009139 ASSERT_THAT(responsiveTokens,
9140 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
9141 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009142 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009143}
9144
9145// If a window is already not responding, the second tap on the same window should be ignored.
9146// We should also log an error to account for the dropped event (not tested here).
9147// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
9148TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
9149 tapOnFocusedWindow();
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009150 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009151 // Receive the events, but don't respond
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009152 const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009153 ASSERT_TRUE(downEventSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009154 const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009155 ASSERT_TRUE(upEventSequenceNum);
9156 const std::chrono::duration timeout =
9157 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009158 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009159
9160 // Tap once again
9161 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009162 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009163 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009164 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009165 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009166 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009167 FOCUSED_WINDOW_LOCATION));
9168 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
9169 // valid touch target
9170 mUnfocusedWindow->assertNoEvents();
9171
9172 // Consume the first tap
9173 mFocusedWindow->finishEvent(*downEventSequenceNum);
9174 mFocusedWindow->finishEvent(*upEventSequenceNum);
9175 ASSERT_TRUE(mDispatcher->waitForIdle());
9176 // The second tap did not go to the focused window
9177 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009178 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08009179 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9180 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009181 mFakePolicy->assertNotifyAnrWasNotCalled();
9182}
9183
9184// If you tap outside of all windows, there will not be ANR
9185TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009186 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009187 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009188 LOCATION_OUTSIDE_ALL_WINDOWS));
9189 ASSERT_TRUE(mDispatcher->waitForIdle());
9190 mFakePolicy->assertNotifyAnrWasNotCalled();
9191}
9192
9193// Since the focused window is paused, tapping on it should not produce any events
9194TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
9195 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009196 mDispatcher->onWindowInfosChanged(
9197 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009198
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009199 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009200 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009201 FOCUSED_WINDOW_LOCATION));
9202
9203 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
9204 ASSERT_TRUE(mDispatcher->waitForIdle());
9205 // Should not ANR because the window is paused, and touches shouldn't go to it
9206 mFakePolicy->assertNotifyAnrWasNotCalled();
9207
9208 mFocusedWindow->assertNoEvents();
9209 mUnfocusedWindow->assertNoEvents();
9210}
9211
9212/**
9213 * 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 -07009214 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009215 * If a different window becomes focused at this time, the key should go to that window instead.
9216 *
9217 * Warning!!!
9218 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
9219 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009220 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009221 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
9222 *
9223 * If that value changes, this test should also change.
9224 */
9225TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
9226 // Set a long ANR timeout to prevent it from triggering
9227 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009228 mDispatcher->onWindowInfosChanged(
9229 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009230
9231 tapOnUnfocusedWindow();
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009232 const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009233 ASSERT_TRUE(downSequenceNum);
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009234 const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009235 ASSERT_TRUE(upSequenceNum);
9236 // Don't finish the events yet, and send a key
9237 // Injection will succeed because we will eventually give up and send the key to the focused
9238 // window even if motions are still being processed.
9239
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009240 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009241 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9242 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009244 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009245 // and the key remains pending, waiting for the touch events to be processed.
9246 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
9247 // under the hood.
9248 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
9249 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009250
9251 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07009252 mFocusedWindow->setFocusable(false);
9253 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009254 mDispatcher->onWindowInfosChanged(
9255 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009256 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009257
9258 // Focus events should precede the key events
9259 mUnfocusedWindow->consumeFocusEvent(true);
9260 mFocusedWindow->consumeFocusEvent(false);
9261
9262 // Finish the tap events, which should unblock dispatcher
9263 mUnfocusedWindow->finishEvent(*downSequenceNum);
9264 mUnfocusedWindow->finishEvent(*upSequenceNum);
9265
9266 // Now that all queues are cleared and no backlog in the connections, the key event
9267 // can finally go to the newly focused "mUnfocusedWindow".
9268 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9269 mFocusedWindow->assertNoEvents();
9270 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009271 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009272}
9273
9274// When the touch stream is split across 2 windows, and one of them does not respond,
9275// then ANR should be raised and the touch should be canceled for the unresponsive window.
9276// The other window should not be affected by that.
9277TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
9278 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00009279 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9280 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9281 {FOCUSED_WINDOW_LOCATION}));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +00009282 mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009283
9284 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00009285 mDispatcher->notifyMotion(
9286 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9287 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009288
9289 const std::chrono::duration timeout =
9290 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08009291 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009292
9293 mUnfocusedWindow->consumeMotionDown();
9294 mFocusedWindow->consumeMotionDown();
9295 // Focused window may or may not receive ACTION_MOVE
9296 // But it should definitely receive ACTION_CANCEL due to the ANR
Siarhei Vishniakoud3061ab2023-12-18 20:41:08 -08009297 const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009298 ASSERT_TRUE(moveOrCancelSequenceNum);
9299 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
9300 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07009301 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009302 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
9303 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
9304 mFocusedWindow->consumeMotionCancel();
9305 } else {
9306 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
9307 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009308 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08009309 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
9310 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009311
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009312 mUnfocusedWindow->assertNoEvents();
9313 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05009314 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07009315}
9316
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009317/**
9318 * If we have no focused window, and a key comes in, we start the ANR timer.
9319 * The focused application should add a focused window before the timer runs out to prevent ANR.
9320 *
9321 * If the user touches another application during this time, the key should be dropped.
9322 * Next, if a new focused window comes in, without toggling the focused application,
9323 * then no ANR should occur.
9324 *
9325 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
9326 * but in some cases the policy may not update the focused application.
9327 */
9328TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
9329 std::shared_ptr<FakeApplicationHandle> focusedApplication =
9330 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07009331 focusedApplication->setDispatchingTimeout(300ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009332 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
9333 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
9334 mFocusedWindow->setFocusable(false);
9335
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009336 mDispatcher->onWindowInfosChanged(
9337 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009338 mFocusedWindow->consumeFocusEvent(false);
9339
9340 // Send a key. The ANR timer should start because there is no focused window.
9341 // 'focusedApplication' will get blamed if this timer completes.
9342 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009343 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009344 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9345 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +00009346 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009347 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009348
9349 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
9350 // then the injected touches won't cause the focused event to get dropped.
9351 // The dispatcher only checks for whether the queue should be pruned upon queueing.
9352 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
9353 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
9354 // For this test, it means that the key would get delivered to the window once it becomes
9355 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009356 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009357
9358 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00009359 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9360 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9361 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009362
9363 // We do not consume the motion right away, because that would require dispatcher to first
9364 // process (== drop) the key event, and by that time, ANR will be raised.
9365 // Set the focused window first.
9366 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009367 mDispatcher->onWindowInfosChanged(
9368 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05009369 setFocusedWindow(mFocusedWindow);
9370 mFocusedWindow->consumeFocusEvent(true);
9371 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
9372 // to another application. This could be a bug / behaviour in the policy.
9373
9374 mUnfocusedWindow->consumeMotionDown();
9375
9376 ASSERT_TRUE(mDispatcher->waitForIdle());
9377 // Should not ANR because we actually have a focused window. It was just added too slowly.
9378 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
9379}
9380
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08009381/**
9382 * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having
9383 * an inconsistent event stream inside the dispatcher. In this test, we make sure that the
9384 * dispatcher doesn't prune pointer events incorrectly.
9385 *
9386 * This test reproduces a crash in InputDispatcher.
9387 * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur.
9388 *
9389 * Keep the currently focused application (mApplication), and have no focused window.
9390 * We set up two additional windows:
9391 * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the
9392 * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application
9393 * window. This window is not focusable, but is touchable.
9394 *
9395 * We first touch the navigation bar, which causes it to inject a key. Since there's no focused
9396 * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now
9397 * blocked. The dispatcher is waiting for 'mApplication' to add a focused window.
9398 *
9399 * Now, we touch "Another window". This window is owned by a different application than
9400 * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused
9401 * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start
9402 * dropping the events from its queue. Ensure that no crash occurs.
9403 *
9404 * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale.
9405 * This does not affect the test running time.
9406 */
9407TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) {
9408 std::shared_ptr<FakeApplicationHandle> systemUiApplication =
9409 std::make_shared<FakeApplicationHandle>();
9410 systemUiApplication->setDispatchingTimeout(3000ms);
9411 mFakePolicy->setStaleEventTimeout(3000ms);
9412 sp<FakeWindowHandle> navigationBar =
9413 sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar",
9414 ADISPLAY_ID_DEFAULT);
9415 navigationBar->setFocusable(false);
9416 navigationBar->setWatchOutsideTouch(true);
9417 navigationBar->setFrame(Rect(0, 0, 100, 100));
9418
9419 mApplication->setDispatchingTimeout(3000ms);
9420 // 'mApplication' is already focused, but we call it again here to make it explicit.
9421 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
9422
9423 std::shared_ptr<FakeApplicationHandle> anotherApplication =
9424 std::make_shared<FakeApplicationHandle>();
9425 sp<FakeWindowHandle> appWindow =
9426 sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window",
9427 ADISPLAY_ID_DEFAULT);
9428 appWindow->setFocusable(false);
9429 appWindow->setFrame(Rect(100, 100, 200, 200));
9430
9431 mDispatcher->onWindowInfosChanged(
9432 {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0});
9433 // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus
9434 mFocusedWindow->consumeFocusEvent(false);
9435
9436 // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher
9437 // in response.
9438 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9439 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9440 .build());
9441 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9442
9443 // Key will not be sent anywhere because we have no focused window. It will remain pending.
9444 // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is.
9445 InputEventInjectionResult result =
9446 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9447 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
9448 /*allowKeyRepeat=*/false);
9449 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
9450
9451 // Finish the gesture - lift up finger and inject ACTION_UP key event
9452 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
9453 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
9454 .build());
9455 result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
9456 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
9457 /*allowKeyRepeat=*/false);
9458 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
9459 // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be
9460 // getting any events yet.
9461 navigationBar->assertNoEvents();
9462
9463 // Now touch "Another window". This touch is going to a different application than the one we
9464 // are waiting for (which is 'mApplication').
9465 // This should cause the dispatcher to drop the pending focus-dispatched events (like the key
9466 // trying to be injected) and to continue processing the rest of the events in the original
9467 // order.
9468 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9469 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
9470 .build());
9471 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP));
9472 navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE));
9473 appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
9474
9475 appWindow->assertNoEvents();
9476 navigationBar->assertNoEvents();
9477}
9478
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009479// These tests ensure we cannot send touch events to a window that's positioned behind a window
9480// that has feature NO_INPUT_CHANNEL.
9481// Layout:
9482// Top (closest to user)
9483// mNoInputWindow (above all windows)
9484// mBottomWindow
9485// Bottom (furthest from user)
9486class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
9487 virtual void SetUp() override {
9488 InputDispatcherTest::SetUp();
9489
9490 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009491 mNoInputWindow =
9492 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
9493 "Window without input channel", ADISPLAY_ID_DEFAULT,
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00009494 /*createInputChannel=*/false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009495 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009496 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
9497 // It's perfectly valid for this window to not have an associated input channel
9498
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009499 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
9500 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009501 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
9502
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009503 mDispatcher->onWindowInfosChanged(
9504 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009505 }
9506
9507protected:
9508 std::shared_ptr<FakeApplicationHandle> mApplication;
9509 sp<FakeWindowHandle> mNoInputWindow;
9510 sp<FakeWindowHandle> mBottomWindow;
9511};
9512
9513TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
9514 PointF touchedPoint = {10, 10};
9515
Prabir Pradhan678438e2023-04-13 19:32:51 +00009516 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9517 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9518 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009519
9520 mNoInputWindow->assertNoEvents();
9521 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
9522 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
9523 // and therefore should prevent mBottomWindow from receiving touches
9524 mBottomWindow->assertNoEvents();
9525}
9526
9527/**
9528 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
9529 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
9530 */
9531TEST_F(InputDispatcherMultiWindowOcclusionTests,
9532 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009533 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
9534 "Window with input channel and NO_INPUT_CHANNEL",
9535 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009536
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009537 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009538 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009539 mDispatcher->onWindowInfosChanged(
9540 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009541
9542 PointF touchedPoint = {10, 10};
9543
Prabir Pradhan678438e2023-04-13 19:32:51 +00009544 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9545 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9546 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05009547
9548 mNoInputWindow->assertNoEvents();
9549 mBottomWindow->assertNoEvents();
9550}
9551
Vishnu Nair958da932020-08-21 17:12:37 -07009552class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
9553protected:
9554 std::shared_ptr<FakeApplicationHandle> mApp;
9555 sp<FakeWindowHandle> mWindow;
9556 sp<FakeWindowHandle> mMirror;
9557
9558 virtual void SetUp() override {
9559 InputDispatcherTest::SetUp();
9560 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009561 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhane7cc69c2024-01-05 21:35:28 +00009562 mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07009563 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
9564 mWindow->setFocusable(true);
9565 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009566 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009567 }
9568};
9569
9570TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
9571 // Request focus on a mirrored window
9572 setFocusedWindow(mMirror);
9573
9574 // window gets focused
9575 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009577 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009578 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
9579}
9580
9581// A focused & mirrored window remains focused only if the window and its mirror are both
9582// focusable.
9583TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
9584 setFocusedWindow(mMirror);
9585
9586 // window gets focused
9587 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009589 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009590 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009592 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009593 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9594
9595 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009596 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009597
9598 // window loses focus since one of the windows associated with the token in not focusable
9599 mWindow->consumeFocusEvent(false);
9600
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009601 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009602 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009603 mWindow->assertNoEvents();
9604}
9605
9606// A focused & mirrored window remains focused until the window and its mirror both become
9607// invisible.
9608TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
9609 setFocusedWindow(mMirror);
9610
9611 // window gets focused
9612 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009613 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009614 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009615 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009616 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009617 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009618 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9619
9620 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009621 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009622
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009623 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009624 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009625 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009626 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009627 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009628 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9629
9630 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009631 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009632
9633 // window loses focus only after all windows associated with the token become invisible.
9634 mWindow->consumeFocusEvent(false);
9635
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009636 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009637 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009638 mWindow->assertNoEvents();
9639}
9640
9641// A focused & mirrored window remains focused until both windows are removed.
9642TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
9643 setFocusedWindow(mMirror);
9644
9645 // window gets focused
9646 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009647 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009648 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009649 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009651 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009652 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9653
9654 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009655 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009656
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009658 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009659 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009660 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009661 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07009662 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
9663
9664 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009665 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009666 mWindow->consumeFocusEvent(false);
9667
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009668 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009669 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07009670 mWindow->assertNoEvents();
9671}
9672
9673// Focus request can be pending until one window becomes visible.
9674TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
9675 // Request focus on an invisible mirror.
9676 mWindow->setVisible(false);
9677 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009678 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009679 setFocusedWindow(mMirror);
9680
9681 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08009682 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009683 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
9684 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07009685
9686 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009687 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07009688
9689 // window gets focused
9690 mWindow->consumeFocusEvent(true);
9691 // window gets the pending key event
9692 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
9693}
Prabir Pradhan99987712020-11-10 18:43:05 -08009694
9695class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
9696protected:
9697 std::shared_ptr<FakeApplicationHandle> mApp;
9698 sp<FakeWindowHandle> mWindow;
9699 sp<FakeWindowHandle> mSecondWindow;
9700
9701 void SetUp() override {
9702 InputDispatcherTest::SetUp();
9703 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009704 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08009705 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009706 mSecondWindow =
9707 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08009708 mSecondWindow->setFocusable(true);
9709
9710 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009711 mDispatcher->onWindowInfosChanged(
9712 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -08009713
9714 setFocusedWindow(mWindow);
9715 mWindow->consumeFocusEvent(true);
9716 }
9717
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009718 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009719 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08009720 }
9721
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009722 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
9723 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08009724 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009725 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
9726 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08009727 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009728 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08009729 }
9730};
9731
9732TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
9733 // Ensure that capture cannot be obtained for unfocused windows.
9734 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
9735 mFakePolicy->assertSetPointerCaptureNotCalled();
9736 mSecondWindow->assertNoEvents();
9737
9738 // Ensure that capture can be enabled from the focus window.
9739 requestAndVerifyPointerCapture(mWindow, true);
9740
9741 // Ensure that capture cannot be disabled from a window that does not have capture.
9742 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
9743 mFakePolicy->assertSetPointerCaptureNotCalled();
9744
9745 // Ensure that capture can be disabled from the window with capture.
9746 requestAndVerifyPointerCapture(mWindow, false);
9747}
9748
9749TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009750 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08009751
9752 setFocusedWindow(mSecondWindow);
9753
9754 // Ensure that the capture disabled event was sent first.
9755 mWindow->consumeCaptureEvent(false);
9756 mWindow->consumeFocusEvent(false);
9757 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009758 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08009759
9760 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009761 notifyPointerCaptureChanged({});
9762 notifyPointerCaptureChanged(request);
9763 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08009764 mWindow->assertNoEvents();
9765 mSecondWindow->assertNoEvents();
9766 mFakePolicy->assertSetPointerCaptureNotCalled();
9767}
9768
9769TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009770 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08009771
9772 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009773 notifyPointerCaptureChanged({});
9774 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08009775
9776 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009777 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08009778 mWindow->consumeCaptureEvent(false);
9779 mWindow->assertNoEvents();
9780}
9781
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009782TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
9783 requestAndVerifyPointerCapture(mWindow, true);
9784
9785 // The first window loses focus.
9786 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009787 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009788 mWindow->consumeCaptureEvent(false);
9789
9790 // Request Pointer Capture from the second window before the notification from InputReader
9791 // arrives.
9792 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009793 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009794
9795 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009796 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009797
9798 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009799 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08009800
9801 mSecondWindow->consumeFocusEvent(true);
9802 mSecondWindow->consumeCaptureEvent(true);
9803}
9804
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00009805TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
9806 // App repeatedly enables and disables capture.
9807 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9808 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9809 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
9810 mFakePolicy->assertSetPointerCaptureCalled(false);
9811 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9812 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9813
9814 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
9815 // first request is now stale, this should do nothing.
9816 notifyPointerCaptureChanged(firstRequest);
9817 mWindow->assertNoEvents();
9818
9819 // InputReader notifies that the second request was enabled.
9820 notifyPointerCaptureChanged(secondRequest);
9821 mWindow->consumeCaptureEvent(true);
9822}
9823
Prabir Pradhan7092e262022-05-03 16:51:09 +00009824TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
9825 requestAndVerifyPointerCapture(mWindow, true);
9826
9827 // App toggles pointer capture off and on.
9828 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
9829 mFakePolicy->assertSetPointerCaptureCalled(false);
9830
9831 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
9832 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
9833
9834 // InputReader notifies that the latest "enable" request was processed, while skipping over the
9835 // preceding "disable" request.
9836 notifyPointerCaptureChanged(enableRequest);
9837
9838 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
9839 // any notifications.
9840 mWindow->assertNoEvents();
9841}
9842
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07009843/**
9844 * One window. Hover mouse in the window, and then start capture. Make sure that the relative
9845 * mouse movements don't affect the previous mouse hovering state.
9846 * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no
9847 * HOVER_MOVE events).
9848 */
9849TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) {
9850 // Mouse hover on the window
9851 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
9852 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
9853 .build());
9854 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
9855 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
9856 .build());
9857
9858 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER)));
9859 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE)));
9860
9861 // Start pointer capture
9862 requestAndVerifyPointerCapture(mWindow, true);
9863
9864 // Send some relative mouse movements and receive them in the window.
9865 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE)
9866 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11))
9867 .build());
9868 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11),
9869 WithSource(AINPUT_SOURCE_MOUSE_RELATIVE)));
9870
9871 // Stop pointer capture
9872 requestAndVerifyPointerCapture(mWindow, false);
9873
9874 // Continue hovering on the window
9875 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
9876 .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115))
9877 .build());
9878 mWindow->consumeMotionEvent(
9879 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
9880
9881 mWindow->assertNoEvents();
9882}
9883
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009884class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
9885protected:
9886 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00009887
9888 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
9889 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
9890
9891 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
9892 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
9893
9894 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
9895 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
9896 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
9897 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
9898 MAXIMUM_OBSCURING_OPACITY);
9899
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009900 static constexpr gui::Uid TOUCHED_APP_UID{10001};
9901 static constexpr gui::Uid APP_B_UID{10002};
9902 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009903
9904 sp<FakeWindowHandle> mTouchWindow;
9905
9906 virtual void SetUp() override {
9907 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009908 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009909 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
9910 }
9911
9912 virtual void TearDown() override {
9913 InputDispatcherTest::TearDown();
9914 mTouchWindow.clear();
9915 }
9916
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009917 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -05009918 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009919 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009920 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009921 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009922 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009923 return window;
9924 }
9925
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009926 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009927 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
9928 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009929 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009930 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009931 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009932 return window;
9933 }
9934
9935 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009936 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9937 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9938 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009939 }
9940};
9941
9942TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009943 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009944 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009945 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009946
9947 touch();
9948
9949 mTouchWindow->assertNoEvents();
9950}
9951
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009952TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00009953 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
9954 const sp<FakeWindowHandle>& w =
9955 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009956 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00009957
9958 touch();
9959
9960 mTouchWindow->assertNoEvents();
9961}
9962
9963TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009964 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
9965 const sp<FakeWindowHandle>& w =
9966 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009967 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009968
9969 touch();
9970
9971 w->assertNoEvents();
9972}
9973
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009974TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009975 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009976 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009977
9978 touch();
9979
9980 mTouchWindow->consumeAnyMotionDown();
9981}
9982
9983TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009984 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009985 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009986 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009987 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009988
9989 touch({PointF{100, 100}});
9990
9991 mTouchWindow->consumeAnyMotionDown();
9992}
9993
9994TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009995 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009996 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009997 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009998
9999 touch();
10000
10001 mTouchWindow->consumeAnyMotionDown();
10002}
10003
10004TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
10005 const sp<FakeWindowHandle>& w =
10006 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010007 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010008
10009 touch();
10010
10011 mTouchWindow->consumeAnyMotionDown();
10012}
10013
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010014TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
10015 const sp<FakeWindowHandle>& w =
10016 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010017 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010018
10019 touch();
10020
10021 w->assertNoEvents();
10022}
10023
10024/**
10025 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
10026 * inside) while letting them pass-through. Note that even though touch passes through the occluding
10027 * window, the occluding window will still receive ACTION_OUTSIDE event.
10028 */
10029TEST_F(InputDispatcherUntrustedTouchesTest,
10030 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
10031 const sp<FakeWindowHandle>& w =
10032 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010033 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010034 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010035
10036 touch();
10037
10038 w->consumeMotionOutside();
10039}
10040
10041TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
10042 const sp<FakeWindowHandle>& w =
10043 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010044 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010045 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010046
10047 touch();
10048
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080010049 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +000010050}
10051
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010052TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010053 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010054 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10055 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010056 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010057
10058 touch();
10059
10060 mTouchWindow->consumeAnyMotionDown();
10061}
10062
10063TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
10064 const sp<FakeWindowHandle>& w =
10065 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10066 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010067 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010068
10069 touch();
10070
10071 mTouchWindow->consumeAnyMotionDown();
10072}
10073
10074TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010075 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010076 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10077 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010078 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010079
10080 touch();
10081
10082 mTouchWindow->assertNoEvents();
10083}
10084
10085TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
10086 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
10087 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010088 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
10089 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010090 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010091 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
10092 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010093 mDispatcher->onWindowInfosChanged(
10094 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010095
10096 touch();
10097
10098 mTouchWindow->assertNoEvents();
10099}
10100
10101TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
10102 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
10103 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010104 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
10105 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010106 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010107 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
10108 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010109 mDispatcher->onWindowInfosChanged(
10110 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010111
10112 touch();
10113
10114 mTouchWindow->consumeAnyMotionDown();
10115}
10116
10117TEST_F(InputDispatcherUntrustedTouchesTest,
10118 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
10119 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010120 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10121 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010122 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010123 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10124 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010125 mDispatcher->onWindowInfosChanged(
10126 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010127
10128 touch();
10129
10130 mTouchWindow->consumeAnyMotionDown();
10131}
10132
10133TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
10134 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010135 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10136 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +000010137 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010138 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10139 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010140 mDispatcher->onWindowInfosChanged(
10141 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +000010142
10143 touch();
10144
10145 mTouchWindow->assertNoEvents();
10146}
10147
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010148TEST_F(InputDispatcherUntrustedTouchesTest,
10149 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
10150 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010151 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10152 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010153 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010154 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10155 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010156 mDispatcher->onWindowInfosChanged(
10157 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010158
10159 touch();
10160
10161 mTouchWindow->assertNoEvents();
10162}
10163
10164TEST_F(InputDispatcherUntrustedTouchesTest,
10165 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
10166 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010167 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10168 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010169 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010170 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10171 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010172 mDispatcher->onWindowInfosChanged(
10173 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010174
10175 touch();
10176
10177 mTouchWindow->consumeAnyMotionDown();
10178}
10179
10180TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
10181 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010182 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
10183 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010184 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010185
10186 touch();
10187
10188 mTouchWindow->consumeAnyMotionDown();
10189}
10190
10191TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
10192 const sp<FakeWindowHandle>& w =
10193 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010194 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +000010195
10196 touch();
10197
10198 mTouchWindow->consumeAnyMotionDown();
10199}
10200
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010201TEST_F(InputDispatcherUntrustedTouchesTest,
10202 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
10203 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
10204 const sp<FakeWindowHandle>& w =
10205 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010206 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010207
10208 touch();
10209
10210 mTouchWindow->assertNoEvents();
10211}
10212
10213TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
10214 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
10215 const sp<FakeWindowHandle>& w =
10216 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010217 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010218
10219 touch();
10220
10221 mTouchWindow->consumeAnyMotionDown();
10222}
10223
10224TEST_F(InputDispatcherUntrustedTouchesTest,
10225 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
10226 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
10227 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +000010228 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10229 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010230 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010231
10232 touch();
10233
10234 mTouchWindow->consumeAnyMotionDown();
10235}
10236
10237TEST_F(InputDispatcherUntrustedTouchesTest,
10238 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
10239 const sp<FakeWindowHandle>& w1 =
10240 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
10241 OPACITY_BELOW_THRESHOLD);
10242 const sp<FakeWindowHandle>& w2 =
10243 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
10244 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010245 mDispatcher->onWindowInfosChanged(
10246 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010247
10248 touch();
10249
10250 mTouchWindow->assertNoEvents();
10251}
10252
10253/**
10254 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
10255 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
10256 * (which alone would result in allowing touches) does not affect the blocking behavior.
10257 */
10258TEST_F(InputDispatcherUntrustedTouchesTest,
10259 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
10260 const sp<FakeWindowHandle>& wB =
10261 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
10262 OPACITY_BELOW_THRESHOLD);
10263 const sp<FakeWindowHandle>& wC =
10264 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
10265 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010266 mDispatcher->onWindowInfosChanged(
10267 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +000010268
10269 touch();
10270
10271 mTouchWindow->assertNoEvents();
10272}
10273
10274/**
10275 * This test is testing that a window from a different UID but with same application token doesn't
10276 * block the touch. Apps can share the application token for close UI collaboration for example.
10277 */
10278TEST_F(InputDispatcherUntrustedTouchesTest,
10279 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
10280 const sp<FakeWindowHandle>& w =
10281 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
10282 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010283 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +000010284
10285 touch();
10286
10287 mTouchWindow->consumeAnyMotionDown();
10288}
10289
arthurhungb89ccb02020-12-30 16:19:01 +080010290class InputDispatcherDragTests : public InputDispatcherTest {
10291protected:
10292 std::shared_ptr<FakeApplicationHandle> mApp;
10293 sp<FakeWindowHandle> mWindow;
10294 sp<FakeWindowHandle> mSecondWindow;
10295 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010296 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010297 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
10298 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +080010299
10300 void SetUp() override {
10301 InputDispatcherTest::SetUp();
10302 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010303 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080010304 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080010305
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010306 mSecondWindow =
10307 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +080010308 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +080010309
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010310 mSpyWindow =
10311 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010312 mSpyWindow->setSpy(true);
10313 mSpyWindow->setTrustedOverlay(true);
10314 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
10315
arthurhungb89ccb02020-12-30 16:19:01 +080010316 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010317 mDispatcher->onWindowInfosChanged(
10318 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
10319 {},
10320 0,
10321 0});
arthurhungb89ccb02020-12-30 16:19:01 +080010322 }
10323
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010324 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
10325 switch (fromSource) {
10326 case AINPUT_SOURCE_TOUCHSCREEN:
10327 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010328 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010329 ADISPLAY_ID_DEFAULT, {50, 50}))
10330 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10331 break;
10332 case AINPUT_SOURCE_STYLUS:
10333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010334 injectMotionEvent(*mDispatcher,
10335 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10336 AINPUT_SOURCE_STYLUS)
10337 .buttonState(
10338 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
10339 .pointer(PointerBuilder(0, ToolType::STYLUS)
10340 .x(50)
10341 .y(50))
10342 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010343 break;
10344 case AINPUT_SOURCE_MOUSE:
10345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010346 injectMotionEvent(*mDispatcher,
10347 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10348 AINPUT_SOURCE_MOUSE)
10349 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
10350 .pointer(PointerBuilder(MOUSE_POINTER_ID,
10351 ToolType::MOUSE)
10352 .x(50)
10353 .y(50))
10354 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010355 break;
10356 default:
10357 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
10358 }
arthurhungb89ccb02020-12-30 16:19:01 +080010359
10360 // Window should receive motion event.
10361 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010362 // Spy window should also receive motion event
10363 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +000010364 }
10365
10366 // Start performing drag, we will create a drag window and transfer touch to it.
10367 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
10368 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010369 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +000010370 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010371 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +000010372 }
arthurhungb89ccb02020-12-30 16:19:01 +080010373
10374 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010375 mDragWindow =
10376 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010377 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010378 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
10379 *mWindow->getInfo(), *mSecondWindow->getInfo()},
10380 {},
10381 0,
10382 0});
arthurhungb89ccb02020-12-30 16:19:01 +080010383
10384 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +000010385 bool transferred =
10386 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +000010387 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +000010388 if (transferred) {
10389 mWindow->consumeMotionCancel();
10390 mDragWindow->consumeMotionDown();
10391 }
10392 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +080010393 }
10394};
10395
10396TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010397 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +080010398
10399 // Move on window.
10400 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010401 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010402 ADISPLAY_ID_DEFAULT, {50, 50}))
10403 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10404 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10405 mWindow->consumeDragEvent(false, 50, 50);
10406 mSecondWindow->assertNoEvents();
10407
10408 // Move to another window.
10409 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010410 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010411 ADISPLAY_ID_DEFAULT, {150, 50}))
10412 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10413 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10414 mWindow->consumeDragEvent(true, 150, 50);
10415 mSecondWindow->consumeDragEvent(false, 50, 50);
10416
10417 // Move back to original window.
10418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010419 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +080010420 ADISPLAY_ID_DEFAULT, {50, 50}))
10421 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10422 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10423 mWindow->consumeDragEvent(false, 50, 50);
10424 mSecondWindow->consumeDragEvent(true, -50, 50);
10425
10426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010427 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10428 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +080010429 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10430 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
10431 mWindow->assertNoEvents();
10432 mSecondWindow->assertNoEvents();
10433}
10434
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010435TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010436 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010437
10438 // No cancel event after drag start
10439 mSpyWindow->assertNoEvents();
10440
10441 const MotionEvent secondFingerDownEvent =
10442 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10443 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010444 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10445 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010446 .build();
10447 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010448 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +000010449 InputEventInjectionSync::WAIT_FOR_RESULT))
10450 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10451
10452 // Receives cancel for first pointer after next pointer down
10453 mSpyWindow->consumeMotionCancel();
10454 mSpyWindow->consumeMotionDown();
10455
10456 mSpyWindow->assertNoEvents();
10457}
10458
arthurhungf452d0b2021-01-06 00:19:52 +080010459TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010460 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +080010461
10462 // Move on window.
10463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010464 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +080010465 ADISPLAY_ID_DEFAULT, {50, 50}))
10466 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10467 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10468 mWindow->consumeDragEvent(false, 50, 50);
10469 mSecondWindow->assertNoEvents();
10470
10471 // Move to another window.
10472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010473 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +080010474 ADISPLAY_ID_DEFAULT, {150, 50}))
10475 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10476 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10477 mWindow->consumeDragEvent(true, 150, 50);
10478 mSecondWindow->consumeDragEvent(false, 50, 50);
10479
10480 // drop to another window.
10481 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010482 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +080010483 {150, 50}))
10484 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10485 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010486 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +080010487 mWindow->assertNoEvents();
10488 mSecondWindow->assertNoEvents();
10489}
10490
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010491TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) {
10492 startDrag();
10493
10494 // No cancel event after drag start
10495 mSpyWindow->assertNoEvents();
10496
10497 const MotionEvent secondFingerDownEvent =
10498 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10499 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
10500 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10501 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
10502 .build();
10503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10504 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
10505 InputEventInjectionSync::WAIT_FOR_RESULT))
10506 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10507
10508 // Receives cancel for first pointer after next pointer down
10509 mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -080010510 mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1})));
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010511 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
10512
10513 mSpyWindow->assertNoEvents();
10514
10515 // Spy window calls pilfer pointers
10516 EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken()));
10517 mDragWindow->assertNoEvents();
10518
10519 const MotionEvent firstFingerMoveEvent =
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080010520 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010521 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
10522 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60))
10523 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
10524 .build();
10525 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou96a15572023-12-18 10:47:52 -080010526 injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari110ba322023-11-17 10:47:16 +000010527 InputEventInjectionSync::WAIT_FOR_RESULT))
10528 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10529
10530 // Drag window should still receive the new event
10531 mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
10532 mDragWindow->assertNoEvents();
10533}
10534
arthurhung6d4bed92021-03-17 11:59:33 +080010535TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010536 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +080010537
10538 // Move on window and keep button pressed.
10539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010540 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010541 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10542 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010543 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010544 .build()))
10545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10546 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10547 mWindow->consumeDragEvent(false, 50, 50);
10548 mSecondWindow->assertNoEvents();
10549
10550 // Move to another window and release button, expect to drop item.
10551 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010552 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010553 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10554 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010555 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010556 .build()))
10557 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10558 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10559 mWindow->assertNoEvents();
10560 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010561 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +080010562
10563 // nothing to the window.
10564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010565 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +080010566 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
10567 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010568 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +080010569 .build()))
10570 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10571 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
10572 mWindow->assertNoEvents();
10573 mSecondWindow->assertNoEvents();
10574}
10575
Arthur Hung54745652022-04-20 07:17:41 +000010576TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010577 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +080010578
10579 // Set second window invisible.
10580 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010581 mDispatcher->onWindowInfosChanged(
10582 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +080010583
10584 // Move on window.
10585 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010586 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010587 ADISPLAY_ID_DEFAULT, {50, 50}))
10588 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10589 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10590 mWindow->consumeDragEvent(false, 50, 50);
10591 mSecondWindow->assertNoEvents();
10592
10593 // Move to another window.
10594 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010595 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010596 ADISPLAY_ID_DEFAULT, {150, 50}))
10597 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10598 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10599 mWindow->consumeDragEvent(true, 150, 50);
10600 mSecondWindow->assertNoEvents();
10601
10602 // drop to another window.
10603 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010604 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +080010605 {150, 50}))
10606 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10607 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010608 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +080010609 mWindow->assertNoEvents();
10610 mSecondWindow->assertNoEvents();
10611}
10612
Arthur Hung54745652022-04-20 07:17:41 +000010613TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010614 // Ensure window could track pointerIds if it didn't support split touch.
10615 mWindow->setPreventSplitting(true);
10616
Arthur Hung54745652022-04-20 07:17:41 +000010617 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010618 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +000010619 {50, 50}))
10620 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10621 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10622
10623 const MotionEvent secondFingerDownEvent =
10624 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10625 .displayId(ADISPLAY_ID_DEFAULT)
10626 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010627 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10628 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010629 .build();
10630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010631 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010632 InputEventInjectionSync::WAIT_FOR_RESULT))
10633 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000010634 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +000010635
10636 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010637 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000010638}
10639
10640TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
10641 // First down on second window.
10642 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010643 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +000010644 {150, 50}))
10645 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10646
10647 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10648
10649 // Second down on first window.
10650 const MotionEvent secondFingerDownEvent =
10651 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10652 .displayId(ADISPLAY_ID_DEFAULT)
10653 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010654 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10655 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010656 .build();
10657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010658 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010659 InputEventInjectionSync::WAIT_FOR_RESULT))
10660 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10661 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10662
10663 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010664 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +000010665
10666 // Move on window.
10667 const MotionEvent secondFingerMoveEvent =
10668 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10669 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010670 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10671 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010672 .build();
10673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010674 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010675 InputEventInjectionSync::WAIT_FOR_RESULT));
10676 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10677 mWindow->consumeDragEvent(false, 50, 50);
10678 mSecondWindow->consumeMotionMove();
10679
10680 // Release the drag pointer should perform drop.
10681 const MotionEvent secondFingerUpEvent =
10682 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
10683 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010684 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10685 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +000010686 .build();
10687 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010688 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +000010689 InputEventInjectionSync::WAIT_FOR_RESULT));
10690 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010691 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +000010692 mWindow->assertNoEvents();
10693 mSecondWindow->consumeMotionMove();
10694}
10695
Arthur Hung3915c1f2022-05-31 07:17:17 +000010696TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010697 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +000010698
10699 // Update window of second display.
10700 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010701 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010702 mDispatcher->onWindowInfosChanged(
10703 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
10704 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
10705 {},
10706 0,
10707 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000010708
10709 // Let second display has a touch state.
10710 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010711 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010712 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
10713 AINPUT_SOURCE_TOUCHSCREEN)
10714 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010715 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +000010716 .build()));
Prabir Pradhan7662a8d2023-12-15 01:58:14 +000010717 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +000010718 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010719 mDispatcher->onWindowInfosChanged(
10720 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
10721 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
10722 {},
10723 0,
10724 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +000010725
10726 // Move on window.
10727 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010728 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010729 ADISPLAY_ID_DEFAULT, {50, 50}))
10730 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10731 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10732 mWindow->consumeDragEvent(false, 50, 50);
10733 mSecondWindow->assertNoEvents();
10734
10735 // Move to another window.
10736 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010737 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010738 ADISPLAY_ID_DEFAULT, {150, 50}))
10739 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10740 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10741 mWindow->consumeDragEvent(true, 150, 50);
10742 mSecondWindow->consumeDragEvent(false, 50, 50);
10743
10744 // drop to another window.
10745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010746 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +000010747 {150, 50}))
10748 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10749 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010750 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +000010751 mWindow->assertNoEvents();
10752 mSecondWindow->assertNoEvents();
10753}
10754
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010755TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
10756 startDrag(true, AINPUT_SOURCE_MOUSE);
10757 // Move on window.
10758 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010759 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010760 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
10761 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010762 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010763 .x(50)
10764 .y(50))
10765 .build()))
10766 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10767 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10768 mWindow->consumeDragEvent(false, 50, 50);
10769 mSecondWindow->assertNoEvents();
10770
10771 // Move to another window.
10772 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010773 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010774 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
10775 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010776 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010777 .x(150)
10778 .y(50))
10779 .build()))
10780 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10781 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
10782 mWindow->consumeDragEvent(true, 150, 50);
10783 mSecondWindow->consumeDragEvent(false, 50, 50);
10784
10785 // drop to another window.
10786 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010787 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010788 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
10789 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010790 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010791 .x(150)
10792 .y(50))
10793 .build()))
10794 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10795 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -070010796 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +000010797 mWindow->assertNoEvents();
10798 mSecondWindow->assertNoEvents();
10799}
10800
Linnan Li5af92f92023-07-14 14:36:22 +080010801/**
10802 * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag
10803 * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash.
10804 */
10805TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) {
10806 // Down on second window
10807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10808 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10809 {150, 50}))
10810 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10811
10812 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown());
10813 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown());
10814
10815 // Down on first window
10816 const MotionEvent secondFingerDownEvent =
10817 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10818 .displayId(ADISPLAY_ID_DEFAULT)
10819 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
10820 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
10821 .build();
10822 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10823 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
10824 InputEventInjectionSync::WAIT_FOR_RESULT))
10825 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10826 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
10827 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove());
10828 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1));
10829
10830 // Start drag on first window
10831 ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN));
10832
10833 // Trigger cancel
10834 mDispatcher->cancelCurrentTouch();
10835 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel());
10836 ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel());
10837 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel());
10838
10839 ASSERT_TRUE(mDispatcher->waitForIdle());
10840 // The D&D finished with nullptr
10841 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
10842
10843 // Remove drag window
10844 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
10845
10846 // Inject a simple gesture, ensure dispatcher not crashed
10847 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10848 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10849 PointF{50, 50}))
10850 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10851 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
10852
10853 const MotionEvent moveEvent =
10854 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10855 .displayId(ADISPLAY_ID_DEFAULT)
10856 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10857 .build();
10858 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10859 injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT,
10860 InputEventInjectionSync::WAIT_FOR_RESULT))
10861 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10862 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove());
10863
10864 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10865 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
10866 {50, 50}))
10867 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10868 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp());
10869}
10870
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000010871TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) {
10872 // Start hovering over the window.
10873 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10874 injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE,
10875 ADISPLAY_ID_DEFAULT, {50, 50}));
10876
10877 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
10878 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)));
10879
10880 ASSERT_FALSE(startDrag(/*sendDown=*/false))
10881 << "Drag and drop should not work with a hovering pointer";
10882}
10883
Vishnu Nair062a8672021-09-03 16:07:44 -070010884class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
10885
10886TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
10887 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010888 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
10889 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010890 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -070010891 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
10892 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010893 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070010894 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000010895 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070010896
10897 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000010898 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010899 window->assertNoEvents();
10900
Prabir Pradhan678438e2023-04-13 19:32:51 +000010901 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10902 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070010903 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
10904 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud908f5a2023-11-16 10:25:12 -080010905 mDispatcher->waitForIdle();
Vishnu Nair062a8672021-09-03 16:07:44 -070010906 window->assertNoEvents();
10907
10908 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010909 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010910 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070010911
Prabir Pradhan678438e2023-04-13 19:32:51 +000010912 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010913 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
10914
Prabir Pradhan678438e2023-04-13 19:32:51 +000010915 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10916 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010917 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10918 window->assertNoEvents();
10919}
10920
10921TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
10922 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
10923 std::make_shared<FakeApplicationHandle>();
10924 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010925 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
10926 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070010927 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010928 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010929 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070010930 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010931 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
10932 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010933 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010934 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070010935 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
10936 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010937 mDispatcher->onWindowInfosChanged(
10938 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070010939 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000010940 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070010941
10942 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000010943 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010944 window->assertNoEvents();
10945
Prabir Pradhan678438e2023-04-13 19:32:51 +000010946 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10947 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070010948 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
10949 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010950 window->assertNoEvents();
10951
10952 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010953 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010954 mDispatcher->onWindowInfosChanged(
10955 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070010956
Prabir Pradhan678438e2023-04-13 19:32:51 +000010957 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010958 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
10959
Prabir Pradhan678438e2023-04-13 19:32:51 +000010960 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10961 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010962 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
10963 window->assertNoEvents();
10964}
10965
10966TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
10967 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
10968 std::make_shared<FakeApplicationHandle>();
10969 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010970 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
10971 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070010972 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010973 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010974 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070010975 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010976 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
10977 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010978 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010979 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070010980 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
10981 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010982 mDispatcher->onWindowInfosChanged(
10983 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070010984 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000010985 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070010986
10987 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000010988 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010989 window->assertNoEvents();
10990
Prabir Pradhan678438e2023-04-13 19:32:51 +000010991 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10992 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070010993 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
10994 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010995 window->assertNoEvents();
10996
10997 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010998 mDispatcher->onWindowInfosChanged(
10999 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070011000
Prabir Pradhan678438e2023-04-13 19:32:51 +000011001 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011002 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
11003
Prabir Pradhan678438e2023-04-13 19:32:51 +000011004 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
11005 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070011006 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11007 window->assertNoEvents();
11008}
11009
Antonio Kantekf16f2832021-09-28 04:39:20 +000011010class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
11011protected:
11012 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +000011013 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +000011014 sp<FakeWindowHandle> mWindow;
11015 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +000011016 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +000011017
11018 void SetUp() override {
11019 InputDispatcherTest::SetUp();
11020
11021 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +000011022 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011023 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011024 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011025 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011026 mSecondWindow =
11027 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011028 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +000011029 mThirdWindow =
11030 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
11031 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
11032 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011033
11034 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011035 mDispatcher->onWindowInfosChanged(
11036 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
11037 {},
11038 0,
11039 0});
Antonio Kantek15beb512022-06-13 22:35:41 +000011040 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011041 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011042
Antonio Kantek15beb512022-06-13 22:35:41 +000011043 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +000011044 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000011045 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -070011046 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
11047 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000011048 mThirdWindow->assertNoEvents();
11049 }
11050
11051 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
11052 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000011053 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +000011054 SECOND_DISPLAY_ID)) {
11055 mWindow->assertNoEvents();
11056 mSecondWindow->assertNoEvents();
11057 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -070011058 }
Antonio Kantekf16f2832021-09-28 04:39:20 +000011059 }
11060
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011061 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +000011062 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -070011063 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
11064 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000011065 mWindow->consumeTouchModeEvent(inTouchMode);
11066 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000011067 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +000011068 }
11069};
11070
Antonio Kantek26defcf2022-02-08 01:12:27 +000011071TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080011072 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +000011073 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
11074 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011075 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +000011076}
11077
Antonio Kantek26defcf2022-02-08 01:12:27 +000011078TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
11079 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011080 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011081 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011082 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011083 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000011084 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -070011085 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +000011086 mWindow->assertNoEvents();
11087 mSecondWindow->assertNoEvents();
11088}
11089
11090TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
11091 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011092 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011093 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011094 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +000011095 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000011096 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000011097}
11098
Antonio Kantekf16f2832021-09-28 04:39:20 +000011099TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080011100 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +000011101 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
11102 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011103 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000011104 mWindow->assertNoEvents();
11105 mSecondWindow->assertNoEvents();
11106}
11107
Antonio Kantek15beb512022-06-13 22:35:41 +000011108TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
11109 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
11110 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
11111 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011112 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +000011113 mWindow->assertNoEvents();
11114 mSecondWindow->assertNoEvents();
11115 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
11116}
11117
Antonio Kantek48710e42022-03-24 14:19:30 -070011118TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
11119 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011120 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
11121 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -070011122 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11123 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
11124
11125 // Then remove focus.
11126 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011127 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -070011128
11129 // Assert that caller can switch touch mode by owning one of the last interacted window.
11130 const WindowInfo& windowInfo = *mWindow->getInfo();
11131 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
11132 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000011133 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -070011134}
11135
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011136class InputDispatcherSpyWindowTest : public InputDispatcherTest {
11137public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011138 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011139 std::shared_ptr<FakeApplicationHandle> application =
11140 std::make_shared<FakeApplicationHandle>();
11141 std::string name = "Fake Spy ";
11142 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011143 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
11144 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011145 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011146 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011147 return spy;
11148 }
11149
11150 sp<FakeWindowHandle> createForeground() {
11151 std::shared_ptr<FakeApplicationHandle> application =
11152 std::make_shared<FakeApplicationHandle>();
11153 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011154 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
11155 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011156 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011157 return window;
11158 }
11159
11160private:
11161 int mSpyCount{0};
11162};
11163
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011164using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011165/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011166 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
11167 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011168TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -070011169 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011170 ScopedSilentDeath _silentDeath;
11171
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011172 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011173 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011174 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -080011175 ".* not a trusted overlay");
11176}
11177
11178/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011179 * Input injection into a display with a spy window but no foreground windows should succeed.
11180 */
11181TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011182 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011183 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011184
11185 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011186 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011187 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11188 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11189}
11190
11191/**
11192 * Verify the order in which different input windows receive events. The touched foreground window
11193 * (if there is one) should always receive the event first. When there are multiple spy windows, the
11194 * spy windows will receive the event according to their Z-order, where the top-most spy window will
11195 * receive events before ones belows it.
11196 *
11197 * Here, we set up a scenario with four windows in the following Z order from the top:
11198 * spy1, spy2, window, spy3.
11199 * We then inject an event and verify that the foreground "window" receives it first, followed by
11200 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
11201 * window.
11202 */
11203TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
11204 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011205 auto spy1 = createSpy();
11206 auto spy2 = createSpy();
11207 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011208 mDispatcher->onWindowInfosChanged(
11209 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011210 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
11211 const size_t numChannels = channels.size();
11212
Michael Wright8e9a8562022-02-09 13:44:29 +000011213 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011214 if (!epollFd.ok()) {
11215 FAIL() << "Failed to create epoll fd";
11216 }
11217
11218 for (size_t i = 0; i < numChannels; i++) {
11219 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
11220 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
11221 FAIL() << "Failed to add fd to epoll";
11222 }
11223 }
11224
11225 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011226 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011227 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11228
11229 std::vector<size_t> eventOrder;
11230 std::vector<struct epoll_event> events(numChannels);
11231 for (;;) {
11232 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
11233 (100ms).count());
11234 if (nFds < 0) {
11235 FAIL() << "Failed to call epoll_wait";
11236 }
11237 if (nFds == 0) {
11238 break; // epoll_wait timed out
11239 }
11240 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -070011241 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -070011242 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011243 channels[i]->consumeMotionDown();
11244 }
11245 }
11246
11247 // Verify the order in which the events were received.
11248 EXPECT_EQ(3u, eventOrder.size());
11249 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
11250 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
11251 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
11252}
11253
11254/**
11255 * A spy window using the NOT_TOUCHABLE flag does not receive events.
11256 */
11257TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
11258 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011259 auto spy = createSpy();
11260 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011261 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011262
11263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011264 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011265 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11266 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11267 spy->assertNoEvents();
11268}
11269
11270/**
11271 * A spy window will only receive gestures that originate within its touchable region. Gestures that
11272 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
11273 * to the window.
11274 */
11275TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
11276 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011277 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011278 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011279 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011280
11281 // Inject an event outside the spy window's touchable region.
11282 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011283 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011284 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11285 window->consumeMotionDown();
11286 spy->assertNoEvents();
11287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011288 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011289 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11290 window->consumeMotionUp();
11291 spy->assertNoEvents();
11292
11293 // Inject an event inside the spy window's touchable region.
11294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011295 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011296 {5, 10}))
11297 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11298 window->consumeMotionDown();
11299 spy->consumeMotionDown();
11300}
11301
11302/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011303 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011304 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011305 */
11306TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
11307 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011308 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011309 auto spy = createSpy();
11310 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011311 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011312 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011313 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011314
11315 // Inject an event outside the spy window's frame and touchable region.
11316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011317 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011318 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011319 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11320 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080011321 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011322}
11323
11324/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011325 * Even when a spy window spans over multiple foreground windows, the spy should receive all
11326 * pointers that are down within its bounds.
11327 */
11328TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
11329 auto windowLeft = createForeground();
11330 windowLeft->setFrame({0, 0, 100, 200});
11331 auto windowRight = createForeground();
11332 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011333 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011334 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011335 mDispatcher->onWindowInfosChanged(
11336 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011337
11338 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011339 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011340 {50, 50}))
11341 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11342 windowLeft->consumeMotionDown();
11343 spy->consumeMotionDown();
11344
11345 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011346 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011347 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011348 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11349 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011350 .build();
11351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011352 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011353 InputEventInjectionSync::WAIT_FOR_RESULT))
11354 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11355 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +000011356 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011357}
11358
11359/**
11360 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
11361 * the spy should receive the second pointer with ACTION_DOWN.
11362 */
11363TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
11364 auto window = createForeground();
11365 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011366 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011367 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011368 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011369
11370 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011371 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011372 {50, 50}))
11373 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11374 window->consumeMotionDown();
11375 spyRight->assertNoEvents();
11376
11377 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011378 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011379 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011380 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
11381 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011382 .build();
11383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011384 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011385 InputEventInjectionSync::WAIT_FOR_RESULT))
11386 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000011387 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080011388 spyRight->consumeMotionDown();
11389}
11390
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011391/**
11392 * The spy window should not be able to affect whether or not touches are split. Only the foreground
11393 * windows should be allowed to control split touch.
11394 */
11395TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080011396 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011397 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011398 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080011399 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011400
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011401 auto window = createForeground();
11402 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011403
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011404 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011405
11406 // First finger down, no window touched.
11407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011408 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011409 {100, 200}))
11410 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11411 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11412 window->assertNoEvents();
11413
11414 // Second finger down on window, the window should receive touch down.
11415 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080011416 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011417 .displayId(ADISPLAY_ID_DEFAULT)
11418 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011419 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11420 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011421 .build();
11422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011423 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011424 InputEventInjectionSync::WAIT_FOR_RESULT))
11425 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11426
11427 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +000011428 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011429}
11430
11431/**
11432 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
11433 * do not receive key events.
11434 */
11435TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011436 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011437 spy->setFocusable(false);
11438
11439 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011440 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011441 setFocusedWindow(window);
11442 window->consumeFocusEvent(true);
11443
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011444 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011445 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11446 window->consumeKeyDown(ADISPLAY_ID_NONE);
11447
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080011449 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
11450 window->consumeKeyUp(ADISPLAY_ID_NONE);
11451
11452 spy->assertNoEvents();
11453}
11454
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011455using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
11456
11457/**
11458 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
11459 * are currently sent to any other windows - including other spy windows - will also be cancelled.
11460 */
11461TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
11462 auto window = createForeground();
11463 auto spy1 = createSpy();
11464 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011465 mDispatcher->onWindowInfosChanged(
11466 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011467
11468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011469 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011470 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11471 window->consumeMotionDown();
11472 spy1->consumeMotionDown();
11473 spy2->consumeMotionDown();
11474
11475 // Pilfer pointers from the second spy window.
11476 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
11477 spy2->assertNoEvents();
11478 spy1->consumeMotionCancel();
11479 window->consumeMotionCancel();
11480
11481 // The rest of the gesture should only be sent to the second spy window.
11482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011483 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011484 ADISPLAY_ID_DEFAULT))
11485 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11486 spy2->consumeMotionMove();
11487 spy1->assertNoEvents();
11488 window->assertNoEvents();
11489}
11490
11491/**
11492 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
11493 * in the middle of the gesture.
11494 */
11495TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
11496 auto window = createForeground();
11497 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011498 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011499
11500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011501 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011502 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11503 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11504 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
11505
11506 window->releaseChannel();
11507
11508 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11509
11510 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011511 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011512 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11513 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
11514}
11515
11516/**
11517 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
11518 * the spy, but not to any other windows.
11519 */
11520TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
11521 auto spy = createSpy();
11522 auto window = createForeground();
11523
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011524 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011525
11526 // First finger down on the window and the spy.
11527 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011528 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011529 {100, 200}))
11530 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11531 spy->consumeMotionDown();
11532 window->consumeMotionDown();
11533
11534 // Spy window pilfers the pointers.
11535 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11536 window->consumeMotionCancel();
11537
11538 // Second finger down on the window and spy, but the window should not receive the pointer down.
11539 const MotionEvent secondFingerDownEvent =
11540 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11541 .displayId(ADISPLAY_ID_DEFAULT)
11542 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011543 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11544 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011545 .build();
11546 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011547 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011548 InputEventInjectionSync::WAIT_FOR_RESULT))
11549 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11550
Harry Cutts33476232023-01-30 19:57:29 +000011551 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011552
11553 // Third finger goes down outside all windows, so injection should fail.
11554 const MotionEvent thirdFingerDownEvent =
11555 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11556 .displayId(ADISPLAY_ID_DEFAULT)
11557 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011558 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
11559 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
11560 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011561 .build();
11562 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011563 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011564 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080011565 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011566
11567 spy->assertNoEvents();
11568 window->assertNoEvents();
11569}
11570
11571/**
11572 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
11573 */
11574TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
11575 auto spy = createSpy();
11576 spy->setFrame(Rect(0, 0, 100, 100));
11577 auto window = createForeground();
11578 window->setFrame(Rect(0, 0, 200, 200));
11579
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011580 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011581
11582 // First finger down on the window only
11583 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011584 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011585 {150, 150}))
11586 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11587 window->consumeMotionDown();
11588
11589 // Second finger down on the spy and window
11590 const MotionEvent secondFingerDownEvent =
11591 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11592 .displayId(ADISPLAY_ID_DEFAULT)
11593 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011594 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
11595 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011596 .build();
11597 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011598 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011599 InputEventInjectionSync::WAIT_FOR_RESULT))
11600 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11601 spy->consumeMotionDown();
11602 window->consumeMotionPointerDown(1);
11603
11604 // Third finger down on the spy and window
11605 const MotionEvent thirdFingerDownEvent =
11606 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11607 .displayId(ADISPLAY_ID_DEFAULT)
11608 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011609 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
11610 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
11611 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011612 .build();
11613 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011614 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011615 InputEventInjectionSync::WAIT_FOR_RESULT))
11616 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11617 spy->consumeMotionPointerDown(1);
11618 window->consumeMotionPointerDown(2);
11619
11620 // Spy window pilfers the pointers.
11621 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Harry Cutts101ee9b2023-07-06 18:04:14 +000011622 window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
11623 window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011624
11625 spy->assertNoEvents();
11626 window->assertNoEvents();
11627}
11628
11629/**
11630 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
11631 * other windows should be canceled. If this results in the cancellation of all pointers for some
11632 * window, then that window should receive ACTION_CANCEL.
11633 */
11634TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
11635 auto spy = createSpy();
11636 spy->setFrame(Rect(0, 0, 100, 100));
11637 auto window = createForeground();
11638 window->setFrame(Rect(0, 0, 200, 200));
11639
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011640 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011641
11642 // First finger down on both spy and window
11643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011644 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011645 {10, 10}))
11646 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11647 window->consumeMotionDown();
11648 spy->consumeMotionDown();
11649
11650 // Second finger down on the spy and window
11651 const MotionEvent secondFingerDownEvent =
11652 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11653 .displayId(ADISPLAY_ID_DEFAULT)
11654 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011655 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
11656 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011657 .build();
11658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011659 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011660 InputEventInjectionSync::WAIT_FOR_RESULT))
11661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11662 spy->consumeMotionPointerDown(1);
11663 window->consumeMotionPointerDown(1);
11664
11665 // Spy window pilfers the pointers.
11666 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11667 window->consumeMotionCancel();
11668
11669 spy->assertNoEvents();
11670 window->assertNoEvents();
11671}
11672
11673/**
11674 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
11675 * be sent to other windows
11676 */
11677TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
11678 auto spy = createSpy();
11679 spy->setFrame(Rect(0, 0, 100, 100));
11680 auto window = createForeground();
11681 window->setFrame(Rect(0, 0, 200, 200));
11682
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011683 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011684
11685 // First finger down on both window and spy
11686 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011687 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011688 {10, 10}))
11689 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11690 window->consumeMotionDown();
11691 spy->consumeMotionDown();
11692
11693 // Spy window pilfers the pointers.
11694 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
11695 window->consumeMotionCancel();
11696
11697 // Second finger down on the window only
11698 const MotionEvent secondFingerDownEvent =
11699 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11700 .displayId(ADISPLAY_ID_DEFAULT)
11701 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011702 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
11703 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011704 .build();
11705 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011706 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000011707 InputEventInjectionSync::WAIT_FOR_RESULT))
11708 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
11709 window->consumeMotionDown();
11710 window->assertNoEvents();
11711
11712 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
11713 spy->consumeMotionMove();
11714 spy->assertNoEvents();
11715}
11716
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011717/**
11718 * A window on the left and a window on the right. Also, a spy window that's above all of the
11719 * windows, and spanning both left and right windows.
11720 * Send simultaneous motion streams from two different devices, one to the left window, and another
11721 * to the right window.
11722 * Pilfer from spy window.
11723 * Check that the pilfering only affects the pointers that are actually being received by the spy.
11724 */
11725TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) {
11726 sp<FakeWindowHandle> spy = createSpy();
11727 spy->setFrame(Rect(0, 0, 200, 200));
11728 sp<FakeWindowHandle> leftWindow = createForeground();
11729 leftWindow->setFrame(Rect(0, 0, 100, 100));
11730
11731 sp<FakeWindowHandle> rightWindow = createForeground();
11732 rightWindow->setFrame(Rect(100, 0, 200, 100));
11733
11734 constexpr int32_t stylusDeviceId = 1;
11735 constexpr int32_t touchDeviceId = 2;
11736
11737 mDispatcher->onWindowInfosChanged(
11738 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
11739
11740 // Stylus down on left window and spy
11741 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
11742 .deviceId(stylusDeviceId)
11743 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
11744 .build());
11745 leftWindow->consumeMotionEvent(
11746 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
11747 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
11748
11749 // Finger down on right window and spy - but spy already has stylus
11750 mDispatcher->notifyMotion(
11751 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
11752 .deviceId(touchDeviceId)
11753 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
11754 .build());
11755 rightWindow->consumeMotionEvent(
11756 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011757 spy->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011758
11759 // Act: pilfer from spy. Spy is currently receiving touch events.
11760 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011761 leftWindow->consumeMotionEvent(
11762 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011763 rightWindow->consumeMotionEvent(
11764 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
11765
11766 // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus
11767 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
11768 .deviceId(stylusDeviceId)
11769 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
11770 .build());
11771 mDispatcher->notifyMotion(
11772 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
11773 .deviceId(touchDeviceId)
11774 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
11775 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070011776 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070011777
11778 spy->assertNoEvents();
11779 leftWindow->assertNoEvents();
11780 rightWindow->assertNoEvents();
11781}
11782
Prabir Pradhan9cd9eb62023-11-22 17:58:06 +000011783TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) {
11784 auto window = createForeground();
11785 auto spy = createSpy();
11786 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
11787
11788 mDispatcher->notifyMotion(
11789 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
11790 .deviceId(1)
11791 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200))
11792 .build());
11793 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
11794 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
11795
11796 // Pilfer pointers from the spy window should fail.
11797 EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken()));
11798 spy->assertNoEvents();
11799 window->assertNoEvents();
11800}
11801
Prabir Pradhand65552b2021-10-07 11:23:50 -070011802class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
11803public:
11804 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
11805 std::shared_ptr<FakeApplicationHandle> overlayApplication =
11806 std::make_shared<FakeApplicationHandle>();
11807 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011808 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
11809 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011810 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011811 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080011812 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011813 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011814 overlay->setTrustedOverlay(true);
11815
11816 std::shared_ptr<FakeApplicationHandle> application =
11817 std::make_shared<FakeApplicationHandle>();
11818 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011819 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
11820 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011821 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011822 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011823
11824 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011825 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011826 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000011827 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011828 return {std::move(overlay), std::move(window)};
11829 }
11830
11831 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000011832 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -070011833 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +000011834 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -070011835 }
11836
11837 void sendStylusEvent(int32_t action) {
11838 NotifyMotionArgs motionArgs =
11839 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
11840 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011841 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +000011842 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -070011843 }
11844};
11845
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011846using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
11847
11848TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -070011849 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080011850 ScopedSilentDeath _silentDeath;
11851
Prabir Pradhand65552b2021-10-07 11:23:50 -070011852 auto [overlay, window] = setupStylusOverlayScenario();
11853 overlay->setTrustedOverlay(false);
11854 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011855 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
11856 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -070011857 ".* not a trusted overlay");
11858}
11859
11860TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
11861 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011862 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011863
11864 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
11865 overlay->consumeMotionDown();
11866 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
11867 overlay->consumeMotionUp();
11868
11869 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
11870 window->consumeMotionDown();
11871 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
11872 window->consumeMotionUp();
11873
11874 overlay->assertNoEvents();
11875 window->assertNoEvents();
11876}
11877
11878TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
11879 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -080011880 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011881 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070011882
11883 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
11884 overlay->consumeMotionDown();
11885 window->consumeMotionDown();
11886 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
11887 overlay->consumeMotionUp();
11888 window->consumeMotionUp();
11889
11890 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
11891 window->consumeMotionDown();
11892 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
11893 window->consumeMotionUp();
11894
11895 overlay->assertNoEvents();
11896 window->assertNoEvents();
11897}
11898
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000011899/**
11900 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
11901 * The scenario is as follows:
11902 * - The stylus interceptor overlay is configured as a spy window.
11903 * - The stylus interceptor spy receives the start of a new stylus gesture.
11904 * - It pilfers pointers and then configures itself to no longer be a spy.
11905 * - The stylus interceptor continues to receive the rest of the gesture.
11906 */
11907TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
11908 auto [overlay, window] = setupStylusOverlayScenario();
11909 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011910 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000011911
11912 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
11913 overlay->consumeMotionDown();
11914 window->consumeMotionDown();
11915
11916 // The interceptor pilfers the pointers.
11917 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
11918 window->consumeMotionCancel();
11919
11920 // The interceptor configures itself so that it is no longer a spy.
11921 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011922 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000011923
11924 // It continues to receive the rest of the stylus gesture.
11925 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
11926 overlay->consumeMotionMove();
11927 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
11928 overlay->consumeMotionUp();
11929
11930 window->assertNoEvents();
11931}
11932
Prabir Pradhan5735a322022-04-11 17:23:34 +000011933struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011934 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000011935 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +000011936 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
11937 std::unique_ptr<InputDispatcher>& mDispatcher;
11938
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011939 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +000011940 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
11941
11942 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011943 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000011944 ADISPLAY_ID_DEFAULT, {100, 200},
11945 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
11946 AMOTION_EVENT_INVALID_CURSOR_POSITION},
11947 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
11948 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
11949 }
11950
11951 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011952 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +000011953 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +000011954 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +000011955 mPolicyFlags);
11956 }
11957
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011958 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +000011959 std::shared_ptr<FakeApplicationHandle> overlayApplication =
11960 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011961 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
11962 name, ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +000011963 window->setOwnerInfo(mPid, mUid);
11964 return window;
11965 }
11966};
11967
11968using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
11969
11970TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011971 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011972 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011973 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011974
11975 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
11976 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
11977 window->consumeMotionDown();
11978
11979 setFocusedWindow(window);
11980 window->consumeFocusEvent(true);
11981
11982 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
11983 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
11984 window->consumeKeyDown(ADISPLAY_ID_NONE);
11985}
11986
11987TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011988 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011989 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011990 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011991
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011992 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011993 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
11994 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
11995
11996 setFocusedWindow(window);
11997 window->consumeFocusEvent(true);
11998
11999 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
12000 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
12001 window->assertNoEvents();
12002}
12003
12004TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012005 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012006 auto window = owner.createWindow("Owned window");
12007 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012008 spy->setSpy(true);
12009 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012010 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012011
12012 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12013 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12014 spy->consumeMotionDown();
12015 window->consumeMotionDown();
12016}
12017
12018TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012019 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012020 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012021
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012022 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012023 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012024 randosSpy->setSpy(true);
12025 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012026 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012027
12028 // The event is targeted at owner's window, so injection should succeed, but the spy should
12029 // not receive the event.
12030 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12031 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12032 randosSpy->assertNoEvents();
12033 window->consumeMotionDown();
12034}
12035
12036TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012037 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012038 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012039
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012040 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012041 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012042 randosSpy->setSpy(true);
12043 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012044 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012045
12046 // A user that has injection permission can inject into any window.
12047 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012048 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000012049 ADISPLAY_ID_DEFAULT));
12050 randosSpy->consumeMotionDown();
12051 window->consumeMotionDown();
12052
12053 setFocusedWindow(randosSpy);
12054 randosSpy->consumeFocusEvent(true);
12055
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070012056 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Prabir Pradhan5735a322022-04-11 17:23:34 +000012057 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
12058 window->assertNoEvents();
12059}
12060
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012061TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012062 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012063 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012064
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000012065 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070012066 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000012067 randosWindow->setFrame(Rect{-10, -10, -5, -5});
12068 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070012069 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000012070
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012071 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +000012072 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
12073 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
12074 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070012075 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +000012076}
12077
Prabir Pradhan64f21d22023-11-28 21:19:42 +000012078using InputDispatcherPointerInWindowTest = InputDispatcherTest;
12079
12080TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) {
12081 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12082
12083 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12084 ADISPLAY_ID_DEFAULT);
12085 left->setFrame(Rect(0, 0, 100, 100));
12086 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12087 "Right Window", ADISPLAY_ID_DEFAULT);
12088 right->setFrame(Rect(100, 0, 200, 100));
12089 sp<FakeWindowHandle> spy =
12090 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
12091 spy->setFrame(Rect(0, 0, 200, 100));
12092 spy->setTrustedOverlay(true);
12093 spy->setSpy(true);
12094
12095 mDispatcher->onWindowInfosChanged(
12096 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
12097
12098 // Hover into the left window.
12099 mDispatcher->notifyMotion(
12100 MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
12101 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50))
12102 .build());
12103
12104 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12105 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12106
12107 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12108 /*pointerId=*/0));
12109 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12110 /*pointerId=*/0));
12111 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12112 /*pointerId=*/0));
12113
12114 // Hover move to the right window.
12115 mDispatcher->notifyMotion(
12116 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
12117 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
12118 .build());
12119
12120 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12121 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12122 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
12123
12124 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12125 /*pointerId=*/0));
12126 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12127 /*pointerId=*/0));
12128 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12129 /*pointerId=*/0));
12130
12131 // Stop hovering.
12132 mDispatcher->notifyMotion(
12133 MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
12134 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50))
12135 .build());
12136
12137 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12138 spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12139
12140 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12141 /*pointerId=*/0));
12142 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12143 /*pointerId=*/0));
12144 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12145 /*pointerId=*/0));
12146}
12147
12148TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) {
12149 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12150
12151 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12152 ADISPLAY_ID_DEFAULT);
12153 left->setFrame(Rect(0, 0, 100, 100));
12154 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12155 "Right Window", ADISPLAY_ID_DEFAULT);
12156 right->setFrame(Rect(100, 0, 200, 100));
12157 sp<FakeWindowHandle> spy =
12158 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT);
12159 spy->setFrame(Rect(0, 0, 200, 100));
12160 spy->setTrustedOverlay(true);
12161 spy->setSpy(true);
12162
12163 mDispatcher->onWindowInfosChanged(
12164 {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0});
12165
12166 // First pointer down on left window.
12167 mDispatcher->notifyMotion(
12168 MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
12169 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12170 .build());
12171
12172 left->consumeMotionDown();
12173 spy->consumeMotionDown();
12174
12175 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12176 /*pointerId=*/0));
12177 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12178 /*pointerId=*/0));
12179 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12180 /*pointerId=*/0));
12181
12182 // Second pointer down on right window.
12183 mDispatcher->notifyMotion(
12184 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
12185 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12186 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
12187 .build());
12188
12189 left->consumeMotionMove();
12190 right->consumeMotionDown();
12191 spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
12192
12193 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12194 /*pointerId=*/0));
12195 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12196 /*pointerId=*/0));
12197 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12198 /*pointerId=*/0));
12199 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12200 /*pointerId=*/1));
12201 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12202 /*pointerId=*/1));
12203 ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12204 /*pointerId=*/1));
12205
12206 // Second pointer up.
12207 mDispatcher->notifyMotion(
12208 MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
12209 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12210 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
12211 .build());
12212
12213 left->consumeMotionMove();
12214 right->consumeMotionUp();
12215 spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
12216
12217 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12218 /*pointerId=*/0));
12219 ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12220 /*pointerId=*/0));
12221 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12222 /*pointerId=*/0));
12223 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12224 /*pointerId=*/1));
12225 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12226 /*pointerId=*/1));
12227 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12228 /*pointerId=*/1));
12229
12230 // First pointer up.
12231 mDispatcher->notifyMotion(
12232 MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
12233 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
12234 .build());
12235
12236 left->consumeMotionUp();
12237 spy->consumeMotionUp();
12238
12239 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12240 /*pointerId=*/0));
12241 ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12242 /*pointerId=*/0));
12243 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12244 /*pointerId=*/0));
12245}
12246
12247TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) {
12248 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
12249
12250 sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window",
12251 ADISPLAY_ID_DEFAULT);
12252 left->setFrame(Rect(0, 0, 100, 100));
12253 sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher,
12254 "Right Window", ADISPLAY_ID_DEFAULT);
12255 right->setFrame(Rect(100, 0, 200, 100));
12256
12257 mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0});
12258
12259 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12260 /*pointerId=*/0));
12261 ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12262 /*pointerId=*/0));
12263
12264 // Hover move into the window.
12265 mDispatcher->notifyMotion(
12266 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12267 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50))
12268 .rawXCursorPosition(50)
12269 .rawYCursorPosition(50)
12270 .deviceId(DEVICE_ID)
12271 .build());
12272
12273 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12274
12275 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12276 /*pointerId=*/0));
12277
12278 // Move the mouse with another device. This cancels the hovering pointer from the first device.
12279 mDispatcher->notifyMotion(
12280 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12281 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50))
12282 .rawXCursorPosition(51)
12283 .rawYCursorPosition(50)
12284 .deviceId(SECOND_DEVICE_ID)
12285 .build());
12286
12287 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12288 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12289
12290 // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets
12291 // a HOVER_EXIT from the first device.
12292 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12293 /*pointerId=*/0));
12294 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT,
12295 SECOND_DEVICE_ID,
12296 /*pointerId=*/0));
12297
12298 // Move the mouse outside the window. Document the current behavior, where the window does not
12299 // receive HOVER_EXIT even though the mouse left the window.
12300 mDispatcher->notifyMotion(
12301 MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
12302 .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50))
12303 .rawXCursorPosition(150)
12304 .rawYCursorPosition(50)
12305 .deviceId(SECOND_DEVICE_ID)
12306 .build());
12307
12308 left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
12309 right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
12310 ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID,
12311 /*pointerId=*/0));
12312 ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT,
12313 SECOND_DEVICE_ID,
12314 /*pointerId=*/0));
12315}
12316
Garfield Tane84e6f92019-08-29 17:28:41 -070012317} // namespace android::inputdispatcher