blob: 91eceb0c4e8d90b57e5e0ee7254639bbf658e740 [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 Pradhane3b28dd2023-10-06 04:19:29 +000020#include "TestEventMatchers.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080021
Cody Heiner166a5af2023-07-07 12:25:00 -070022#include <NotifyArgsBuilders.h>
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070023#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080024#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080025#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070026#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070027#include <binder/Binder.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000028#include <com_android_input_flags.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000029#include <fcntl.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000030#include <flag_macros.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080031#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100033#include <input/Input.h>
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070034#include <input/PrintTools.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080036#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100037
Garfield Tan1c7bc862020-01-28 13:24:04 -080038#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080039#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070040#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080041#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080042#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Garfield Tan1c7bc862020-01-28 13:24:04 -080044using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050045using android::gui::FocusRequest;
46using android::gui::TouchOcclusionMode;
47using android::gui::WindowInfo;
48using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080049using android::os::InputEventInjectionResult;
50using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080051
Garfield Tane84e6f92019-08-29 17:28:41 -070052namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
Dominik Laskowski2f01d772022-03-23 16:01:29 -070054using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080055using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070056
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070057namespace {
58
Michael Wrightd02c5b62014-02-10 15:10:22 -080059// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000060static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
62// An arbitrary device id.
Prabir Pradhan9205e422023-05-16 20:06:13 +000063static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080064static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080065
Jeff Brownf086ddb2014-02-11 14:28:48 -080066// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000067static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
68static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080069
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000070// Ensure common actions are interchangeable between keys and motions for convenience.
71static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN);
72static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080073static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
74static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
75static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
76static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070077static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080078static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070079static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080080static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080081static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080082/**
83 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
84 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
85 * index 0) is the new pointer going down. The same pointer could have been placed at a different
86 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
87 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
88 * pointer id=0 leaves but the pointer id=1 remains.
89 */
90static constexpr int32_t POINTER_0_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080092static constexpr int32_t POINTER_1_DOWN =
93 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000094static constexpr int32_t POINTER_2_DOWN =
95 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000096static constexpr int32_t POINTER_3_DOWN =
97 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000098static constexpr int32_t POINTER_0_UP =
99 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800100static constexpr int32_t POINTER_1_UP =
101 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +0000102static constexpr int32_t POINTER_2_UP =
103 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800104
Antonio Kantek15beb512022-06-13 22:35:41 +0000105// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000106static constexpr gui::Pid WINDOW_PID{999};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000107static constexpr gui::Uid WINDOW_UID{1001};
Prabir Pradhan5735a322022-04-11 17:23:34 +0000108
Antonio Kantek15beb512022-06-13 22:35:41 +0000109// The default pid and uid for the windows created on the secondary display by the test.
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000110static constexpr gui::Pid SECONDARY_WINDOW_PID{1010};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000111static constexpr gui::Uid SECONDARY_WINDOW_UID{1012};
Antonio Kantek15beb512022-06-13 22:35:41 +0000112
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000113// An arbitrary pid of the gesture monitor window
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000114static constexpr gui::Pid MONITOR_PID{2001};
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000115
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700116/**
117 * If we expect to receive the event, the timeout can be made very long. When the test are running
118 * correctly, we will actually never wait until the end of the timeout because the wait will end
119 * when the event comes in. Still, this value shouldn't be infinite. During development, a local
120 * change may cause the test to fail. This timeout should be short enough to not annoy so that the
121 * developer can see the failure quickly (on human scale).
122 */
123static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms;
124/**
125 * When no event is expected, we can have a very short timeout. A large value here would slow down
126 * the tests. In the unlikely event of system being too slow, the event may still be present but the
127 * timeout would complete before it is consumed. This would result in test flakiness. If this
128 * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this
129 * would get noticed and addressed quickly.
130 */
131static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms;
132
Arthur Hungc539dbb2022-12-08 07:45:36 +0000133static constexpr int expectedWallpaperFlags =
134 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
135
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800136using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
137
Gang Wang342c9272020-01-13 13:15:04 -0500138/**
139 * Return a DOWN key event with KEYCODE_A.
140 */
141static KeyEvent getTestKeyEvent() {
142 KeyEvent event;
143
Garfield Tanfbe732e2020-01-24 11:26:14 -0800144 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
145 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
146 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500147 return event;
148}
149
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150// --- FakeInputDispatcherPolicy ---
151
152class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000153 struct AnrResult {
154 sp<IBinder> token{};
155 gui::Pid pid{gui::Pid::INVALID};
156 };
Prabir Pradhanedd96402022-02-15 01:46:16 -0800157
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000159 FakeInputDispatcherPolicy() = default;
160 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800161
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800162 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700163 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700164 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700165 EXPECT_EQ(event.getDisplayId(), args.displayId);
166
167 const auto& keyEvent = static_cast<const KeyEvent&>(event);
168 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
169 EXPECT_EQ(keyEvent.getAction(), args.action);
170 });
Jackal Guof9696682018-10-05 12:23:23 +0800171 }
172
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700173 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
174 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700175 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700176 EXPECT_EQ(event.getDisplayId(), args.displayId);
177
178 const auto& motionEvent = static_cast<const MotionEvent&>(event);
179 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
180 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000181 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
182 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
183 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
184 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700185 });
Jackal Guof9696682018-10-05 12:23:23 +0800186 }
187
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700188 void assertFilterInputEventWasNotCalled() {
189 std::scoped_lock lock(mLock);
190 ASSERT_EQ(nullptr, mFilteredEvent);
191 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800193 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700194 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800195 ASSERT_TRUE(mConfigurationChangedTime)
196 << "Timed out waiting for configuration changed call";
197 ASSERT_EQ(*mConfigurationChangedTime, when);
198 mConfigurationChangedTime = std::nullopt;
199 }
200
201 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700202 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800203 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800204 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800205 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
206 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
207 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
208 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
209 mLastNotifySwitch = std::nullopt;
210 }
211
chaviwfd6d3512019-03-25 13:23:49 -0700212 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700213 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800214 ASSERT_EQ(touchedToken, mOnPointerDownToken);
215 mOnPointerDownToken.clear();
216 }
217
218 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700219 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800220 ASSERT_TRUE(mOnPointerDownToken == nullptr)
221 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700222 }
223
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700224 // This function must be called soon after the expected ANR timer starts,
225 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500226 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700227 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500228 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800229 std::unique_lock lock(mLock);
230 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500231 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800232 ASSERT_NO_FATAL_FAILURE(
233 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500234 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700235 }
236
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000237 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800238 const sp<WindowInfoHandle>& window) {
239 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
240 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
241 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500242 }
243
Prabir Pradhanedd96402022-02-15 01:46:16 -0800244 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
245 const sp<IBinder>& expectedToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000246 gui::Pid expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800247 std::unique_lock lock(mLock);
248 android::base::ScopedLockAssertion assumeLocked(mLock);
249 AnrResult result;
250 ASSERT_NO_FATAL_FAILURE(result =
251 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000252 ASSERT_EQ(expectedToken, result.token);
253 ASSERT_EQ(expectedPid, result.pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500254 }
255
Prabir Pradhanedd96402022-02-15 01:46:16 -0800256 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000257 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500258 std::unique_lock lock(mLock);
259 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800260 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
261 const auto& [token, _] = result;
262 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000263 }
264
Prabir Pradhanedd96402022-02-15 01:46:16 -0800265 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000266 gui::Pid expectedPid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800267 std::unique_lock lock(mLock);
268 android::base::ScopedLockAssertion assumeLocked(mLock);
269 AnrResult result;
270 ASSERT_NO_FATAL_FAILURE(
271 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000272 ASSERT_EQ(expectedToken, result.token);
273 ASSERT_EQ(expectedPid, result.pid);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800274 }
275
276 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000277 sp<IBinder> getResponsiveWindowToken() {
278 std::unique_lock lock(mLock);
279 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800280 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
281 const auto& [token, _] = result;
282 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700283 }
284
285 void assertNotifyAnrWasNotCalled() {
286 std::scoped_lock lock(mLock);
287 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800288 ASSERT_TRUE(mAnrWindows.empty());
289 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500290 << "ANR was not called, but please also consume the 'connection is responsive' "
291 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700292 }
293
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000294 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800295 std::unique_lock lock(mLock);
296 base::ScopedLockAssertion assumeLocked(mLock);
297
298 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
299 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000300 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800301 enabled;
302 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000303 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
304 << ") to be called.";
305 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800306 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000307 auto request = *mPointerCaptureRequest;
308 mPointerCaptureRequest.reset();
309 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800310 }
311
312 void assertSetPointerCaptureNotCalled() {
313 std::unique_lock lock(mLock);
314 base::ScopedLockAssertion assumeLocked(mLock);
315
316 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000317 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800318 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000319 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800320 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000321 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800322 }
323
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -0700324 void assertDropTargetEquals(const InputDispatcherInterface& dispatcher,
325 const sp<IBinder>& targetToken) {
326 dispatcher.waitForIdle();
arthurhungf452d0b2021-01-06 00:19:52 +0800327 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800328 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800329 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800330 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800331 }
332
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800333 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
334 std::unique_lock lock(mLock);
335 base::ScopedLockAssertion assumeLocked(mLock);
336 std::optional<sp<IBinder>> receivedToken =
337 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
338 mNotifyInputChannelBroken);
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000339 ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token";
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800340 ASSERT_EQ(token, *receivedToken);
341 }
342
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800343 /**
344 * Set policy timeout. A value of zero means next key will not be intercepted.
345 */
346 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
347 mInterceptKeyTimeout = timeout;
348 }
349
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700350 void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; }
351
Josep del Riob3981622023-04-18 15:49:45 +0000352 void assertUserActivityPoked() {
353 std::scoped_lock lock(mLock);
354 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
355 }
356
357 void assertUserActivityNotPoked() {
358 std::scoped_lock lock(mLock);
359 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
360 }
361
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000362 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000363 ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
364 }
365
366 void assertNotifyDeviceInteractionWasNotCalled() {
367 ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
368 }
369
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000370 void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) {
371 std::scoped_lock lock(mLock);
372 mUnhandledKeyHandler = handler;
373 }
374
375 void assertUnhandledKeyReported(int32_t keycode) {
376 std::unique_lock lock(mLock);
377 base::ScopedLockAssertion assumeLocked(mLock);
378 std::optional<int32_t> unhandledKeycode =
379 getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock,
380 mNotifyUnhandledKey);
381 ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported";
382 ASSERT_EQ(unhandledKeycode, keycode);
383 }
384
385 void assertUnhandledKeyNotReported() {
386 std::unique_lock lock(mLock);
387 base::ScopedLockAssertion assumeLocked(mLock);
388 std::optional<int32_t> unhandledKeycode =
389 getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock,
390 mNotifyUnhandledKey);
391 ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported";
392 }
393
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700395 std::mutex mLock;
396 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
397 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
398 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
399 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800400
Prabir Pradhan99987712020-11-10 18:43:05 -0800401 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000402
403 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800404
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700405 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700406 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800407 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
408 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700409 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800410 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
411 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700412
arthurhungf452d0b2021-01-06 00:19:52 +0800413 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800414 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000415 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800416
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800417 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
418
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700419 std::chrono::nanoseconds mStaleEventTimeout = 1000ms;
420
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000421 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000422
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000423 std::condition_variable mNotifyUnhandledKey;
424 std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock);
425 std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock);
426
Prabir Pradhanedd96402022-02-15 01:46:16 -0800427 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
428 // for a specific container to become non-empty. When the container is non-empty, return the
429 // first entry from the container and erase it.
430 template <class T>
431 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
432 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
433 // If there is an ANR, Dispatcher won't be idle because there are still events
434 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
435 // before checking if ANR was called.
436 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
437 // to provide it some time to act. 100ms seems reasonable.
438 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
439 const std::chrono::time_point start = std::chrono::steady_clock::now();
440 std::optional<T> token =
441 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
442 if (!token.has_value()) {
443 ADD_FAILURE() << "Did not receive the ANR callback";
444 return {};
445 }
446
447 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
448 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
449 // the dispatcher started counting before this function was called
450 if (std::chrono::abs(timeout - waited) > 100ms) {
451 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
452 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
453 << "ms, but waited "
454 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
455 << "ms instead";
456 }
457 return *token;
458 }
459
460 template <class T>
461 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
462 std::queue<T>& storage,
463 std::unique_lock<std::mutex>& lock,
464 std::condition_variable& condition)
465 REQUIRES(mLock) {
466 condition.wait_for(lock, timeout,
467 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
468 if (storage.empty()) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800469 return std::nullopt;
470 }
471 T item = storage.front();
472 storage.pop();
473 return std::make_optional(item);
474 }
475
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600476 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700477 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800478 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 }
480
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000481 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800482 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700483 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800484 ASSERT_TRUE(pid.has_value());
485 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700486 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500487 }
488
Prabir Pradhanedd96402022-02-15 01:46:16 -0800489 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000490 std::optional<gui::Pid> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500491 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800492 ASSERT_TRUE(pid.has_value());
493 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500494 mNotifyAnr.notify_all();
495 }
496
497 void notifyNoFocusedWindowAnr(
498 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
499 std::scoped_lock lock(mLock);
500 mAnrApplications.push(applicationHandle);
501 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 }
503
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800504 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
505 std::scoped_lock lock(mLock);
506 mBrokenInputChannels.push(connectionToken);
507 mNotifyInputChannelBroken.notify_all();
508 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800509
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600510 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700511
Chris Yef59a2f42020-10-16 12:55:26 -0700512 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
513 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
514 const std::vector<float>& values) override {}
515
516 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
517 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000518
Chris Yefb552902021-02-03 17:18:37 -0800519 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
520
Prabir Pradhana41d2442023-04-20 21:30:40 +0000521 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700522 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000523 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700524 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000525 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
526 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800527 break;
528 }
529
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700530 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000531 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
532 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800533 break;
534 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700535 default: {
536 ADD_FAILURE() << "Should only filter keys or motions";
537 break;
538 }
Jackal Guof9696682018-10-05 12:23:23 +0800539 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 return true;
541 }
542
Prabir Pradhana41d2442023-04-20 21:30:40 +0000543 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
544 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800545 // Clear intercept state when we handled the event.
546 mInterceptKeyTimeout = 0ms;
547 }
548 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600550 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551
Prabir Pradhana41d2442023-04-20 21:30:40 +0000552 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800553 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
554 // Clear intercept state so we could dispatch the event in next wake.
555 mInterceptKeyTimeout = 0ms;
556 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 }
558
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000559 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event,
Prabir Pradhana41d2442023-04-20 21:30:40 +0000560 uint32_t) override {
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000561 std::scoped_lock lock(mLock);
562 mReportedUnhandledKeycodes.emplace(event.getKeyCode());
563 mNotifyUnhandledKey.notify_all();
564 return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 }
566
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600567 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
568 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700569 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800570 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
571 * essentially a passthrough for notifySwitch.
572 */
Harry Cutts33476232023-01-30 19:57:29 +0000573 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 }
575
Josep del Riob3981622023-04-18 15:49:45 +0000576 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
577 std::scoped_lock lock(mLock);
578 mPokedUserActivity = true;
579 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700581 bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override {
582 return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout;
583 }
584
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600585 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700586 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700587 mOnPointerDownToken = newToken;
588 }
589
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000590 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800591 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000592 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800593 mPointerCaptureChangedCondition.notify_all();
594 }
595
arthurhungf452d0b2021-01-06 00:19:52 +0800596 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
597 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800598 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800599 mDropTargetWindowToken = token;
600 }
601
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000602 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000603 const std::set<gui::Uid>& uids) override {
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000604 ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids));
605 }
606
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700607 void assertFilterInputEventWasCalledInternal(
608 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700609 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800610 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700611 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800612 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800613 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614};
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700615} // namespace
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617// --- InputDispatcherTest ---
618
619class InputDispatcherTest : public testing::Test {
620protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000621 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700622 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000624 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000625 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700626 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
627
Harry Cutts101ee9b2023-07-06 18:04:14 +0000628 mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000629 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700630 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 }
632
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000633 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700634 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000635 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700636 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700638
639 /**
640 * Used for debugging when writing the test
641 */
642 void dumpDispatcherState() {
643 std::string dump;
644 mDispatcher->dump(dump);
645 std::stringstream ss(dump);
646 std::string to;
647
648 while (std::getline(ss, to, '\n')) {
649 ALOGE("%s", to.c_str());
650 }
651 }
Vishnu Nair958da932020-08-21 17:12:37 -0700652
Chavi Weingarten847e8512023-03-29 00:26:09 +0000653 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700654 FocusRequest request;
655 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000656 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700657 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
658 request.displayId = window->getInfo()->displayId;
659 mDispatcher->setFocusedWindow(request);
660 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661};
662
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
664 KeyEvent event;
665
666 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800667 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
668 INVALID_HMAC,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000669 /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600670 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800671 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000672 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000673 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 << "Should reject key events with undefined action.";
675
676 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800677 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
678 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600679 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800680 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000681 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000682 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683 << "Should reject key events with ACTION_MULTIPLE.";
684}
685
686TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
687 MotionEvent event;
688 PointerProperties pointerProperties[MAX_POINTERS + 1];
689 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800690 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 pointerProperties[i].clear();
692 pointerProperties[i].id = i;
693 pointerCoords[i].clear();
694 }
695
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800696 // Some constants commonly used below
697 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
698 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
699 constexpr int32_t metaState = AMETA_NONE;
700 constexpr MotionClassification classification = MotionClassification::NONE;
701
chaviw9eaa22c2020-07-01 16:21:27 -0700702 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800704 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000705 /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700706 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700707 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
708 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000709 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800710 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000711 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000712 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 << "Should reject motion events with undefined action.";
714
715 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800716 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800717 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
718 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
719 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
720 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000721 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800722 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000723 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000724 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 << "Should reject motion events with pointer down index too large.";
726
Garfield Tanfbe732e2020-01-24 11:26:14 -0800727 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700728 AMOTION_EVENT_ACTION_POINTER_DOWN |
729 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700730 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
731 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700732 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000733 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800734 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000735 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000736 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800737 << "Should reject motion events with pointer down index too small.";
738
739 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800740 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800741 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
742 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
743 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
744 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000745 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800746 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000747 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000748 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800749 << "Should reject motion events with pointer up index too large.";
750
Garfield Tanfbe732e2020-01-24 11:26:14 -0800751 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700752 AMOTION_EVENT_ACTION_POINTER_UP |
753 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700754 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
755 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700756 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000757 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800758 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000759 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000760 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761 << "Should reject motion events with pointer up index too small.";
762
763 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800764 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
765 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700766 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700767 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
768 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000769 /*pointerCount=*/0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800770 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000771 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000772 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 << "Should reject motion events with 0 pointers.";
774
Garfield Tanfbe732e2020-01-24 11:26:14 -0800775 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
776 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700777 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700778 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
779 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000780 /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800781 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000782 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000783 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 << "Should reject motion events with more than MAX_POINTERS pointers.";
785
786 // Rejects motion events with invalid pointer ids.
787 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800788 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
789 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700790 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700791 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
792 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000793 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800794 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000795 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000796 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797 << "Should reject motion events with pointer ids less than 0.";
798
799 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800800 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
801 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700802 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700803 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
804 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000805 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800806 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000807 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000808 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
810
811 // Rejects motion events with duplicate pointer ids.
812 pointerProperties[0].id = 1;
813 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800814 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
815 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700816 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700817 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
818 ARBITRARY_TIME,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000819 /*pointerCount=*/2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800820 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000821 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000822 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 << "Should reject motion events with duplicate pointer ids.";
824}
825
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800826/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
827
828TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
829 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000830 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800831 ASSERT_TRUE(mDispatcher->waitForIdle());
832
833 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
834}
835
836TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000837 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
838 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000839 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800840
841 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
842 args.policyFlags |= POLICY_FLAG_TRUSTED;
843 mFakePolicy->assertNotifySwitchWasCalled(args);
844}
845
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700846namespace {
847
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700848static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700849// Default input dispatching timeout if there is no focused application or paused window
850// from which to determine an appropriate dispatching timeout.
851static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
852 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
853 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800854
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800855class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800856public:
Garfield Tan15601662020-09-22 15:32:38 -0700857 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700858 : mConsumer(std::move(clientChannel)), mName(name) {}
chaviwd1c23182019-12-20 18:44:56 -0800859
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000860 InputEvent* consume(std::chrono::milliseconds timeout, bool handled = false) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700861 InputEvent* event;
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700862 std::optional<uint32_t> consumeSeq = receiveEvent(timeout, &event);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700863 if (!consumeSeq) {
864 return nullptr;
865 }
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000866 finishEvent(*consumeSeq, handled);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700867 return event;
868 }
869
870 /**
871 * Receive an event without acknowledging it.
872 * Return the sequence number that could later be used to send finished signal.
873 */
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700874 std::optional<uint32_t> receiveEvent(std::chrono::milliseconds timeout,
875 InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800876 uint32_t consumeSeq;
877 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800878
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800879 std::chrono::time_point start = std::chrono::steady_clock::now();
880 status_t status = WOULD_BLOCK;
881 while (status == WOULD_BLOCK) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700882 status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
883 &event);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800884 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700885 if (elapsed > timeout) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800886 break;
887 }
888 }
889
890 if (status == WOULD_BLOCK) {
891 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700892 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800893 }
894
895 if (status != OK) {
896 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700897 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800898 }
899 if (event == nullptr) {
900 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700901 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800902 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700903 if (outEvent != nullptr) {
904 *outEvent = event;
905 }
906 return consumeSeq;
907 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800908
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700909 /**
910 * To be used together with "receiveEvent" to complete the consumption of an event.
911 */
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +0000912 void finishEvent(uint32_t consumeSeq, bool handled = true) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700913 const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700914 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800915 }
916
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000917 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700918 const status_t status = mConsumer.sendTimeline(inputEventId, timeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000919 ASSERT_EQ(OK, status);
920 }
921
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700922 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000923 std::optional<int32_t> expectedDisplayId,
924 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700925 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800926
927 ASSERT_NE(nullptr, event) << mName.c_str()
928 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800929 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700930 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
931 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800932
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000933 if (expectedDisplayId.has_value()) {
934 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
935 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800936
Tiger Huang8664f8c2018-10-11 19:14:35 +0800937 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700938 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800939 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700940 ASSERT_THAT(keyEvent, WithKeyAction(expectedAction));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000941 if (expectedFlags.has_value()) {
942 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
943 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800944 break;
945 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700946 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800947 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700948 ASSERT_THAT(motionEvent, WithMotionAction(expectedAction));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000949 if (expectedFlags.has_value()) {
950 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
951 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800952 break;
953 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700954 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100955 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
956 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700957 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800958 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
959 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700960 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000961 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
962 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700963 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800964 FAIL() << "Use 'consumeDragEvent' for DRAG events";
965 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800966 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800967 }
968
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800969 MotionEvent* consumeMotion() {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700970 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800971
972 if (event == nullptr) {
973 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
974 return nullptr;
975 }
976
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700977 if (event->getType() != InputEventType::MOTION) {
978 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800979 return nullptr;
980 }
981 return static_cast<MotionEvent*>(event);
982 }
983
984 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
985 MotionEvent* motionEvent = consumeMotion();
986 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
987 ASSERT_THAT(*motionEvent, matcher);
988 }
989
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100990 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -0700991 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100992 ASSERT_NE(nullptr, event) << mName.c_str()
993 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700994 ASSERT_EQ(InputEventType::FOCUS, event->getType())
995 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100996
997 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
998 << mName.c_str() << ": event displayId should always be NONE.";
999
1000 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1001 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001002 }
1003
Prabir Pradhan99987712020-11-10 18:43:05 -08001004 void consumeCaptureEvent(bool hasCapture) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001005 const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Prabir Pradhan99987712020-11-10 18:43:05 -08001006 ASSERT_NE(nullptr, event) << mName.c_str()
1007 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001008 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1009 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001010
1011 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1012 << mName.c_str() << ": event displayId should always be NONE.";
1013
1014 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1015 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1016 }
1017
arthurhungb89ccb02020-12-30 16:19:01 +08001018 void consumeDragEvent(bool isExiting, float x, float y) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001019 const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
arthurhungb89ccb02020-12-30 16:19:01 +08001020 ASSERT_NE(nullptr, event) << mName.c_str()
1021 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001022 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001023
1024 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1025 << mName.c_str() << ": event displayId should always be NONE.";
1026
1027 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1028 EXPECT_EQ(isExiting, dragEvent.isExiting());
1029 EXPECT_EQ(x, dragEvent.getX());
1030 EXPECT_EQ(y, dragEvent.getY());
1031 }
1032
Antonio Kantekf16f2832021-09-28 04:39:20 +00001033 void consumeTouchModeEvent(bool inTouchMode) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001034 const InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Antonio Kantekf16f2832021-09-28 04:39:20 +00001035 ASSERT_NE(nullptr, event) << mName.c_str()
1036 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001037 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1038 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001039
1040 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1041 << mName.c_str() << ": event displayId should always be NONE.";
1042 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1043 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1044 }
1045
chaviwd1c23182019-12-20 18:44:56 -08001046 void assertNoEvents() {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001047 InputEvent* event = consume(CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001048 if (event == nullptr) {
1049 return;
1050 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001051 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001052 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1053 ADD_FAILURE() << "Received key event "
1054 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001055 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001056 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1057 ADD_FAILURE() << "Received motion event "
1058 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001059 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001060 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1061 ADD_FAILURE() << "Received focus event, hasFocus = "
1062 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001063 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001064 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1065 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1066 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001067 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001068 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1069 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1070 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001071 }
1072 FAIL() << mName.c_str()
1073 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001074 }
1075
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001076 sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); }
chaviwd1c23182019-12-20 18:44:56 -08001077
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001078 int getChannelFd() { return mConsumer.getChannel()->getFd().get(); }
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001079
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001080private:
1081 InputConsumer mConsumer;
chaviwd1c23182019-12-20 18:44:56 -08001082 PreallocatedInputEventFactory mEventFactory;
1083
1084 std::string mName;
1085};
1086
chaviw3277faf2021-05-19 16:45:23 -05001087class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001088public:
1089 static const int32_t WIDTH = 600;
1090 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001091
Chris Yea209fde2020-07-22 13:54:51 -07001092 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001093 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001094 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001095 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001096 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001097 base::Result<std::unique_ptr<InputChannel>> channel =
1098 dispatcher->createInputChannel(name);
1099 token = (*channel)->getConnectionToken();
1100 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001101 }
1102
1103 inputApplicationHandle->updateInfo();
1104 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1105
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001106 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001107 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001108 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001109 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001110 mInfo.alpha = 1.0;
Chavi Weingarten7f019192023-08-08 20:39:01 +00001111 mInfo.frame = Rect(0, 0, WIDTH, HEIGHT);
chaviw1ff3d1e2020-07-01 15:53:47 -07001112 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001113 mInfo.globalScaleFactor = 1.0;
1114 mInfo.touchableRegion.clear();
1115 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001116 mInfo.ownerPid = WINDOW_PID;
1117 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001118 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001119 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001120 }
1121
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001122 sp<FakeWindowHandle> clone(int32_t displayId) {
1123 sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)");
1124 handle->mInfo = mInfo;
1125 handle->mInfo.displayId = displayId;
1126 handle->mInfo.id = sId++;
1127 handle->mInputReceiver = mInputReceiver;
1128 return handle;
1129 }
1130
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001131 void setTouchable(bool touchable) {
1132 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1133 }
chaviwd1c23182019-12-20 18:44:56 -08001134
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001135 void setFocusable(bool focusable) {
1136 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1137 }
1138
1139 void setVisible(bool visible) {
1140 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1141 }
Vishnu Nair958da932020-08-21 17:12:37 -07001142
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001143 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001144 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001145 }
1146
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001147 void setPaused(bool paused) {
1148 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1149 }
1150
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001151 void setPreventSplitting(bool preventSplitting) {
1152 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001153 }
1154
1155 void setSlippery(bool slippery) {
1156 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1157 }
1158
1159 void setWatchOutsideTouch(bool watchOutside) {
1160 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1161 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001162
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001163 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1164
1165 void setInterceptsStylus(bool interceptsStylus) {
1166 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1167 }
1168
1169 void setDropInput(bool dropInput) {
1170 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1171 }
1172
1173 void setDropInputIfObscured(bool dropInputIfObscured) {
1174 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1175 }
1176
1177 void setNoInputChannel(bool noInputChannel) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1179 }
1180
Josep del Riob3981622023-04-18 15:49:45 +00001181 void setDisableUserActivity(bool disableUserActivity) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1183 }
1184
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001185 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1186
chaviw3277faf2021-05-19 16:45:23 -05001187 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001188
Bernardo Rufino7393d172021-02-26 13:56:11 +00001189 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1190
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001191 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
Chavi Weingarten7f019192023-08-08 20:39:01 +00001192 mInfo.frame = frame;
chaviwd1c23182019-12-20 18:44:56 -08001193 mInfo.touchableRegion.clear();
1194 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001195
1196 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1197 ui::Transform translate;
1198 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1199 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001200 }
1201
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001202 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1203
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001204 void setIsWallpaper(bool isWallpaper) {
1205 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1206 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001207
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001208 void setDupTouchToWallpaper(bool hasWallpaper) {
1209 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1210 }
chaviwd1c23182019-12-20 18:44:56 -08001211
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001212 void setTrustedOverlay(bool trustedOverlay) {
1213 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1214 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001215
chaviw9eaa22c2020-07-01 16:21:27 -07001216 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1217 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1218 }
1219
1220 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001221
yunho.shinf4a80b82020-11-16 21:13:57 +09001222 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1223
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00001224 KeyEvent* consumeKey(bool handled = true) {
1225 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001226 if (event == nullptr) {
1227 ADD_FAILURE() << "Consume failed : no event";
1228 return nullptr;
1229 }
1230 if (event->getType() != InputEventType::KEY) {
1231 ADD_FAILURE() << "Instead of key event, got " << *event;
1232 return nullptr;
1233 }
1234 return static_cast<KeyEvent*>(event);
1235 }
1236
1237 void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) {
1238 KeyEvent* keyEvent = consumeKey();
1239 ASSERT_NE(nullptr, keyEvent) << "Did not get a key event, but expected " << matcher;
1240 ASSERT_THAT(*keyEvent, matcher);
1241 }
1242
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001243 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001244 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001245 }
1246
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001247 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001248 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001249 }
1250
Svet Ganov5d3bc372020-01-26 23:11:07 -08001251 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001252 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001253 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1254 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001255 }
1256
1257 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001258 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001259 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1260 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001261 }
1262
1263 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001264 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001265 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1266 }
1267
1268 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1269 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001270 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001271 expectedFlags);
1272 }
1273
Svet Ganov5d3bc372020-01-26 23:11:07 -08001274 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001275 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1276 int32_t expectedFlags = 0) {
1277 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1278 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001279 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001280 }
1281
1282 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001283 int32_t expectedFlags = 0) {
1284 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1285 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001286 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001287 }
1288
1289 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001290 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001291 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001292 expectedFlags);
1293 }
1294
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001295 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1296 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001297 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001298 expectedFlags);
1299 }
1300
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001301 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1302 int32_t expectedFlags = 0) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001303 MotionEvent* motionEvent = consumeMotion();
1304 ASSERT_NE(nullptr, motionEvent);
1305 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent->getActionMasked());
1306 EXPECT_EQ(0.f, motionEvent->getRawPointerCoords(0)->getX());
1307 EXPECT_EQ(0.f, motionEvent->getRawPointerCoords(0)->getY());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001308 }
1309
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001310 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1311 ASSERT_NE(mInputReceiver, nullptr)
1312 << "Cannot consume events from a window with no receiver";
1313 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1314 }
1315
Prabir Pradhan99987712020-11-10 18:43:05 -08001316 void consumeCaptureEvent(bool hasCapture) {
1317 ASSERT_NE(mInputReceiver, nullptr)
1318 << "Cannot consume events from a window with no receiver";
1319 mInputReceiver->consumeCaptureEvent(hasCapture);
1320 }
1321
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001322 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1323 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001324 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001325 ASSERT_THAT(*motionEvent, matcher);
1326 }
1327
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001328 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001329 std::optional<int32_t> expectedDisplayId,
1330 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001331 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1332 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1333 expectedFlags);
1334 }
1335
arthurhungb89ccb02020-12-30 16:19:01 +08001336 void consumeDragEvent(bool isExiting, float x, float y) {
1337 mInputReceiver->consumeDragEvent(isExiting, x, y);
1338 }
1339
Antonio Kantekf16f2832021-09-28 04:39:20 +00001340 void consumeTouchModeEvent(bool inTouchMode) {
1341 ASSERT_NE(mInputReceiver, nullptr)
1342 << "Cannot consume events from a window with no receiver";
1343 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1344 }
1345
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001346 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001347 if (mInputReceiver == nullptr) {
1348 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1349 return std::nullopt;
1350 }
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001351 return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED, outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001352 }
1353
1354 void finishEvent(uint32_t sequenceNum) {
1355 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1356 mInputReceiver->finishEvent(sequenceNum);
1357 }
1358
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001359 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1360 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1361 mInputReceiver->sendTimeline(inputEventId, timeline);
1362 }
1363
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00001364 InputEvent* consume(std::chrono::milliseconds timeout, bool handled = true) {
chaviwaf87b3e2019-10-01 16:59:28 -07001365 if (mInputReceiver == nullptr) {
1366 return nullptr;
1367 }
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00001368 return mInputReceiver->consume(timeout, handled);
chaviwaf87b3e2019-10-01 16:59:28 -07001369 }
1370
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001371 MotionEvent* consumeMotion() {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001372 InputEvent* event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001373 if (event == nullptr) {
1374 ADD_FAILURE() << "Consume failed : no event";
1375 return nullptr;
1376 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001377 if (event->getType() != InputEventType::MOTION) {
1378 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001379 return nullptr;
1380 }
1381 return static_cast<MotionEvent*>(event);
1382 }
1383
Arthur Hungb92218b2018-08-14 12:00:21 +08001384 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001385 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001386 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001387 return; // Can't receive events if the window does not have input channel
1388 }
1389 ASSERT_NE(nullptr, mInputReceiver)
1390 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001391 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001392 }
1393
chaviwaf87b3e2019-10-01 16:59:28 -07001394 sp<IBinder> getToken() { return mInfo.token; }
1395
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001396 const std::string& getName() { return mName; }
1397
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00001398 void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001399 mInfo.ownerPid = ownerPid;
1400 mInfo.ownerUid = ownerUid;
1401 }
1402
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00001403 gui::Pid getPid() const { return mInfo.ownerPid; }
Prabir Pradhanedd96402022-02-15 01:46:16 -08001404
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001405 void destroyReceiver() { mInputReceiver = nullptr; }
1406
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001407 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1408
chaviwd1c23182019-12-20 18:44:56 -08001409private:
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001410 FakeWindowHandle(std::string name) : mName(name){};
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001411 const std::string mName;
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001412 std::shared_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001413 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07001414 friend class sp<FakeWindowHandle>;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001415};
1416
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001417std::atomic<int32_t> FakeWindowHandle::sId{1};
1418
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001419class FakeMonitorReceiver {
1420public:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001421 FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId)
1422 : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {}
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001423
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001424 sp<IBinder> getToken() { return mInputReceiver.getToken(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001425
1426 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001427 mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
1428 expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001429 }
1430
1431 std::optional<int32_t> receiveEvent() {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001432 return mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001433 }
1434
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001435 void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001436
1437 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001438 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
1439 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001440 }
1441
1442 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001443 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
1444 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001445 }
1446
1447 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001448 mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
1449 expectedDisplayId, expectedFlags);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001450 }
1451
1452 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001453 mInputReceiver.consumeMotionEvent(
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001454 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
1455 WithDisplayId(expectedDisplayId),
1456 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
1457 }
1458
1459 void consumeMotionPointerDown(int32_t pointerIdx) {
1460 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1461 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001462 mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
1463 /*expectedFlags=*/0);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001464 }
1465
1466 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001467 mInputReceiver.consumeMotionEvent(matcher);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001468 }
1469
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001470 MotionEvent* consumeMotion() { return mInputReceiver.consumeMotion(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001471
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001472 void assertNoEvents() { mInputReceiver.assertNoEvents(); }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001473
1474private:
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001475 FakeInputReceiver mInputReceiver;
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00001476};
1477
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001478static InputEventInjectionResult injectKey(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001479 InputDispatcher& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001480 int32_t displayId = ADISPLAY_ID_NONE,
1481 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001482 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001483 bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {},
Prabir Pradhan5735a322022-04-11 17:23:34 +00001484 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001485 KeyEvent event;
1486 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1487
1488 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001489 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001490 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1491 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001492
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001493 if (!allowKeyRepeat) {
1494 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1495 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001496 // Inject event until dispatch out.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001497 return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001498}
1499
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001500static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) {
1501 InputEventInjectionResult result =
1502 injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE,
1503 InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED);
1504 if (result != InputEventInjectionResult::TIMED_OUT) {
1505 FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result);
1506 }
1507}
1508
1509static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001510 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001511 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001512}
1513
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001514// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1515// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1516// has to be woken up to process the repeating key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001517static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher,
1518 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001519 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001520 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001521 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001522}
1523
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001524static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001525 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001526 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001527}
1528
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001529static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001530 InputDispatcher& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001531 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001532 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001533 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001534 return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1535 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001536}
1537
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001538static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001539 InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId,
1540 const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001541 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001542 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1543 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001544 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001545 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00001546 std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001547 MotionEventBuilder motionBuilder =
1548 MotionEventBuilder(action, source)
1549 .displayId(displayId)
1550 .eventTime(eventTime)
1551 .rawXCursorPosition(cursorPosition.x)
1552 .rawYCursorPosition(cursorPosition.y)
1553 .pointer(
1554 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1555 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1556 motionBuilder.downTime(eventTime);
1557 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001558
1559 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001560 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1561 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001562}
1563
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001564static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source,
1565 int32_t displayId,
1566 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001567 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001568}
1569
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001570static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source,
1571 int32_t displayId,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001572 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001573 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001574}
1575
Jackal Guof9696682018-10-05 12:23:23 +08001576static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1577 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1578 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001579 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001580 displayId, POLICY_FLAG_PASS_TO_USER, action, /*flags=*/0, AKEYCODE_A, KEY_A,
1581 AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001582
1583 return args;
1584}
1585
Josep del Riob3981622023-04-18 15:49:45 +00001586static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1587 int32_t displayId = ADISPLAY_ID_NONE) {
1588 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1589 // Define a valid key event.
1590 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001591 displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, AMETA_META_ON,
Josep del Riob3981622023-04-18 15:49:45 +00001592 currentTime);
1593
1594 return args;
1595}
1596
1597static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1598 int32_t displayId = ADISPLAY_ID_NONE) {
1599 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1600 // Define a valid key event.
1601 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001602 displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, KEY_ASSISTANT,
Josep del Riob3981622023-04-18 15:49:45 +00001603 AMETA_NONE, currentTime);
1604
1605 return args;
1606}
1607
Prabir Pradhan678438e2023-04-13 19:32:51 +00001608[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1609 int32_t displayId,
1610 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001611 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001612 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1613 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1614 }
1615
chaviwd1c23182019-12-20 18:44:56 -08001616 PointerProperties pointerProperties[pointerCount];
1617 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001618
chaviwd1c23182019-12-20 18:44:56 -08001619 for (size_t i = 0; i < pointerCount; i++) {
1620 pointerProperties[i].clear();
1621 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001622 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001623
chaviwd1c23182019-12-20 18:44:56 -08001624 pointerCoords[i].clear();
1625 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1626 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1627 }
Jackal Guof9696682018-10-05 12:23:23 +08001628
1629 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1630 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001631 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001632 POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, /*flags=*/0,
1633 AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001634 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001635 pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001636 AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +00001637 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{});
Jackal Guof9696682018-10-05 12:23:23 +08001638
1639 return args;
1640}
1641
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001642static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1643 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1644}
1645
chaviwd1c23182019-12-20 18:44:56 -08001646static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1647 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1648}
1649
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001650static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1651 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001652 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001653}
1654
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -07001655} // namespace
1656
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001657/**
1658 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1659 * broken channel.
1660 */
1661TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1662 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1663 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001664 sp<FakeWindowHandle>::make(application, mDispatcher,
1665 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001666
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001667 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001668
1669 // Window closes its channel, but the window remains.
1670 window->destroyReceiver();
1671 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1672}
1673
Arthur Hungb92218b2018-08-14 12:00:21 +08001674TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001675 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001676 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1677 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001678
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001679 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001680 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001681 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001682 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001683
1684 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001685 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001686}
1687
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001688TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1689 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001690 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1691 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001692
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001693 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001694 // Inject a MotionEvent to an unknown display.
1695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001696 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001697 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1698
1699 // Window should receive motion event.
1700 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1701}
1702
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001703/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001704 * Calling onWindowInfosChanged once should not cause any issues.
1705 * This test serves as a sanity check for the next test, where onWindowInfosChanged is
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001706 * called twice.
1707 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001708TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001709 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001710 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1711 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001712 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001713
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001714 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001715 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001716 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001717 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001718 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001719
1720 // Window should receive motion event.
1721 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1722}
1723
1724/**
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001725 * Calling onWindowInfosChanged twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001726 */
1727TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001728 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001729 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1730 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001731 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001732
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001733 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
1734 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001735 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001736 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001737 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001739
1740 // Window should receive motion event.
1741 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1742}
1743
Arthur Hungb92218b2018-08-14 12:00:21 +08001744// The foreground window should receive the first touch down event.
1745TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001746 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001747 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001748 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001749 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001750 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001751
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001752 mDispatcher->onWindowInfosChanged(
1753 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001755 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001756 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001757
1758 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001759 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001760 windowSecond->assertNoEvents();
1761}
1762
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001763/**
1764 * Two windows: A top window, and a wallpaper behind the window.
1765 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1766 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001767 * 1. foregroundWindow <-- dup touch to wallpaper
1768 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001769 */
1770TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1771 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1772 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001773 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001774 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001775 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001776 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001777 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001778
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001779 mDispatcher->onWindowInfosChanged(
1780 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001781 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001782 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001783 {100, 200}))
1784 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1785
1786 // Both foreground window and its wallpaper should receive the touch down
1787 foregroundWindow->consumeMotionDown();
1788 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1789
1790 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001791 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001792 ADISPLAY_ID_DEFAULT, {110, 200}))
1793 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1794
1795 foregroundWindow->consumeMotionMove();
1796 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1797
1798 // Now the foreground window goes away, but the wallpaper stays
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001799 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001800 foregroundWindow->consumeMotionCancel();
1801 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1802 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1803}
1804
1805/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001806 * Two fingers down on the window, and lift off the first finger.
1807 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1808 * contains a single pointer.
1809 */
1810TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1811 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1812 sp<FakeWindowHandle> window =
1813 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1814
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001815 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001816 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001817 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1818 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1819 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001820 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001821 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1822 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1823 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1824 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001825 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001826 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1827 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1828 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
1829 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001830 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
1831 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
1832 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
1833
1834 // Remove the window. The gesture should be canceled
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001835 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001836 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
1837 window->consumeMotionEvent(
1838 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
1839}
1840
1841/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001842 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
1843 * with the following differences:
1844 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
1845 * clean up the connection.
1846 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
1847 * Ensure that there's no crash in the dispatcher.
1848 */
1849TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
1850 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1851 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001852 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001853 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001854 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001855 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001856 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001857
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001858 mDispatcher->onWindowInfosChanged(
1859 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001860 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001861 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001862 {100, 200}))
1863 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1864
1865 // Both foreground window and its wallpaper should receive the touch down
1866 foregroundWindow->consumeMotionDown();
1867 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1868
1869 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001870 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001871 ADISPLAY_ID_DEFAULT, {110, 200}))
1872 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1873
1874 foregroundWindow->consumeMotionMove();
1875 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1876
1877 // Wallpaper closes its channel, but the window remains.
1878 wallpaperWindow->destroyReceiver();
1879 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
1880
1881 // Now the foreground window goes away, but the wallpaper stays, even though its channel
1882 // is no longer valid.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001883 mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001884 foregroundWindow->consumeMotionCancel();
1885}
1886
Arthur Hungc539dbb2022-12-08 07:45:36 +00001887class ShouldSplitTouchFixture : public InputDispatcherTest,
1888 public ::testing::WithParamInterface<bool> {};
1889INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
1890 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08001891/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001892 * A single window that receives touch (on top), and a wallpaper window underneath it.
1893 * The top window gets a multitouch gesture.
1894 * Ensure that wallpaper gets the same gesture.
1895 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00001896TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001897 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00001898 sp<FakeWindowHandle> foregroundWindow =
1899 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
1900 foregroundWindow->setDupTouchToWallpaper(true);
1901 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001902
1903 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001904 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001905 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001906
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001907 mDispatcher->onWindowInfosChanged(
1908 {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001909
1910 // Touch down on top window
1911 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001912 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001913 {100, 100}))
1914 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1915
1916 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00001917 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001918 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1919
1920 // Second finger down on the top window
1921 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001922 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001923 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001924 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1925 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001926 .build();
1927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001928 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001929 InputEventInjectionSync::WAIT_FOR_RESULT))
1930 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1931
Harry Cutts33476232023-01-30 19:57:29 +00001932 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
1933 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001934 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00001935
1936 const MotionEvent secondFingerUpEvent =
1937 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
1938 .displayId(ADISPLAY_ID_DEFAULT)
1939 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001940 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
1941 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001942 .build();
1943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001944 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00001945 InputEventInjectionSync::WAIT_FOR_RESULT))
1946 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1947 foregroundWindow->consumeMotionPointerUp(0);
1948 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1949
1950 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001951 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001952 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
1953 AINPUT_SOURCE_TOUCHSCREEN)
1954 .displayId(ADISPLAY_ID_DEFAULT)
1955 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Harry Cutts101ee9b2023-07-06 18:04:14 +00001956 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08001957 .x(100)
1958 .y(100))
1959 .build(),
1960 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00001961 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1962 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
1963 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001964}
1965
1966/**
1967 * Two windows: a window on the left and window on the right.
1968 * A third window, wallpaper, is behind both windows, and spans both top windows.
1969 * The first touch down goes to the left window. A second pointer touches down on the right window.
1970 * The touch is split, so both left and right windows should receive ACTION_DOWN.
1971 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
1972 * ACTION_POINTER_DOWN(1).
1973 */
1974TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
1975 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1976 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001977 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001978 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001979 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001980
1981 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001982 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001983 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001984 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001985
1986 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001987 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001988 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001989 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001990
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07001991 mDispatcher->onWindowInfosChanged(
1992 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
1993 {},
1994 0,
1995 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001996
1997 // Touch down on left window
1998 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07001999 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002000 {100, 100}))
2001 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2002
2003 // Both foreground window and its wallpaper should receive the touch down
2004 leftWindow->consumeMotionDown();
2005 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2006
2007 // Second finger down on the right window
2008 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002009 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002010 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002011 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2012 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002013 .build();
2014 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002015 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002016 InputEventInjectionSync::WAIT_FOR_RESULT))
2017 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2018
2019 leftWindow->consumeMotionMove();
2020 // Since the touch is split, right window gets ACTION_DOWN
2021 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002022 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002023 expectedWallpaperFlags);
2024
2025 // Now, leftWindow, which received the first finger, disappears.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002026 mDispatcher->onWindowInfosChanged(
2027 {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002028 leftWindow->consumeMotionCancel();
2029 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2030 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2031
2032 // The pointer that's still down on the right window moves, and goes to the right window only.
2033 // As far as the dispatcher's concerned though, both pointers are still present.
2034 const MotionEvent secondFingerMoveEvent =
2035 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2036 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002037 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2038 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002039 .build();
2040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002041 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002042 InputEventInjectionSync::WAIT_FOR_RESULT));
2043 rightWindow->consumeMotionMove();
2044
2045 leftWindow->assertNoEvents();
2046 rightWindow->assertNoEvents();
2047 wallpaperWindow->assertNoEvents();
2048}
2049
Arthur Hungc539dbb2022-12-08 07:45:36 +00002050/**
2051 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2052 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2053 * The right window should receive ACTION_DOWN.
2054 */
2055TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002056 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002057 sp<FakeWindowHandle> leftWindow =
2058 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2059 leftWindow->setFrame(Rect(0, 0, 200, 200));
2060 leftWindow->setDupTouchToWallpaper(true);
2061 leftWindow->setSlippery(true);
2062
2063 sp<FakeWindowHandle> rightWindow =
2064 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2065 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002066
2067 sp<FakeWindowHandle> wallpaperWindow =
2068 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2069 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002070
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002071 mDispatcher->onWindowInfosChanged(
2072 {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()},
2073 {},
2074 0,
2075 0});
Arthur Hung74c248d2022-11-23 07:09:59 +00002076
Arthur Hungc539dbb2022-12-08 07:45:36 +00002077 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002078 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002079 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002080 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002081 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002082
2083 // Both foreground window and its wallpaper should receive the touch down
2084 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002085 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2086
Arthur Hungc539dbb2022-12-08 07:45:36 +00002087 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002088 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002089 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002090 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002091 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2092
Arthur Hungc539dbb2022-12-08 07:45:36 +00002093 leftWindow->consumeMotionCancel();
2094 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2095 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002096}
2097
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002098/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002099 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2100 * interactive, it might stop sending this flag.
2101 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2102 * to have a consistent input stream.
2103 *
2104 * Test procedure:
2105 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2106 * DOWN (new gesture).
2107 *
2108 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2109 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2110 *
2111 * We technically just need a single window here, but we are using two windows (spy on top and a
2112 * regular window below) to emulate the actual situation where it happens on the device.
2113 */
2114TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2115 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2116 sp<FakeWindowHandle> spyWindow =
2117 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2118 spyWindow->setFrame(Rect(0, 0, 200, 200));
2119 spyWindow->setTrustedOverlay(true);
2120 spyWindow->setSpy(true);
2121
2122 sp<FakeWindowHandle> window =
2123 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2124 window->setFrame(Rect(0, 0, 200, 200));
2125
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002126 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002127 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002128
2129 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002130 mDispatcher->notifyMotion(
2131 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2132 .deviceId(touchDeviceId)
2133 .policyFlags(DEFAULT_POLICY_FLAGS)
2134 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2135 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002136
Prabir Pradhan678438e2023-04-13 19:32:51 +00002137 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2138 .deviceId(touchDeviceId)
2139 .policyFlags(DEFAULT_POLICY_FLAGS)
2140 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2141 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2142 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002143 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2144 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2145 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2146 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2147
2148 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002149 mDispatcher->notifyMotion(
2150 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2151 .deviceId(touchDeviceId)
2152 .policyFlags(0)
2153 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2154 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2155 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002156 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2157 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2158
2159 // We don't need to reset the device to reproduce the issue, but the reset event typically
2160 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002161 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002162
2163 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002164 mDispatcher->notifyMotion(
2165 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2166 .deviceId(touchDeviceId)
2167 .policyFlags(DEFAULT_POLICY_FLAGS)
2168 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2169 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002170 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2171 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2172
2173 // No more events
2174 spyWindow->assertNoEvents();
2175 window->assertNoEvents();
2176}
2177
2178/**
Linnan Li907ae732023-09-05 17:14:21 +08002179 * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers.
2180 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2181 * interactive, it might stop sending this flag.
2182 * We've already ensured the consistency of the touch event in this case, and we should also ensure
2183 * the consistency of the hover event in this case.
2184 *
2185 * Test procedure:
2186 * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT
2187 * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT
2188 *
2189 * We expect to receive two full streams of hover events.
2190 */
2191TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) {
2192 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2193
2194 sp<FakeWindowHandle> window =
2195 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2196 window->setFrame(Rect(0, 0, 300, 300));
2197
2198 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2199
2200 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2201 .policyFlags(DEFAULT_POLICY_FLAGS)
2202 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2203 .build());
2204 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2205
2206 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2207 .policyFlags(DEFAULT_POLICY_FLAGS)
2208 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2209 .build());
2210 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2211
2212 // Send hover exit without the default policy flags.
2213 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2214 .policyFlags(0)
2215 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2216 .build());
2217
2218 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2219
2220 // Send a simple hover event stream, ensure dispatcher not crashed and window can receive
2221 // right event.
2222 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2223 .policyFlags(DEFAULT_POLICY_FLAGS)
2224 .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201))
2225 .build());
2226 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2227
2228 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2229 .policyFlags(DEFAULT_POLICY_FLAGS)
2230 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
2231 .build());
2232 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2233
2234 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2235 .policyFlags(DEFAULT_POLICY_FLAGS)
2236 .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202))
2237 .build());
2238 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2239}
2240
2241/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002242 * Two windows: a window on the left and a window on the right.
2243 * Mouse is hovered from the right window into the left window.
2244 * Next, we tap on the left window, where the cursor was last seen.
2245 * The second tap is done onto the right window.
2246 * The mouse and tap are from two different devices.
2247 * We technically don't need to set the downtime / eventtime for these events, but setting these
2248 * explicitly helps during debugging.
2249 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2250 * In the buggy implementation, a tap on the right window would cause a crash.
2251 */
2252TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2253 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2254 sp<FakeWindowHandle> leftWindow =
2255 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2256 leftWindow->setFrame(Rect(0, 0, 200, 200));
2257
2258 sp<FakeWindowHandle> rightWindow =
2259 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2260 rightWindow->setFrame(Rect(200, 0, 400, 200));
2261
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002262 mDispatcher->onWindowInfosChanged(
2263 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002264 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2265 // stale.
2266 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2267 const int32_t mouseDeviceId = 6;
2268 const int32_t touchDeviceId = 4;
2269 // Move the cursor from right
2270 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002271 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002272 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2273 AINPUT_SOURCE_MOUSE)
2274 .deviceId(mouseDeviceId)
2275 .downTime(baseTime + 10)
2276 .eventTime(baseTime + 20)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002277 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002278 .build()));
2279 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2280
2281 // .. to the left window
2282 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002283 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002284 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2285 AINPUT_SOURCE_MOUSE)
2286 .deviceId(mouseDeviceId)
2287 .downTime(baseTime + 10)
2288 .eventTime(baseTime + 30)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002289 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002290 .build()));
2291 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2292 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2293 // Now tap the left window
2294 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002295 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002296 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2297 AINPUT_SOURCE_TOUCHSCREEN)
2298 .deviceId(touchDeviceId)
2299 .downTime(baseTime + 40)
2300 .eventTime(baseTime + 40)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002301 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002302 .build()));
2303 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2304 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2305
2306 // release tap
2307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002308 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002309 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2310 AINPUT_SOURCE_TOUCHSCREEN)
2311 .deviceId(touchDeviceId)
2312 .downTime(baseTime + 40)
2313 .eventTime(baseTime + 50)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002314 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002315 .build()));
2316 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2317
2318 // Tap the window on the right
2319 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002320 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002321 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2322 AINPUT_SOURCE_TOUCHSCREEN)
2323 .deviceId(touchDeviceId)
2324 .downTime(baseTime + 60)
2325 .eventTime(baseTime + 60)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002326 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002327 .build()));
2328 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2329
2330 // release tap
2331 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002332 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002333 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2334 AINPUT_SOURCE_TOUCHSCREEN)
2335 .deviceId(touchDeviceId)
2336 .downTime(baseTime + 60)
2337 .eventTime(baseTime + 70)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07002338 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002339 .build()));
2340 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2341
2342 // No more events
2343 leftWindow->assertNoEvents();
2344 rightWindow->assertNoEvents();
2345}
2346
2347/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002348 * Start hovering in a window. While this hover is still active, make another window appear on top.
2349 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2350 * While the top window is present, the hovering is stopped.
2351 * Later, hovering gets resumed again.
2352 * Ensure that new hover gesture is handled correctly.
2353 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2354 * to the window that's currently being hovered over.
2355 */
2356TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2357 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2358 sp<FakeWindowHandle> window =
2359 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2360 window->setFrame(Rect(0, 0, 200, 200));
2361
2362 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002363 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002364
2365 // Start hovering in the window
2366 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2367 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2368 .build());
2369 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2370
2371 // Now, an obscuring window appears!
2372 sp<FakeWindowHandle> obscuringWindow =
2373 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2374 ADISPLAY_ID_DEFAULT,
2375 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2376 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2377 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2378 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2379 obscuringWindow->setNoInputChannel(true);
2380 obscuringWindow->setFocusable(false);
2381 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002382 mDispatcher->onWindowInfosChanged(
2383 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002384
2385 // While this new obscuring window is present, the hovering is stopped
2386 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2387 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2388 .build());
2389 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2390
2391 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002392 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002393
2394 // And a new hover gesture starts.
2395 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2396 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2397 .build());
2398 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2399}
2400
2401/**
2402 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2403 * the obscuring window.
2404 */
2405TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2406 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2407 sp<FakeWindowHandle> window =
2408 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2409 window->setFrame(Rect(0, 0, 200, 200));
2410
2411 // Only a single window is present at first
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002412 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002413
2414 // Start hovering in the window
2415 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2416 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2417 .build());
2418 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2419
2420 // Now, an obscuring window appears!
2421 sp<FakeWindowHandle> obscuringWindow =
2422 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2423 ADISPLAY_ID_DEFAULT,
2424 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2425 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2426 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2427 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2428 obscuringWindow->setNoInputChannel(true);
2429 obscuringWindow->setFocusable(false);
2430 obscuringWindow->setAlpha(1.0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002431 mDispatcher->onWindowInfosChanged(
2432 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002433
2434 // While this new obscuring window is present, the hovering continues. The event can't go to the
2435 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2436 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2437 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2438 .build());
2439 obscuringWindow->assertNoEvents();
2440 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2441
2442 // Now the obscuring window goes away.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002443 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002444
2445 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2446 // so it should generate a HOVER_ENTER
2447 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2448 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2449 .build());
2450 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2451
2452 // Now the MOVE should be getting dispatched normally
2453 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2454 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2455 .build());
2456 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2457}
2458
2459/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002460 * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll
2461 * events are delivered to the window.
2462 */
2463TEST_F(InputDispatcherTest, HoverMoveAndScroll) {
2464 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2465 sp<FakeWindowHandle> window =
2466 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2467 window->setFrame(Rect(0, 0, 200, 200));
2468 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2469
2470 // Start hovering in the window
2471 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2472 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
2473 .build());
2474 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2475
2476 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
2477 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2478 .build());
2479 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2480
2481 // Scroll with the mouse
2482 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE)
2483 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2484 .build());
2485 window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL));
2486}
2487
2488using InputDispatcherMultiDeviceTest = InputDispatcherTest;
2489
2490/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002491 * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that
2492 * touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002493 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002494TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002495 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2496 sp<FakeWindowHandle> window =
2497 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2498 window->setFrame(Rect(0, 0, 200, 200));
2499
2500 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2501
2502 constexpr int32_t touchDeviceId = 4;
2503 constexpr int32_t stylusDeviceId = 2;
2504
2505 // Stylus down
2506 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2507 .deviceId(stylusDeviceId)
2508 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2509 .build());
2510 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2511
2512 // Touch down
2513 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2514 .deviceId(touchDeviceId)
2515 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2516 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002517
2518 // Touch move
2519 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2520 .deviceId(touchDeviceId)
2521 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2522 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002523 // Touch is ignored because stylus is already down
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002524
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002525 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002526 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2527 .deviceId(stylusDeviceId)
2528 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2529 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002530 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2531 WithCoords(101, 111)));
2532
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002533 window->assertNoEvents();
2534}
2535
2536/**
2537 * One window and one spy window. Stylus down on the window. Next, touch from another device goes
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002538 * down. Ensure that touch is dropped, because stylus should be preferred over touch.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002539 * Similar test as above, but with added SPY window.
2540 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002541TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002542 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2543 sp<FakeWindowHandle> window =
2544 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2545 sp<FakeWindowHandle> spyWindow =
2546 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2547 spyWindow->setFrame(Rect(0, 0, 200, 200));
2548 spyWindow->setTrustedOverlay(true);
2549 spyWindow->setSpy(true);
2550 window->setFrame(Rect(0, 0, 200, 200));
2551
2552 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
2553
2554 constexpr int32_t touchDeviceId = 4;
2555 constexpr int32_t stylusDeviceId = 2;
2556
2557 // Stylus down
2558 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2559 .deviceId(stylusDeviceId)
2560 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2561 .build());
2562 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2563 spyWindow->consumeMotionEvent(
2564 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2565
2566 // Touch down
2567 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2568 .deviceId(touchDeviceId)
2569 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2570 .build());
2571
2572 // Touch move
2573 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2574 .deviceId(touchDeviceId)
2575 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2576 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002577
2578 // Touch is ignored because stylus is already down
2579
2580 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002581 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2582 .deviceId(stylusDeviceId)
2583 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2584 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002585 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2586 WithCoords(101, 111)));
2587 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2588 WithCoords(101, 111)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002589
2590 window->assertNoEvents();
2591 spyWindow->assertNoEvents();
2592}
2593
2594/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002595 * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that
2596 * touch is not dropped, because stylus hover should be ignored.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002597 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002598TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDoesNotBlockTouchDown) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002599 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2600 sp<FakeWindowHandle> window =
2601 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2602 window->setFrame(Rect(0, 0, 200, 200));
2603
2604 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2605
2606 constexpr int32_t touchDeviceId = 4;
2607 constexpr int32_t stylusDeviceId = 2;
2608
2609 // Stylus down on the window
2610 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2611 .deviceId(stylusDeviceId)
2612 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2613 .build());
2614 window->consumeMotionEvent(
2615 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2616
2617 // Touch down on window
2618 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2619 .deviceId(touchDeviceId)
2620 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2621 .build());
2622 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2623 .deviceId(touchDeviceId)
2624 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2625 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002626
2627 // Stylus hover is canceled because touch is down
2628 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT),
2629 WithDeviceId(stylusDeviceId), WithCoords(100, 110)));
2630 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId),
2631 WithCoords(140, 145)));
2632 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId),
2633 WithCoords(141, 146)));
2634
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002635 // Subsequent stylus movements are ignored
2636 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2637 .deviceId(stylusDeviceId)
2638 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2639 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002640
2641 // but subsequent touches continue to be delivered
2642 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2643 .deviceId(touchDeviceId)
2644 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2645 .build());
2646 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId),
2647 WithCoords(142, 147)));
2648}
2649
2650/**
2651 * One window. Touch down on the window. Then, stylus hover on the window from another device.
2652 * Ensure that touch is not canceled, because stylus hover should be dropped.
2653 */
2654TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusHover) {
2655 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2656 sp<FakeWindowHandle> window =
2657 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2658 window->setFrame(Rect(0, 0, 200, 200));
2659
2660 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2661
2662 constexpr int32_t touchDeviceId = 4;
2663 constexpr int32_t stylusDeviceId = 2;
2664
2665 // Touch down on window
2666 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2667 .deviceId(touchDeviceId)
2668 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2669 .build());
2670 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2671 .deviceId(touchDeviceId)
2672 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2673 .build());
2674 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2675 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2676
2677 // Stylus hover on the window
2678 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2679 .deviceId(stylusDeviceId)
2680 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2681 .build());
2682 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2683 .deviceId(stylusDeviceId)
2684 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2685 .build());
2686 // Stylus hover movement is dropped
2687
2688 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2689 .deviceId(touchDeviceId)
2690 .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147))
2691 .build());
2692 // Subsequent touch movements are delivered correctly
2693 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId),
2694 WithCoords(142, 147)));
2695}
2696
2697/**
2698 * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that
2699 * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should
2700 * become active.
2701 */
2702TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) {
2703 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2704 sp<FakeWindowHandle> window =
2705 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2706 window->setFrame(Rect(0, 0, 200, 200));
2707
2708 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2709
2710 constexpr int32_t stylusDeviceId1 = 3;
2711 constexpr int32_t stylusDeviceId2 = 5;
2712
2713 // Touch down on window
2714 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2715 .deviceId(stylusDeviceId1)
2716 .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100))
2717 .build());
2718 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2719 .deviceId(stylusDeviceId1)
2720 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101))
2721 .build());
2722 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1)));
2723 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1)));
2724
2725 // Second stylus down
2726 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2727 .deviceId(stylusDeviceId2)
2728 .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10))
2729 .build());
2730 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2731 .deviceId(stylusDeviceId2)
2732 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11))
2733 .build());
2734
2735 // First stylus is canceled, second one takes over.
2736 window->consumeMotionEvent(
2737 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1)));
2738 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2)));
2739 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2)));
2740
2741 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2742 .deviceId(stylusDeviceId1)
2743 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102))
2744 .build());
2745 // Subsequent stylus movements are delivered correctly
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002746 window->assertNoEvents();
2747}
2748
2749/**
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002750 * One window. Touch down on the window. Then, stylus down on the window from another device.
2751 * Ensure that is canceled, because stylus down should be preferred over touch.
2752 */
2753TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) {
2754 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2755 sp<FakeWindowHandle> window =
2756 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2757 window->setFrame(Rect(0, 0, 200, 200));
2758
2759 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
2760
2761 constexpr int32_t touchDeviceId = 4;
2762 constexpr int32_t stylusDeviceId = 2;
2763
2764 // Touch down on window
2765 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2766 .deviceId(touchDeviceId)
2767 .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145))
2768 .build());
2769 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2770 .deviceId(touchDeviceId)
2771 .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146))
2772 .build());
2773 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2774 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
2775
2776 // Stylus down on the window
2777 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2778 .deviceId(stylusDeviceId)
2779 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110))
2780 .build());
2781 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
2782 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2783
2784 // Subsequent stylus movements are delivered correctly
2785 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2786 .deviceId(stylusDeviceId)
2787 .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111))
2788 .build());
2789 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId),
2790 WithCoords(101, 111)));
2791}
2792
2793/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002794 * Two windows: a window on the left and a window on the right.
2795 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2796 * down. Then, on the left window, also place second touch pointer down.
2797 * This test tries to reproduce a crash.
2798 * In the buggy implementation, second pointer down on the left window would cause a crash.
2799 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002800TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002801 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2802 sp<FakeWindowHandle> leftWindow =
2803 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2804 leftWindow->setFrame(Rect(0, 0, 200, 200));
2805
2806 sp<FakeWindowHandle> rightWindow =
2807 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2808 rightWindow->setFrame(Rect(200, 0, 400, 200));
2809
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07002810 mDispatcher->onWindowInfosChanged(
2811 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002812
2813 const int32_t touchDeviceId = 4;
2814 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002815
2816 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002817 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2818 .deviceId(mouseDeviceId)
2819 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2820 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002821 leftWindow->consumeMotionEvent(
2822 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2823
2824 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002825 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2826 .deviceId(mouseDeviceId)
2827 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2828 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2829 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002830
2831 leftWindow->consumeMotionEvent(
2832 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2833 leftWindow->consumeMotionEvent(
2834 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2835
Prabir Pradhan678438e2023-04-13 19:32:51 +00002836 mDispatcher->notifyMotion(
2837 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2838 .deviceId(mouseDeviceId)
2839 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2840 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2841 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2842 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002843 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2844
2845 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002846 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2847 .deviceId(touchDeviceId)
2848 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2849 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002850 leftWindow->assertNoEvents();
2851
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002852 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2853
2854 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002855 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2856 .deviceId(touchDeviceId)
2857 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2858 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2859 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002860 // Since this is now a new splittable pointer going down on the left window, and it's coming
2861 // from a different device, the current gesture in the left window (pointer down) should first
2862 // be canceled.
2863 leftWindow->consumeMotionEvent(
2864 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002865 leftWindow->consumeMotionEvent(
2866 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2867 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2868 // current implementation.
2869 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2870 rightWindow->consumeMotionEvent(
2871 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2872
2873 leftWindow->assertNoEvents();
2874 rightWindow->assertNoEvents();
2875}
2876
2877/**
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002878 * Two windows: a window on the left and a window on the right.
2879 * Mouse is hovered on the left window and stylus is hovered on the right window.
2880 */
2881TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) {
2882 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2883 sp<FakeWindowHandle> leftWindow =
2884 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2885 leftWindow->setFrame(Rect(0, 0, 200, 200));
2886
2887 sp<FakeWindowHandle> rightWindow =
2888 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2889 rightWindow->setFrame(Rect(200, 0, 400, 200));
2890
2891 mDispatcher->onWindowInfosChanged(
2892 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
2893
2894 const int32_t stylusDeviceId = 3;
2895 const int32_t mouseDeviceId = 6;
2896
2897 // Start hovering over the left window
2898 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2899 .deviceId(mouseDeviceId)
2900 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
2901 .build());
2902 leftWindow->consumeMotionEvent(
2903 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2904
2905 // Stylus hovered on right window
2906 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2907 .deviceId(stylusDeviceId)
2908 .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100))
2909 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002910 rightWindow->consumeMotionEvent(
2911 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
2912
2913 // Subsequent HOVER_MOVE events are dispatched correctly.
2914 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
2915 .deviceId(mouseDeviceId)
2916 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120))
2917 .build());
2918 leftWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002919 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002920
2921 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2922 .deviceId(stylusDeviceId)
2923 .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110))
2924 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002925 rightWindow->consumeMotionEvent(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002926 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002927
2928 leftWindow->assertNoEvents();
2929 rightWindow->assertNoEvents();
2930}
2931
2932/**
2933 * Three windows: a window on the left and a window on the right.
2934 * And a spy window that's positioned above all of them.
2935 * Stylus down on the left window and remains down. Touch goes down on the right and remains down.
2936 * Check the stream that's received by the spy.
2937 */
2938TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) {
2939 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2940
2941 sp<FakeWindowHandle> spyWindow =
2942 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2943 spyWindow->setFrame(Rect(0, 0, 400, 400));
2944 spyWindow->setTrustedOverlay(true);
2945 spyWindow->setSpy(true);
2946
2947 sp<FakeWindowHandle> leftWindow =
2948 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2949 leftWindow->setFrame(Rect(0, 0, 200, 200));
2950
2951 sp<FakeWindowHandle> rightWindow =
2952 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2953
2954 rightWindow->setFrame(Rect(200, 0, 400, 200));
2955
2956 mDispatcher->onWindowInfosChanged(
2957 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
2958
2959 const int32_t stylusDeviceId = 1;
2960 const int32_t touchDeviceId = 2;
2961
2962 // Stylus down on the left window
2963 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2964 .deviceId(stylusDeviceId)
2965 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2966 .build());
2967 leftWindow->consumeMotionEvent(
2968 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2969 spyWindow->consumeMotionEvent(
2970 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
2971
2972 // Touch down on the right window
2973 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2974 .deviceId(touchDeviceId)
2975 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2976 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002977 leftWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002978 rightWindow->consumeMotionEvent(
2979 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002980
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002981 // Spy window does not receive touch events, because stylus events take precedence, and it
2982 // already has an active stylus gesture.
2983
2984 // Stylus movements continue. They should be delivered to the left window and to the spy window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002985 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS)
2986 .deviceId(stylusDeviceId)
2987 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2988 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002989 leftWindow->consumeMotionEvent(
2990 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
2991 spyWindow->consumeMotionEvent(
2992 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002993
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002994 // Further MOVE events keep going to the right window only
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07002995 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2996 .deviceId(touchDeviceId)
2997 .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110))
2998 .build());
2999 rightWindow->consumeMotionEvent(
3000 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003001
3002 spyWindow->assertNoEvents();
3003 leftWindow->assertNoEvents();
3004 rightWindow->assertNoEvents();
3005}
3006
3007/**
3008 * Three windows: a window on the left, a window on the right, and a spy window positioned above
3009 * both.
3010 * Check hover in left window and touch down in the right window.
3011 * At first, spy should receive hover, but the touch down should cancel hovering inside spy.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003012 * At the same time, left and right should be getting independent streams of hovering and touch,
3013 * respectively.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003014 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003015TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlockedByTouchWithSpy) {
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003016 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3017
3018 sp<FakeWindowHandle> spyWindow =
3019 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3020 spyWindow->setFrame(Rect(0, 0, 400, 400));
3021 spyWindow->setTrustedOverlay(true);
3022 spyWindow->setSpy(true);
3023
3024 sp<FakeWindowHandle> leftWindow =
3025 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3026 leftWindow->setFrame(Rect(0, 0, 200, 200));
3027
3028 sp<FakeWindowHandle> rightWindow =
3029 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3030 rightWindow->setFrame(Rect(200, 0, 400, 200));
3031
3032 mDispatcher->onWindowInfosChanged(
3033 {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
3034
3035 const int32_t stylusDeviceId = 1;
3036 const int32_t touchDeviceId = 2;
3037
3038 // Stylus hover on the left window
3039 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3040 .deviceId(stylusDeviceId)
3041 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
3042 .build());
3043 leftWindow->consumeMotionEvent(
3044 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3045 spyWindow->consumeMotionEvent(
3046 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId)));
3047
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003048 // Touch down on the right window. Spy doesn't receive this touch because it already has
3049 // stylus hovering there.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003050 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3051 .deviceId(touchDeviceId)
3052 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3053 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003054 leftWindow->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003055 spyWindow->consumeMotionEvent(
3056 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId)));
3057 spyWindow->consumeMotionEvent(
3058 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3059 rightWindow->consumeMotionEvent(
3060 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3061
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003062 // Stylus movements continue. They should be delivered to the left window only.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003063 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
3064 .deviceId(stylusDeviceId)
3065 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
3066 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003067 leftWindow->consumeMotionEvent(
3068 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003069
3070 // Touch movements continue. They should be delivered to the right window and to the spy
3071 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3072 .deviceId(touchDeviceId)
3073 .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101))
3074 .build());
3075 spyWindow->consumeMotionEvent(
3076 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
3077 rightWindow->consumeMotionEvent(
3078 AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId)));
3079
3080 spyWindow->assertNoEvents();
3081 leftWindow->assertNoEvents();
3082 rightWindow->assertNoEvents();
3083}
3084
3085/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003086 * On a single window, use two different devices: mouse and touch.
3087 * Touch happens first, with two pointers going down, and then the first pointer leaving.
3088 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
3089 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
3090 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
3091 * represent a new gesture.
3092 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003093TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003094 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3095 sp<FakeWindowHandle> window =
3096 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3097 window->setFrame(Rect(0, 0, 400, 400));
3098
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003099 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003100
3101 const int32_t touchDeviceId = 4;
3102 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003103
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003104 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003105 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3106 .deviceId(touchDeviceId)
3107 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3108 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003109 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003110 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3111 .deviceId(touchDeviceId)
3112 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3113 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3114 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003115 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003116 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
3117 .deviceId(touchDeviceId)
3118 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3119 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3120 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003121 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3122 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3123 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
3124
3125 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00003126 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3127 .deviceId(mouseDeviceId)
3128 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3129 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3130 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003131
3132 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08003133 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003134 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3135
Prabir Pradhan678438e2023-04-13 19:32:51 +00003136 mDispatcher->notifyMotion(
3137 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3138 .deviceId(mouseDeviceId)
3139 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3140 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3141 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
3142 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003143 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3144
3145 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003146 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3147 .deviceId(touchDeviceId)
3148 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3149 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
3150 .build());
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003151 // Since we already canceled this touch gesture, it will be ignored until a completely new
3152 // gesture is started. This is easier to implement than trying to keep track of the new pointer
3153 // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN.
3154 // However, mouse movements should continue to work.
3155 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3156 .deviceId(mouseDeviceId)
3157 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3158 .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110))
3159 .build());
3160 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId)));
3161
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003162 window->assertNoEvents();
3163}
3164
3165/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003166 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
3167 * the injected event.
3168 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003169TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) {
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003170 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3171 sp<FakeWindowHandle> window =
3172 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3173 window->setFrame(Rect(0, 0, 400, 400));
3174
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003175 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003176
3177 const int32_t touchDeviceId = 4;
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003178 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
3179 // completion.
3180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003181 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003182 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3183 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003184 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003185 .build()));
3186 window->consumeMotionEvent(
3187 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3188
3189 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
3190 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003191 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3192 .deviceId(touchDeviceId)
3193 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3194 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08003195
3196 window->consumeMotionEvent(
3197 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
3198 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3199}
3200
3201/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003202 * This test is similar to the test above, but the sequence of injected events is different.
3203 *
3204 * Two windows: a window on the left and a window on the right.
3205 * Mouse is hovered over the left window.
3206 * Next, we tap on the left window, where the cursor was last seen.
3207 *
3208 * After that, we inject one finger down onto the right window, and then a second finger down onto
3209 * the left window.
3210 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
3211 * window (first), and then another on the left window (second).
3212 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
3213 * In the buggy implementation, second finger down on the left window would cause a crash.
3214 */
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003215TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) {
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003216 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3217 sp<FakeWindowHandle> leftWindow =
3218 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
3219 leftWindow->setFrame(Rect(0, 0, 200, 200));
3220
3221 sp<FakeWindowHandle> rightWindow =
3222 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
3223 rightWindow->setFrame(Rect(200, 0, 400, 200));
3224
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003225 mDispatcher->onWindowInfosChanged(
3226 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003227
3228 const int32_t mouseDeviceId = 6;
3229 const int32_t touchDeviceId = 4;
3230 // Hover over the left window. Keep the cursor there.
3231 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003232 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003233 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3234 AINPUT_SOURCE_MOUSE)
3235 .deviceId(mouseDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003236 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003237 .build()));
3238 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3239
3240 // Tap on left window
3241 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003242 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003243 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3244 AINPUT_SOURCE_TOUCHSCREEN)
3245 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003246 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003247 .build()));
3248
3249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003250 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003251 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3252 AINPUT_SOURCE_TOUCHSCREEN)
3253 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003254 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003255 .build()));
3256 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3257 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3258 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
3259
3260 // First finger down on right window
3261 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003262 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003263 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3264 AINPUT_SOURCE_TOUCHSCREEN)
3265 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003266 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003267 .build()));
3268 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3269
3270 // Second finger down on the left window
3271 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003272 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003273 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3274 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003275 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
3276 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08003277 .build()));
3278 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
3279 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
3280
3281 // No more events
3282 leftWindow->assertNoEvents();
3283 rightWindow->assertNoEvents();
3284}
3285
3286/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003287 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
3288 * While the touch is down, new hover events from the stylus device should be ignored. After the
3289 * touch is gone, stylus hovering should start working again.
3290 */
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003291TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDroppedWhenTouchTap) {
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003292 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3293 sp<FakeWindowHandle> window =
3294 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3295 window->setFrame(Rect(0, 0, 200, 200));
3296
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003297 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003298
3299 const int32_t stylusDeviceId = 5;
3300 const int32_t touchDeviceId = 4;
3301 // Start hovering with stylus
3302 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003303 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003304 MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003305 .deviceId(stylusDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003306 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003307 .build()));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003308 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003309
3310 // Finger down on the window
3311 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003312 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003313 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003314 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003315 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003316 .build()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003317 // The touch device should cause hover to stop!
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003318 window->consumeMotionEvent(
3319 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId)));
3320 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003321
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003322 // Continue hovering with stylus.
3323 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003324 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003325 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3326 AINPUT_SOURCE_STYLUS)
3327 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003328 .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003329 .build()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003330 // Hovers are now ignored
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003331
3332 // Lift up the finger
3333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003334 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003335 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3336 AINPUT_SOURCE_TOUCHSCREEN)
3337 .deviceId(touchDeviceId)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003338 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003339 .build()));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003340 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003341
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003343 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003344 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3345 AINPUT_SOURCE_STYLUS)
3346 .deviceId(stylusDeviceId)
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07003347 .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70))
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003348 .build()));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07003349 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3350 WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08003351 window->assertNoEvents();
3352}
3353
3354/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003355 * A spy window above a window with no input channel.
3356 * Start hovering with a stylus device, and then tap with it.
3357 * Ensure spy window receives the entire sequence.
3358 */
3359TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
3360 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3361 sp<FakeWindowHandle> spyWindow =
3362 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3363 spyWindow->setFrame(Rect(0, 0, 200, 200));
3364 spyWindow->setTrustedOverlay(true);
3365 spyWindow->setSpy(true);
3366 sp<FakeWindowHandle> window =
3367 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3368 window->setNoInputChannel(true);
3369 window->setFrame(Rect(0, 0, 200, 200));
3370
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003371 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003372
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003373 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00003374 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3375 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3376 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003377 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3378 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003379 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3380 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3381 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003382 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3383
3384 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003385 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
3386 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3387 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003388 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3389
3390 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00003391 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
3392 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3393 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003394 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
3395
3396 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00003397 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3398 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3399 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003400 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3401 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00003402 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
3403 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
3404 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003405 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3406
3407 // No more events
3408 spyWindow->assertNoEvents();
3409 window->assertNoEvents();
3410}
3411
3412/**
3413 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
3414 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3415 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3416 * While the mouse is down, new move events from the touch device should be ignored.
3417 */
3418TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3419 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3420 sp<FakeWindowHandle> spyWindow =
3421 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3422 spyWindow->setFrame(Rect(0, 0, 200, 200));
3423 spyWindow->setTrustedOverlay(true);
3424 spyWindow->setSpy(true);
3425 sp<FakeWindowHandle> window =
3426 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3427 window->setFrame(Rect(0, 0, 200, 200));
3428
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003429 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003430
3431 const int32_t mouseDeviceId = 7;
3432 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003433
3434 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003435 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3436 .deviceId(mouseDeviceId)
3437 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3438 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003439 spyWindow->consumeMotionEvent(
3440 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3441 window->consumeMotionEvent(
3442 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3443
3444 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003445 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3446 .deviceId(touchDeviceId)
3447 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3448 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003449 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3450 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3451 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3452 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3453
Prabir Pradhan678438e2023-04-13 19:32:51 +00003454 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3455 .deviceId(touchDeviceId)
3456 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3457 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003458 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3459 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3460
3461 // Pilfer the stream
3462 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3463 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3464
Prabir Pradhan678438e2023-04-13 19:32:51 +00003465 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3466 .deviceId(touchDeviceId)
3467 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3468 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003469 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3470
3471 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003472 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3473 .deviceId(mouseDeviceId)
3474 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3475 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3476 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003477
3478 spyWindow->consumeMotionEvent(
3479 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3480 spyWindow->consumeMotionEvent(
3481 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3482 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3483
Prabir Pradhan678438e2023-04-13 19:32:51 +00003484 mDispatcher->notifyMotion(
3485 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3486 .deviceId(mouseDeviceId)
3487 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3488 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3489 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3490 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003491 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3492 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3493
3494 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003495 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3496 .deviceId(mouseDeviceId)
3497 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3498 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3499 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003500 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3501 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3502
3503 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003504 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3505 .deviceId(touchDeviceId)
3506 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3507 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003508
3509 // No more events
3510 spyWindow->assertNoEvents();
3511 window->assertNoEvents();
3512}
3513
3514/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003515 * On the display, have a single window, and also an area where there's no window.
3516 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
3517 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
3518 */
3519TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3520 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3521 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003522 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003523
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003524 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003525
3526 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00003527 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003528
3529 mDispatcher->waitForIdle();
3530 window->assertNoEvents();
3531
3532 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003533 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003534 mDispatcher->waitForIdle();
3535 window->consumeMotionDown();
3536}
3537
3538/**
3539 * Same test as above, but instead of touching the empty space, the first touch goes to
3540 * non-touchable window.
3541 */
3542TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3543 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3544 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003545 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003546 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3547 window1->setTouchable(false);
3548 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003549 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003550 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3551
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003552 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003553
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003554 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003555 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003556
3557 mDispatcher->waitForIdle();
3558 window1->assertNoEvents();
3559 window2->assertNoEvents();
3560
3561 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003562 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003563 mDispatcher->waitForIdle();
3564 window2->consumeMotionDown();
3565}
3566
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003567/**
3568 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3569 * to the event time of the first ACTION_DOWN sent to the particular window.
3570 */
3571TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3572 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3573 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003574 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003575 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3576 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003577 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003578 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3579
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003580 mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0});
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003581
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003582 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003583 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003584
3585 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003586
3587 MotionEvent* motionEvent1 = window1->consumeMotion();
3588 ASSERT_NE(motionEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003589 window2->assertNoEvents();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003590 nsecs_t downTimeForWindow1 = motionEvent1->getDownTime();
3591 ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003592
3593 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003594 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003595 mDispatcher->waitForIdle();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003596 MotionEvent* motionEvent2 = window2->consumeMotion();
3597 ASSERT_NE(motionEvent2, nullptr);
3598 nsecs_t downTimeForWindow2 = motionEvent2->getDownTime();
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003599 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003600 ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime());
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003601
3602 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003603 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003604 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003605 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003606
3607 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003608 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003609 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003610 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003611
3612 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3613 window1->consumeMotionMove();
3614 window1->assertNoEvents();
3615
3616 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003617 mDispatcher->notifyMotion(
3618 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003619 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003620 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003621
Prabir Pradhan678438e2023-04-13 19:32:51 +00003622 mDispatcher->notifyMotion(
3623 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003624 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003625 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003626}
3627
Garfield Tandf26e862020-07-01 20:18:19 -07003628TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003629 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003630 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003631 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003632 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003633 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003634 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003635 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003636
3637 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3638
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003639 mDispatcher->onWindowInfosChanged(
3640 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07003641
3642 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003644 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003645 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3646 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003647 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003648 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003649 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003650
3651 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003653 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003654 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3655 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003656 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003657 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003658 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3659 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003660
3661 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003662 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003663 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003664 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3665 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003666 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003667 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003668 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3669 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003670
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003671 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003672 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003673 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3674 AINPUT_SOURCE_MOUSE)
3675 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3676 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003677 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003678 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003679 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003680
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003681 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003682 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003683 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3684 AINPUT_SOURCE_MOUSE)
3685 .buttonState(0)
3686 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003687 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003688 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003689 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003690
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003691 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003692 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003693 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3694 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003695 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003696 .build()));
3697 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3698
3699 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003701 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003702 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3703 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003704 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003705 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003706 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003707
3708 // No more events
3709 windowLeft->assertNoEvents();
3710 windowRight->assertNoEvents();
3711}
3712
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003713/**
3714 * Put two fingers down (and don't release them) and click the mouse button.
3715 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3716 * currently active gesture should be canceled, and the new one should proceed.
3717 */
3718TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3719 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3720 sp<FakeWindowHandle> window =
3721 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3722 window->setFrame(Rect(0, 0, 600, 800));
3723
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003724 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003725
3726 const int32_t touchDeviceId = 4;
3727 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003728
3729 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003730 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3731 .deviceId(touchDeviceId)
3732 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3733 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003734
Prabir Pradhan678438e2023-04-13 19:32:51 +00003735 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3736 .deviceId(touchDeviceId)
3737 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3738 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3739 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003740 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3741 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3742
3743 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003744 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3745 .deviceId(mouseDeviceId)
3746 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3747 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3748 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003749 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3750 WithPointerCount(2u)));
3751 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3752
Prabir Pradhan678438e2023-04-13 19:32:51 +00003753 mDispatcher->notifyMotion(
3754 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3755 .deviceId(mouseDeviceId)
3756 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3757 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3758 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3759 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003760 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3761
3762 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3763 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003764 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3765 .deviceId(touchDeviceId)
3766 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3767 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3768 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003769 window->assertNoEvents();
3770}
3771
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003772TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3773 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3774
3775 sp<FakeWindowHandle> spyWindow =
3776 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3777 spyWindow->setFrame(Rect(0, 0, 600, 800));
3778 spyWindow->setTrustedOverlay(true);
3779 spyWindow->setSpy(true);
3780 sp<FakeWindowHandle> window =
3781 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3782 window->setFrame(Rect(0, 0, 600, 800));
3783
3784 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003785 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003786
3787 // Send mouse cursor to the window
3788 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003789 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003790 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3791 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003792 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003793 .build()));
3794
3795 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3796 WithSource(AINPUT_SOURCE_MOUSE)));
3797 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3798 WithSource(AINPUT_SOURCE_MOUSE)));
3799
3800 window->assertNoEvents();
3801 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003802}
3803
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003804TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3805 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3806
3807 sp<FakeWindowHandle> spyWindow =
3808 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3809 spyWindow->setFrame(Rect(0, 0, 600, 800));
3810 spyWindow->setTrustedOverlay(true);
3811 spyWindow->setSpy(true);
3812 sp<FakeWindowHandle> window =
3813 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3814 window->setFrame(Rect(0, 0, 600, 800));
3815
3816 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003817 mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003818
3819 // Send mouse cursor to the window
3820 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003821 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003822 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3823 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003824 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003825 .build()));
3826
3827 // Move mouse cursor
3828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003829 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003830 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3831 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003832 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003833 .build()));
3834
3835 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3836 WithSource(AINPUT_SOURCE_MOUSE)));
3837 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3838 WithSource(AINPUT_SOURCE_MOUSE)));
3839 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3840 WithSource(AINPUT_SOURCE_MOUSE)));
3841 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3842 WithSource(AINPUT_SOURCE_MOUSE)));
3843 // Touch down on the window
3844 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003845 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003846 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3847 AINPUT_SOURCE_TOUCHSCREEN)
3848 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003849 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003850 .build()));
3851 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3852 WithSource(AINPUT_SOURCE_MOUSE)));
3853 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3854 WithSource(AINPUT_SOURCE_MOUSE)));
3855 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3856 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3857 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3858 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3859
3860 // pilfer the motion, retaining the gesture on the spy window.
3861 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3862 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3863 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3864
3865 // Touch UP on the window
3866 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003867 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003868 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3869 AINPUT_SOURCE_TOUCHSCREEN)
3870 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003871 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003872 .build()));
3873 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3874 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3875
3876 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3877 // to send a new gesture. It should again go to both windows (spy and the window below), just
3878 // like the first gesture did, before pilfering. The window configuration has not changed.
3879
3880 // One more tap - DOWN
3881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003882 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003883 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3884 AINPUT_SOURCE_TOUCHSCREEN)
3885 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003886 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003887 .build()));
3888 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3889 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3890 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3891 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3892
3893 // Touch UP on the window
3894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003895 injectMotionEvent(*mDispatcher,
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003896 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3897 AINPUT_SOURCE_TOUCHSCREEN)
3898 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003899 .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250))
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003900 .build()));
3901 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3902 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3903 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3904 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3905
3906 window->assertNoEvents();
3907 spyWindow->assertNoEvents();
3908}
3909
Garfield Tandf26e862020-07-01 20:18:19 -07003910// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3911// directly in this test.
3912TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003913 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003914 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003915 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003916 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003917
3918 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3919
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003920 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Garfield Tandf26e862020-07-01 20:18:19 -07003921
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003922 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003923 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003924 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3925 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003926 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003927 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003928 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003929 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003931 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003932 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3933 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003934 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003935 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003936 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3937 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003938
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003939 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003940 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003941 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3942 AINPUT_SOURCE_MOUSE)
3943 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3944 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003945 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003946 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003947 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003948
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003949 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003950 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003951 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3952 AINPUT_SOURCE_MOUSE)
3953 .buttonState(0)
3954 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003955 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003956 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003957 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003958
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003959 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003960 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003961 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3962 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003963 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003964 .build()));
3965 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3966
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003967 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3968 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3969 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003970 injectMotionEvent(*mDispatcher,
Garfield Tandf26e862020-07-01 20:18:19 -07003971 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3972 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003973 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003974 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003975 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003976}
3977
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003978/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003979 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3980 * is generated.
3981 */
3982TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3983 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3984 sp<FakeWindowHandle> window =
3985 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3986 window->setFrame(Rect(0, 0, 1200, 800));
3987
3988 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3989
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07003990 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003991
3992 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003993 injectMotionEvent(*mDispatcher,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003994 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3995 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07003996 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003997 .build()));
3998 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3999
4000 // Remove the window, but keep the channel.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004001 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004002 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
4003}
4004
4005/**
Daniel Norman7487dfa2023-08-02 16:39:45 -07004006 * Test that invalid HOVER events sent by accessibility do not cause a fatal crash.
4007 */
Ameer Armalycff4fa52023-10-04 23:45:11 +00004008TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash,
4009 REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags,
4010 a11y_crash_on_inconsistent_event_stream))) {
Daniel Norman7487dfa2023-08-02 16:39:45 -07004011 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4012 sp<FakeWindowHandle> window =
4013 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4014 window->setFrame(Rect(0, 0, 1200, 800));
4015
4016 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4017
4018 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
4019
4020 MotionEventBuilder hoverEnterBuilder =
4021 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4022 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
4023 .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
4024 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4025 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4026 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4027 injectMotionEvent(*mDispatcher, hoverEnterBuilder.build()));
4028 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4029 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
4030}
4031
4032/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004033 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
4034 */
4035TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
4036 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4037 sp<FakeWindowHandle> window =
4038 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4039 window->setFrame(Rect(0, 0, 100, 100));
4040
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004041 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004042
4043 const int32_t mouseDeviceId = 7;
4044 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004045
4046 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00004047 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
4048 .deviceId(mouseDeviceId)
4049 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
4050 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004051 window->consumeMotionEvent(
4052 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
4053
4054 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00004055 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4056 .deviceId(touchDeviceId)
4057 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4058 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08004059
4060 window->consumeMotionEvent(
4061 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
4062 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
4063}
4064
4065/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004066 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004067 * The tap causes a HOVER_EXIT event to be generated because the current event
4068 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004069 */
4070TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
4071 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4072 sp<FakeWindowHandle> window =
4073 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4074 window->setFrame(Rect(0, 0, 100, 100));
4075
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004076 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004077 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
4078 .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50))
4079 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004080 ASSERT_NO_FATAL_FAILURE(
4081 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
4082 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004083
4084 // Tap on the window
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004085 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4086 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4087 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004088 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00004089 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
4090 WithSource(AINPUT_SOURCE_MOUSE))));
4091
4092 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004093 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4094 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4095
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004096 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4097 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
4098 .build());
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08004099 ASSERT_NO_FATAL_FAILURE(
4100 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
4101 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
4102}
4103
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004104TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
4105 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4106 sp<FakeWindowHandle> windowDefaultDisplay =
4107 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4108 ADISPLAY_ID_DEFAULT);
4109 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
4110 sp<FakeWindowHandle> windowSecondDisplay =
4111 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
4112 SECOND_DISPLAY_ID);
4113 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
4114
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004115 mDispatcher->onWindowInfosChanged(
4116 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004117
4118 // Set cursor position in window in default display and check that hover enter and move
4119 // events are generated.
4120 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004121 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004122 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4123 AINPUT_SOURCE_MOUSE)
4124 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004125 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004126 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004127 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004128
4129 // Remove all windows in secondary display and check that no event happens on window in
4130 // primary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004131 mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0});
4132
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004133 windowDefaultDisplay->assertNoEvents();
4134
4135 // Move cursor position in window in default display and check that only hover move
4136 // event is generated and not hover enter event.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004137 mDispatcher->onWindowInfosChanged(
4138 {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004139 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004140 injectMotionEvent(*mDispatcher,
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004141 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
4142 AINPUT_SOURCE_MOUSE)
4143 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004144 .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700))
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004145 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08004146 windowDefaultDisplay->consumeMotionEvent(
4147 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4148 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02004149 windowDefaultDisplay->assertNoEvents();
4150}
4151
Garfield Tan00f511d2019-06-12 16:55:40 -07004152TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07004153 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07004154
4155 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004156 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004157 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004158 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004159 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004160 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07004161
4162 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
4163
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004164 mDispatcher->onWindowInfosChanged(
4165 {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Garfield Tan00f511d2019-06-12 16:55:40 -07004166
4167 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
4168 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004169 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004170 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07004171 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08004172 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07004173 windowRight->assertNoEvents();
4174}
4175
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004176TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004177 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004178 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4179 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07004180 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004181
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004182 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07004183 setFocusedWindow(window);
4184
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004185 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004186
Prabir Pradhan678438e2023-04-13 19:32:51 +00004187 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004188
4189 // Window should receive key down event.
4190 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4191
4192 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
4193 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004194 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004195 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004196 AKEY_EVENT_FLAG_CANCELED);
4197}
4198
4199TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07004200 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004201 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4202 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004203
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004204 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004205
Prabir Pradhan678438e2023-04-13 19:32:51 +00004206 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4207 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004208
4209 // Window should receive motion down event.
4210 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4211
4212 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
4213 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004214 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08004215 window->consumeMotionEvent(
4216 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08004217}
4218
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004219TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
4220 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4221 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4222 "Fake Window", ADISPLAY_ID_DEFAULT);
4223
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004224 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07004225
4226 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4227 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
4228 .build());
4229
4230 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4231
4232 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
4233 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
4234 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
4235
4236 // After the device has been reset, a new hovering stream can be sent to the window
4237 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
4238 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
4239 .build());
4240 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
4241}
4242
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004243TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
4244 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004245 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4246 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004247 window->setFocusable(true);
4248
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004249 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004250 setFocusedWindow(window);
4251
4252 window->consumeFocusEvent(true);
4253
Prabir Pradhan678438e2023-04-13 19:32:51 +00004254 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004255 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
4256 const nsecs_t injectTime = keyArgs.eventTime;
4257 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00004258 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004259 // The dispatching time should be always greater than or equal to intercept key timeout.
4260 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4261 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
4262 std::chrono::nanoseconds(interceptKeyTimeout).count());
4263}
4264
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004265/**
4266 * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set.
4267 */
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004268TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
4269 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004270 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4271 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004272 window->setFocusable(true);
4273
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004274 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004275 setFocusedWindow(window);
4276
4277 window->consumeFocusEvent(true);
4278
Prabir Pradhan678438e2023-04-13 19:32:51 +00004279 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004280 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf54d2ab2023-06-09 13:23:53 -07004281
4282 // Set a value that's significantly larger than the default consumption timeout. If the
4283 // implementation is correct, the actual value doesn't matter; it won't slow down the test.
4284 mFakePolicy->setInterceptKeyTimeout(600ms);
4285 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
4286 // Window should receive key event immediately when same key up.
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08004287 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4288}
4289
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004290/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004291 * Two windows. First is a regular window. Second does not overlap with the first, and has
4292 * WATCH_OUTSIDE_TOUCH.
4293 * Both windows are owned by the same UID.
4294 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
4295 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
4296 */
4297TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
4298 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004299 sp<FakeWindowHandle> window =
4300 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004301 window->setFrame(Rect{0, 0, 100, 100});
4302
4303 sp<FakeWindowHandle> outsideWindow =
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07004304 sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window",
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004305 ADISPLAY_ID_DEFAULT);
4306 outsideWindow->setFrame(Rect{100, 100, 200, 200});
4307 outsideWindow->setWatchOutsideTouch(true);
4308 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004309 mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004310
4311 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004312 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4313 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4314 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004315 window->consumeMotionDown();
4316 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
4317 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
4318 outsideWindow->consumeMotionEvent(
4319 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
4320}
4321
4322/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004323 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
4324 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
4325 * ACTION_OUTSIDE event is sent per gesture.
4326 */
4327TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
4328 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
4329 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004330 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4331 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004332 window->setWatchOutsideTouch(true);
4333 window->setFrame(Rect{0, 0, 100, 100});
4334 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004335 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4336 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004337 secondWindow->setFrame(Rect{100, 100, 200, 200});
4338 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004339 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
4340 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004341 thirdWindow->setFrame(Rect{200, 200, 300, 300});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004342 mDispatcher->onWindowInfosChanged(
4343 {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0});
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004344
4345 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004346 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4347 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4348 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004349 window->assertNoEvents();
4350 secondWindow->assertNoEvents();
4351
4352 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
4353 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004354 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4355 ADISPLAY_ID_DEFAULT,
4356 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08004357 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
4358 window->consumeMotionEvent(
4359 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004360 secondWindow->consumeMotionDown();
4361 thirdWindow->assertNoEvents();
4362
4363 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
4364 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004365 mDispatcher->notifyMotion(
4366 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4367 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004368 window->assertNoEvents();
4369 secondWindow->consumeMotionMove();
4370 thirdWindow->consumeMotionDown();
4371}
4372
Prabir Pradhan814fe082022-07-22 20:22:18 +00004373TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
4374 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004375 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4376 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004377 window->setFocusable(true);
4378
Patrick Williamsd828f302023-04-28 17:52:08 -05004379 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004380 setFocusedWindow(window);
4381
4382 window->consumeFocusEvent(true);
4383
Prabir Pradhan678438e2023-04-13 19:32:51 +00004384 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
4385 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
4386 mDispatcher->notifyKey(keyDown);
4387 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004388
4389 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4390 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
4391
4392 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05004393 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00004394
4395 window->consumeFocusEvent(false);
4396
Prabir Pradhan678438e2023-04-13 19:32:51 +00004397 mDispatcher->notifyKey(keyDown);
4398 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00004399 window->assertNoEvents();
4400}
4401
Arthur Hung96483742022-11-15 03:30:48 +00004402TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
4403 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4404 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4405 "Fake Window", ADISPLAY_ID_DEFAULT);
4406 // Ensure window is non-split and have some transform.
4407 window->setPreventSplitting(true);
4408 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05004409 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00004410
4411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004412 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung96483742022-11-15 03:30:48 +00004413 {50, 50}))
4414 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4415 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4416
4417 const MotionEvent secondFingerDownEvent =
4418 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4419 .displayId(ADISPLAY_ID_DEFAULT)
4420 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004421 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4422 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00004423 .build();
4424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004425 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung96483742022-11-15 03:30:48 +00004426 InputEventInjectionSync::WAIT_FOR_RESULT))
4427 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4428
4429 const MotionEvent* event = window->consumeMotion();
4430 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4431 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4432 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4433 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4434 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
4435}
4436
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07004437/**
4438 * Two windows: a splittable and a non-splittable.
4439 * The non-splittable window shouldn't receive any "incomplete" gestures.
4440 * Send the first pointer to the splittable window, and then touch the non-splittable window.
4441 * The second pointer should be dropped because the initial window is splittable, so it won't get
4442 * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any
4443 * "incomplete" gestures.
4444 */
4445TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) {
4446 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4447 sp<FakeWindowHandle> leftWindow =
4448 sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window",
4449 ADISPLAY_ID_DEFAULT);
4450 leftWindow->setPreventSplitting(false);
4451 leftWindow->setFrame(Rect(0, 0, 100, 100));
4452 sp<FakeWindowHandle> rightWindow =
4453 sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window",
4454 ADISPLAY_ID_DEFAULT);
4455 rightWindow->setPreventSplitting(true);
4456 rightWindow->setFrame(Rect(100, 100, 200, 200));
4457 mDispatcher->onWindowInfosChanged(
4458 {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
4459
4460 // Touch down on left, splittable window
4461 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4462 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
4463 .build());
4464 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4465
4466 mDispatcher->notifyMotion(
4467 MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4468 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4469 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
4470 .build());
4471 leftWindow->assertNoEvents();
4472 rightWindow->assertNoEvents();
4473}
4474
Harry Cuttsb166c002023-05-09 13:06:05 +00004475TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4476 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4477 sp<FakeWindowHandle> window =
4478 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4479 window->setFrame(Rect(0, 0, 400, 400));
4480 sp<FakeWindowHandle> trustedOverlay =
4481 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4482 ADISPLAY_ID_DEFAULT);
4483 trustedOverlay->setSpy(true);
4484 trustedOverlay->setTrustedOverlay(true);
4485
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004486 mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004487
4488 // Start a three-finger touchpad swipe
4489 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4490 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4491 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4492 .build());
4493 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4494 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4495 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4496 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4497 .build());
4498 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4499 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4500 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4501 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4502 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4503 .build());
4504
4505 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4506 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4507 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4508
4509 // Move the swipe a bit
4510 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4511 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4512 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4513 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4514 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4515 .build());
4516
4517 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4518
4519 // End the swipe
4520 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4521 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4522 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4523 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4524 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4525 .build());
4526 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4527 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4528 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4529 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4530 .build());
4531 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4532 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4533 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4534 .build());
4535
4536 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4537 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4538 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4539
4540 window->assertNoEvents();
4541}
4542
4543TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4544 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4545 sp<FakeWindowHandle> window =
4546 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4547 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07004548 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cuttsb166c002023-05-09 13:06:05 +00004549
4550 // Start a three-finger touchpad swipe
4551 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4552 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4553 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4554 .build());
4555 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4556 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4557 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4558 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4559 .build());
4560 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4561 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4562 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4563 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4564 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4565 .build());
4566
4567 // Move the swipe a bit
4568 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4569 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4570 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4571 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4572 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4573 .build());
4574
4575 // End the swipe
4576 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4577 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4578 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4579 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4580 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4581 .build());
4582 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4583 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4584 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4585 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4586 .build());
4587 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4588 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4589 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4590 .build());
4591
4592 window->assertNoEvents();
4593}
4594
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004595/**
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004596 * Send a two-pointer gesture to a single window. The window's orientation changes in response to
4597 * the first pointer.
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004598 * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004599 */
4600TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) {
4601 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4602 sp<FakeWindowHandle> window =
4603 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4604 window->setFrame(Rect(0, 0, 400, 400));
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004605 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004606
4607 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
4608 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4609 .downTime(baseTime + 10)
4610 .eventTime(baseTime + 10)
4611 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4612 .build());
4613
4614 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4615
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004616 // Change the transform so that the orientation is now different from original.
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004617 window->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004618
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004619 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004620
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004621 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4622 .downTime(baseTime + 10)
4623 .eventTime(baseTime + 30)
4624 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4625 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4626 .build());
4627
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004628 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4629
4630 // Finish the gesture and start a new one. Ensure all events are sent to the window.
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004631 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
4632 .downTime(baseTime + 10)
4633 .eventTime(baseTime + 40)
4634 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4635 .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200))
4636 .build());
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004637
4638 window->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4639
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004640 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
4641 .downTime(baseTime + 10)
4642 .eventTime(baseTime + 50)
4643 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
4644 .build());
4645
Prabir Pradhan69d00bf2023-06-23 19:55:18 +00004646 window->consumeMotionEvent(WithMotionAction(ACTION_UP));
4647
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004648 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4649 .downTime(baseTime + 60)
4650 .eventTime(baseTime + 60)
4651 .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40))
4652 .build());
4653
Siarhei Vishniakou700424c2023-07-18 17:18:42 -07004654 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07004655}
4656
4657/**
Hu Guo771a7692023-09-17 20:51:08 +08004658 * When there are multiple screens, such as screen projection to TV or screen recording, if the
4659 * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and
4660 * its coordinates should be converted by the transform of the windows of target screen.
4661 */
4662TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) {
4663 // This case will create a window and a spy window on the default display and mirror
4664 // window on the second display. cancel event is sent through spy window pilferPointers
4665 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4666
4667 sp<FakeWindowHandle> spyWindowDefaultDisplay =
4668 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
4669 spyWindowDefaultDisplay->setTrustedOverlay(true);
4670 spyWindowDefaultDisplay->setSpy(true);
4671
4672 sp<FakeWindowHandle> windowDefaultDisplay =
4673 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
4674 ADISPLAY_ID_DEFAULT);
4675 windowDefaultDisplay->setWindowTransform(1, 0, 0, 1);
4676
4677 sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID);
4678 windowSecondDisplay->setWindowTransform(2, 0, 0, 2);
4679
4680 // Add the windows to the dispatcher
4681 mDispatcher->onWindowInfosChanged(
4682 {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(),
4683 *windowSecondDisplay->getInfo()},
4684 {},
4685 0,
4686 0});
4687
4688 // Send down to ADISPLAY_ID_DEFAULT
4689 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4690 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4691 {100, 100}))
4692 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4693
4694 spyWindowDefaultDisplay->consumeMotionDown();
4695 windowDefaultDisplay->consumeMotionDown();
4696
4697 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken()));
4698
4699 // windowDefaultDisplay gets cancel
4700 MotionEvent* event = windowDefaultDisplay->consumeMotion();
4701 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction());
4702
4703 // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the
4704 // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y
4705 // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of
4706 // SECOND_DISPLAY_ID, the x and y coordinates are 200
4707 EXPECT_EQ(100, event->getX(0));
4708 EXPECT_EQ(100, event->getY(0));
4709}
4710
4711/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004712 * Ensure the correct coordinate spaces are used by InputDispatcher.
4713 *
4714 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4715 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4716 * space.
4717 */
4718class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4719public:
4720 void SetUp() override {
4721 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004722 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004723 }
4724
4725 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4726 gui::DisplayInfo info;
4727 info.displayId = displayId;
4728 info.transform = transform;
4729 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004730 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004731 }
4732
4733 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4734 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004735 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004736 }
4737
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004738 void removeAllWindowsAndDisplays() {
4739 mDisplayInfos.clear();
4740 mWindowInfos.clear();
4741 }
4742
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004743 // Set up a test scenario where the display has a scaled projection and there are two windows
4744 // on the display.
4745 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4746 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4747 // respectively.
4748 ui::Transform displayTransform;
4749 displayTransform.set(2, 0, 0, 4);
4750 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4751
4752 std::shared_ptr<FakeApplicationHandle> application =
4753 std::make_shared<FakeApplicationHandle>();
4754
4755 // Add two windows to the display. Their frames are represented in the display space.
4756 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004757 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4758 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004759 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4760 addWindow(firstWindow);
4761
4762 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004763 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4764 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004765 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4766 addWindow(secondWindow);
4767 return {std::move(firstWindow), std::move(secondWindow)};
4768 }
4769
4770private:
4771 std::vector<gui::DisplayInfo> mDisplayInfos;
4772 std::vector<gui::WindowInfo> mWindowInfos;
4773};
4774
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004775TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004776 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4777 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004778 // selected so that if the hit test was performed with the point and the bounds being in
4779 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004780 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4781 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4782 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004783
4784 firstWindow->consumeMotionDown();
4785 secondWindow->assertNoEvents();
4786}
4787
4788// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4789// the event should be treated as being in the logical display space.
4790TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4791 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4792 // Send down to the first window. The point is represented in the logical display space. The
4793 // point is selected so that if the hit test was done in logical display space, then it would
4794 // end up in the incorrect window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004795 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004796 PointF{75 * 2, 55 * 4});
4797
4798 firstWindow->consumeMotionDown();
4799 secondWindow->assertNoEvents();
4800}
4801
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004802// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4803// event should be treated as being in the logical display space.
4804TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4805 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4806
4807 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4808 ui::Transform injectedEventTransform;
4809 injectedEventTransform.set(matrix);
4810 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4811 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4812
4813 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4814 .displayId(ADISPLAY_ID_DEFAULT)
4815 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004816 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004817 .x(untransformedPoint.x)
4818 .y(untransformedPoint.y))
4819 .build();
4820 event.transform(matrix);
4821
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07004822 injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT,
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004823 InputEventInjectionSync::WAIT_FOR_RESULT);
4824
4825 firstWindow->consumeMotionDown();
4826 secondWindow->assertNoEvents();
4827}
4828
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004829TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4830 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4831
4832 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004833 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4834 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4835 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004836
4837 firstWindow->assertNoEvents();
4838 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004839 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004840 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4841
4842 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4843 EXPECT_EQ(300, event->getRawX(0));
4844 EXPECT_EQ(880, event->getRawY(0));
4845
4846 // Ensure that the x and y values are in the window's coordinate space.
4847 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4848 // the logical display space. This will be the origin of the window space.
4849 EXPECT_EQ(100, event->getX(0));
4850 EXPECT_EQ(80, event->getY(0));
4851}
4852
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004853TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) {
4854 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4855 // The monitor will always receive events in the logical display's coordinate space, because
4856 // it does not have a window.
Prabir Pradhanfb549072023-10-05 19:17:36 +00004857 FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004858
4859 // Send down to the first window.
4860 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4861 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
4862 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
4863 monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
4864
4865 // Second pointer goes down on second window.
4866 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4867 ADISPLAY_ID_DEFAULT,
4868 {PointF{50, 100}, PointF{150, 220}}));
4869 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80)));
4870 const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}},
4871 {1, PointF{300, 880}}};
4872 monitor.consumeMotionEvent(
4873 AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers)));
4874
4875 mDispatcher->cancelCurrentTouch();
4876
4877 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
4878 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80)));
4879 monitor.consumeMotionEvent(
4880 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers)));
4881}
4882
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004883TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) {
4884 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4885
4886 // Send down to the first window.
4887 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4888 ADISPLAY_ID_DEFAULT, {PointF{50, 100}}));
4889 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400)));
4890
4891 // The pointer is transferred to the second window, and the second window receives it in the
4892 // correct coordinate space.
4893 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4894 firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400)));
4895 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400)));
4896}
4897
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00004898TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) {
4899 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4900
4901 // Send hover move to the second window, and ensure it shows up as hover enter.
4902 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
4903 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
4904 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
4905 WithCoords(100, 80), WithRawCoords(300, 880)));
4906
4907 // Touch down at the same location and ensure a hover exit is synthesized.
4908 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
4909 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
4910 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
4911 WithRawCoords(300, 880)));
4912 secondWindow->consumeMotionEvent(
4913 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
4914 secondWindow->assertNoEvents();
4915 firstWindow->assertNoEvents();
4916}
4917
Prabir Pradhan453ae732023-10-13 14:30:14 +00004918// Same as above, but while the window is being mirrored.
4919TEST_F(InputDispatcherDisplayProjectionTest,
4920 SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) {
4921 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4922
4923 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4924 ui::Transform secondDisplayTransform;
4925 secondDisplayTransform.set(matrix);
4926 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
4927
4928 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
4929 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
4930 addWindow(secondWindowClone);
4931
4932 // Send hover move to the second window, and ensure it shows up as hover enter.
4933 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS,
4934 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
4935 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
4936 WithCoords(100, 80), WithRawCoords(300, 880)));
4937
4938 // Touch down at the same location and ensure a hover exit is synthesized for the correct
4939 // display.
4940 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS,
4941 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
4942 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
4943 WithRawCoords(300, 880)));
4944 secondWindow->consumeMotionEvent(
4945 AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880)));
4946 secondWindow->assertNoEvents();
4947 firstWindow->assertNoEvents();
4948}
4949
Prabir Pradhan0dfcac72023-10-05 20:04:21 +00004950TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) {
4951 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4952
4953 // Send hover enter to second window
4954 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
4955 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
4956 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
4957 WithCoords(100, 80), WithRawCoords(300, 880)));
4958
4959 mDispatcher->cancelCurrentTouch();
4960
4961 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
4962 WithRawCoords(300, 880)));
4963 secondWindow->assertNoEvents();
4964 firstWindow->assertNoEvents();
4965}
4966
Prabir Pradhan453ae732023-10-13 14:30:14 +00004967// Same as above, but while the window is being mirrored.
Prabir Pradhan16463382023-10-12 23:03:19 +00004968TEST_F(InputDispatcherDisplayProjectionTest,
4969 SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) {
4970 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4971
4972 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4973 ui::Transform secondDisplayTransform;
4974 secondDisplayTransform.set(matrix);
4975 addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform);
4976
4977 sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID);
4978 secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4);
4979 addWindow(secondWindowClone);
4980
4981 // Send hover enter to second window
4982 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS,
4983 ADISPLAY_ID_DEFAULT, {PointF{150, 220}}));
4984 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER),
4985 WithCoords(100, 80), WithRawCoords(300, 880),
4986 WithDisplayId(ADISPLAY_ID_DEFAULT)));
4987
4988 mDispatcher->cancelCurrentTouch();
4989
4990 // Ensure the cancelation happens with the correct displayId and the correct coordinates.
4991 secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80),
4992 WithRawCoords(300, 880),
4993 WithDisplayId(ADISPLAY_ID_DEFAULT)));
4994 secondWindow->assertNoEvents();
4995 firstWindow->assertNoEvents();
4996}
4997
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004998/** Ensure consistent behavior of InputDispatcher in all orientations. */
4999class InputDispatcherDisplayOrientationFixture
5000 : public InputDispatcherDisplayProjectionTest,
5001 public ::testing::WithParamInterface<ui::Rotation> {};
5002
5003// This test verifies the touchable region of a window for all rotations of the display by tapping
5004// in different locations on the display, specifically points close to the four corners of a
5005// window.
5006TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
5007 constexpr static int32_t displayWidth = 400;
5008 constexpr static int32_t displayHeight = 800;
5009
5010 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5011
5012 const auto rotation = GetParam();
5013
5014 // Set up the display with the specified rotation.
5015 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
5016 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
5017 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
5018 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
5019 logicalDisplayWidth, logicalDisplayHeight);
5020 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
5021
5022 // Create a window with its bounds determined in the logical display.
5023 const Rect frameInLogicalDisplay(100, 100, 200, 300);
5024 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
5025 sp<FakeWindowHandle> window =
5026 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
5027 window->setFrame(frameInDisplay, displayTransform);
5028 addWindow(window);
5029
5030 // The following points in logical display space should be inside the window.
5031 static const std::array<vec2, 4> insidePoints{
5032 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
5033 for (const auto pointInsideWindow : insidePoints) {
5034 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
5035 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005036 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5037 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5038 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005039 window->consumeMotionDown();
5040
Prabir Pradhan678438e2023-04-13 19:32:51 +00005041 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5042 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5043 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005044 window->consumeMotionUp();
5045 }
5046
5047 // The following points in logical display space should be outside the window.
5048 static const std::array<vec2, 5> outsidePoints{
5049 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
5050 for (const auto pointOutsideWindow : outsidePoints) {
5051 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
5052 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00005053 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5054 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5055 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005056
Prabir Pradhan678438e2023-04-13 19:32:51 +00005057 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
5058 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5059 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005060 }
5061 window->assertNoEvents();
5062}
5063
5064// Run the precision tests for all rotations.
5065INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
5066 InputDispatcherDisplayOrientationFixture,
5067 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
5068 ui::ROTATION_270),
5069 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
5070 return ftl::enum_string(testParamInfo.param);
5071 });
5072
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005073using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
5074 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005075
5076class TransferTouchFixture : public InputDispatcherTest,
5077 public ::testing::WithParamInterface<TransferFunction> {};
5078
5079TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07005080 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005081
5082 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005083 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005084 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5085 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005086 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005087 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005088 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5089 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005090 sp<FakeWindowHandle> wallpaper =
5091 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
5092 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005093 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005094 mDispatcher->onWindowInfosChanged(
5095 {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005096
5097 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005098 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5099 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005100
Svet Ganov5d3bc372020-01-26 23:11:07 -08005101 // Only the first window should get the down event
5102 firstWindow->consumeMotionDown();
5103 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005104 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005105
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005106 // Transfer touch to the second window
5107 TransferFunction f = GetParam();
5108 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5109 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005110 // The first window gets cancel and the second gets down
5111 firstWindow->consumeMotionCancel();
5112 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005113 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005114
5115 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005116 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5117 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005118 // The first window gets no events and the second gets up
5119 firstWindow->assertNoEvents();
5120 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00005121 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005122}
5123
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005124/**
5125 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
5126 * from. When we have spy windows, there are several windows to choose from: either spy, or the
5127 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
5128 * natural to the user.
5129 * In this test, we are sending a pointer to both spy window and first window. We then try to
5130 * transfer touch to the second window. The dispatcher should identify the first window as the
5131 * one that should lose the gesture, and therefore the action should be to move the gesture from
5132 * the first window to the second.
5133 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
5134 * the other API, as well.
5135 */
5136TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
5137 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5138
5139 // Create a couple of windows + a spy window
5140 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005141 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005142 spyWindow->setTrustedOverlay(true);
5143 spyWindow->setSpy(true);
5144 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005145 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005146 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005147 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005148
5149 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005150 mDispatcher->onWindowInfosChanged(
5151 {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005152
5153 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005154 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5155 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005156 // Only the first window and spy should get the down event
5157 spyWindow->consumeMotionDown();
5158 firstWindow->consumeMotionDown();
5159
5160 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
5161 // if f === 'transferTouch'.
5162 TransferFunction f = GetParam();
5163 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5164 ASSERT_TRUE(success);
5165 // The first window gets cancel and the second gets down
5166 firstWindow->consumeMotionCancel();
5167 secondWindow->consumeMotionDown();
5168
5169 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005170 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5171 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005172 // The first window gets no events and the second+spy get up
5173 firstWindow->assertNoEvents();
5174 spyWindow->consumeMotionUp();
5175 secondWindow->consumeMotionUp();
5176}
5177
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005178TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005179 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005180
5181 PointF touchPoint = {10, 10};
5182
5183 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005184 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005185 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5186 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005187 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005188 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005189 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5190 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005191 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005192
5193 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005194 mDispatcher->onWindowInfosChanged(
5195 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005196
5197 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005198 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5199 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5200 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005201 // Only the first window should get the down event
5202 firstWindow->consumeMotionDown();
5203 secondWindow->assertNoEvents();
5204
5205 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005206 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5207 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005208 // Only the first window should get the pointer down event
5209 firstWindow->consumeMotionPointerDown(1);
5210 secondWindow->assertNoEvents();
5211
5212 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005213 TransferFunction f = GetParam();
5214 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5215 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005216 // The first window gets cancel and the second gets down and pointer down
5217 firstWindow->consumeMotionCancel();
5218 secondWindow->consumeMotionDown();
5219 secondWindow->consumeMotionPointerDown(1);
5220
5221 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005222 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5223 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005224 // The first window gets nothing and the second gets pointer up
5225 firstWindow->assertNoEvents();
5226 secondWindow->consumeMotionPointerUp(1);
5227
5228 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005229 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5230 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005231 // The first window gets nothing and the second gets up
5232 firstWindow->assertNoEvents();
5233 secondWindow->consumeMotionUp();
5234}
5235
Arthur Hungc539dbb2022-12-08 07:45:36 +00005236TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
5237 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5238
5239 // Create a couple of windows
5240 sp<FakeWindowHandle> firstWindow =
5241 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5242 ADISPLAY_ID_DEFAULT);
5243 firstWindow->setDupTouchToWallpaper(true);
5244 sp<FakeWindowHandle> secondWindow =
5245 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5246 ADISPLAY_ID_DEFAULT);
5247 secondWindow->setDupTouchToWallpaper(true);
5248
5249 sp<FakeWindowHandle> wallpaper1 =
5250 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
5251 wallpaper1->setIsWallpaper(true);
5252
5253 sp<FakeWindowHandle> wallpaper2 =
5254 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
5255 wallpaper2->setIsWallpaper(true);
5256 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005257 mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(),
5258 *secondWindow->getInfo(), *wallpaper2->getInfo()},
5259 {},
5260 0,
5261 0});
Arthur Hungc539dbb2022-12-08 07:45:36 +00005262
5263 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005264 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5265 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005266
5267 // Only the first window should get the down event
5268 firstWindow->consumeMotionDown();
5269 secondWindow->assertNoEvents();
5270 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5271 wallpaper2->assertNoEvents();
5272
5273 // Transfer touch focus to the second window
5274 TransferFunction f = GetParam();
5275 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
5276 ASSERT_TRUE(success);
5277
5278 // The first window gets cancel and the second gets down
5279 firstWindow->consumeMotionCancel();
5280 secondWindow->consumeMotionDown();
5281 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5282 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5283
5284 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005285 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5286 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00005287 // The first window gets no events and the second gets up
5288 firstWindow->assertNoEvents();
5289 secondWindow->consumeMotionUp();
5290 wallpaper1->assertNoEvents();
5291 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
5292}
5293
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005294// For the cases of single pointer touch and two pointers non-split touch, the api's
5295// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
5296// for the case where there are multiple pointers split across several windows.
5297INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
5298 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005299 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
5300 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005301 return dispatcher->transferTouch(destChannelToken,
5302 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005303 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005304 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
5305 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005306 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00005307 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005308 }));
5309
Svet Ganov5d3bc372020-01-26 23:11:07 -08005310TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07005311 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08005312
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005313 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005314 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5315 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005316 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005317
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005318 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005319 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5320 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08005321 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005322
5323 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005324 mDispatcher->onWindowInfosChanged(
5325 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Svet Ganov5d3bc372020-01-26 23:11:07 -08005326
5327 PointF pointInFirst = {300, 200};
5328 PointF pointInSecond = {300, 600};
5329
5330 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005331 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5332 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5333 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005334 // Only the first window should get the down event
5335 firstWindow->consumeMotionDown();
5336 secondWindow->assertNoEvents();
5337
5338 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005339 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5340 ADISPLAY_ID_DEFAULT,
5341 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005342 // The first window gets a move and the second a down
5343 firstWindow->consumeMotionMove();
5344 secondWindow->consumeMotionDown();
5345
5346 // Transfer touch focus to the second window
5347 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
5348 // The first window gets cancel and the new gets pointer down (it already saw down)
5349 firstWindow->consumeMotionCancel();
5350 secondWindow->consumeMotionPointerDown(1);
5351
5352 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005353 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5354 ADISPLAY_ID_DEFAULT,
5355 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005356 // The first window gets nothing and the second gets pointer up
5357 firstWindow->assertNoEvents();
5358 secondWindow->consumeMotionPointerUp(1);
5359
5360 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005361 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5362 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08005363 // The first window gets nothing and the second gets up
5364 firstWindow->assertNoEvents();
5365 secondWindow->consumeMotionUp();
5366}
5367
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005368// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
5369// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
5370// touch is not supported, so the touch should continue on those windows and the transferred-to
5371// window should get nothing.
5372TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
5373 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5374
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005375 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005376 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5377 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005378 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005379
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005380 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005381 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5382 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005383 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005384
5385 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005386 mDispatcher->onWindowInfosChanged(
5387 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005388
5389 PointF pointInFirst = {300, 200};
5390 PointF pointInSecond = {300, 600};
5391
5392 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005393 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5394 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5395 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005396 // Only the first window should get the down event
5397 firstWindow->consumeMotionDown();
5398 secondWindow->assertNoEvents();
5399
5400 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005401 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5402 ADISPLAY_ID_DEFAULT,
5403 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005404 // The first window gets a move and the second a down
5405 firstWindow->consumeMotionMove();
5406 secondWindow->consumeMotionDown();
5407
5408 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005409 const bool transferred =
5410 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005411 // The 'transferTouch' call should not succeed, because there are 2 touched windows
5412 ASSERT_FALSE(transferred);
5413 firstWindow->assertNoEvents();
5414 secondWindow->assertNoEvents();
5415
5416 // The rest of the dispatch should proceed as normal
5417 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005418 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
5419 ADISPLAY_ID_DEFAULT,
5420 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005421 // The first window gets MOVE and the second gets pointer up
5422 firstWindow->consumeMotionMove();
5423 secondWindow->consumeMotionUp();
5424
5425 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005426 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5427 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005428 // The first window gets nothing and the second gets up
5429 firstWindow->consumeMotionUp();
5430 secondWindow->assertNoEvents();
5431}
5432
Arthur Hungabbb9d82021-09-01 14:52:30 +00005433// This case will create two windows and one mirrored window on the default display and mirror
5434// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
5435// the windows info of second display before default display.
5436TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
5437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5438 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005439 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005440 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005441 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005442 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005443 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005444
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005445 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005446 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005447
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005448 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005449 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005450
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005451 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005452 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005453
5454 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005455 mDispatcher->onWindowInfosChanged(
5456 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5457 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5458 *secondWindowInPrimary->getInfo()},
5459 {},
5460 0,
5461 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005462
5463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005464 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005465 {50, 50}))
5466 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5467
5468 // Window should receive motion event.
5469 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5470
5471 // Transfer touch focus
5472 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
5473 secondWindowInPrimary->getToken()));
5474 // The first window gets cancel.
5475 firstWindowInPrimary->consumeMotionCancel();
5476 secondWindowInPrimary->consumeMotionDown();
5477
5478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005479 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005480 ADISPLAY_ID_DEFAULT, {150, 50}))
5481 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5482 firstWindowInPrimary->assertNoEvents();
5483 secondWindowInPrimary->consumeMotionMove();
5484
5485 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005486 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005487 {150, 50}))
5488 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5489 firstWindowInPrimary->assertNoEvents();
5490 secondWindowInPrimary->consumeMotionUp();
5491}
5492
5493// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
5494// 'transferTouch' api.
5495TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
5496 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5497 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005498 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005499 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005500 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005501 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005502 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005503
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005504 sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005505 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005506
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005507 sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005508 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005509
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07005510 sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005511 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005512
5513 // Update window info, let it find window handle of second display first.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005514 mDispatcher->onWindowInfosChanged(
5515 {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(),
5516 *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(),
5517 *secondWindowInPrimary->getInfo()},
5518 {},
5519 0,
5520 0});
Arthur Hungabbb9d82021-09-01 14:52:30 +00005521
5522 // Touch on second display.
5523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005524 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID,
5525 {50, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005526 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5527
5528 // Window should receive motion event.
5529 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
5530
5531 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005532 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00005533
5534 // The first window gets cancel.
5535 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
5536 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
5537
5538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005539 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005540 SECOND_DISPLAY_ID, {150, 50}))
5541 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5542 firstWindowInPrimary->assertNoEvents();
5543 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
5544
5545 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005546 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
Arthur Hungabbb9d82021-09-01 14:52:30 +00005547 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5548 firstWindowInPrimary->assertNoEvents();
5549 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
5550}
5551
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005552TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005553 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005554 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5555 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005556
Vishnu Nair47074b82020-08-14 11:54:47 -07005557 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005558 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005559 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005560
5561 window->consumeFocusEvent(true);
5562
Prabir Pradhan678438e2023-04-13 19:32:51 +00005563 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005564
5565 // Window should receive key down event.
5566 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00005567
5568 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005569 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005570 mFakePolicy->assertUserActivityPoked();
5571}
5572
5573TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
5574 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5575 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5576 "Fake Window", ADISPLAY_ID_DEFAULT);
5577
5578 window->setDisableUserActivity(true);
5579 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005580 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005581 setFocusedWindow(window);
5582
5583 window->consumeFocusEvent(true);
5584
5585 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5586
5587 // Window should receive key down event.
5588 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5589
5590 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005591 mDispatcher->waitForIdle();
Josep del Riob3981622023-04-18 15:49:45 +00005592 mFakePolicy->assertUserActivityNotPoked();
5593}
5594
5595TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
5596 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5597 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5598 "Fake Window", ADISPLAY_ID_DEFAULT);
5599
5600 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005601 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005602 setFocusedWindow(window);
5603
5604 window->consumeFocusEvent(true);
5605
5606 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5607 mDispatcher->waitForIdle();
5608
5609 // System key is not passed down
5610 window->assertNoEvents();
5611
5612 // Should have poked user activity
5613 mFakePolicy->assertUserActivityPoked();
5614}
5615
5616TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
5617 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5618 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5619 "Fake Window", ADISPLAY_ID_DEFAULT);
5620
5621 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005622 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005623 setFocusedWindow(window);
5624
5625 window->consumeFocusEvent(true);
5626
5627 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5628 mDispatcher->waitForIdle();
5629
5630 // System key is not passed down
5631 window->assertNoEvents();
5632
5633 // Should have poked user activity
5634 mFakePolicy->assertUserActivityPoked();
5635}
5636
5637TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
5638 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5639 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5640 "Fake Window", ADISPLAY_ID_DEFAULT);
5641
5642 window->setDisableUserActivity(true);
5643 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005644 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Josep del Riob3981622023-04-18 15:49:45 +00005645 setFocusedWindow(window);
5646
5647 window->consumeFocusEvent(true);
5648
5649 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
5650 mDispatcher->waitForIdle();
5651
5652 // System key is not passed down
5653 window->assertNoEvents();
5654
5655 // Should have poked user activity
5656 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005657}
5658
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005659TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
5660 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5661 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5662 "Fake Window", ADISPLAY_ID_DEFAULT);
5663
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005664 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005665
5666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005667 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005668 ADISPLAY_ID_DEFAULT, {100, 100}))
5669 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5670
5671 window->consumeMotionEvent(
5672 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
5673
5674 // Should have poked user activity
Siarhei Vishniakou4fd86732023-05-24 17:33:01 +00005675 mDispatcher->waitForIdle();
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07005676 mFakePolicy->assertUserActivityPoked();
5677}
5678
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005679TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005680 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005681 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5682 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005683
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005684 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005685
Prabir Pradhan678438e2023-04-13 19:32:51 +00005686 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005687 mDispatcher->waitForIdle();
5688
5689 window->assertNoEvents();
5690}
5691
5692// If a window is touchable, but does not have focus, it should receive motion events, but not keys
5693TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07005694 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005695 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5696 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005697
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005698 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005699
5700 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00005701 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005702 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00005703 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5704 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005705
5706 // Window should receive only the motion event
5707 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5708 window->assertNoEvents(); // Key event or focus event will not be received
5709}
5710
arthurhungea3f4fc2020-12-21 23:18:53 +08005711TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
5712 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5713
arthurhungea3f4fc2020-12-21 23:18:53 +08005714 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005715 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
5716 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08005717 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08005718
arthurhungea3f4fc2020-12-21 23:18:53 +08005719 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005720 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
5721 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08005722 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08005723
5724 // Add the windows to the dispatcher
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005725 mDispatcher->onWindowInfosChanged(
5726 {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0});
arthurhungea3f4fc2020-12-21 23:18:53 +08005727
5728 PointF pointInFirst = {300, 200};
5729 PointF pointInSecond = {300, 600};
5730
5731 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005732 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5733 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5734 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005735 // Only the first window should get the down event
5736 firstWindow->consumeMotionDown();
5737 secondWindow->assertNoEvents();
5738
5739 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005740 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5741 ADISPLAY_ID_DEFAULT,
5742 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005743 // The first window gets a move and the second a down
5744 firstWindow->consumeMotionMove();
5745 secondWindow->consumeMotionDown();
5746
5747 // Send pointer cancel to the second window
5748 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005749 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08005750 {pointInFirst, pointInSecond});
5751 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00005752 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08005753 // The first window gets move and the second gets cancel.
5754 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5755 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5756
5757 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005758 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5759 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08005760 // The first window gets up and the second gets nothing.
5761 firstWindow->consumeMotionUp();
5762 secondWindow->assertNoEvents();
5763}
5764
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005765TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
5766 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5767
5768 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005769 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005770 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005771 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
5772 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
5773 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
5774
Harry Cutts33476232023-01-30 19:57:29 +00005775 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005776 window->assertNoEvents();
5777 mDispatcher->waitForIdle();
5778}
5779
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005780using InputDispatcherMonitorTest = InputDispatcherTest;
5781
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005782/**
5783 * Two entities that receive touch: A window, and a global monitor.
5784 * The touch goes to the window, and then the window disappears.
5785 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
5786 * for the monitor, as well.
5787 * 1. foregroundWindow
5788 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
5789 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005790TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005791 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5792 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005793 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005794
Prabir Pradhanfb549072023-10-05 19:17:36 +00005795 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005796
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005797 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005798 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005799 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005800 {100, 200}))
5801 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5802
5803 // Both the foreground window and the global monitor should receive the touch down
5804 window->consumeMotionDown();
5805 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5806
5807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005808 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005809 ADISPLAY_ID_DEFAULT, {110, 200}))
5810 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5811
5812 window->consumeMotionMove();
5813 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5814
5815 // Now the foreground window goes away
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005816 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005817 window->consumeMotionCancel();
5818 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5819
5820 // If more events come in, there will be no more foreground window to send them to. This will
5821 // cause a cancel for the monitor, as well.
5822 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005823 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005824 ADISPLAY_ID_DEFAULT, {120, 200}))
5825 << "Injection should fail because the window was removed";
5826 window->assertNoEvents();
5827 // Global monitor now gets the cancel
5828 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5829}
5830
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005831TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005832 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005833 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5834 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005835 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00005836
Prabir Pradhanfb549072023-10-05 19:17:36 +00005837 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005838
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005839 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005840 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005841 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005842 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005843 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005844}
5845
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005846TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
Prabir Pradhanfb549072023-10-05 19:17:36 +00005847 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005848
Chris Yea209fde2020-07-22 13:54:51 -07005849 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005850 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5851 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005852 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Michael Wright3a240c42019-12-10 20:53:41 +00005853
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005854 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005855 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005856 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005857 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005858 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005859
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005860 // Pilfer pointers from the monitor.
5861 // This should not do anything and the window should continue to receive events.
5862 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005863
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005864 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005865 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005866 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005867 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005868
5869 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5870 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005871}
5872
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005873TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005874 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005875 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5876 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005877 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Evan Rosky84f07f02021-04-16 10:42:42 -07005878 window->setWindowOffset(20, 40);
5879 window->setWindowTransform(0, 1, -1, 0);
5880
Prabir Pradhanfb549072023-10-05 19:17:36 +00005881 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005882
5883 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005884 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Evan Rosky84f07f02021-04-16 10:42:42 -07005885 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5886 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5887 MotionEvent* event = monitor.consumeMotion();
5888 // Even though window has transform, gesture monitor must not.
5889 ASSERT_EQ(ui::Transform(), event->getTransform());
5890}
5891
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005892TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005893 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhanfb549072023-10-05 19:17:36 +00005894 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005895
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005896 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005897 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005898 << "Injection should fail if there is a monitor, but no touchable window";
5899 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005900}
5901
chaviw81e2bb92019-12-18 15:03:51 -08005902TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005903 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005904 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5905 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005906
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005907 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
chaviw81e2bb92019-12-18 15:03:51 -08005908
5909 NotifyMotionArgs motionArgs =
5910 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5911 ADISPLAY_ID_DEFAULT);
5912
Prabir Pradhan678438e2023-04-13 19:32:51 +00005913 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005914 // Window should receive motion down event.
5915 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5916
5917 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005918 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005919 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5920 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5921 motionArgs.pointerCoords[0].getX() - 10);
5922
Prabir Pradhan678438e2023-04-13 19:32:51 +00005923 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005924 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005925 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005926}
5927
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005928/**
5929 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5930 * the device default right away. In the test scenario, we check both the default value,
5931 * and the action of enabling / disabling.
5932 */
5933TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005934 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005935 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5936 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005937 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005938
5939 // Set focused application.
5940 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005941 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005942
5943 SCOPED_TRACE("Check default value of touch mode");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005944 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005945 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005946 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005947
5948 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005949 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005950 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00005951 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005952
5953 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005954 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005955 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005956 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005957 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005958 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005959 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005960 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005961
5962 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005963 window->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005964 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Harry Cutts33476232023-01-30 19:57:29 +00005965 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005966
5967 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005968 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005969 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005970 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005971 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005972 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005973 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005974 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005975
5976 window->assertNoEvents();
5977}
5978
Gang Wange9087892020-01-07 12:17:14 -05005979TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005980 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005981 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5982 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005983
5984 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005985 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005986
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07005987 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07005988 setFocusedWindow(window);
5989
Harry Cutts33476232023-01-30 19:57:29 +00005990 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005991
Prabir Pradhan678438e2023-04-13 19:32:51 +00005992 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5993 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005994
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07005995 KeyEvent* event = window->consumeKey();
Gang Wange9087892020-01-07 12:17:14 -05005996 ASSERT_NE(event, nullptr);
5997
5998 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5999 ASSERT_NE(verified, nullptr);
6000 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
6001
6002 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
6003 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
6004 ASSERT_EQ(keyArgs.source, verified->source);
6005 ASSERT_EQ(keyArgs.displayId, verified->displayId);
6006
6007 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
6008
6009 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05006010 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006011 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05006012 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
6013 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
6014 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
6015 ASSERT_EQ(0, verifiedKey.repeatCount);
6016}
6017
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006018TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07006019 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006020 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
6021 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006022
6023 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6024
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006025 ui::Transform transform;
6026 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6027
6028 gui::DisplayInfo displayInfo;
6029 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
6030 displayInfo.transform = transform;
6031
Patrick Williamsd828f302023-04-28 17:52:08 -05006032 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006033
Prabir Pradhan678438e2023-04-13 19:32:51 +00006034 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006035 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6036 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006037 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006038
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006039 MotionEvent* event = window->consumeMotion();
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006040 ASSERT_NE(event, nullptr);
6041
6042 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
6043 ASSERT_NE(verified, nullptr);
6044 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
6045
6046 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
6047 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
6048 EXPECT_EQ(motionArgs.source, verified->source);
6049 EXPECT_EQ(motionArgs.displayId, verified->displayId);
6050
6051 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
6052
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07006053 const vec2 rawXY =
6054 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
6055 motionArgs.pointerCoords[0].getXYValue());
6056 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
6057 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006058 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006059 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006060 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08006061 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
6062 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
6063}
6064
chaviw09c8d2d2020-08-24 15:48:26 -07006065/**
6066 * Ensure that separate calls to sign the same data are generating the same key.
6067 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
6068 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
6069 * tests.
6070 */
6071TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
6072 KeyEvent event = getTestKeyEvent();
6073 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6074
6075 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
6076 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
6077 ASSERT_EQ(hmac1, hmac2);
6078}
6079
6080/**
6081 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
6082 */
6083TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
6084 KeyEvent event = getTestKeyEvent();
6085 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
6086 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
6087
6088 verifiedEvent.deviceId += 1;
6089 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6090
6091 verifiedEvent.source += 1;
6092 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6093
6094 verifiedEvent.eventTimeNanos += 1;
6095 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6096
6097 verifiedEvent.displayId += 1;
6098 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6099
6100 verifiedEvent.action += 1;
6101 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6102
6103 verifiedEvent.downTimeNanos += 1;
6104 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6105
6106 verifiedEvent.flags += 1;
6107 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6108
6109 verifiedEvent.keyCode += 1;
6110 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6111
6112 verifiedEvent.scanCode += 1;
6113 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6114
6115 verifiedEvent.metaState += 1;
6116 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6117
6118 verifiedEvent.repeatCount += 1;
6119 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
6120}
6121
Vishnu Nair958da932020-08-21 17:12:37 -07006122TEST_F(InputDispatcherTest, SetFocusedWindow) {
6123 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6124 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006125 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006126 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006127 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006128 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6129
6130 // Top window is also focusable but is not granted focus.
6131 windowTop->setFocusable(true);
6132 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006133 mDispatcher->onWindowInfosChanged(
6134 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006135 setFocusedWindow(windowSecond);
6136
6137 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006138 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006139 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006140
6141 // Focused window should receive event.
6142 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6143 windowTop->assertNoEvents();
6144}
6145
6146TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
6147 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6148 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006149 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006150 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6151
6152 window->setFocusable(true);
6153 // Release channel for window is no longer valid.
6154 window->releaseChannel();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006155 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006156 setFocusedWindow(window);
6157
6158 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006159 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006160
6161 // window channel is invalid, so it should not receive any input event.
6162 window->assertNoEvents();
6163}
6164
6165TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
6166 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6167 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006168 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006169 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07006170 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6171
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006172 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006173 setFocusedWindow(window);
6174
6175 // Test inject a key down, should timeout.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006176 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Vishnu Nair958da932020-08-21 17:12:37 -07006177
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08006178 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07006179 window->assertNoEvents();
6180}
6181
6182TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
6183 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6184 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006185 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006186 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006187 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006188 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6189
6190 windowTop->setFocusable(true);
6191 windowSecond->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006192 mDispatcher->onWindowInfosChanged(
6193 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006194 setFocusedWindow(windowTop);
6195 windowTop->consumeFocusEvent(true);
6196
Chavi Weingarten847e8512023-03-29 00:26:09 +00006197 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006198 mDispatcher->onWindowInfosChanged(
6199 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006200 windowSecond->consumeFocusEvent(true);
6201 windowTop->consumeFocusEvent(false);
6202
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006204 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006205
6206 // Focused window should receive event.
6207 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
6208}
6209
Chavi Weingarten847e8512023-03-29 00:26:09 +00006210TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07006211 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6212 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006213 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006214 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006215 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006216 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6217
6218 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00006219 windowSecond->setFocusable(false);
6220 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006221 mDispatcher->onWindowInfosChanged(
6222 {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0});
Chavi Weingarten847e8512023-03-29 00:26:09 +00006223 setFocusedWindow(windowTop);
6224 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07006225
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006226 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Chavi Weingarten847e8512023-03-29 00:26:09 +00006227 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006228
6229 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00006230 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07006231 windowSecond->assertNoEvents();
6232}
6233
6234TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
6235 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6236 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006237 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006238 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006239 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
6240 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006241 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6242
6243 window->setFocusable(true);
6244 previousFocusedWindow->setFocusable(true);
6245 window->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006246 mDispatcher->onWindowInfosChanged(
6247 {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006248 setFocusedWindow(previousFocusedWindow);
6249 previousFocusedWindow->consumeFocusEvent(true);
6250
6251 // Requesting focus on invisible window takes focus from currently focused window.
6252 setFocusedWindow(window);
6253 previousFocusedWindow->consumeFocusEvent(false);
6254
6255 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006257 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
6258 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07006259
6260 // Window does not get focus event or key down.
6261 window->assertNoEvents();
6262
6263 // Window becomes visible.
6264 window->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006265 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006266
6267 // Window receives focus event.
6268 window->consumeFocusEvent(true);
6269 // Focused window receives key down.
6270 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6271}
6272
Vishnu Nair599f1412021-06-21 10:39:58 -07006273TEST_F(InputDispatcherTest, DisplayRemoved) {
6274 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6275 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006276 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07006277 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6278
6279 // window is granted focus.
6280 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006281 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair599f1412021-06-21 10:39:58 -07006282 setFocusedWindow(window);
6283 window->consumeFocusEvent(true);
6284
6285 // When a display is removed window loses focus.
6286 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
6287 window->consumeFocusEvent(false);
6288}
6289
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006290/**
6291 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
6292 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
6293 * of the 'slipperyEnterWindow'.
6294 *
6295 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
6296 * a way so that the touched location is no longer covered by the top window.
6297 *
6298 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
6299 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
6300 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
6301 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
6302 * with ACTION_DOWN).
6303 * Thus, the touch has been transferred from the top window into the bottom window, because the top
6304 * window moved itself away from the touched location and had Flag::SLIPPERY.
6305 *
6306 * Even though the top window moved away from the touched location, it is still obscuring the bottom
6307 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
6308 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
6309 *
6310 * In this test, we ensure that the event received by the bottom window has
6311 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
6312 */
6313TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006314 constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1};
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006315 constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1};
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006316
6317 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6318 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6319
6320 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006321 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08006322 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006323 // Make sure this one overlaps the bottom window
6324 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
6325 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
6326 // one. Windows with the same owner are not considered to be occluding each other.
6327 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
6328
6329 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006330 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006331 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
6332
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006333 mDispatcher->onWindowInfosChanged(
6334 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006335
6336 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00006337 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
6338 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6339 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006340 slipperyExitWindow->consumeMotionDown();
6341 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006342 mDispatcher->onWindowInfosChanged(
6343 {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006344
Prabir Pradhan678438e2023-04-13 19:32:51 +00006345 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
6346 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6347 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006348
6349 slipperyExitWindow->consumeMotionCancel();
6350
6351 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
6352 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
6353}
6354
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006355/**
6356 * Two windows, one on the left and another on the right. The left window is slippery. The right
6357 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
6358 * touch moves from the left window into the right window, the gesture should continue to go to the
6359 * left window. Touch shouldn't slip because the right window can't receive touches. This test
6360 * reproduces a crash.
6361 */
6362TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
6363 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6364
6365 sp<FakeWindowHandle> leftSlipperyWindow =
6366 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6367 leftSlipperyWindow->setSlippery(true);
6368 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
6369
6370 sp<FakeWindowHandle> rightDropTouchesWindow =
6371 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6372 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
6373 rightDropTouchesWindow->setDropInput(true);
6374
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006375 mDispatcher->onWindowInfosChanged(
6376 {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07006377
6378 // Start touch in the left window
6379 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6380 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6381 .build());
6382 leftSlipperyWindow->consumeMotionDown();
6383
6384 // And move it into the right window
6385 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6386 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
6387 .build());
6388
6389 // Since the right window isn't eligible to receive input, touch does not slip.
6390 // The left window continues to receive the gesture.
6391 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6392 rightDropTouchesWindow->assertNoEvents();
6393}
6394
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07006395/**
6396 * A single window is on screen first. Touch is injected into that window. Next, a second window
6397 * appears. Since the first window is slippery, touch will move from the first window to the second.
6398 */
6399TEST_F(InputDispatcherTest, InjectedTouchSlips) {
6400 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6401 sp<FakeWindowHandle> originalWindow =
6402 sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT);
6403 originalWindow->setFrame(Rect(0, 0, 200, 200));
6404 originalWindow->setSlippery(true);
6405
6406 sp<FakeWindowHandle> appearingWindow =
6407 sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT);
6408 appearingWindow->setFrame(Rect(0, 0, 200, 200));
6409
6410 mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0});
6411
6412 // Touch down on the original window
6413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6414 injectMotionEvent(*mDispatcher,
6415 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6416 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
6417 .build()));
6418 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6419
6420 // Now, a new window appears. This could be, for example, a notification shade that appears
6421 // after user starts to drag down on the launcher window.
6422 mDispatcher->onWindowInfosChanged(
6423 {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0});
6424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6425 injectMotionEvent(*mDispatcher,
6426 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6427 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110))
6428 .build()));
6429 originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
6430 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
6431 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6432 injectMotionEvent(*mDispatcher,
6433 MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6434 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
6435 .build()));
6436 appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
6437
6438 originalWindow->assertNoEvents();
6439 appearingWindow->assertNoEvents();
6440}
6441
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006442TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006443 using Uid = gui::Uid;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006444 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6445
6446 sp<FakeWindowHandle> leftWindow =
6447 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
6448 leftWindow->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006449 leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006450
6451 sp<FakeWindowHandle> rightSpy =
6452 sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT);
6453 rightSpy->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006454 rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006455 rightSpy->setSpy(true);
6456 rightSpy->setTrustedOverlay(true);
6457
6458 sp<FakeWindowHandle> rightWindow =
6459 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
6460 rightWindow->setFrame(Rect(100, 0, 200, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006461 rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006462
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006463 mDispatcher->onWindowInfosChanged(
6464 {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006465
6466 // Touch in the left window
6467 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6468 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6469 .build());
6470 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown());
6471 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006472 ASSERT_NO_FATAL_FAILURE(
6473 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006474
6475 // Touch another finger over the right windows
6476 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
6477 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6478 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6479 .build());
6480 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown());
6481 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown());
6482 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove());
6483 mDispatcher->waitForIdle();
6484 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006485 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID,
6486 {Uid{101}, Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006487
6488 // Release finger over left window. The UP actions are not treated as device interaction.
6489 // The windows that did not receive the UP pointer will receive MOVE events, but since this
6490 // is part of the UP action, we do not treat this as device interaction.
6491 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
6492 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
6493 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6494 .build());
6495 ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp());
6496 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
6497 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
6498 mDispatcher->waitForIdle();
6499 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
6500
6501 // Move remaining finger
6502 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
6503 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6504 .build());
6505 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove());
6506 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove());
6507 mDispatcher->waitForIdle();
6508 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006509 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006510
6511 // Release all fingers
6512 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
6513 .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50))
6514 .build());
6515 ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp());
6516 ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp());
6517 mDispatcher->waitForIdle();
6518 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
6519}
6520
6521TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) {
6522 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
6523
6524 sp<FakeWindowHandle> window =
6525 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
6526 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006527 window->setOwnerInfo(gui::Pid{1}, gui::Uid{101});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006528
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006529 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006530 setFocusedWindow(window);
6531 ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true));
6532
6533 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build());
6534 ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT));
6535 mDispatcher->waitForIdle();
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00006536 ASSERT_NO_FATAL_FAILURE(
6537 mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}}));
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00006538
6539 // The UP actions are not treated as device interaction.
6540 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build());
6541 ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT));
6542 mDispatcher->waitForIdle();
6543 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled());
6544}
6545
Prabir Pradhanb0dad3a2023-11-02 20:52:47 +00006546class InputDispatcherFallbackKeyTest : public InputDispatcherTest {
6547protected:
6548 std::shared_ptr<FakeApplicationHandle> mApp;
6549 sp<FakeWindowHandle> mWindow;
6550
6551 virtual void SetUp() override {
6552 InputDispatcherTest::SetUp();
6553
6554 mApp = std::make_shared<FakeApplicationHandle>();
6555
6556 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
6557 mWindow->setFrame(Rect(0, 0, 100, 100));
6558
6559 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
6560 setFocusedWindow(mWindow);
6561 ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true));
6562 }
6563
6564 void setFallback(int32_t keycode) {
6565 mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) {
6566 return KeyEventBuilder(event).keyCode(keycode).build();
6567 });
6568 }
6569
6570 void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) {
6571 KeyEvent* event = mWindow->consumeKey(handled);
6572 ASSERT_NE(event, nullptr) << "Did not receive key event";
6573 ASSERT_THAT(*event, matcher);
6574 }
6575};
6576
6577TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) {
6578 mDispatcher->notifyKey(
6579 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6580 consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
6581 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
6582}
6583
6584TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) {
6585 mDispatcher->notifyKey(
6586 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6587 consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A)));
6588 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6589}
6590
6591TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) {
6592 mDispatcher->notifyKey(
6593 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6594
6595 // Do not handle this key event.
6596 consumeKey(/*handled=*/false,
6597 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6598 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6599
6600 // Since the policy did not request any fallback to be generated, ensure there are no events.
6601 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
6602}
6603
6604TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) {
6605 setFallback(AKEYCODE_B);
6606 mDispatcher->notifyKey(
6607 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6608
6609 // Do not handle this key event.
6610 consumeKey(/*handled=*/false,
6611 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6612
6613 // Since the key was not handled, ensure the fallback event was dispatched instead.
6614 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6615 consumeKey(/*handled=*/true,
6616 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
6617 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
6618
6619 // Release the original key, and ensure the fallback key is also released.
6620 mDispatcher->notifyKey(
6621 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6622 consumeKey(/*handled=*/false,
6623 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6624 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6625 consumeKey(/*handled=*/true,
6626 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
6627 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
6628
6629 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
6630 mWindow->assertNoEvents();
6631}
6632
6633TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) {
6634 setFallback(AKEYCODE_B);
6635 mDispatcher->notifyKey(
6636 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6637
6638 // Do not handle this key event, but handle the fallback.
6639 consumeKey(/*handled=*/false,
6640 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6641 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6642 consumeKey(/*handled=*/true,
6643 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
6644 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
6645
6646 // Release the original key, and ensure the fallback key is also released.
6647 mDispatcher->notifyKey(
6648 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6649 // But this time, the app handles the original key.
6650 consumeKey(/*handled=*/true,
6651 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6652 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6653 // Ensure the fallback key is canceled.
6654 consumeKey(/*handled=*/true,
6655 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
6656 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
6657
6658 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
6659 mWindow->assertNoEvents();
6660}
6661
6662TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) {
6663 setFallback(AKEYCODE_B);
6664 mDispatcher->notifyKey(
6665 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6666
6667 // Do not handle this key event.
6668 consumeKey(/*handled=*/false,
6669 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6671 // App does not handle the fallback either, so ensure another fallback is not generated.
6672 setFallback(AKEYCODE_C);
6673 consumeKey(/*handled=*/false,
6674 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
6675 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
6676
6677 // Release the original key, and ensure the fallback key is also released.
6678 setFallback(AKEYCODE_B);
6679 mDispatcher->notifyKey(
6680 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6681 consumeKey(/*handled=*/false,
6682 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6683 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6684 consumeKey(/*handled=*/false,
6685 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
6686 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
6687
6688 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
6689 mWindow->assertNoEvents();
6690}
6691
6692TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) {
6693 setFallback(AKEYCODE_B);
6694 mDispatcher->notifyKey(
6695 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6696
6697 // Do not handle this key event, so fallback is generated.
6698 consumeKey(/*handled=*/false,
6699 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6700 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6701 consumeKey(/*handled=*/true,
6702 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
6703 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
6704
6705 // Release the original key, but assume the policy is misbehaving and it
6706 // generates an inconsistent fallback to the one from the DOWN event.
6707 setFallback(AKEYCODE_C);
6708 mDispatcher->notifyKey(
6709 KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6710 consumeKey(/*handled=*/false,
6711 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6712 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6713 // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency.
6714 consumeKey(/*handled=*/true,
6715 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
6716 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
6717
6718 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
6719 mWindow->assertNoEvents();
6720}
6721
6722TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) {
6723 setFallback(AKEYCODE_B);
6724 mDispatcher->notifyKey(
6725 KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
6726
6727 // Do not handle this key event, so fallback is generated.
6728 consumeKey(/*handled=*/false,
6729 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0)));
6730 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6731 consumeKey(/*handled=*/true,
6732 AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B),
6733 WithFlags(AKEY_EVENT_FLAG_FALLBACK)));
6734
6735 // The original key is canceled.
6736 mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD)
6737 .keyCode(AKEYCODE_A)
6738 .addFlag(AKEY_EVENT_FLAG_CANCELED)
6739 .build());
6740 consumeKey(/*handled=*/false,
6741 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A),
6742 WithFlags(AKEY_EVENT_FLAG_CANCELED)));
6743 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A));
6744 // Ensure the fallback key is also canceled due to the original key being canceled.
6745 consumeKey(/*handled=*/true,
6746 AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B),
6747 WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED)));
6748
6749 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported());
6750 mWindow->assertNoEvents();
6751}
6752
Garfield Tan1c7bc862020-01-28 13:24:04 -08006753class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
6754protected:
6755 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
6756 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
6757
Chris Yea209fde2020-07-22 13:54:51 -07006758 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08006759 sp<FakeWindowHandle> mWindow;
6760
6761 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00006762 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Prabir Pradhana41d2442023-04-20 21:30:40 +00006763 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Harry Cutts101ee9b2023-07-06 18:04:14 +00006764 mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09006765 mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Garfield Tan1c7bc862020-01-28 13:24:04 -08006766 ASSERT_EQ(OK, mDispatcher->start());
6767
6768 setUpWindow();
6769 }
6770
6771 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07006772 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006773 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08006774
Vishnu Nair47074b82020-08-14 11:54:47 -07006775 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006776 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006777 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08006778 mWindow->consumeFocusEvent(true);
6779 }
6780
Chris Ye2ad95392020-09-01 13:44:44 -07006781 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08006782 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07006783 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08006784 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00006785 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08006786
6787 // Window should receive key down event.
6788 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6789 }
6790
6791 void expectKeyRepeatOnce(int32_t repeatCount) {
6792 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006793 mWindow->consumeKeyEvent(
6794 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount)));
Garfield Tan1c7bc862020-01-28 13:24:04 -08006795 }
6796
Chris Ye2ad95392020-09-01 13:44:44 -07006797 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08006798 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07006799 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08006800 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00006801 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08006802
6803 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006804 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00006805 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08006806 }
6807};
6808
6809TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00006810 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07006811 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
6812 expectKeyRepeatOnce(repeatCount);
6813 }
6814}
6815
6816TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00006817 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07006818 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
6819 expectKeyRepeatOnce(repeatCount);
6820 }
Harry Cutts33476232023-01-30 19:57:29 +00006821 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07006822 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08006823 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
6824 expectKeyRepeatOnce(repeatCount);
6825 }
6826}
6827
6828TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00006829 sendAndConsumeKeyDown(/*deviceId=*/1);
6830 expectKeyRepeatOnce(/*repeatCount=*/1);
6831 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07006832 mWindow->assertNoEvents();
6833}
6834
6835TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00006836 sendAndConsumeKeyDown(/*deviceId=*/1);
6837 expectKeyRepeatOnce(/*repeatCount=*/1);
6838 sendAndConsumeKeyDown(/*deviceId=*/2);
6839 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07006840 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00006841 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07006842 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00006843 expectKeyRepeatOnce(/*repeatCount=*/2);
6844 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07006845 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00006846 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07006847 mWindow->assertNoEvents();
6848}
6849
6850TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00006851 sendAndConsumeKeyDown(/*deviceId=*/1);
6852 expectKeyRepeatOnce(/*repeatCount=*/1);
6853 sendAndConsumeKeyDown(/*deviceId=*/2);
6854 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07006855 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00006856 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07006857 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08006858 mWindow->assertNoEvents();
6859}
6860
liushenxiang42232912021-05-21 20:24:09 +08006861TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
6862 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00006863 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006864 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08006865 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
6866 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
6867 mWindow->assertNoEvents();
6868}
6869
Garfield Tan1c7bc862020-01-28 13:24:04 -08006870TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00006871 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00006872 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08006873 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006874 KeyEvent* repeatEvent = mWindow->consumeKey();
Garfield Tan1c7bc862020-01-28 13:24:04 -08006875 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
6876 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
6877 IdGenerator::getSource(repeatEvent->getId()));
6878 }
6879}
6880
6881TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00006882 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00006883 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08006884
6885 std::unordered_set<int32_t> idSet;
6886 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006887 KeyEvent* repeatEvent = mWindow->consumeKey();
Garfield Tan1c7bc862020-01-28 13:24:04 -08006888 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
6889 int32_t id = repeatEvent->getId();
6890 EXPECT_EQ(idSet.end(), idSet.find(id));
6891 idSet.insert(id);
6892 }
6893}
6894
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006895/* Test InputDispatcher for MultiDisplay */
6896class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
6897public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006898 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006899 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08006900
Chris Yea209fde2020-07-22 13:54:51 -07006901 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006902 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006903 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006904
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006905 // Set focus window for primary display, but focused display would be second one.
6906 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07006907 windowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006908 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
6909
Vishnu Nair958da932020-08-21 17:12:37 -07006910 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006911 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08006912
Chris Yea209fde2020-07-22 13:54:51 -07006913 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006914 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006915 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006916 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006917 // Set focus display to second one.
6918 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
6919 // Set focus window for second display.
6920 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07006921 windowInSecondary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006922 mDispatcher->onWindowInfosChanged(
6923 {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07006924 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006925 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006926 }
6927
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006928 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006929 InputDispatcherTest::TearDown();
6930
Chris Yea209fde2020-07-22 13:54:51 -07006931 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006932 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07006933 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006934 windowInSecondary.clear();
6935 }
6936
6937protected:
Chris Yea209fde2020-07-22 13:54:51 -07006938 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006939 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07006940 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006941 sp<FakeWindowHandle> windowInSecondary;
6942};
6943
6944TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
6945 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006946 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006947 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006948 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006949 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08006950 windowInSecondary->assertNoEvents();
6951
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006952 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006953 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006954 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006955 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08006956 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006957 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08006958}
6959
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006960TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08006961 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006963 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006964 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006965 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08006966 windowInSecondary->assertNoEvents();
6967
6968 // Test inject a key down without display id specified.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006970 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08006971 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006972 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08006973
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006974 // Remove all windows in secondary display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07006975 mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0});
Arthur Hungb92218b2018-08-14 12:00:21 +08006976
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006977 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006978 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006979 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08006980
6981 // Test inject a key down, should timeout because of no target window.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006982 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Arthur Hungb92218b2018-08-14 12:00:21 +08006983 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006984 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08006985 windowInSecondary->assertNoEvents();
6986}
6987
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006988// Test per-display input monitors for motion event.
6989TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08006990 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00006991 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08006992 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00006993 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006994
6995 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006996 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07006997 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006998 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006999 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007000 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007001 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007002 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007003
7004 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007005 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007006 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007007 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007008 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007009 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007010 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08007011 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007012
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007013 // Lift up the touch from the second display
7014 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007015 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08007016 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7017 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
7018 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
7019
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007020 // Test inject a non-pointer motion event.
7021 // If specific a display, it will dispatch to the focused window of particular display,
7022 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007023 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007024 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007025 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007026 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007027 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007028 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007029 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007030}
7031
7032// Test per-display input monitors for key event.
7033TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007034 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08007035 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007036 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08007037 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007038 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007039
7040 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007041 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007042 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007043 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08007044 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08007045 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08007046 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08007047}
7048
Vishnu Nair958da932020-08-21 17:12:37 -07007049TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
7050 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007051 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07007052 secondWindowInPrimary->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007053 mDispatcher->onWindowInfosChanged(
7054 {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(),
7055 *windowInSecondary->getInfo()},
7056 {},
7057 0,
7058 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007059 setFocusedWindow(secondWindowInPrimary);
7060 windowInPrimary->consumeFocusEvent(false);
7061 secondWindowInPrimary->consumeFocusEvent(true);
7062
7063 // Test inject a key down.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007064 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
7065 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007066 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007067 windowInPrimary->assertNoEvents();
7068 windowInSecondary->assertNoEvents();
7069 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7070}
7071
Arthur Hungdfd528e2021-12-08 13:23:04 +00007072TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
7073 FakeMonitorReceiver monitorInPrimary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007074 FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007075 FakeMonitorReceiver monitorInSecondary =
Prabir Pradhanfb549072023-10-05 19:17:36 +00007076 FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007077
7078 // Test touch down on primary display.
7079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007080 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007081 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7082 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7083 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
7084
7085 // Test touch down on second display.
7086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007087 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
Arthur Hungdfd528e2021-12-08 13:23:04 +00007088 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7089 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
7090 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
7091
7092 // Trigger cancel touch.
7093 mDispatcher->cancelCurrentTouch();
7094 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7095 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
7096 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
7097 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
7098
7099 // Test inject a move motion event, no window/monitor should receive the event.
7100 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007101 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007102 ADISPLAY_ID_DEFAULT, {110, 200}))
7103 << "Inject motion event should return InputEventInjectionResult::FAILED";
7104 windowInPrimary->assertNoEvents();
7105 monitorInPrimary.assertNoEvents();
7106
7107 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007108 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungdfd528e2021-12-08 13:23:04 +00007109 SECOND_DISPLAY_ID, {110, 200}))
7110 << "Inject motion event should return InputEventInjectionResult::FAILED";
7111 windowInSecondary->assertNoEvents();
7112 monitorInSecondary.assertNoEvents();
7113}
7114
Jackal Guof9696682018-10-05 12:23:23 +08007115class InputFilterTest : public InputDispatcherTest {
7116protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007117 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
7118 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08007119 NotifyMotionArgs motionArgs;
7120
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007121 motionArgs =
7122 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007123 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007124 motionArgs =
7125 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007126 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007127 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007128 if (expectToBeFiltered) {
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007129 const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue());
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007130 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08007131 } else {
7132 mFakePolicy->assertFilterInputEventWasNotCalled();
7133 }
7134 }
7135
7136 void testNotifyKey(bool expectToBeFiltered) {
7137 NotifyKeyArgs keyArgs;
7138
7139 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007140 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007141 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00007142 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007143 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08007144
7145 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08007146 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08007147 } else {
7148 mFakePolicy->assertFilterInputEventWasNotCalled();
7149 }
7150 }
7151};
7152
7153// Test InputFilter for MotionEvent
7154TEST_F(InputFilterTest, MotionEvent_InputFilter) {
7155 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007156 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7157 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007158
7159 // Enable InputFilter
7160 mDispatcher->setInputFilterEnabled(true);
7161 // Test touch on both primary and second display, and check if both events are filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007162 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true);
7163 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007164
7165 // Disable InputFilter
7166 mDispatcher->setInputFilterEnabled(false);
7167 // Test touch on both primary and second display, and check if both events aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007168 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false);
7169 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007170}
7171
7172// Test InputFilter for KeyEvent
7173TEST_F(InputFilterTest, KeyEvent_InputFilter) {
7174 // Since the InputFilter is disabled by default, check if key event aren't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007175 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007176
7177 // Enable InputFilter
7178 mDispatcher->setInputFilterEnabled(true);
7179 // Send a key event, and check if it is filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007180 testNotifyKey(/*expectToBeFiltered=*/true);
Jackal Guof9696682018-10-05 12:23:23 +08007181
7182 // Disable InputFilter
7183 mDispatcher->setInputFilterEnabled(false);
7184 // Send a key event, and check if it isn't filtered.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007185 testNotifyKey(/*expectToBeFiltered=*/false);
Jackal Guof9696682018-10-05 12:23:23 +08007186}
7187
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007188// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
7189// logical display coordinate space.
7190TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
7191 ui::Transform firstDisplayTransform;
7192 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
7193 ui::Transform secondDisplayTransform;
7194 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
7195
7196 std::vector<gui::DisplayInfo> displayInfos(2);
7197 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
7198 displayInfos[0].transform = firstDisplayTransform;
7199 displayInfos[1].displayId = SECOND_DISPLAY_ID;
7200 displayInfos[1].transform = secondDisplayTransform;
7201
Patrick Williamsd828f302023-04-28 17:52:08 -05007202 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007203
7204 // Enable InputFilter
7205 mDispatcher->setInputFilterEnabled(true);
7206
7207 // Ensure the correct transforms are used for the displays.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007208 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform);
7209 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07007210}
7211
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007212class InputFilterInjectionPolicyTest : public InputDispatcherTest {
7213protected:
7214 virtual void SetUp() override {
7215 InputDispatcherTest::SetUp();
7216
7217 /**
7218 * We don't need to enable input filter to test the injected event policy, but we enabled it
7219 * here to make the tests more realistic, since this policy only matters when inputfilter is
7220 * on.
7221 */
7222 mDispatcher->setInputFilterEnabled(true);
7223
7224 std::shared_ptr<InputApplicationHandle> application =
7225 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007226 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
7227 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007228
7229 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
7230 mWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007231 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007232 setFocusedWindow(mWindow);
7233 mWindow->consumeFocusEvent(true);
7234 }
7235
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007236 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7237 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007238 KeyEvent event;
7239
7240 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7241 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
7242 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00007243 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007244 const int32_t additionalPolicyFlags =
7245 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
7246 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007247 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00007248 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007249 policyFlags | additionalPolicyFlags));
7250
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007251 mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags)));
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007252 }
7253
7254 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
7255 int32_t flags) {
7256 MotionEvent event;
7257 PointerProperties pointerProperties[1];
7258 PointerCoords pointerCoords[1];
7259 pointerProperties[0].clear();
7260 pointerProperties[0].id = 0;
7261 pointerCoords[0].clear();
7262 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
7263 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
7264
7265 ui::Transform identityTransform;
7266 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
7267 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
7268 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
7269 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
7270 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07007271 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07007272 eventTime,
Harry Cutts101ee9b2023-07-06 18:04:14 +00007273 /*pointerCount=*/1, pointerProperties, pointerCoords);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007274
7275 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
7276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007277 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou4648fea2023-06-27 01:00:12 +00007278 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007279 policyFlags | additionalPolicyFlags));
7280
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007281 mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId)));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007282 }
7283
7284private:
7285 sp<FakeWindowHandle> mWindow;
7286};
7287
7288TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007289 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
7290 // filter. Without it, the event will no different from a regularly injected event, and the
7291 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00007292 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
7293 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007294}
7295
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007296TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007297 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00007298 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007299 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
7300}
7301
7302TEST_F(InputFilterInjectionPolicyTest,
7303 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
7304 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00007305 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00007306 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007307}
7308
7309TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00007310 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
7311 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00007312}
7313
chaviwfd6d3512019-03-25 13:23:49 -07007314class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007315 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07007316 InputDispatcherTest::SetUp();
7317
Chris Yea209fde2020-07-22 13:54:51 -07007318 std::shared_ptr<FakeApplicationHandle> application =
7319 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007320 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007321 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07007322 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07007323
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007324 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007325 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007326 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07007327
7328 // Set focused application.
7329 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07007330 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07007331
7332 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007333 mDispatcher->onWindowInfosChanged(
7334 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007335 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007336 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07007337 }
7338
Prabir Pradhan3608aad2019-10-02 17:08:26 -07007339 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07007340 InputDispatcherTest::TearDown();
7341
7342 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007343 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07007344 }
7345
7346protected:
7347 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08007348 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07007349 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07007350};
7351
7352// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
7353// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
7354// the onPointerDownOutsideFocus callback.
7355TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007357 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07007358 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007359 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07007360 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07007361
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007362 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07007363 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
7364}
7365
7366// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
7367// DOWN on the window that doesn't have focus. Ensure no window received the
7368// onPointerDownOutsideFocus callback.
7369TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007370 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007371 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT,
7372 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007373 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07007374 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07007375
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007376 ASSERT_TRUE(mDispatcher->waitForIdle());
7377 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07007378}
7379
7380// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
7381// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
7382TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08007383 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007384 injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007385 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07007386 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07007387
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007388 ASSERT_TRUE(mDispatcher->waitForIdle());
7389 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07007390}
7391
7392// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
7393// DOWN on the window that already has focus. Ensure no window received the
7394// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10007395TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007397 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07007398 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007399 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07007400 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07007401
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08007402 ASSERT_TRUE(mDispatcher->waitForIdle());
7403 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07007404}
7405
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08007406// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
7407// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
7408TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
7409 const MotionEvent event =
7410 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
7411 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007412 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08007413 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
7414 .build();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08007416 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
7417 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
7418
7419 ASSERT_TRUE(mDispatcher->waitForIdle());
7420 mFakePolicy->assertOnPointerDownWasNotCalled();
7421 // Ensure that the unfocused window did not receive any FOCUS events.
7422 mUnfocusedWindow->assertNoEvents();
7423}
7424
chaviwaf87b3e2019-10-01 16:59:28 -07007425// These tests ensures we can send touch events to a single client when there are multiple input
7426// windows that point to the same client token.
7427class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
7428 virtual void SetUp() override {
7429 InputDispatcherTest::SetUp();
7430
Chris Yea209fde2020-07-22 13:54:51 -07007431 std::shared_ptr<FakeApplicationHandle> application =
7432 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007433 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
7434 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07007435 mWindow1->setFrame(Rect(0, 0, 100, 100));
7436
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007437 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
7438 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07007439 mWindow2->setFrame(Rect(100, 100, 200, 200));
7440
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007441 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07007442 }
7443
7444protected:
7445 sp<FakeWindowHandle> mWindow1;
7446 sp<FakeWindowHandle> mWindow2;
7447
7448 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05007449 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07007450 vec2 vals = windowInfo->transform.transform(point.x, point.y);
7451 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07007452 }
7453
7454 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
7455 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01007456 const std::string name = window->getName();
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007457 MotionEvent* motionEvent = window->consumeMotion();
chaviwaf87b3e2019-10-01 16:59:28 -07007458
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007459 ASSERT_NE(nullptr, motionEvent)
7460 << name.c_str() << ": consumer should have returned non-NULL event.";
chaviwaf87b3e2019-10-01 16:59:28 -07007461
Siarhei Vishniakouf4043212023-09-18 19:33:03 -07007462 ASSERT_THAT(*motionEvent, WithMotionAction(expectedAction));
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007463 ASSERT_EQ(points.size(), motionEvent->getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07007464
7465 for (size_t i = 0; i < points.size(); i++) {
7466 float expectedX = points[i].x;
7467 float expectedY = points[i].y;
7468
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007469 EXPECT_EQ(expectedX, motionEvent->getX(i))
chaviwaf87b3e2019-10-01 16:59:28 -07007470 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007471 << ", got " << motionEvent->getX(i);
7472 EXPECT_EQ(expectedY, motionEvent->getY(i))
chaviwaf87b3e2019-10-01 16:59:28 -07007473 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007474 << ", got " << motionEvent->getY(i);
chaviwaf87b3e2019-10-01 16:59:28 -07007475 }
7476 }
chaviw9eaa22c2020-07-01 16:21:27 -07007477
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08007478 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07007479 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007480 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
7481 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07007482
7483 // Always consume from window1 since it's the window that has the InputReceiver
7484 consumeMotionEvent(mWindow1, action, expectedPoints);
7485 }
chaviwaf87b3e2019-10-01 16:59:28 -07007486};
7487
7488TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
7489 // Touch Window 1
7490 PointF touchedPoint = {10, 10};
7491 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07007492 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07007493
7494 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07007495 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07007496
7497 // Touch Window 2
7498 touchedPoint = {150, 150};
7499 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07007500 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07007501}
7502
chaviw9eaa22c2020-07-01 16:21:27 -07007503TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
7504 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07007505 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007506 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviwaf87b3e2019-10-01 16:59:28 -07007507
7508 // Touch Window 1
7509 PointF touchedPoint = {10, 10};
7510 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07007511 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07007512 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07007513 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07007514
7515 // Touch Window 2
7516 touchedPoint = {150, 150};
7517 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07007518 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
7519 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07007520
chaviw9eaa22c2020-07-01 16:21:27 -07007521 // Update the transform so rotation is set
7522 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007523 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07007524 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
7525 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07007526}
7527
chaviw9eaa22c2020-07-01 16:21:27 -07007528TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007529 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007530 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007531
7532 // Touch Window 1
7533 std::vector<PointF> touchedPoints = {PointF{10, 10}};
7534 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07007535 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007536
7537 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07007538 touchedPoints.push_back(PointF{150, 150});
7539 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007540 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007541
chaviw9eaa22c2020-07-01 16:21:27 -07007542 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007543 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07007544 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007545
chaviw9eaa22c2020-07-01 16:21:27 -07007546 // Update the transform so rotation is set for Window 2
7547 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007548 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07007549 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007550 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007551}
7552
chaviw9eaa22c2020-07-01 16:21:27 -07007553TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007554 mWindow2->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007555 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007556
7557 // Touch Window 1
7558 std::vector<PointF> touchedPoints = {PointF{10, 10}};
7559 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07007560 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007561
7562 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07007563 touchedPoints.push_back(PointF{150, 150});
7564 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007565
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007566 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007567
7568 // Move both windows
7569 touchedPoints = {{20, 20}, {175, 175}};
7570 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
7571 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
7572
chaviw9eaa22c2020-07-01 16:21:27 -07007573 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007574
chaviw9eaa22c2020-07-01 16:21:27 -07007575 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007576 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07007577 expectedPoints.pop_back();
7578
7579 // Touch Window 2
7580 mWindow2->setWindowTransform(0, -1, 1, 0);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007581 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
chaviw9eaa22c2020-07-01 16:21:27 -07007582 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007583 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07007584
7585 // Move both windows
7586 touchedPoints = {{20, 20}, {175, 175}};
7587 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
7588 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
7589
7590 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007591}
7592
7593TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
7594 mWindow1->setWindowScale(0.5f, 0.5f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007595 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007596
7597 // Touch Window 1
7598 std::vector<PointF> touchedPoints = {PointF{10, 10}};
7599 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07007600 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007601
7602 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07007603 touchedPoints.push_back(PointF{150, 150});
7604 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007605
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08007606 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007607
7608 // Move both windows
7609 touchedPoints = {{20, 20}, {175, 175}};
7610 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
7611 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
7612
chaviw9eaa22c2020-07-01 16:21:27 -07007613 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00007614}
7615
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07007616/**
7617 * When one of the windows is slippery, the touch should not slip into the other window with the
7618 * same input channel.
7619 */
7620TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
7621 mWindow1->setSlippery(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007622 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07007623
7624 // Touch down in window 1
7625 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
7626 ADISPLAY_ID_DEFAULT, {{50, 50}}));
7627 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
7628
7629 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
7630 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
7631 // getting generated.
7632 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
7633 ADISPLAY_ID_DEFAULT, {{150, 150}}));
7634
7635 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
7636}
7637
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07007638/**
7639 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
7640 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
7641 * that the pointer is hovering over may have a different transform.
7642 */
7643TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007644 mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07007645
7646 // Start hover in window 1
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07007647 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN)
7648 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
7649 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07007650 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
7651 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07007652 // Move hover to window 2.
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07007653 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
7654 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150))
7655 .build());
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07007656 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
7657 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
7658 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
7659}
7660
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007661class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
7662 virtual void SetUp() override {
7663 InputDispatcherTest::SetUp();
7664
Chris Yea209fde2020-07-22 13:54:51 -07007665 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007666 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007667 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
7668 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007669 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007670 mWindow->setDispatchingTimeout(100ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07007671 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007672
7673 // Set focused application.
7674 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
7675
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007676 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07007677 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007678 mWindow->consumeFocusEvent(true);
7679 }
7680
7681 virtual void TearDown() override {
7682 InputDispatcherTest::TearDown();
7683 mWindow.clear();
7684 }
7685
7686protected:
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007687 static constexpr std::chrono::duration SPY_TIMEOUT = 200ms;
Chris Yea209fde2020-07-22 13:54:51 -07007688 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007689 sp<FakeWindowHandle> mWindow;
7690 static constexpr PointF WINDOW_LOCATION = {20, 20};
7691
7692 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007694 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007695 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007696 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007697 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007698 WINDOW_LOCATION));
7699 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007700
7701 sp<FakeWindowHandle> addSpyWindow() {
7702 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007703 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007704 spy->setTrustedOverlay(true);
7705 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007706 spy->setSpy(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007707 spy->setDispatchingTimeout(SPY_TIMEOUT);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007708 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007709 return spy;
7710 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007711};
7712
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007713// Send a tap and respond, which should not cause an ANR.
7714TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
7715 tapOnWindow();
7716 mWindow->consumeMotionDown();
7717 mWindow->consumeMotionUp();
7718 ASSERT_TRUE(mDispatcher->waitForIdle());
7719 mFakePolicy->assertNotifyAnrWasNotCalled();
7720}
7721
7722// Send a regular key and respond, which should not cause an ANR.
7723TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007725 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7726 ASSERT_TRUE(mDispatcher->waitForIdle());
7727 mFakePolicy->assertNotifyAnrWasNotCalled();
7728}
7729
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05007730TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
7731 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007732 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05007733 mWindow->consumeFocusEvent(false);
7734
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007735 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007736 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7737 InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED,
Harry Cutts33476232023-01-30 19:57:29 +00007738 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007739 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05007740 // Key will not go to window because we have no focused window.
7741 // The 'no focused window' ANR timer should start instead.
7742
7743 // Now, the focused application goes away.
7744 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
7745 // The key should get dropped and there should be no ANR.
7746
7747 ASSERT_TRUE(mDispatcher->waitForIdle());
7748 mFakePolicy->assertNotifyAnrWasNotCalled();
7749}
7750
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007751// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007752// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
7753// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007754TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007755 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007756 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007757 WINDOW_LOCATION));
7758
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007759 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
7760 ASSERT_TRUE(sequenceNum);
7761 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007762 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007763
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007764 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007765 mWindow->consumeMotionEvent(
7766 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007767 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007768 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007769}
7770
7771// Send a key to the app and have the app not respond right away.
7772TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
7773 // Inject a key, and don't respond - expect that ANR is called.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007774 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007775 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
7776 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007777 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007778 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07007779 ASSERT_TRUE(mDispatcher->waitForIdle());
7780}
7781
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007782// We have a focused application, but no focused window
7783TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07007784 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007785 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007786 mWindow->consumeFocusEvent(false);
7787
7788 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007789 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007790 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007791 WINDOW_LOCATION));
7792 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
7793 mDispatcher->waitForIdle();
7794 mFakePolicy->assertNotifyAnrWasNotCalled();
7795
7796 // Once a focused event arrives, we get an ANR for this application
7797 // We specify the injection timeout to be smaller than the application timeout, to ensure that
7798 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007799 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007800 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07007801 InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007802 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007803 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07007804 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007805 ASSERT_TRUE(mDispatcher->waitForIdle());
7806}
7807
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08007808/**
7809 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
7810 * there will not be an ANR.
7811 */
7812TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
7813 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007814 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08007815 mWindow->consumeFocusEvent(false);
7816
7817 KeyEvent event;
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07007818 static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
7819 mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08007820 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
7821 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
7822
7823 // Define a valid key down event that is stale (too old).
7824 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
Harry Cutts101ee9b2023-07-06 18:04:14 +00007825 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00007826 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08007827
7828 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
7829
7830 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007831 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08007832 InputEventInjectionSync::WAIT_FOR_RESULT,
7833 INJECT_EVENT_TIMEOUT, policyFlags);
7834 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
7835 << "Injection should fail because the event is stale";
7836
7837 ASSERT_TRUE(mDispatcher->waitForIdle());
7838 mFakePolicy->assertNotifyAnrWasNotCalled();
7839 mWindow->assertNoEvents();
7840}
7841
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007842// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007843// Make sure that we don't notify policy twice about the same ANR.
7844TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07007845 const std::chrono::duration appTimeout = 400ms;
7846 mApplication->setDispatchingTimeout(appTimeout);
7847 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
7848
Vishnu Nair47074b82020-08-14 11:54:47 -07007849 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007850 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007851 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007852
7853 // Once a focused event arrives, we get an ANR for this application
7854 // We specify the injection timeout to be smaller than the application timeout, to ensure that
7855 // injection times out (instead of failing).
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07007856 const std::chrono::duration eventInjectionTimeout = 100ms;
7857 ASSERT_LT(eventInjectionTimeout, appTimeout);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007858 const InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007859 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou06405fc2023-09-22 13:40:51 -07007860 InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout,
7861 /*allowKeyRepeat=*/false);
7862 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
7863 << "result=" << ftl::enum_string(result);
7864 // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
7865 // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
7866 std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
7867 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007868
Vishnu Naire4df8752022-09-08 09:17:55 -07007869 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007870 // ANR should not be raised again. It is up to policy to do that if it desires.
7871 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007872
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007873 // If we now get a focused window, the ANR should stop, but the policy handles that via
7874 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007875 ASSERT_TRUE(mDispatcher->waitForIdle());
7876}
7877
7878// We have a focused application, but no focused window
7879TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07007880 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07007881 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007882 mWindow->consumeFocusEvent(false);
7883
7884 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007885 ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007886
Vishnu Naire4df8752022-09-08 09:17:55 -07007887 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
7888 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007889
7890 // Future focused events get dropped right away
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007891 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007892 ASSERT_TRUE(mDispatcher->waitForIdle());
7893 mWindow->assertNoEvents();
7894}
7895
7896/**
7897 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
7898 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
7899 * If we process 1 of the events, but ANR on the second event with the same timestamp,
7900 * the ANR mechanism should still work.
7901 *
7902 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
7903 * DOWN event, while not responding on the second one.
7904 */
7905TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
7906 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007907 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007908 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
7909 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
7910 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007911 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007912
7913 // Now send ACTION_UP, with identical timestamp
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007914 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007915 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
7916 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
7917 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007918 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007919
7920 // We have now sent down and up. Let's consume first event and then ANR on the second.
7921 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7922 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007923 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007924}
7925
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007926// A spy window can receive an ANR
7927TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
7928 sp<FakeWindowHandle> spy = addSpyWindow();
7929
7930 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007931 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007932 WINDOW_LOCATION));
7933 mWindow->consumeMotionDown();
7934
7935 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
7936 ASSERT_TRUE(sequenceNum);
7937 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007938 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007939
7940 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007941 spy->consumeMotionEvent(
7942 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007943 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007944 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007945}
7946
7947// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007948// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007949TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
7950 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007951
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007953 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007954 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07007955 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007956
7957 // Stuck on the ACTION_UP
7958 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007959 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007960
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007961 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007962 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007963 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7964 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007965
7966 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
7967 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007968 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007969 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007970 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007971}
7972
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007973// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007974// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007975TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
7976 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007977
7978 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007979 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7980 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007981
7982 mWindow->consumeMotionDown();
7983 // Stuck on the ACTION_UP
7984 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007985 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007986
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007987 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007988 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007989 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
7990 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007991
7992 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
7993 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007994 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007995 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007996 spy->assertNoEvents();
7997}
7998
7999TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008000 mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008001
Prabir Pradhanfb549072023-10-05 19:17:36 +00008002 FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008003
8004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008005 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008006 WINDOW_LOCATION));
8007
8008 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8009 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
8010 ASSERT_TRUE(consumeSeq);
8011
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008012 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(),
8013 MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008014
8015 monitor.finishEvent(*consumeSeq);
8016 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
8017
8018 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008019 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008020}
8021
8022// If a window is unresponsive, then you get anr. if the window later catches up and starts to
8023// process events, you don't get an anr. When the window later becomes unresponsive again, you
8024// get an ANR again.
8025// 1. tap -> block on ACTION_UP -> receive ANR
8026// 2. consume all pending events (= queue becomes healthy again)
8027// 3. tap again -> block on ACTION_UP again -> receive ANR second time
8028TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
8029 tapOnWindow();
8030
8031 mWindow->consumeMotionDown();
8032 // Block on ACTION_UP
8033 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008034 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008035 mWindow->consumeMotionUp(); // Now the connection should be healthy again
8036 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008037 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008038 mWindow->assertNoEvents();
8039
8040 tapOnWindow();
8041 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008042 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008043 mWindow->consumeMotionUp();
8044
8045 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008046 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008047 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008048 mWindow->assertNoEvents();
8049}
8050
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008051// If a connection remains unresponsive for a while, make sure policy is only notified once about
8052// it.
8053TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008055 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008056 WINDOW_LOCATION));
8057
8058 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008059 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008060 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008061 // 'notifyConnectionUnresponsive' should only be called once per connection
8062 mFakePolicy->assertNotifyAnrWasNotCalled();
8063 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008064 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08008065 mWindow->consumeMotionEvent(
8066 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008067 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008068 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08008069 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008070 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008071}
8072
8073/**
8074 * 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 -07008075 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008076 *
8077 * Warning!!!
8078 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
8079 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008080 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008081 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
8082 *
8083 * If that value changes, this test should also change.
8084 */
8085TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
8086 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008087 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008088
8089 tapOnWindow();
8090 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
8091 ASSERT_TRUE(downSequenceNum);
8092 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
8093 ASSERT_TRUE(upSequenceNum);
8094 // Don't finish the events yet, and send a key
8095 // Injection will "succeed" because we will eventually give up and send the key to the focused
8096 // window even if motions are still being processed. But because the injection timeout is short,
8097 // we will receive INJECTION_TIMED_OUT as the result.
8098
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008099 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008100 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
8101 InputEventInjectionSync::WAIT_FOR_RESULT, 100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008102 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008103 // Key will not be sent to the window, yet, because the window is still processing events
8104 // and the key remains pending, waiting for the touch events to be processed
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008105 // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR.
8106 // Rely here on the fact that it uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood.
8107 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
8108 mWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008109
8110 std::this_thread::sleep_for(500ms);
8111 // if we wait long enough though, dispatcher will give up, and still send the key
8112 // to the focused window, even though we have not yet finished the motion event
8113 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8114 mWindow->finishEvent(*downSequenceNum);
8115 mWindow->finishEvent(*upSequenceNum);
8116}
8117
8118/**
8119 * If a window is processing a motion event, and then a key event comes in, the key event should
8120 * not go to the focused window until the motion is processed.
8121 * If then a new motion comes in, then the pending key event should be going to the currently
8122 * focused window right away.
8123 */
8124TEST_F(InputDispatcherSingleWindowAnr,
8125 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
8126 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008127 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008128
8129 tapOnWindow();
8130 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
8131 ASSERT_TRUE(downSequenceNum);
8132 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
8133 ASSERT_TRUE(upSequenceNum);
8134 // Don't finish the events yet, and send a key
8135 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008137 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
8138 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008139 // At this point, key is still pending, and should not be sent to the application yet.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008140 // Make sure the `assertNoEvents` check doesn't take too long. It uses
8141 // CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood.
8142 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
8143 mWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008144
8145 // Now tap down again. It should cause the pending key to go to the focused window right away.
8146 tapOnWindow();
8147 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
8148 // the other events yet. We can finish events in any order.
8149 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
8150 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
8151 mWindow->consumeMotionDown();
8152 mWindow->consumeMotionUp();
8153 mWindow->assertNoEvents();
8154}
8155
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -07008156/**
8157 * Send an event to the app and have the app not respond right away.
8158 * When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
8159 * So InputDispatcher will enqueue ACTION_CANCEL event as well.
8160 * At some point, the window becomes responsive again.
8161 * Ensure that subsequent events get dropped, and the next gesture is delivered.
8162 */
8163TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) {
8164 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8165 .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10))
8166 .build());
8167
8168 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
8169 ASSERT_TRUE(sequenceNum);
8170 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
8171 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
8172
8173 mWindow->finishEvent(*sequenceNum);
8174 mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
8175 ASSERT_TRUE(mDispatcher->waitForIdle());
8176 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
8177
8178 // Now that the window is responsive, let's continue the gesture.
8179 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8180 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
8181 .build());
8182
8183 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8184 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
8185 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
8186 .build());
8187
8188 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8189 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
8190 .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3))
8191 .build());
8192 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN)
8193 .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11))
8194 .build());
8195 // We already canceled this pointer, so the window shouldn't get any new events.
8196 mWindow->assertNoEvents();
8197
8198 // Start another one.
8199 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8200 .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15))
8201 .build());
8202 mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
8203}
8204
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008205class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
8206 virtual void SetUp() override {
8207 InputDispatcherTest::SetUp();
8208
Chris Yea209fde2020-07-22 13:54:51 -07008209 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008210 mApplication->setDispatchingTimeout(100ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008211 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
8212 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008213 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008214 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008215 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008216
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008217 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
8218 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008219 mFocusedWindow->setDispatchingTimeout(100ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008220 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008221
8222 // Set focused application.
8223 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07008224 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008225
8226 // Expect one focus window exist in display.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008227 mDispatcher->onWindowInfosChanged(
8228 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008229 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008230 mFocusedWindow->consumeFocusEvent(true);
8231 }
8232
8233 virtual void TearDown() override {
8234 InputDispatcherTest::TearDown();
8235
8236 mUnfocusedWindow.clear();
8237 mFocusedWindow.clear();
8238 }
8239
8240protected:
Chris Yea209fde2020-07-22 13:54:51 -07008241 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008242 sp<FakeWindowHandle> mUnfocusedWindow;
8243 sp<FakeWindowHandle> mFocusedWindow;
8244 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
8245 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
8246 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
8247
8248 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
8249
8250 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
8251
8252private:
8253 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008255 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008256 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008257 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008258 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008259 location));
8260 }
8261};
8262
8263// If we have 2 windows that are both unresponsive, the one with the shortest timeout
8264// should be ANR'd first.
8265TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008266 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07008267 injectMotionEvent(*mDispatcher,
8268 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8269 AINPUT_SOURCE_TOUCHSCREEN)
8270 .pointer(PointerBuilder(0, ToolType::FINGER)
8271 .x(FOCUSED_WINDOW_LOCATION.x)
8272 .y(FOCUSED_WINDOW_LOCATION.y))
8273 .build()));
8274 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8275 injectMotionEvent(*mDispatcher,
8276 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
8277 AINPUT_SOURCE_TOUCHSCREEN)
8278 .pointer(PointerBuilder(0, ToolType::FINGER)
8279 .x(FOCUSED_WINDOW_LOCATION.x)
8280 .y(FOCUSED_WINDOW_LOCATION.y))
8281 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008282 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07008283 mFocusedWindow->consumeMotionUp();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008284 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00008285 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008286 // We consumed all events, so no ANR
8287 ASSERT_TRUE(mDispatcher->waitForIdle());
8288 mFakePolicy->assertNotifyAnrWasNotCalled();
8289
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008290 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07008291 injectMotionEvent(*mDispatcher,
8292 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8293 AINPUT_SOURCE_TOUCHSCREEN)
8294 .pointer(PointerBuilder(0, ToolType::FINGER)
8295 .x(FOCUSED_WINDOW_LOCATION.x)
8296 .y(FOCUSED_WINDOW_LOCATION.y))
8297 .build()));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008298 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
8299 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008300
8301 const std::chrono::duration timeout =
8302 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008303 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07008304
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008305 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008306 mFocusedWindow->consumeMotionDown();
8307 // This cancel is generated because the connection was unresponsive
8308 mFocusedWindow->consumeMotionCancel();
8309 mFocusedWindow->assertNoEvents();
8310 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008311 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008312 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
8313 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008314 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008315}
8316
8317// If we have 2 windows with identical timeouts that are both unresponsive,
8318// it doesn't matter which order they should have ANR.
8319// But we should receive ANR for both.
8320TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
8321 // Set the timeout for unfocused window to match the focused window
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008322 mUnfocusedWindow->setDispatchingTimeout(
8323 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008324 mDispatcher->onWindowInfosChanged(
8325 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008326
8327 tapOnFocusedWindow();
8328 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008329 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008330 std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken(
8331 mFocusedWindow->getDispatchingTimeout(
8332 DISPATCHING_TIMEOUT)),
8333 mFakePolicy->getUnresponsiveWindowToken(0ms)};
8334
8335 ASSERT_THAT(anrConnectionTokens,
8336 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
8337 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008338
8339 ASSERT_TRUE(mDispatcher->waitForIdle());
8340 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008341
8342 mFocusedWindow->consumeMotionDown();
8343 mFocusedWindow->consumeMotionUp();
8344 mUnfocusedWindow->consumeMotionOutside();
8345
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008346 std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(),
8347 mFakePolicy->getResponsiveWindowToken()};
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008348
8349 // Both applications should be marked as responsive, in any order
Siarhei Vishniakouf83c6932023-07-07 17:48:10 -07008350 ASSERT_THAT(responsiveTokens,
8351 ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()),
8352 testing::Eq(mUnfocusedWindow->getToken())));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008353 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008354}
8355
8356// If a window is already not responding, the second tap on the same window should be ignored.
8357// We should also log an error to account for the dropped event (not tested here).
8358// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
8359TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
8360 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008361 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00008362 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008363 // Receive the events, but don't respond
8364 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
8365 ASSERT_TRUE(downEventSequenceNum);
8366 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
8367 ASSERT_TRUE(upEventSequenceNum);
8368 const std::chrono::duration timeout =
8369 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008370 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008371
8372 // Tap once again
8373 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008374 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008375 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008376 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008377 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008378 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008379 FOCUSED_WINDOW_LOCATION));
8380 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
8381 // valid touch target
8382 mUnfocusedWindow->assertNoEvents();
8383
8384 // Consume the first tap
8385 mFocusedWindow->finishEvent(*downEventSequenceNum);
8386 mFocusedWindow->finishEvent(*upEventSequenceNum);
8387 ASSERT_TRUE(mDispatcher->waitForIdle());
8388 // The second tap did not go to the focused window
8389 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008390 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08008391 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
8392 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008393 mFakePolicy->assertNotifyAnrWasNotCalled();
8394}
8395
8396// If you tap outside of all windows, there will not be ANR
8397TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008398 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008399 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008400 LOCATION_OUTSIDE_ALL_WINDOWS));
8401 ASSERT_TRUE(mDispatcher->waitForIdle());
8402 mFakePolicy->assertNotifyAnrWasNotCalled();
8403}
8404
8405// Since the focused window is paused, tapping on it should not produce any events
8406TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
8407 mFocusedWindow->setPaused(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008408 mDispatcher->onWindowInfosChanged(
8409 {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008410
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008411 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008412 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008413 FOCUSED_WINDOW_LOCATION));
8414
8415 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
8416 ASSERT_TRUE(mDispatcher->waitForIdle());
8417 // Should not ANR because the window is paused, and touches shouldn't go to it
8418 mFakePolicy->assertNotifyAnrWasNotCalled();
8419
8420 mFocusedWindow->assertNoEvents();
8421 mUnfocusedWindow->assertNoEvents();
8422}
8423
8424/**
8425 * If a window is processing a motion event, and then a key event comes in, the key event should
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008426 * not get delivered to the focused window until the motion is processed.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008427 * If a different window becomes focused at this time, the key should go to that window instead.
8428 *
8429 * Warning!!!
8430 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
8431 * and the injection timeout that we specify when injecting the key.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008432 * We must have the injection timeout (100ms) be smaller than
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008433 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
8434 *
8435 * If that value changes, this test should also change.
8436 */
8437TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
8438 // Set a long ANR timeout to prevent it from triggering
8439 mFocusedWindow->setDispatchingTimeout(2s);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008440 mDispatcher->onWindowInfosChanged(
8441 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008442
8443 tapOnUnfocusedWindow();
8444 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
8445 ASSERT_TRUE(downSequenceNum);
8446 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
8447 ASSERT_TRUE(upSequenceNum);
8448 // Don't finish the events yet, and send a key
8449 // Injection will succeed because we will eventually give up and send the key to the focused
8450 // window even if motions are still being processed.
8451
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008452 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008453 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
8454 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008455 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008456 // Key will not be sent to the window, yet, because the window is still processing events
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008457 // and the key remains pending, waiting for the touch events to be processed.
8458 // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED
8459 // under the hood.
8460 static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms);
8461 mFocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008462
8463 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07008464 mFocusedWindow->setFocusable(false);
8465 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008466 mDispatcher->onWindowInfosChanged(
8467 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008468 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008469
8470 // Focus events should precede the key events
8471 mUnfocusedWindow->consumeFocusEvent(true);
8472 mFocusedWindow->consumeFocusEvent(false);
8473
8474 // Finish the tap events, which should unblock dispatcher
8475 mUnfocusedWindow->finishEvent(*downSequenceNum);
8476 mUnfocusedWindow->finishEvent(*upSequenceNum);
8477
8478 // Now that all queues are cleared and no backlog in the connections, the key event
8479 // can finally go to the newly focused "mUnfocusedWindow".
8480 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8481 mFocusedWindow->assertNoEvents();
8482 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008483 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008484}
8485
8486// When the touch stream is split across 2 windows, and one of them does not respond,
8487// then ANR should be raised and the touch should be canceled for the unresponsive window.
8488// The other window should not be affected by that.
8489TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
8490 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00008491 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8492 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8493 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008494 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00008495 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008496
8497 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00008498 mDispatcher->notifyMotion(
8499 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8500 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008501
8502 const std::chrono::duration timeout =
8503 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08008504 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008505
8506 mUnfocusedWindow->consumeMotionDown();
8507 mFocusedWindow->consumeMotionDown();
8508 // Focused window may or may not receive ACTION_MOVE
8509 // But it should definitely receive ACTION_CANCEL due to the ANR
8510 InputEvent* event;
8511 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
8512 ASSERT_TRUE(moveOrCancelSequenceNum);
8513 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
8514 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008515 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008516 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
8517 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
8518 mFocusedWindow->consumeMotionCancel();
8519 } else {
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
8521 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008522 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08008523 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
8524 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008525
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008526 mUnfocusedWindow->assertNoEvents();
8527 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05008528 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07008529}
8530
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05008531/**
8532 * If we have no focused window, and a key comes in, we start the ANR timer.
8533 * The focused application should add a focused window before the timer runs out to prevent ANR.
8534 *
8535 * If the user touches another application during this time, the key should be dropped.
8536 * Next, if a new focused window comes in, without toggling the focused application,
8537 * then no ANR should occur.
8538 *
8539 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
8540 * but in some cases the policy may not update the focused application.
8541 */
8542TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
8543 std::shared_ptr<FakeApplicationHandle> focusedApplication =
8544 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07008545 focusedApplication->setDispatchingTimeout(300ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05008546 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
8547 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
8548 mFocusedWindow->setFocusable(false);
8549
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008550 mDispatcher->onWindowInfosChanged(
8551 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05008552 mFocusedWindow->consumeFocusEvent(false);
8553
8554 // Send a key. The ANR timer should start because there is no focused window.
8555 // 'focusedApplication' will get blamed if this timer completes.
8556 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008557 InputEventInjectionResult result =
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008558 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
8559 InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms,
Harry Cutts33476232023-01-30 19:57:29 +00008560 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05008562
8563 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
8564 // then the injected touches won't cause the focused event to get dropped.
8565 // The dispatcher only checks for whether the queue should be pruned upon queueing.
8566 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
8567 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
8568 // For this test, it means that the key would get delivered to the window once it becomes
8569 // focused.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008570 std::this_thread::sleep_for(100ms);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05008571
8572 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00008573 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8574 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8575 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05008576
8577 // We do not consume the motion right away, because that would require dispatcher to first
8578 // process (== drop) the key event, and by that time, ANR will be raised.
8579 // Set the focused window first.
8580 mFocusedWindow->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008581 mDispatcher->onWindowInfosChanged(
8582 {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05008583 setFocusedWindow(mFocusedWindow);
8584 mFocusedWindow->consumeFocusEvent(true);
8585 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
8586 // to another application. This could be a bug / behaviour in the policy.
8587
8588 mUnfocusedWindow->consumeMotionDown();
8589
8590 ASSERT_TRUE(mDispatcher->waitForIdle());
8591 // Should not ANR because we actually have a focused window. It was just added too slowly.
8592 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
8593}
8594
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008595// These tests ensure we cannot send touch events to a window that's positioned behind a window
8596// that has feature NO_INPUT_CHANNEL.
8597// Layout:
8598// Top (closest to user)
8599// mNoInputWindow (above all windows)
8600// mBottomWindow
8601// Bottom (furthest from user)
8602class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
8603 virtual void SetUp() override {
8604 InputDispatcherTest::SetUp();
8605
8606 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008607 mNoInputWindow =
8608 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
8609 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00008610 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008611 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008612 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
8613 // It's perfectly valid for this window to not have an associated input channel
8614
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008615 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
8616 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008617 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
8618
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008619 mDispatcher->onWindowInfosChanged(
8620 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008621 }
8622
8623protected:
8624 std::shared_ptr<FakeApplicationHandle> mApplication;
8625 sp<FakeWindowHandle> mNoInputWindow;
8626 sp<FakeWindowHandle> mBottomWindow;
8627};
8628
8629TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
8630 PointF touchedPoint = {10, 10};
8631
Prabir Pradhan678438e2023-04-13 19:32:51 +00008632 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8633 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8634 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008635
8636 mNoInputWindow->assertNoEvents();
8637 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
8638 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
8639 // and therefore should prevent mBottomWindow from receiving touches
8640 mBottomWindow->assertNoEvents();
8641}
8642
8643/**
8644 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
8645 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
8646 */
8647TEST_F(InputDispatcherMultiWindowOcclusionTests,
8648 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008649 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
8650 "Window with input channel and NO_INPUT_CHANNEL",
8651 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008652
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008653 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008654 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008655 mDispatcher->onWindowInfosChanged(
8656 {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0});
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008657
8658 PointF touchedPoint = {10, 10};
8659
Prabir Pradhan678438e2023-04-13 19:32:51 +00008660 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8661 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8662 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05008663
8664 mNoInputWindow->assertNoEvents();
8665 mBottomWindow->assertNoEvents();
8666}
8667
Vishnu Nair958da932020-08-21 17:12:37 -07008668class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
8669protected:
8670 std::shared_ptr<FakeApplicationHandle> mApp;
8671 sp<FakeWindowHandle> mWindow;
8672 sp<FakeWindowHandle> mMirror;
8673
8674 virtual void SetUp() override {
8675 InputDispatcherTest::SetUp();
8676 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008677 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
8678 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
8679 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07008680 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
8681 mWindow->setFocusable(true);
8682 mMirror->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008683 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008684 }
8685};
8686
8687TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
8688 // Request focus on a mirrored window
8689 setFocusedWindow(mMirror);
8690
8691 // window gets focused
8692 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008693 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008694 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008695 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
8696}
8697
8698// A focused & mirrored window remains focused only if the window and its mirror are both
8699// focusable.
8700TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
8701 setFocusedWindow(mMirror);
8702
8703 // window gets focused
8704 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008705 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008706 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008707 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008708 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008709 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008710 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
8711
8712 mMirror->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008713 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008714
8715 // window loses focus since one of the windows associated with the token in not focusable
8716 mWindow->consumeFocusEvent(false);
8717
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008718 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008719 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07008720 mWindow->assertNoEvents();
8721}
8722
8723// A focused & mirrored window remains focused until the window and its mirror both become
8724// invisible.
8725TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
8726 setFocusedWindow(mMirror);
8727
8728 // window gets focused
8729 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008731 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008732 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008734 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008735 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
8736
8737 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008738 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008739
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008740 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008741 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008742 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008743 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008744 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008745 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
8746
8747 mWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008748 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008749
8750 // window loses focus only after all windows associated with the token become invisible.
8751 mWindow->consumeFocusEvent(false);
8752
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008753 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008754 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07008755 mWindow->assertNoEvents();
8756}
8757
8758// A focused & mirrored window remains focused until both windows are removed.
8759TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
8760 setFocusedWindow(mMirror);
8761
8762 // window gets focused
8763 mWindow->consumeFocusEvent(true);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008764 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008765 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008766 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008767 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008768 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008769 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
8770
8771 // single window is removed but the window token remains focused
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008772 mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008773
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008774 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008775 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008776 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008777 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008778 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07008779 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
8780
8781 // Both windows are removed
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008782 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008783 mWindow->consumeFocusEvent(false);
8784
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008785 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008786 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07008787 mWindow->assertNoEvents();
8788}
8789
8790// Focus request can be pending until one window becomes visible.
8791TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
8792 // Request focus on an invisible mirror.
8793 mWindow->setVisible(false);
8794 mMirror->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008795 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008796 setFocusedWindow(mMirror);
8797
8798 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08008799 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07008800 injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0,
8801 ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07008802
8803 mMirror->setVisible(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008804 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0});
Vishnu Nair958da932020-08-21 17:12:37 -07008805
8806 // window gets focused
8807 mWindow->consumeFocusEvent(true);
8808 // window gets the pending key event
8809 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8810}
Prabir Pradhan99987712020-11-10 18:43:05 -08008811
8812class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
8813protected:
8814 std::shared_ptr<FakeApplicationHandle> mApp;
8815 sp<FakeWindowHandle> mWindow;
8816 sp<FakeWindowHandle> mSecondWindow;
8817
8818 void SetUp() override {
8819 InputDispatcherTest::SetUp();
8820 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008821 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08008822 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008823 mSecondWindow =
8824 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08008825 mSecondWindow->setFocusable(true);
8826
8827 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07008828 mDispatcher->onWindowInfosChanged(
8829 {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Prabir Pradhan99987712020-11-10 18:43:05 -08008830
8831 setFocusedWindow(mWindow);
8832 mWindow->consumeFocusEvent(true);
8833 }
8834
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008835 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00008836 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08008837 }
8838
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008839 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
8840 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08008841 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008842 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
8843 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08008844 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008845 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08008846 }
8847};
8848
8849TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
8850 // Ensure that capture cannot be obtained for unfocused windows.
8851 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
8852 mFakePolicy->assertSetPointerCaptureNotCalled();
8853 mSecondWindow->assertNoEvents();
8854
8855 // Ensure that capture can be enabled from the focus window.
8856 requestAndVerifyPointerCapture(mWindow, true);
8857
8858 // Ensure that capture cannot be disabled from a window that does not have capture.
8859 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
8860 mFakePolicy->assertSetPointerCaptureNotCalled();
8861
8862 // Ensure that capture can be disabled from the window with capture.
8863 requestAndVerifyPointerCapture(mWindow, false);
8864}
8865
8866TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008867 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08008868
8869 setFocusedWindow(mSecondWindow);
8870
8871 // Ensure that the capture disabled event was sent first.
8872 mWindow->consumeCaptureEvent(false);
8873 mWindow->consumeFocusEvent(false);
8874 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008875 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08008876
8877 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008878 notifyPointerCaptureChanged({});
8879 notifyPointerCaptureChanged(request);
8880 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08008881 mWindow->assertNoEvents();
8882 mSecondWindow->assertNoEvents();
8883 mFakePolicy->assertSetPointerCaptureNotCalled();
8884}
8885
8886TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008887 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08008888
8889 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008890 notifyPointerCaptureChanged({});
8891 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08008892
8893 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008894 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08008895 mWindow->consumeCaptureEvent(false);
8896 mWindow->assertNoEvents();
8897}
8898
Prabir Pradhan167e6d92021-02-04 16:18:17 -08008899TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
8900 requestAndVerifyPointerCapture(mWindow, true);
8901
8902 // The first window loses focus.
8903 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008904 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08008905 mWindow->consumeCaptureEvent(false);
8906
8907 // Request Pointer Capture from the second window before the notification from InputReader
8908 // arrives.
8909 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008910 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08008911
8912 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008913 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08008914
8915 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008916 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08008917
8918 mSecondWindow->consumeFocusEvent(true);
8919 mSecondWindow->consumeCaptureEvent(true);
8920}
8921
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00008922TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
8923 // App repeatedly enables and disables capture.
8924 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
8925 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
8926 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
8927 mFakePolicy->assertSetPointerCaptureCalled(false);
8928 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
8929 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
8930
8931 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
8932 // first request is now stale, this should do nothing.
8933 notifyPointerCaptureChanged(firstRequest);
8934 mWindow->assertNoEvents();
8935
8936 // InputReader notifies that the second request was enabled.
8937 notifyPointerCaptureChanged(secondRequest);
8938 mWindow->consumeCaptureEvent(true);
8939}
8940
Prabir Pradhan7092e262022-05-03 16:51:09 +00008941TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
8942 requestAndVerifyPointerCapture(mWindow, true);
8943
8944 // App toggles pointer capture off and on.
8945 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
8946 mFakePolicy->assertSetPointerCaptureCalled(false);
8947
8948 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
8949 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
8950
8951 // InputReader notifies that the latest "enable" request was processed, while skipping over the
8952 // preceding "disable" request.
8953 notifyPointerCaptureChanged(enableRequest);
8954
8955 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
8956 // any notifications.
8957 mWindow->assertNoEvents();
8958}
8959
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -07008960/**
8961 * One window. Hover mouse in the window, and then start capture. Make sure that the relative
8962 * mouse movements don't affect the previous mouse hovering state.
8963 * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no
8964 * HOVER_MOVE events).
8965 */
8966TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) {
8967 // Mouse hover on the window
8968 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
8969 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
8970 .build());
8971 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
8972 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110))
8973 .build());
8974
8975 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER)));
8976 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE)));
8977
8978 // Start pointer capture
8979 requestAndVerifyPointerCapture(mWindow, true);
8980
8981 // Send some relative mouse movements and receive them in the window.
8982 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE)
8983 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11))
8984 .build());
8985 mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11),
8986 WithSource(AINPUT_SOURCE_MOUSE_RELATIVE)));
8987
8988 // Stop pointer capture
8989 requestAndVerifyPointerCapture(mWindow, false);
8990
8991 // Continue hovering on the window
8992 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE)
8993 .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115))
8994 .build());
8995 mWindow->consumeMotionEvent(
8996 AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE)));
8997
8998 mWindow->assertNoEvents();
8999}
9000
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009001class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
9002protected:
9003 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00009004
9005 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
9006 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
9007
9008 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
9009 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
9010
9011 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
9012 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
9013 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
9014 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
9015 MAXIMUM_OBSCURING_OPACITY);
9016
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009017 static constexpr gui::Uid TOUCHED_APP_UID{10001};
9018 static constexpr gui::Uid APP_B_UID{10002};
9019 static constexpr gui::Uid APP_C_UID{10003};
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009020
9021 sp<FakeWindowHandle> mTouchWindow;
9022
9023 virtual void SetUp() override {
9024 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009025 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009026 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
9027 }
9028
9029 virtual void TearDown() override {
9030 InputDispatcherTest::TearDown();
9031 mTouchWindow.clear();
9032 }
9033
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009034 sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode,
chaviw3277faf2021-05-19 16:45:23 -05009035 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009036 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009037 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009038 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009039 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009040 return window;
9041 }
9042
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00009043 sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009044 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
9045 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009046 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009047 // Generate an arbitrary PID based on the UID
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009048 window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009049 return window;
9050 }
9051
9052 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009053 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9054 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9055 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009056 }
9057};
9058
9059TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009060 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009061 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009062 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009063
9064 touch();
9065
9066 mTouchWindow->assertNoEvents();
9067}
9068
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009069TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00009070 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
9071 const sp<FakeWindowHandle>& w =
9072 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009073 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00009074
9075 touch();
9076
9077 mTouchWindow->assertNoEvents();
9078}
9079
9080TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009081 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
9082 const sp<FakeWindowHandle>& w =
9083 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009084 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009085
9086 touch();
9087
9088 w->assertNoEvents();
9089}
9090
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009091TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009092 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009093 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009094
9095 touch();
9096
9097 mTouchWindow->consumeAnyMotionDown();
9098}
9099
9100TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009101 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009102 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009103 w->setFrame(Rect(0, 0, 50, 50));
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009104 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009105
9106 touch({PointF{100, 100}});
9107
9108 mTouchWindow->consumeAnyMotionDown();
9109}
9110
9111TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009112 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009113 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009114 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009115
9116 touch();
9117
9118 mTouchWindow->consumeAnyMotionDown();
9119}
9120
9121TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
9122 const sp<FakeWindowHandle>& w =
9123 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009124 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009125
9126 touch();
9127
9128 mTouchWindow->consumeAnyMotionDown();
9129}
9130
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009131TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
9132 const sp<FakeWindowHandle>& w =
9133 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009134 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009135
9136 touch();
9137
9138 w->assertNoEvents();
9139}
9140
9141/**
9142 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
9143 * inside) while letting them pass-through. Note that even though touch passes through the occluding
9144 * window, the occluding window will still receive ACTION_OUTSIDE event.
9145 */
9146TEST_F(InputDispatcherUntrustedTouchesTest,
9147 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
9148 const sp<FakeWindowHandle>& w =
9149 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009150 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009151 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009152
9153 touch();
9154
9155 w->consumeMotionOutside();
9156}
9157
9158TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
9159 const sp<FakeWindowHandle>& w =
9160 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009161 w->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009162 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009163
9164 touch();
9165
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009166 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00009167}
9168
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009169TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009170 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009171 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9172 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009173 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009174
9175 touch();
9176
9177 mTouchWindow->consumeAnyMotionDown();
9178}
9179
9180TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
9181 const sp<FakeWindowHandle>& w =
9182 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9183 MAXIMUM_OBSCURING_OPACITY);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009184 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009185
9186 touch();
9187
9188 mTouchWindow->consumeAnyMotionDown();
9189}
9190
9191TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009192 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009193 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9194 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009195 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009196
9197 touch();
9198
9199 mTouchWindow->assertNoEvents();
9200}
9201
9202TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
9203 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
9204 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009205 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
9206 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009207 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009208 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
9209 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009210 mDispatcher->onWindowInfosChanged(
9211 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009212
9213 touch();
9214
9215 mTouchWindow->assertNoEvents();
9216}
9217
9218TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
9219 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
9220 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009221 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
9222 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009223 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009224 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
9225 OPACITY_FAR_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009226 mDispatcher->onWindowInfosChanged(
9227 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009228
9229 touch();
9230
9231 mTouchWindow->consumeAnyMotionDown();
9232}
9233
9234TEST_F(InputDispatcherUntrustedTouchesTest,
9235 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
9236 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009237 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9238 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009239 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009240 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
9241 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009242 mDispatcher->onWindowInfosChanged(
9243 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009244
9245 touch();
9246
9247 mTouchWindow->consumeAnyMotionDown();
9248}
9249
9250TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
9251 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009252 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9253 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00009254 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009255 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
9256 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009257 mDispatcher->onWindowInfosChanged(
9258 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00009259
9260 touch();
9261
9262 mTouchWindow->assertNoEvents();
9263}
9264
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009265TEST_F(InputDispatcherUntrustedTouchesTest,
9266 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
9267 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009268 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
9269 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009270 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009271 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9272 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009273 mDispatcher->onWindowInfosChanged(
9274 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009275
9276 touch();
9277
9278 mTouchWindow->assertNoEvents();
9279}
9280
9281TEST_F(InputDispatcherUntrustedTouchesTest,
9282 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
9283 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009284 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
9285 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009286 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009287 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9288 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009289 mDispatcher->onWindowInfosChanged(
9290 {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009291
9292 touch();
9293
9294 mTouchWindow->consumeAnyMotionDown();
9295}
9296
9297TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
9298 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009299 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
9300 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009301 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009302
9303 touch();
9304
9305 mTouchWindow->consumeAnyMotionDown();
9306}
9307
9308TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
9309 const sp<FakeWindowHandle>& w =
9310 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009311 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino6d52e542021-02-15 18:38:10 +00009312
9313 touch();
9314
9315 mTouchWindow->consumeAnyMotionDown();
9316}
9317
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00009318TEST_F(InputDispatcherUntrustedTouchesTest,
9319 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
9320 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
9321 const sp<FakeWindowHandle>& w =
9322 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009323 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00009324
9325 touch();
9326
9327 mTouchWindow->assertNoEvents();
9328}
9329
9330TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
9331 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
9332 const sp<FakeWindowHandle>& w =
9333 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009334 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00009335
9336 touch();
9337
9338 mTouchWindow->consumeAnyMotionDown();
9339}
9340
9341TEST_F(InputDispatcherUntrustedTouchesTest,
9342 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
9343 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
9344 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00009345 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9346 OPACITY_ABOVE_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009347 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00009348
9349 touch();
9350
9351 mTouchWindow->consumeAnyMotionDown();
9352}
9353
9354TEST_F(InputDispatcherUntrustedTouchesTest,
9355 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
9356 const sp<FakeWindowHandle>& w1 =
9357 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
9358 OPACITY_BELOW_THRESHOLD);
9359 const sp<FakeWindowHandle>& w2 =
9360 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
9361 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009362 mDispatcher->onWindowInfosChanged(
9363 {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00009364
9365 touch();
9366
9367 mTouchWindow->assertNoEvents();
9368}
9369
9370/**
9371 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
9372 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
9373 * (which alone would result in allowing touches) does not affect the blocking behavior.
9374 */
9375TEST_F(InputDispatcherUntrustedTouchesTest,
9376 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
9377 const sp<FakeWindowHandle>& wB =
9378 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
9379 OPACITY_BELOW_THRESHOLD);
9380 const sp<FakeWindowHandle>& wC =
9381 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
9382 OPACITY_BELOW_THRESHOLD);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009383 mDispatcher->onWindowInfosChanged(
9384 {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufino7393d172021-02-26 13:56:11 +00009385
9386 touch();
9387
9388 mTouchWindow->assertNoEvents();
9389}
9390
9391/**
9392 * This test is testing that a window from a different UID but with same application token doesn't
9393 * block the touch. Apps can share the application token for close UI collaboration for example.
9394 */
9395TEST_F(InputDispatcherUntrustedTouchesTest,
9396 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
9397 const sp<FakeWindowHandle>& w =
9398 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
9399 w->setApplicationToken(mTouchWindow->getApplicationToken());
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009400 mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0});
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00009401
9402 touch();
9403
9404 mTouchWindow->consumeAnyMotionDown();
9405}
9406
arthurhungb89ccb02020-12-30 16:19:01 +08009407class InputDispatcherDragTests : public InputDispatcherTest {
9408protected:
9409 std::shared_ptr<FakeApplicationHandle> mApp;
9410 sp<FakeWindowHandle> mWindow;
9411 sp<FakeWindowHandle> mSecondWindow;
9412 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00009413 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009414 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
9415 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08009416
9417 void SetUp() override {
9418 InputDispatcherTest::SetUp();
9419 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009420 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08009421 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08009422
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009423 mSecondWindow =
9424 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08009425 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08009426
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009427 mSpyWindow =
9428 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00009429 mSpyWindow->setSpy(true);
9430 mSpyWindow->setTrustedOverlay(true);
9431 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
9432
arthurhungb89ccb02020-12-30 16:19:01 +08009433 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009434 mDispatcher->onWindowInfosChanged(
9435 {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()},
9436 {},
9437 0,
9438 0});
arthurhungb89ccb02020-12-30 16:19:01 +08009439 }
9440
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009441 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
9442 switch (fromSource) {
9443 case AINPUT_SOURCE_TOUCHSCREEN:
9444 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009445 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009446 ADISPLAY_ID_DEFAULT, {50, 50}))
9447 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9448 break;
9449 case AINPUT_SOURCE_STYLUS:
9450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009451 injectMotionEvent(*mDispatcher,
9452 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9453 AINPUT_SOURCE_STYLUS)
9454 .buttonState(
9455 AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
9456 .pointer(PointerBuilder(0, ToolType::STYLUS)
9457 .x(50)
9458 .y(50))
9459 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009460 break;
9461 case AINPUT_SOURCE_MOUSE:
9462 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009463 injectMotionEvent(*mDispatcher,
9464 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9465 AINPUT_SOURCE_MOUSE)
9466 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
9467 .pointer(PointerBuilder(MOUSE_POINTER_ID,
9468 ToolType::MOUSE)
9469 .x(50)
9470 .y(50))
9471 .build()));
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009472 break;
9473 default:
9474 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
9475 }
arthurhungb89ccb02020-12-30 16:19:01 +08009476
9477 // Window should receive motion event.
9478 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00009479 // Spy window should also receive motion event
9480 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00009481 }
9482
9483 // Start performing drag, we will create a drag window and transfer touch to it.
9484 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
9485 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009486 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00009487 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009488 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00009489 }
arthurhungb89ccb02020-12-30 16:19:01 +08009490
9491 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009492 mDragWindow =
9493 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00009494 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009495 mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(),
9496 *mWindow->getInfo(), *mSecondWindow->getInfo()},
9497 {},
9498 0,
9499 0});
arthurhungb89ccb02020-12-30 16:19:01 +08009500
9501 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00009502 bool transferred =
9503 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00009504 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00009505 if (transferred) {
9506 mWindow->consumeMotionCancel();
9507 mDragWindow->consumeMotionDown();
9508 }
9509 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08009510 }
9511};
9512
9513TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009514 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08009515
9516 // Move on window.
9517 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009518 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +08009519 ADISPLAY_ID_DEFAULT, {50, 50}))
9520 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9521 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9522 mWindow->consumeDragEvent(false, 50, 50);
9523 mSecondWindow->assertNoEvents();
9524
9525 // Move to another window.
9526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009527 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +08009528 ADISPLAY_ID_DEFAULT, {150, 50}))
9529 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9530 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9531 mWindow->consumeDragEvent(true, 150, 50);
9532 mSecondWindow->consumeDragEvent(false, 50, 50);
9533
9534 // Move back to original window.
9535 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009536 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungb89ccb02020-12-30 16:19:01 +08009537 ADISPLAY_ID_DEFAULT, {50, 50}))
9538 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9539 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9540 mWindow->consumeDragEvent(false, 50, 50);
9541 mSecondWindow->consumeDragEvent(true, -50, 50);
9542
9543 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009544 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9545 {50, 50}))
arthurhungb89ccb02020-12-30 16:19:01 +08009546 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9547 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9548 mWindow->assertNoEvents();
9549 mSecondWindow->assertNoEvents();
9550}
9551
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00009552TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009553 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00009554
9555 // No cancel event after drag start
9556 mSpyWindow->assertNoEvents();
9557
9558 const MotionEvent secondFingerDownEvent =
9559 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9560 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009561 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9562 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00009563 .build();
9564 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009565 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00009566 InputEventInjectionSync::WAIT_FOR_RESULT))
9567 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9568
9569 // Receives cancel for first pointer after next pointer down
9570 mSpyWindow->consumeMotionCancel();
9571 mSpyWindow->consumeMotionDown();
9572
9573 mSpyWindow->assertNoEvents();
9574}
9575
arthurhungf452d0b2021-01-06 00:19:52 +08009576TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009577 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08009578
9579 // Move on window.
9580 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009581 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +08009582 ADISPLAY_ID_DEFAULT, {50, 50}))
9583 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9584 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9585 mWindow->consumeDragEvent(false, 50, 50);
9586 mSecondWindow->assertNoEvents();
9587
9588 // Move to another window.
9589 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009590 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
arthurhungf452d0b2021-01-06 00:19:52 +08009591 ADISPLAY_ID_DEFAULT, {150, 50}))
9592 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9593 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9594 mWindow->consumeDragEvent(true, 150, 50);
9595 mSecondWindow->consumeDragEvent(false, 50, 50);
9596
9597 // drop to another window.
9598 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009599 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungf452d0b2021-01-06 00:19:52 +08009600 {150, 50}))
9601 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9602 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07009603 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhungf452d0b2021-01-06 00:19:52 +08009604 mWindow->assertNoEvents();
9605 mSecondWindow->assertNoEvents();
9606}
9607
arthurhung6d4bed92021-03-17 11:59:33 +08009608TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009609 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08009610
9611 // Move on window and keep button pressed.
9612 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009613 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +08009614 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
9615 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009616 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08009617 .build()))
9618 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9619 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9620 mWindow->consumeDragEvent(false, 50, 50);
9621 mSecondWindow->assertNoEvents();
9622
9623 // Move to another window and release button, expect to drop item.
9624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009625 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +08009626 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
9627 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009628 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08009629 .build()))
9630 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9631 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9632 mWindow->assertNoEvents();
9633 mSecondWindow->assertNoEvents();
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07009634 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
arthurhung6d4bed92021-03-17 11:59:33 +08009635
9636 // nothing to the window.
9637 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009638 injectMotionEvent(*mDispatcher,
arthurhung6d4bed92021-03-17 11:59:33 +08009639 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
9640 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009641 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08009642 .build()))
9643 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9644 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9645 mWindow->assertNoEvents();
9646 mSecondWindow->assertNoEvents();
9647}
9648
Arthur Hung54745652022-04-20 07:17:41 +00009649TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009650 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08009651
9652 // Set second window invisible.
9653 mSecondWindow->setVisible(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009654 mDispatcher->onWindowInfosChanged(
9655 {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
Arthur Hung6d0571e2021-04-09 20:18:16 +08009656
9657 // Move on window.
9658 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009659 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +08009660 ADISPLAY_ID_DEFAULT, {50, 50}))
9661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9662 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9663 mWindow->consumeDragEvent(false, 50, 50);
9664 mSecondWindow->assertNoEvents();
9665
9666 // Move to another window.
9667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009668 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung6d0571e2021-04-09 20:18:16 +08009669 ADISPLAY_ID_DEFAULT, {150, 50}))
9670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9671 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9672 mWindow->consumeDragEvent(true, 150, 50);
9673 mSecondWindow->assertNoEvents();
9674
9675 // drop to another window.
9676 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009677 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung6d0571e2021-04-09 20:18:16 +08009678 {150, 50}))
9679 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9680 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07009681 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
Arthur Hung6d0571e2021-04-09 20:18:16 +08009682 mWindow->assertNoEvents();
9683 mSecondWindow->assertNoEvents();
9684}
9685
Arthur Hung54745652022-04-20 07:17:41 +00009686TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009687 // Ensure window could track pointerIds if it didn't support split touch.
9688 mWindow->setPreventSplitting(true);
9689
Arthur Hung54745652022-04-20 07:17:41 +00009690 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009691 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +00009692 {50, 50}))
9693 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9694 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9695
9696 const MotionEvent secondFingerDownEvent =
9697 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9698 .displayId(ADISPLAY_ID_DEFAULT)
9699 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009700 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9701 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00009702 .build();
9703 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009704 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +00009705 InputEventInjectionSync::WAIT_FOR_RESULT))
9706 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009707 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00009708
9709 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009710 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00009711}
9712
9713TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
9714 // First down on second window.
9715 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009716 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung54745652022-04-20 07:17:41 +00009717 {150, 50}))
9718 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9719
9720 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9721
9722 // Second down on first window.
9723 const MotionEvent secondFingerDownEvent =
9724 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9725 .displayId(ADISPLAY_ID_DEFAULT)
9726 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009727 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
9728 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00009729 .build();
9730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009731 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +00009732 InputEventInjectionSync::WAIT_FOR_RESULT))
9733 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9734 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9735
9736 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009737 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00009738
9739 // Move on window.
9740 const MotionEvent secondFingerMoveEvent =
9741 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
9742 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009743 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
9744 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00009745 .build();
9746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009747 injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +00009748 InputEventInjectionSync::WAIT_FOR_RESULT));
9749 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9750 mWindow->consumeDragEvent(false, 50, 50);
9751 mSecondWindow->consumeMotionMove();
9752
9753 // Release the drag pointer should perform drop.
9754 const MotionEvent secondFingerUpEvent =
9755 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
9756 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009757 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
9758 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00009759 .build();
9760 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009761 injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
Arthur Hung54745652022-04-20 07:17:41 +00009762 InputEventInjectionSync::WAIT_FOR_RESULT));
9763 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07009764 mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken());
Arthur Hung54745652022-04-20 07:17:41 +00009765 mWindow->assertNoEvents();
9766 mSecondWindow->consumeMotionMove();
9767}
9768
Arthur Hung3915c1f2022-05-31 07:17:17 +00009769TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009770 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00009771
9772 // Update window of second display.
9773 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009774 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009775 mDispatcher->onWindowInfosChanged(
9776 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
9777 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
9778 {},
9779 0,
9780 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +00009781
9782 // Let second display has a touch state.
9783 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009784 injectMotionEvent(*mDispatcher,
Arthur Hung3915c1f2022-05-31 07:17:17 +00009785 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
9786 AINPUT_SOURCE_TOUCHSCREEN)
9787 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009788 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00009789 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07009790 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00009791 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00009792 // Update window again.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009793 mDispatcher->onWindowInfosChanged(
9794 {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(),
9795 *mSecondWindow->getInfo(), *windowInSecondary->getInfo()},
9796 {},
9797 0,
9798 0});
Arthur Hung3915c1f2022-05-31 07:17:17 +00009799
9800 // Move on window.
9801 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009802 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +00009803 ADISPLAY_ID_DEFAULT, {50, 50}))
9804 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9805 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9806 mWindow->consumeDragEvent(false, 50, 50);
9807 mSecondWindow->assertNoEvents();
9808
9809 // Move to another window.
9810 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009811 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Arthur Hung3915c1f2022-05-31 07:17:17 +00009812 ADISPLAY_ID_DEFAULT, {150, 50}))
9813 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9814 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9815 mWindow->consumeDragEvent(true, 150, 50);
9816 mSecondWindow->consumeDragEvent(false, 50, 50);
9817
9818 // drop to another window.
9819 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009820 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hung3915c1f2022-05-31 07:17:17 +00009821 {150, 50}))
9822 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9823 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07009824 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hung3915c1f2022-05-31 07:17:17 +00009825 mWindow->assertNoEvents();
9826 mSecondWindow->assertNoEvents();
9827}
9828
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009829TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
9830 startDrag(true, AINPUT_SOURCE_MOUSE);
9831 // Move on window.
9832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009833 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009834 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
9835 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009836 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009837 .x(50)
9838 .y(50))
9839 .build()))
9840 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9841 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9842 mWindow->consumeDragEvent(false, 50, 50);
9843 mSecondWindow->assertNoEvents();
9844
9845 // Move to another window.
9846 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009847 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009848 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
9849 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009850 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009851 .x(150)
9852 .y(50))
9853 .build()))
9854 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9855 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
9856 mWindow->consumeDragEvent(true, 150, 50);
9857 mSecondWindow->consumeDragEvent(false, 50, 50);
9858
9859 // drop to another window.
9860 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009861 injectMotionEvent(*mDispatcher,
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009862 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
9863 .buttonState(0)
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -07009864 .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009865 .x(150)
9866 .y(50))
9867 .build()))
9868 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9869 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07009870 mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken());
Arthur Hungb75c2aa2022-07-15 09:35:36 +00009871 mWindow->assertNoEvents();
9872 mSecondWindow->assertNoEvents();
9873}
9874
Linnan Li5af92f92023-07-14 14:36:22 +08009875/**
9876 * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag
9877 * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash.
9878 */
9879TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) {
9880 // Down on second window
9881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9882 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9883 {150, 50}))
9884 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9885
9886 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown());
9887 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown());
9888
9889 // Down on first window
9890 const MotionEvent secondFingerDownEvent =
9891 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9892 .displayId(ADISPLAY_ID_DEFAULT)
9893 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
9894 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9895 .build();
9896 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9897 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9898 InputEventInjectionSync::WAIT_FOR_RESULT))
9899 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9900 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
9901 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove());
9902 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1));
9903
9904 // Start drag on first window
9905 ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN));
9906
9907 // Trigger cancel
9908 mDispatcher->cancelCurrentTouch();
9909 ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel());
9910 ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel());
9911 ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel());
9912
9913 ASSERT_TRUE(mDispatcher->waitForIdle());
9914 // The D&D finished with nullptr
9915 mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr);
9916
9917 // Remove drag window
9918 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0});
9919
9920 // Inject a simple gesture, ensure dispatcher not crashed
9921 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9922 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9923 PointF{50, 50}))
9924 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9925 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
9926
9927 const MotionEvent moveEvent =
9928 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
9929 .displayId(ADISPLAY_ID_DEFAULT)
9930 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9931 .build();
9932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9933 injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT,
9934 InputEventInjectionSync::WAIT_FOR_RESULT))
9935 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9936 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove());
9937
9938 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9939 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9940 {50, 50}))
9941 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9942 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp());
9943}
9944
Vishnu Nair062a8672021-09-03 16:07:44 -07009945class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
9946
9947TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
9948 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009949 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
9950 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009951 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07009952 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9953 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009954 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07009955 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009956 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07009957
9958 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00009959 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07009960 window->assertNoEvents();
9961
Prabir Pradhan678438e2023-04-13 19:32:51 +00009962 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9963 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07009964 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
9965 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07009966 window->assertNoEvents();
9967
9968 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009969 window->setDropInput(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009970 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07009971
Prabir Pradhan678438e2023-04-13 19:32:51 +00009972 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07009973 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
9974
Prabir Pradhan678438e2023-04-13 19:32:51 +00009975 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
9976 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07009977 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9978 window->assertNoEvents();
9979}
9980
9981TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
9982 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
9983 std::make_shared<FakeApplicationHandle>();
9984 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009985 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
9986 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07009987 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009988 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009989 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07009990 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009991 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
9992 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009993 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00009994 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -07009995 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9996 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -07009997 mDispatcher->onWindowInfosChanged(
9998 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -07009999 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000010000 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070010001
10002 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000010003 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010004 window->assertNoEvents();
10005
Prabir Pradhan678438e2023-04-13 19:32:51 +000010006 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10007 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070010008 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
10009 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010010 window->assertNoEvents();
10011
10012 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010013 window->setDropInputIfObscured(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010014 mDispatcher->onWindowInfosChanged(
10015 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070010016
Prabir Pradhan678438e2023-04-13 19:32:51 +000010017 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010018 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
10019
Prabir Pradhan678438e2023-04-13 19:32:51 +000010020 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10021 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010022 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
10023 window->assertNoEvents();
10024}
10025
10026TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
10027 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
10028 std::make_shared<FakeApplicationHandle>();
10029 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010030 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
10031 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -070010032 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010033 obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010034 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -070010035 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010036 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
10037 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010038 window->setDropInputIfObscured(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010039 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Vishnu Nair062a8672021-09-03 16:07:44 -070010040 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
10041 window->setFocusable(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010042 mDispatcher->onWindowInfosChanged(
10043 {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070010044 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000010045 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -070010046
10047 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +000010048 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010049 window->assertNoEvents();
10050
Prabir Pradhan678438e2023-04-13 19:32:51 +000010051 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10052 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070010053 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
10054 ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010055 window->assertNoEvents();
10056
10057 // When the window is no longer obscured because it went on top, it should get input
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010058 mDispatcher->onWindowInfosChanged(
10059 {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0});
Vishnu Nair062a8672021-09-03 16:07:44 -070010060
Prabir Pradhan678438e2023-04-13 19:32:51 +000010061 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010062 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
10063
Prabir Pradhan678438e2023-04-13 19:32:51 +000010064 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
10065 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -070010066 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10067 window->assertNoEvents();
10068}
10069
Antonio Kantekf16f2832021-09-28 04:39:20 +000010070class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
10071protected:
10072 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +000010073 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +000010074 sp<FakeWindowHandle> mWindow;
10075 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +000010076 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +000010077
10078 void SetUp() override {
10079 InputDispatcherTest::SetUp();
10080
10081 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +000010082 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010083 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000010084 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000010085 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010086 mSecondWindow =
10087 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +000010088 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +000010089 mThirdWindow =
10090 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
10091 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
10092 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +000010093
10094 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010095 mDispatcher->onWindowInfosChanged(
10096 {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()},
10097 {},
10098 0,
10099 0});
Antonio Kantek15beb512022-06-13 22:35:41 +000010100 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +000010101 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000010102
Antonio Kantek15beb512022-06-13 22:35:41 +000010103 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +000010104 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000010105 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -070010106 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
10107 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000010108 mThirdWindow->assertNoEvents();
10109 }
10110
10111 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
10112 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +000010113 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +000010114 SECOND_DISPLAY_ID)) {
10115 mWindow->assertNoEvents();
10116 mSecondWindow->assertNoEvents();
10117 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -070010118 }
Antonio Kantekf16f2832021-09-28 04:39:20 +000010119 }
10120
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010121 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Antonio Kantek15beb512022-06-13 22:35:41 +000010122 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -070010123 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
10124 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000010125 mWindow->consumeTouchModeEvent(inTouchMode);
10126 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +000010127 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +000010128 }
10129};
10130
Antonio Kantek26defcf2022-02-08 01:12:27 +000010131TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080010132 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +000010133 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
10134 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000010135 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +000010136}
10137
Antonio Kantek26defcf2022-02-08 01:12:27 +000010138TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
10139 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010140 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010141 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010142 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek26defcf2022-02-08 01:12:27 +000010143 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000010144 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -070010145 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +000010146 mWindow->assertNoEvents();
10147 mSecondWindow->assertNoEvents();
10148}
10149
10150TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
10151 const WindowInfo& windowInfo = *mWindow->getInfo();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010152 gui::Pid ownerPid = windowInfo.ownerPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010153 gui::Uid ownerUid = windowInfo.ownerUid;
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010154 mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID);
Antonio Kantek15beb512022-06-13 22:35:41 +000010155 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +000010156 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +000010157}
10158
Antonio Kantekf16f2832021-09-28 04:39:20 +000010159TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -080010160 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +000010161 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
10162 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000010163 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +000010164 mWindow->assertNoEvents();
10165 mSecondWindow->assertNoEvents();
10166}
10167
Antonio Kantek15beb512022-06-13 22:35:41 +000010168TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
10169 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
10170 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
10171 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000010172 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +000010173 mWindow->assertNoEvents();
10174 mSecondWindow->assertNoEvents();
10175 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
10176}
10177
Antonio Kantek48710e42022-03-24 14:19:30 -070010178TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
10179 // Interact with the window first.
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
10181 injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT))
Antonio Kantek48710e42022-03-24 14:19:30 -070010182 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
10183 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
10184
10185 // Then remove focus.
10186 mWindow->setFocusable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010187 mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
Antonio Kantek48710e42022-03-24 14:19:30 -070010188
10189 // Assert that caller can switch touch mode by owning one of the last interacted window.
10190 const WindowInfo& windowInfo = *mWindow->getInfo();
10191 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
10192 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +000010193 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -070010194}
10195
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010196class InputDispatcherSpyWindowTest : public InputDispatcherTest {
10197public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010198 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010199 std::shared_ptr<FakeApplicationHandle> application =
10200 std::make_shared<FakeApplicationHandle>();
10201 std::string name = "Fake Spy ";
10202 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010203 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
10204 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010205 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -080010206 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010207 return spy;
10208 }
10209
10210 sp<FakeWindowHandle> createForeground() {
10211 std::shared_ptr<FakeApplicationHandle> application =
10212 std::make_shared<FakeApplicationHandle>();
10213 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010214 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
10215 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010216 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010217 return window;
10218 }
10219
10220private:
10221 int mSpyCount{0};
10222};
10223
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080010224using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010225/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -080010226 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
10227 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080010228TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
Siarhei Vishniakou21f77bd2023-07-18 17:51:35 -070010229 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080010230 ScopedSilentDeath _silentDeath;
10231
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010232 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -080010233 spy->setTrustedOverlay(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010234 ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}),
Prabir Pradhan5c85e052021-12-22 02:27:12 -080010235 ".* not a trusted overlay");
10236}
10237
10238/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010239 * Input injection into a display with a spy window but no foreground windows should succeed.
10240 */
10241TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010242 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010243 mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010244
10245 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010246 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010247 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10248 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10249}
10250
10251/**
10252 * Verify the order in which different input windows receive events. The touched foreground window
10253 * (if there is one) should always receive the event first. When there are multiple spy windows, the
10254 * spy windows will receive the event according to their Z-order, where the top-most spy window will
10255 * receive events before ones belows it.
10256 *
10257 * Here, we set up a scenario with four windows in the following Z order from the top:
10258 * spy1, spy2, window, spy3.
10259 * We then inject an event and verify that the foreground "window" receives it first, followed by
10260 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
10261 * window.
10262 */
10263TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
10264 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010265 auto spy1 = createSpy();
10266 auto spy2 = createSpy();
10267 auto spy3 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010268 mDispatcher->onWindowInfosChanged(
10269 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010270 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
10271 const size_t numChannels = channels.size();
10272
Michael Wright8e9a8562022-02-09 13:44:29 +000010273 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010274 if (!epollFd.ok()) {
10275 FAIL() << "Failed to create epoll fd";
10276 }
10277
10278 for (size_t i = 0; i < numChannels; i++) {
10279 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
10280 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
10281 FAIL() << "Failed to add fd to epoll";
10282 }
10283 }
10284
10285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010286 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010287 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10288
10289 std::vector<size_t> eventOrder;
10290 std::vector<struct epoll_event> events(numChannels);
10291 for (;;) {
10292 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
10293 (100ms).count());
10294 if (nFds < 0) {
10295 FAIL() << "Failed to call epoll_wait";
10296 }
10297 if (nFds == 0) {
10298 break; // epoll_wait timed out
10299 }
10300 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -070010301 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -070010302 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010303 channels[i]->consumeMotionDown();
10304 }
10305 }
10306
10307 // Verify the order in which the events were received.
10308 EXPECT_EQ(3u, eventOrder.size());
10309 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
10310 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
10311 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
10312}
10313
10314/**
10315 * A spy window using the NOT_TOUCHABLE flag does not receive events.
10316 */
10317TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
10318 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010319 auto spy = createSpy();
10320 spy->setTouchable(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010321 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010322
10323 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010324 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010325 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10326 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10327 spy->assertNoEvents();
10328}
10329
10330/**
10331 * A spy window will only receive gestures that originate within its touchable region. Gestures that
10332 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
10333 * to the window.
10334 */
10335TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
10336 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010337 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010338 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010339 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010340
10341 // Inject an event outside the spy window's touchable region.
10342 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010343 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010344 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10345 window->consumeMotionDown();
10346 spy->assertNoEvents();
10347 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010348 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010349 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10350 window->consumeMotionUp();
10351 spy->assertNoEvents();
10352
10353 // Inject an event inside the spy window's touchable region.
10354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010355 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010356 {5, 10}))
10357 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10358 window->consumeMotionDown();
10359 spy->consumeMotionDown();
10360}
10361
10362/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010363 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080010364 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010365 */
10366TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
10367 auto window = createForeground();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010368 window->setOwnerInfo(gui::Pid{12}, gui::Uid{34});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010369 auto spy = createSpy();
10370 spy->setWatchOutsideTouch(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010371 spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010372 spy->setFrame(Rect{0, 0, 20, 20});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010373 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010374
10375 // Inject an event outside the spy window's frame and touchable region.
10376 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010377 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080010378 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010379 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10380 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -080010381 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010382}
10383
10384/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010385 * Even when a spy window spans over multiple foreground windows, the spy should receive all
10386 * pointers that are down within its bounds.
10387 */
10388TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
10389 auto windowLeft = createForeground();
10390 windowLeft->setFrame({0, 0, 100, 200});
10391 auto windowRight = createForeground();
10392 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010393 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010394 spy->setFrame({0, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010395 mDispatcher->onWindowInfosChanged(
10396 {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010397
10398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010399 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010400 {50, 50}))
10401 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10402 windowLeft->consumeMotionDown();
10403 spy->consumeMotionDown();
10404
10405 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080010406 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010407 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010408 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10409 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010410 .build();
10411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010412 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010413 InputEventInjectionSync::WAIT_FOR_RESULT))
10414 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10415 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +000010416 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010417}
10418
10419/**
10420 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
10421 * the spy should receive the second pointer with ACTION_DOWN.
10422 */
10423TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
10424 auto window = createForeground();
10425 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010426 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010427 spyRight->setFrame({100, 0, 200, 200});
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010428 mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010429
10430 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010431 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010432 {50, 50}))
10433 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10434 window->consumeMotionDown();
10435 spyRight->assertNoEvents();
10436
10437 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080010438 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010439 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010440 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
10441 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010442 .build();
10443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010444 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010445 InputEventInjectionSync::WAIT_FOR_RESULT))
10446 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +000010447 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -080010448 spyRight->consumeMotionDown();
10449}
10450
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010451/**
10452 * The spy window should not be able to affect whether or not touches are split. Only the foreground
10453 * windows should be allowed to control split touch.
10454 */
10455TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080010456 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010457 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010458 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -080010459 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010460
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010461 auto window = createForeground();
10462 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010463
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010464 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010465
10466 // First finger down, no window touched.
10467 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010468 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010469 {100, 200}))
10470 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10471 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10472 window->assertNoEvents();
10473
10474 // Second finger down on window, the window should receive touch down.
10475 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080010476 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010477 .displayId(ADISPLAY_ID_DEFAULT)
10478 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010479 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
10480 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010481 .build();
10482 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010483 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010484 InputEventInjectionSync::WAIT_FOR_RESULT))
10485 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10486
10487 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +000010488 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010489}
10490
10491/**
10492 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
10493 * do not receive key events.
10494 */
10495TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010496 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010497 spy->setFocusable(false);
10498
10499 auto window = createForeground();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010500 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010501 setFocusedWindow(window);
10502 window->consumeFocusEvent(true);
10503
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010504 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010505 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
10506 window->consumeKeyDown(ADISPLAY_ID_NONE);
10507
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010508 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -080010509 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
10510 window->consumeKeyUp(ADISPLAY_ID_NONE);
10511
10512 spy->assertNoEvents();
10513}
10514
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010515using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
10516
10517/**
10518 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
10519 * are currently sent to any other windows - including other spy windows - will also be cancelled.
10520 */
10521TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
10522 auto window = createForeground();
10523 auto spy1 = createSpy();
10524 auto spy2 = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010525 mDispatcher->onWindowInfosChanged(
10526 {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010527
10528 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010529 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010530 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10531 window->consumeMotionDown();
10532 spy1->consumeMotionDown();
10533 spy2->consumeMotionDown();
10534
10535 // Pilfer pointers from the second spy window.
10536 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
10537 spy2->assertNoEvents();
10538 spy1->consumeMotionCancel();
10539 window->consumeMotionCancel();
10540
10541 // The rest of the gesture should only be sent to the second spy window.
10542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010543 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010544 ADISPLAY_ID_DEFAULT))
10545 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10546 spy2->consumeMotionMove();
10547 spy1->assertNoEvents();
10548 window->assertNoEvents();
10549}
10550
10551/**
10552 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
10553 * in the middle of the gesture.
10554 */
10555TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
10556 auto window = createForeground();
10557 auto spy = createSpy();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010558 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010559
10560 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010561 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010562 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10563 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10564 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
10565
10566 window->releaseChannel();
10567
10568 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
10569
10570 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010571 injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010572 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10573 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
10574}
10575
10576/**
10577 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
10578 * the spy, but not to any other windows.
10579 */
10580TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
10581 auto spy = createSpy();
10582 auto window = createForeground();
10583
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010584 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010585
10586 // First finger down on the window and the spy.
10587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010588 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010589 {100, 200}))
10590 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10591 spy->consumeMotionDown();
10592 window->consumeMotionDown();
10593
10594 // Spy window pilfers the pointers.
10595 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
10596 window->consumeMotionCancel();
10597
10598 // Second finger down on the window and spy, but the window should not receive the pointer down.
10599 const MotionEvent secondFingerDownEvent =
10600 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10601 .displayId(ADISPLAY_ID_DEFAULT)
10602 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010603 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
10604 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010605 .build();
10606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010607 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010608 InputEventInjectionSync::WAIT_FOR_RESULT))
10609 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10610
Harry Cutts33476232023-01-30 19:57:29 +000010611 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010612
10613 // Third finger goes down outside all windows, so injection should fail.
10614 const MotionEvent thirdFingerDownEvent =
10615 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10616 .displayId(ADISPLAY_ID_DEFAULT)
10617 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010618 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
10619 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
10620 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010621 .build();
10622 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010623 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010624 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080010625 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010626
10627 spy->assertNoEvents();
10628 window->assertNoEvents();
10629}
10630
10631/**
10632 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
10633 */
10634TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
10635 auto spy = createSpy();
10636 spy->setFrame(Rect(0, 0, 100, 100));
10637 auto window = createForeground();
10638 window->setFrame(Rect(0, 0, 200, 200));
10639
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010640 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010641
10642 // First finger down on the window only
10643 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010644 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010645 {150, 150}))
10646 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10647 window->consumeMotionDown();
10648
10649 // Second finger down on the spy and window
10650 const MotionEvent secondFingerDownEvent =
10651 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10652 .displayId(ADISPLAY_ID_DEFAULT)
10653 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010654 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
10655 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010656 .build();
10657 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010658 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010659 InputEventInjectionSync::WAIT_FOR_RESULT))
10660 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10661 spy->consumeMotionDown();
10662 window->consumeMotionPointerDown(1);
10663
10664 // Third finger down on the spy and window
10665 const MotionEvent thirdFingerDownEvent =
10666 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10667 .displayId(ADISPLAY_ID_DEFAULT)
10668 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010669 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
10670 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
10671 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010672 .build();
10673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010674 injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010675 InputEventInjectionSync::WAIT_FOR_RESULT))
10676 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10677 spy->consumeMotionPointerDown(1);
10678 window->consumeMotionPointerDown(2);
10679
10680 // Spy window pilfers the pointers.
10681 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Harry Cutts101ee9b2023-07-06 18:04:14 +000010682 window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
10683 window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010684
10685 spy->assertNoEvents();
10686 window->assertNoEvents();
10687}
10688
10689/**
10690 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
10691 * other windows should be canceled. If this results in the cancellation of all pointers for some
10692 * window, then that window should receive ACTION_CANCEL.
10693 */
10694TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
10695 auto spy = createSpy();
10696 spy->setFrame(Rect(0, 0, 100, 100));
10697 auto window = createForeground();
10698 window->setFrame(Rect(0, 0, 200, 200));
10699
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010700 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010701
10702 // First finger down on both spy and window
10703 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010704 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010705 {10, 10}))
10706 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10707 window->consumeMotionDown();
10708 spy->consumeMotionDown();
10709
10710 // Second finger down on the spy and window
10711 const MotionEvent secondFingerDownEvent =
10712 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10713 .displayId(ADISPLAY_ID_DEFAULT)
10714 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010715 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
10716 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010717 .build();
10718 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010719 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010720 InputEventInjectionSync::WAIT_FOR_RESULT))
10721 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10722 spy->consumeMotionPointerDown(1);
10723 window->consumeMotionPointerDown(1);
10724
10725 // Spy window pilfers the pointers.
10726 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
10727 window->consumeMotionCancel();
10728
10729 spy->assertNoEvents();
10730 window->assertNoEvents();
10731}
10732
10733/**
10734 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
10735 * be sent to other windows
10736 */
10737TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
10738 auto spy = createSpy();
10739 spy->setFrame(Rect(0, 0, 100, 100));
10740 auto window = createForeground();
10741 window->setFrame(Rect(0, 0, 200, 200));
10742
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010743 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010744
10745 // First finger down on both window and spy
10746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010747 injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010748 {10, 10}))
10749 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10750 window->consumeMotionDown();
10751 spy->consumeMotionDown();
10752
10753 // Spy window pilfers the pointers.
10754 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
10755 window->consumeMotionCancel();
10756
10757 // Second finger down on the window only
10758 const MotionEvent secondFingerDownEvent =
10759 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10760 .displayId(ADISPLAY_ID_DEFAULT)
10761 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010762 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
10763 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010764 .build();
10765 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010766 injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000010767 InputEventInjectionSync::WAIT_FOR_RESULT))
10768 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
10769 window->consumeMotionDown();
10770 window->assertNoEvents();
10771
10772 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
10773 spy->consumeMotionMove();
10774 spy->assertNoEvents();
10775}
10776
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070010777/**
10778 * A window on the left and a window on the right. Also, a spy window that's above all of the
10779 * windows, and spanning both left and right windows.
10780 * Send simultaneous motion streams from two different devices, one to the left window, and another
10781 * to the right window.
10782 * Pilfer from spy window.
10783 * Check that the pilfering only affects the pointers that are actually being received by the spy.
10784 */
10785TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) {
10786 sp<FakeWindowHandle> spy = createSpy();
10787 spy->setFrame(Rect(0, 0, 200, 200));
10788 sp<FakeWindowHandle> leftWindow = createForeground();
10789 leftWindow->setFrame(Rect(0, 0, 100, 100));
10790
10791 sp<FakeWindowHandle> rightWindow = createForeground();
10792 rightWindow->setFrame(Rect(100, 0, 200, 100));
10793
10794 constexpr int32_t stylusDeviceId = 1;
10795 constexpr int32_t touchDeviceId = 2;
10796
10797 mDispatcher->onWindowInfosChanged(
10798 {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0});
10799
10800 // Stylus down on left window and spy
10801 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
10802 .deviceId(stylusDeviceId)
10803 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
10804 .build());
10805 leftWindow->consumeMotionEvent(
10806 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
10807 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId)));
10808
10809 // Finger down on right window and spy - but spy already has stylus
10810 mDispatcher->notifyMotion(
10811 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
10812 .deviceId(touchDeviceId)
10813 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
10814 .build());
10815 rightWindow->consumeMotionEvent(
10816 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010817 spy->assertNoEvents();
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070010818
10819 // Act: pilfer from spy. Spy is currently receiving touch events.
10820 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010821 leftWindow->consumeMotionEvent(
10822 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070010823 rightWindow->consumeMotionEvent(
10824 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
10825
10826 // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus
10827 mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
10828 .deviceId(stylusDeviceId)
10829 .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52))
10830 .build());
10831 mDispatcher->notifyMotion(
10832 MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
10833 .deviceId(touchDeviceId)
10834 .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52))
10835 .build());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070010836 spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId)));
Siarhei Vishniakou0b251a32023-09-20 16:24:42 -070010837
10838 spy->assertNoEvents();
10839 leftWindow->assertNoEvents();
10840 rightWindow->assertNoEvents();
10841}
10842
Prabir Pradhand65552b2021-10-07 11:23:50 -070010843class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
10844public:
10845 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
10846 std::shared_ptr<FakeApplicationHandle> overlayApplication =
10847 std::make_shared<FakeApplicationHandle>();
10848 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010849 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
10850 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070010851 overlay->setFocusable(false);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010852 overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -080010853 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010854 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070010855 overlay->setTrustedOverlay(true);
10856
10857 std::shared_ptr<FakeApplicationHandle> application =
10858 std::make_shared<FakeApplicationHandle>();
10859 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010860 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
10861 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -070010862 window->setFocusable(true);
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010863 window->setOwnerInfo(gui::Pid{222}, gui::Uid{222});
Prabir Pradhand65552b2021-10-07 11:23:50 -070010864
10865 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010866 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070010867 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +000010868 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -070010869 return {std::move(overlay), std::move(window)};
10870 }
10871
10872 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +000010873 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -070010874 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +000010875 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -070010876 }
10877
10878 void sendStylusEvent(int32_t action) {
10879 NotifyMotionArgs motionArgs =
10880 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
10881 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010882 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +000010883 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -070010884 }
10885};
10886
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080010887using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
10888
10889TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
Siarhei Vishniakouad3b6822023-06-22 14:17:35 -070010890 testing::GTEST_FLAG(death_test_style) = "threadsafe";
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080010891 ScopedSilentDeath _silentDeath;
10892
Prabir Pradhand65552b2021-10-07 11:23:50 -070010893 auto [overlay, window] = setupStylusOverlayScenario();
10894 overlay->setTrustedOverlay(false);
10895 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010896 ASSERT_DEATH(mDispatcher->onWindowInfosChanged(
10897 {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}),
Prabir Pradhand65552b2021-10-07 11:23:50 -070010898 ".* not a trusted overlay");
10899}
10900
10901TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
10902 auto [overlay, window] = setupStylusOverlayScenario();
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010903 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070010904
10905 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
10906 overlay->consumeMotionDown();
10907 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
10908 overlay->consumeMotionUp();
10909
10910 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
10911 window->consumeMotionDown();
10912 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
10913 window->consumeMotionUp();
10914
10915 overlay->assertNoEvents();
10916 window->assertNoEvents();
10917}
10918
10919TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
10920 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -080010921 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010922 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhand65552b2021-10-07 11:23:50 -070010923
10924 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
10925 overlay->consumeMotionDown();
10926 window->consumeMotionDown();
10927 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
10928 overlay->consumeMotionUp();
10929 window->consumeMotionUp();
10930
10931 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
10932 window->consumeMotionDown();
10933 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
10934 window->consumeMotionUp();
10935
10936 overlay->assertNoEvents();
10937 window->assertNoEvents();
10938}
10939
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000010940/**
10941 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
10942 * The scenario is as follows:
10943 * - The stylus interceptor overlay is configured as a spy window.
10944 * - The stylus interceptor spy receives the start of a new stylus gesture.
10945 * - It pilfers pointers and then configures itself to no longer be a spy.
10946 * - The stylus interceptor continues to receive the rest of the gesture.
10947 */
10948TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
10949 auto [overlay, window] = setupStylusOverlayScenario();
10950 overlay->setSpy(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010951 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000010952
10953 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
10954 overlay->consumeMotionDown();
10955 window->consumeMotionDown();
10956
10957 // The interceptor pilfers the pointers.
10958 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
10959 window->consumeMotionCancel();
10960
10961 // The interceptor configures itself so that it is no longer a spy.
10962 overlay->setSpy(false);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070010963 mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan6dfbf262022-03-14 15:24:30 +000010964
10965 // It continues to receive the rest of the stylus gesture.
10966 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
10967 overlay->consumeMotionMove();
10968 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
10969 overlay->consumeMotionUp();
10970
10971 window->assertNoEvents();
10972}
10973
Prabir Pradhan5735a322022-04-11 17:23:34 +000010974struct User {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010975 gui::Pid mPid;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000010976 gui::Uid mUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +000010977 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
10978 std::unique_ptr<InputDispatcher>& mDispatcher;
10979
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000010980 User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid)
Prabir Pradhan5735a322022-04-11 17:23:34 +000010981 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
10982
10983 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010984 return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000010985 ADISPLAY_ID_DEFAULT, {100, 200},
10986 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
10987 AMOTION_EVENT_INVALID_CURSOR_POSITION},
10988 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
10989 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
10990 }
10991
10992 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070010993 return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +000010994 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +000010995 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +000010996 mPolicyFlags);
10997 }
10998
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070010999 sp<FakeWindowHandle> createWindow(const char* name) const {
Prabir Pradhan5735a322022-04-11 17:23:34 +000011000 std::shared_ptr<FakeApplicationHandle> overlayApplication =
11001 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011002 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
11003 name, ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +000011004 window->setOwnerInfo(mPid, mUid);
11005 return window;
11006 }
11007};
11008
11009using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
11010
11011TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011012 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011013 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011014 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011015
11016 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
11017 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
11018 window->consumeMotionDown();
11019
11020 setFocusedWindow(window);
11021 window->consumeFocusEvent(true);
11022
11023 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
11024 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
11025 window->consumeKeyDown(ADISPLAY_ID_NONE);
11026}
11027
11028TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011029 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011030 auto window = owner.createWindow("Owned window");
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011031 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011032
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011033 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011034 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
11035 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
11036
11037 setFocusedWindow(window);
11038 window->consumeFocusEvent(true);
11039
11040 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
11041 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
11042 window->assertNoEvents();
11043}
11044
11045TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011046 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011047 auto window = owner.createWindow("Owned window");
11048 auto spy = owner.createWindow("Owned spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000011049 spy->setSpy(true);
11050 spy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011051 mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011052
11053 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
11054 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
11055 spy->consumeMotionDown();
11056 window->consumeMotionDown();
11057}
11058
11059TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011060 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011061 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000011062
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011063 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011064 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000011065 randosSpy->setSpy(true);
11066 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011067 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011068
11069 // The event is targeted at owner's window, so injection should succeed, but the spy should
11070 // not receive the event.
11071 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
11072 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
11073 randosSpy->assertNoEvents();
11074 window->consumeMotionDown();
11075}
11076
11077TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011078 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011079 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000011080
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011081 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011082 auto randosSpy = rando.createWindow("Rando's spy");
Prabir Pradhan5735a322022-04-11 17:23:34 +000011083 randosSpy->setSpy(true);
11084 randosSpy->setTrustedOverlay(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011085 mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011086
11087 // A user that has injection permission can inject into any window.
11088 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011089 injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
Prabir Pradhan5735a322022-04-11 17:23:34 +000011090 ADISPLAY_ID_DEFAULT));
11091 randosSpy->consumeMotionDown();
11092 window->consumeMotionDown();
11093
11094 setFocusedWindow(randosSpy);
11095 randosSpy->consumeFocusEvent(true);
11096
Siarhei Vishniakoub237f9e2023-07-21 16:42:23 -070011097 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher));
Prabir Pradhan5735a322022-04-11 17:23:34 +000011098 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
11099 window->assertNoEvents();
11100}
11101
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070011102TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011103 auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011104 auto window = owner.createWindow("Owned window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000011105
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000011106 auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21});
Siarhei Vishniakoue3ce4122023-08-23 10:26:46 -070011107 auto randosWindow = rando.createWindow("Rando's window");
Prabir Pradhan5735a322022-04-11 17:23:34 +000011108 randosWindow->setFrame(Rect{-10, -10, -5, -5});
11109 randosWindow->setWatchOutsideTouch(true);
Siarhei Vishniakouc41de372023-07-20 13:14:26 -070011110 mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0});
Prabir Pradhan5735a322022-04-11 17:23:34 +000011111
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070011112 // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids.
Prabir Pradhan5735a322022-04-11 17:23:34 +000011113 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
11114 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
11115 window->consumeMotionDown();
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -070011116 randosWindow->assertNoEvents();
Prabir Pradhan5735a322022-04-11 17:23:34 +000011117}
11118
Garfield Tane84e6f92019-08-29 17:28:41 -070011119} // namespace android::inputdispatcher