blob: 017f10baf8cd812c31ee4676bf4e631bbc38b90f [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"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070019#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080020#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080021#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070022#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070023#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000024#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080025#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100027#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080029#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080032#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070033#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080034#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080035#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
Garfield Tan1c7bc862020-01-28 13:24:04 -080037using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050038using android::gui::FocusRequest;
39using android::gui::TouchOcclusionMode;
40using android::gui::WindowInfo;
41using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080042using android::os::InputEventInjectionResult;
43using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080044
Garfield Tane84e6f92019-08-29 17:28:41 -070045namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070047using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080048using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049
Michael Wrightd02c5b62014-02-10 15:10:22 -080050// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000051static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080052
53// An arbitrary device id.
Prabir Pradhan5735a322022-04-11 17:23:34 +000054static constexpr int32_t DEVICE_ID = 1;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080055static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Jeff Brownf086ddb2014-02-11 14:28:48 -080057// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000058static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
59static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080060
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080061static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
62static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
63static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
64static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070065static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080066static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080067static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080068static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080069/**
70 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
71 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
72 * index 0) is the new pointer going down. The same pointer could have been placed at a different
73 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
74 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
75 * pointer id=0 leaves but the pointer id=1 remains.
76 */
77static constexpr int32_t POINTER_0_DOWN =
78 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080079static constexpr int32_t POINTER_1_DOWN =
80 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000081static constexpr int32_t POINTER_2_DOWN =
82 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000083static constexpr int32_t POINTER_3_DOWN =
84 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000085static constexpr int32_t POINTER_0_UP =
86 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080087static constexpr int32_t POINTER_1_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000089static constexpr int32_t POINTER_2_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080091
Antonio Kantek15beb512022-06-13 22:35:41 +000092// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000093static constexpr int32_t WINDOW_PID = 999;
94static constexpr int32_t WINDOW_UID = 1001;
95
Antonio Kantek15beb512022-06-13 22:35:41 +000096// The default pid and uid for the windows created on the secondary display by the test.
97static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
98static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
99
Prabir Pradhan5735a322022-04-11 17:23:34 +0000100// The default policy flags to use for event injection by tests.
101static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000103// An arbitrary pid of the gesture monitor window
104static constexpr int32_t MONITOR_PID = 2001;
105
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800106static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
107
Arthur Hungc539dbb2022-12-08 07:45:36 +0000108static constexpr int expectedWallpaperFlags =
109 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
110
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800111using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
112
chaviwd1c23182019-12-20 18:44:56 -0800113struct PointF {
114 float x;
115 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800116 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800117};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Gang Wang342c9272020-01-13 13:15:04 -0500119/**
120 * Return a DOWN key event with KEYCODE_A.
121 */
122static KeyEvent getTestKeyEvent() {
123 KeyEvent event;
124
Garfield Tanfbe732e2020-01-24 11:26:14 -0800125 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
126 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
127 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500128 return event;
129}
130
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000131static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
132 ASSERT_EQ(expectedAction, receivedAction)
133 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
134 << MotionEvent::actionToString(receivedAction);
135}
136
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800137MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
138 bool matches = action == arg.getAction();
139 if (!matches) {
140 *result_listener << "expected action " << MotionEvent::actionToString(action)
141 << ", but got " << MotionEvent::actionToString(arg.getAction());
142 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800143 if (action == AMOTION_EVENT_ACTION_DOWN) {
144 if (!matches) {
145 *result_listener << "; ";
146 }
147 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
148 matches &= arg.getDownTime() == arg.getEventTime();
149 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800150 if (action == AMOTION_EVENT_ACTION_CANCEL) {
151 if (!matches) {
152 *result_listener << "; ";
153 }
154 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
155 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
156 }
157 return matches;
158}
159
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800160MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
161 return arg.getDownTime() == downTime;
162}
163
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800164MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
165 return arg.getDisplayId() == displayId;
166}
167
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800168MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
169 return arg.getDeviceId() == deviceId;
170}
171
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800172MATCHER_P(WithSource, source, "InputEvent with specified source") {
173 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
174 << inputEventSourceToString(arg.getSource());
175 return arg.getSource() == source;
176}
177
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800178MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
179 return arg.getFlags() == flags;
180}
181
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800182MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
183 if (arg.getPointerCount() != 1) {
184 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
185 return false;
186 }
Harry Cutts33476232023-01-30 19:57:29 +0000187 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800188}
189
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800190MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
191 return arg.getPointerCount() == pointerCount;
192}
193
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800194MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
195 // Build a map for the received pointers, by pointer id
196 std::map<int32_t /*pointerId*/, PointF> actualPointers;
197 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
198 const int32_t pointerId = arg.getPointerId(pointerIndex);
199 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
200 }
201 return pointers == actualPointers;
202}
203
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204// --- FakeInputDispatcherPolicy ---
205
206class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
207 InputDispatcherConfiguration mConfig;
208
Prabir Pradhanedd96402022-02-15 01:46:16 -0800209 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
210
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000212 FakeInputDispatcherPolicy() = default;
213 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800214
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800215 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700217 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700218 EXPECT_EQ(event.getDisplayId(), args.displayId);
219
220 const auto& keyEvent = static_cast<const KeyEvent&>(event);
221 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
222 EXPECT_EQ(keyEvent.getAction(), args.action);
223 });
Jackal Guof9696682018-10-05 12:23:23 +0800224 }
225
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700226 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
227 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700228 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700229 EXPECT_EQ(event.getDisplayId(), args.displayId);
230
231 const auto& motionEvent = static_cast<const MotionEvent&>(event);
232 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
233 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000234 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
236 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
237 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700238 });
Jackal Guof9696682018-10-05 12:23:23 +0800239 }
240
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700241 void assertFilterInputEventWasNotCalled() {
242 std::scoped_lock lock(mLock);
243 ASSERT_EQ(nullptr, mFilteredEvent);
244 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700247 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800248 ASSERT_TRUE(mConfigurationChangedTime)
249 << "Timed out waiting for configuration changed call";
250 ASSERT_EQ(*mConfigurationChangedTime, when);
251 mConfigurationChangedTime = std::nullopt;
252 }
253
254 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700255 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800257 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800258 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
259 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
260 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
261 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
262 mLastNotifySwitch = std::nullopt;
263 }
264
chaviwfd6d3512019-03-25 13:23:49 -0700265 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700266 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800267 ASSERT_EQ(touchedToken, mOnPointerDownToken);
268 mOnPointerDownToken.clear();
269 }
270
271 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700272 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800273 ASSERT_TRUE(mOnPointerDownToken == nullptr)
274 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700275 }
276
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700277 // This function must be called soon after the expected ANR timer starts,
278 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500279 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700280 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500281 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800282 std::unique_lock lock(mLock);
283 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500284 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800285 ASSERT_NO_FATAL_FAILURE(
286 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500287 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700288 }
289
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000290 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800291 const sp<WindowInfoHandle>& window) {
292 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
293 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
294 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500295 }
296
Prabir Pradhanedd96402022-02-15 01:46:16 -0800297 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
298 const sp<IBinder>& expectedToken,
299 int32_t expectedPid) {
300 std::unique_lock lock(mLock);
301 android::base::ScopedLockAssertion assumeLocked(mLock);
302 AnrResult result;
303 ASSERT_NO_FATAL_FAILURE(result =
304 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
305 const auto& [token, pid] = result;
306 ASSERT_EQ(expectedToken, token);
307 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500308 }
309
Prabir Pradhanedd96402022-02-15 01:46:16 -0800310 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000311 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500312 std::unique_lock lock(mLock);
313 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800314 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
315 const auto& [token, _] = result;
316 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000317 }
318
Prabir Pradhanedd96402022-02-15 01:46:16 -0800319 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
320 int32_t expectedPid) {
321 std::unique_lock lock(mLock);
322 android::base::ScopedLockAssertion assumeLocked(mLock);
323 AnrResult result;
324 ASSERT_NO_FATAL_FAILURE(
325 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
326 const auto& [token, pid] = result;
327 ASSERT_EQ(expectedToken, token);
328 ASSERT_EQ(expectedPid, pid);
329 }
330
331 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000332 sp<IBinder> getResponsiveWindowToken() {
333 std::unique_lock lock(mLock);
334 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800335 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
336 const auto& [token, _] = result;
337 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700338 }
339
340 void assertNotifyAnrWasNotCalled() {
341 std::scoped_lock lock(mLock);
342 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800343 ASSERT_TRUE(mAnrWindows.empty());
344 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500345 << "ANR was not called, but please also consume the 'connection is responsive' "
346 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700347 }
348
Garfield Tan1c7bc862020-01-28 13:24:04 -0800349 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
350 mConfig.keyRepeatTimeout = timeout;
351 mConfig.keyRepeatDelay = delay;
352 }
353
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000354 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800355 std::unique_lock lock(mLock);
356 base::ScopedLockAssertion assumeLocked(mLock);
357
358 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
359 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000360 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800361 enabled;
362 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
364 << ") to be called.";
365 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800366 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000367 auto request = *mPointerCaptureRequest;
368 mPointerCaptureRequest.reset();
369 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800370 }
371
372 void assertSetPointerCaptureNotCalled() {
373 std::unique_lock lock(mLock);
374 base::ScopedLockAssertion assumeLocked(mLock);
375
376 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000377 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800378 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800380 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000381 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800382 }
383
arthurhungf452d0b2021-01-06 00:19:52 +0800384 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
385 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800386 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800387 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800388 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800389 }
390
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800391 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
392 std::unique_lock lock(mLock);
393 base::ScopedLockAssertion assumeLocked(mLock);
394 std::optional<sp<IBinder>> receivedToken =
395 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
396 mNotifyInputChannelBroken);
397 ASSERT_TRUE(receivedToken.has_value());
398 ASSERT_EQ(token, *receivedToken);
399 }
400
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800401 /**
402 * Set policy timeout. A value of zero means next key will not be intercepted.
403 */
404 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
405 mInterceptKeyTimeout = timeout;
406 }
407
Josep del Riob3981622023-04-18 15:49:45 +0000408 void assertUserActivityPoked() {
409 std::scoped_lock lock(mLock);
410 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
411 }
412
413 void assertUserActivityNotPoked() {
414 std::scoped_lock lock(mLock);
415 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
416 }
417
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700419 std::mutex mLock;
420 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
421 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
422 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
423 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800424
Prabir Pradhan99987712020-11-10 18:43:05 -0800425 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000426
427 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800428
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700429 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700430 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800431 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
432 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700433 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800434 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
435 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700436
arthurhungf452d0b2021-01-06 00:19:52 +0800437 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800438 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000439 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800440
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800441 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
442
Prabir Pradhanedd96402022-02-15 01:46:16 -0800443 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
444 // for a specific container to become non-empty. When the container is non-empty, return the
445 // first entry from the container and erase it.
446 template <class T>
447 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
448 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
449 // If there is an ANR, Dispatcher won't be idle because there are still events
450 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
451 // before checking if ANR was called.
452 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
453 // to provide it some time to act. 100ms seems reasonable.
454 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
455 const std::chrono::time_point start = std::chrono::steady_clock::now();
456 std::optional<T> token =
457 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
458 if (!token.has_value()) {
459 ADD_FAILURE() << "Did not receive the ANR callback";
460 return {};
461 }
462
463 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
464 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
465 // the dispatcher started counting before this function was called
466 if (std::chrono::abs(timeout - waited) > 100ms) {
467 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
468 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
469 << "ms, but waited "
470 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
471 << "ms instead";
472 }
473 return *token;
474 }
475
476 template <class T>
477 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
478 std::queue<T>& storage,
479 std::unique_lock<std::mutex>& lock,
480 std::condition_variable& condition)
481 REQUIRES(mLock) {
482 condition.wait_for(lock, timeout,
483 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
484 if (storage.empty()) {
485 ADD_FAILURE() << "Did not receive the expected callback";
486 return std::nullopt;
487 }
488 T item = storage.front();
489 storage.pop();
490 return std::make_optional(item);
491 }
492
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600493 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700494 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800495 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 }
497
Prabir Pradhanedd96402022-02-15 01:46:16 -0800498 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
499 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700500 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800501 ASSERT_TRUE(pid.has_value());
502 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700503 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500504 }
505
Prabir Pradhanedd96402022-02-15 01:46:16 -0800506 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
507 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500508 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800509 ASSERT_TRUE(pid.has_value());
510 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500511 mNotifyAnr.notify_all();
512 }
513
514 void notifyNoFocusedWindowAnr(
515 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
516 std::scoped_lock lock(mLock);
517 mAnrApplications.push(applicationHandle);
518 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 }
520
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800521 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
522 std::scoped_lock lock(mLock);
523 mBrokenInputChannels.push(connectionToken);
524 mNotifyInputChannelBroken.notify_all();
525 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600527 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700528
Chris Yef59a2f42020-10-16 12:55:26 -0700529 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
530 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
531 const std::vector<float>& values) override {}
532
533 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
534 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000535
Chris Yefb552902021-02-03 17:18:37 -0800536 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
537
Prabir Pradhana41d2442023-04-20 21:30:40 +0000538 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
Prabir Pradhana41d2442023-04-20 21:30:40 +0000540 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700541 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000542 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700543 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000544 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
545 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800546 break;
547 }
548
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700549 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000550 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
551 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800552 break;
553 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700554 default: {
555 ADD_FAILURE() << "Should only filter keys or motions";
556 break;
557 }
Jackal Guof9696682018-10-05 12:23:23 +0800558 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 return true;
560 }
561
Prabir Pradhana41d2442023-04-20 21:30:40 +0000562 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
563 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800564 // Clear intercept state when we handled the event.
565 mInterceptKeyTimeout = 0ms;
566 }
567 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600569 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570
Prabir Pradhana41d2442023-04-20 21:30:40 +0000571 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800572 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
573 // Clear intercept state so we could dispatch the event in next wake.
574 mInterceptKeyTimeout = 0ms;
575 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 }
577
Prabir Pradhana41d2442023-04-20 21:30:40 +0000578 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
579 uint32_t) override {
580 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 }
582
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600583 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
584 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700585 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800586 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
587 * essentially a passthrough for notifySwitch.
588 */
Harry Cutts33476232023-01-30 19:57:29 +0000589 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591
Josep del Riob3981622023-04-18 15:49:45 +0000592 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
593 std::scoped_lock lock(mLock);
594 mPokedUserActivity = true;
595 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600597 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700598 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700599 mOnPointerDownToken = newToken;
600 }
601
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000602 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800603 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000604 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800605 mPointerCaptureChangedCondition.notify_all();
606 }
607
arthurhungf452d0b2021-01-06 00:19:52 +0800608 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
609 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800610 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800611 mDropTargetWindowToken = token;
612 }
613
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700614 void assertFilterInputEventWasCalledInternal(
615 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700616 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800617 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700618 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800619 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800620 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621};
622
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623// --- InputDispatcherTest ---
624
625class InputDispatcherTest : public testing::Test {
626protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000627 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700628 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000630 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000631 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
632 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800633 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000634 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700635 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 }
637
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000638 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700639 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000640 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700641 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700643
644 /**
645 * Used for debugging when writing the test
646 */
647 void dumpDispatcherState() {
648 std::string dump;
649 mDispatcher->dump(dump);
650 std::stringstream ss(dump);
651 std::string to;
652
653 while (std::getline(ss, to, '\n')) {
654 ALOGE("%s", to.c_str());
655 }
656 }
Vishnu Nair958da932020-08-21 17:12:37 -0700657
Chavi Weingarten847e8512023-03-29 00:26:09 +0000658 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700659 FocusRequest request;
660 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000661 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700662 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
663 request.displayId = window->getInfo()->displayId;
664 mDispatcher->setFocusedWindow(request);
665 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666};
667
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
669 KeyEvent event;
670
671 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800672 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
673 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600674 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
675 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800676 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000677 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000678 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 << "Should reject key events with undefined action.";
680
681 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800682 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
683 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600684 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800685 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000686 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000687 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 << "Should reject key events with ACTION_MULTIPLE.";
689}
690
691TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
692 MotionEvent event;
693 PointerProperties pointerProperties[MAX_POINTERS + 1];
694 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800695 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 pointerProperties[i].clear();
697 pointerProperties[i].id = i;
698 pointerCoords[i].clear();
699 }
700
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800701 // Some constants commonly used below
702 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
703 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
704 constexpr int32_t metaState = AMETA_NONE;
705 constexpr MotionClassification classification = MotionClassification::NONE;
706
chaviw9eaa22c2020-07-01 16:21:27 -0700707 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800709 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700710 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
711 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700712 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
713 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700714 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800715 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000716 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000717 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 << "Should reject motion events with undefined action.";
719
720 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800721 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800722 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
723 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
724 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
725 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500726 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800727 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000728 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000729 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 << "Should reject motion events with pointer down index too large.";
731
Garfield Tanfbe732e2020-01-24 11:26:14 -0800732 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700733 AMOTION_EVENT_ACTION_POINTER_DOWN |
734 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700735 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
736 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700737 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500738 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800739 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000740 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000741 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 << "Should reject motion events with pointer down index too small.";
743
744 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800745 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800746 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
747 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
748 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
749 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500750 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800751 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000752 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000753 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 << "Should reject motion events with pointer up index too large.";
755
Garfield Tanfbe732e2020-01-24 11:26:14 -0800756 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700757 AMOTION_EVENT_ACTION_POINTER_UP |
758 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700759 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
760 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700761 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500762 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800763 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000764 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000765 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766 << "Should reject motion events with pointer up index too small.";
767
768 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800769 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
770 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700771 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700772 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
773 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700774 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800775 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000776 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000777 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800778 << "Should reject motion events with 0 pointers.";
779
Garfield Tanfbe732e2020-01-24 11:26:14 -0800780 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
781 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700782 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700783 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
784 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700785 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800786 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000787 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000788 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 << "Should reject motion events with more than MAX_POINTERS pointers.";
790
791 // Rejects motion events with invalid pointer ids.
792 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800793 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
794 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700795 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700796 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
797 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700798 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800799 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000800 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000801 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 << "Should reject motion events with pointer ids less than 0.";
803
804 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800805 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
806 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700807 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700808 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
809 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700810 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800811 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000812 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000813 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
815
816 // Rejects motion events with duplicate pointer ids.
817 pointerProperties[0].id = 1;
818 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800819 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
820 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700821 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700822 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
823 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700824 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800825 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000826 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000827 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 << "Should reject motion events with duplicate pointer ids.";
829}
830
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800831/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
832
833TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
834 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000835 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800836 ASSERT_TRUE(mDispatcher->waitForIdle());
837
838 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
839}
840
841TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000842 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
843 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000844 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800845
846 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
847 args.policyFlags |= POLICY_FLAG_TRUSTED;
848 mFakePolicy->assertNotifySwitchWasCalled(args);
849}
850
Arthur Hungb92218b2018-08-14 12:00:21 +0800851// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700852static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700853// Default input dispatching timeout if there is no focused application or paused window
854// from which to determine an appropriate dispatching timeout.
855static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
856 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
857 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800858
859class FakeApplicationHandle : public InputApplicationHandle {
860public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700861 FakeApplicationHandle() {
862 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700863 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500864 mInfo.dispatchingTimeoutMillis =
865 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700866 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800867 virtual ~FakeApplicationHandle() {}
868
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000869 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700870
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500871 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
872 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700873 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800874};
875
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800876class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800877public:
Garfield Tan15601662020-09-22 15:32:38 -0700878 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800879 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700880 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800881 }
882
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800883 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700884 InputEvent* event;
885 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
886 if (!consumeSeq) {
887 return nullptr;
888 }
889 finishEvent(*consumeSeq);
890 return event;
891 }
892
893 /**
894 * Receive an event without acknowledging it.
895 * Return the sequence number that could later be used to send finished signal.
896 */
897 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800898 uint32_t consumeSeq;
899 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800900
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800901 std::chrono::time_point start = std::chrono::steady_clock::now();
902 status_t status = WOULD_BLOCK;
903 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000904 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800905 &event);
906 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
907 if (elapsed > 100ms) {
908 break;
909 }
910 }
911
912 if (status == WOULD_BLOCK) {
913 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700914 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800915 }
916
917 if (status != OK) {
918 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700919 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800920 }
921 if (event == nullptr) {
922 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800924 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700925 if (outEvent != nullptr) {
926 *outEvent = event;
927 }
928 return consumeSeq;
929 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800930
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700931 /**
932 * To be used together with "receiveEvent" to complete the consumption of an event.
933 */
934 void finishEvent(uint32_t consumeSeq) {
935 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
936 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800937 }
938
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000939 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
940 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
941 ASSERT_EQ(OK, status);
942 }
943
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700944 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000945 std::optional<int32_t> expectedDisplayId,
946 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800947 InputEvent* event = consume();
948
949 ASSERT_NE(nullptr, event) << mName.c_str()
950 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800951 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700952 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
953 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800954
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955 if (expectedDisplayId.has_value()) {
956 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
957 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800958
Tiger Huang8664f8c2018-10-11 19:14:35 +0800959 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700960 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800961 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
962 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000963 if (expectedFlags.has_value()) {
964 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
965 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800966 break;
967 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700968 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800969 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000970 assertMotionAction(expectedAction, motionEvent.getAction());
971
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000972 if (expectedFlags.has_value()) {
973 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
974 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800975 break;
976 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700977 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100978 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
979 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700980 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800981 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
982 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700983 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000984 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
985 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700986 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800987 FAIL() << "Use 'consumeDragEvent' for DRAG events";
988 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800989 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800990 }
991
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800992 MotionEvent* consumeMotion() {
993 InputEvent* event = consume();
994
995 if (event == nullptr) {
996 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
997 return nullptr;
998 }
999
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001000 if (event->getType() != InputEventType::MOTION) {
1001 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001002 return nullptr;
1003 }
1004 return static_cast<MotionEvent*>(event);
1005 }
1006
1007 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1008 MotionEvent* motionEvent = consumeMotion();
1009 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1010 ASSERT_THAT(*motionEvent, matcher);
1011 }
1012
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001013 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1014 InputEvent* event = consume();
1015 ASSERT_NE(nullptr, event) << mName.c_str()
1016 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001017 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1018 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001019
1020 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1021 << mName.c_str() << ": event displayId should always be NONE.";
1022
1023 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1024 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001025 }
1026
Prabir Pradhan99987712020-11-10 18:43:05 -08001027 void consumeCaptureEvent(bool hasCapture) {
1028 const InputEvent* event = consume();
1029 ASSERT_NE(nullptr, event) << mName.c_str()
1030 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001031 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1032 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001033
1034 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1035 << mName.c_str() << ": event displayId should always be NONE.";
1036
1037 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1038 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1039 }
1040
arthurhungb89ccb02020-12-30 16:19:01 +08001041 void consumeDragEvent(bool isExiting, float x, float y) {
1042 const InputEvent* event = consume();
1043 ASSERT_NE(nullptr, event) << mName.c_str()
1044 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001045 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001046
1047 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1048 << mName.c_str() << ": event displayId should always be NONE.";
1049
1050 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1051 EXPECT_EQ(isExiting, dragEvent.isExiting());
1052 EXPECT_EQ(x, dragEvent.getX());
1053 EXPECT_EQ(y, dragEvent.getY());
1054 }
1055
Antonio Kantekf16f2832021-09-28 04:39:20 +00001056 void consumeTouchModeEvent(bool inTouchMode) {
1057 const InputEvent* event = consume();
1058 ASSERT_NE(nullptr, event) << mName.c_str()
1059 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001060 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1061 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001062
1063 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1064 << mName.c_str() << ": event displayId should always be NONE.";
1065 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1066 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1067 }
1068
chaviwd1c23182019-12-20 18:44:56 -08001069 void assertNoEvents() {
1070 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001071 if (event == nullptr) {
1072 return;
1073 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001074 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1076 ADD_FAILURE() << "Received key event "
1077 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001078 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001079 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1080 ADD_FAILURE() << "Received motion event "
1081 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001082 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001083 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1084 ADD_FAILURE() << "Received focus event, hasFocus = "
1085 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001086 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001087 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1088 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1089 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001090 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001091 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1092 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1093 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001094 }
1095 FAIL() << mName.c_str()
1096 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001097 }
1098
1099 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1100
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001101 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1102
chaviwd1c23182019-12-20 18:44:56 -08001103protected:
1104 std::unique_ptr<InputConsumer> mConsumer;
1105 PreallocatedInputEventFactory mEventFactory;
1106
1107 std::string mName;
1108};
1109
chaviw3277faf2021-05-19 16:45:23 -05001110class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001111public:
1112 static const int32_t WIDTH = 600;
1113 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001114
Chris Yea209fde2020-07-22 13:54:51 -07001115 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001116 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001118 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001119 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001120 base::Result<std::unique_ptr<InputChannel>> channel =
1121 dispatcher->createInputChannel(name);
1122 token = (*channel)->getConnectionToken();
1123 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001124 }
1125
1126 inputApplicationHandle->updateInfo();
1127 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1128
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001129 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001130 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001131 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001132 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001133 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001134 mInfo.frameLeft = 0;
1135 mInfo.frameTop = 0;
1136 mInfo.frameRight = WIDTH;
1137 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001138 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001139 mInfo.globalScaleFactor = 1.0;
1140 mInfo.touchableRegion.clear();
1141 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001142 mInfo.ownerPid = WINDOW_PID;
1143 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001144 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001145 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001146 }
1147
Arthur Hungabbb9d82021-09-01 14:52:30 +00001148 sp<FakeWindowHandle> clone(
1149 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001150 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001151 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001152 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1153 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001154 return handle;
1155 }
1156
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001157 void setTouchable(bool touchable) {
1158 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1159 }
chaviwd1c23182019-12-20 18:44:56 -08001160
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001161 void setFocusable(bool focusable) {
1162 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1163 }
1164
1165 void setVisible(bool visible) {
1166 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1167 }
Vishnu Nair958da932020-08-21 17:12:37 -07001168
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001169 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001170 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001171 }
1172
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001173 void setPaused(bool paused) {
1174 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1175 }
1176
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001177 void setPreventSplitting(bool preventSplitting) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001179 }
1180
1181 void setSlippery(bool slippery) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1183 }
1184
1185 void setWatchOutsideTouch(bool watchOutside) {
1186 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1187 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001188
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001189 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1190
1191 void setInterceptsStylus(bool interceptsStylus) {
1192 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1193 }
1194
1195 void setDropInput(bool dropInput) {
1196 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1197 }
1198
1199 void setDropInputIfObscured(bool dropInputIfObscured) {
1200 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1201 }
1202
1203 void setNoInputChannel(bool noInputChannel) {
1204 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1205 }
1206
Josep del Riob3981622023-04-18 15:49:45 +00001207 void setDisableUserActivity(bool disableUserActivity) {
1208 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1209 }
1210
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001211 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1212
chaviw3277faf2021-05-19 16:45:23 -05001213 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001214
Bernardo Rufino7393d172021-02-26 13:56:11 +00001215 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1216
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001217 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001218 mInfo.frameLeft = frame.left;
1219 mInfo.frameTop = frame.top;
1220 mInfo.frameRight = frame.right;
1221 mInfo.frameBottom = frame.bottom;
1222 mInfo.touchableRegion.clear();
1223 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001224
1225 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1226 ui::Transform translate;
1227 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1228 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001229 }
1230
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001231 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1232
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001233 void setIsWallpaper(bool isWallpaper) {
1234 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1235 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001236
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001237 void setDupTouchToWallpaper(bool hasWallpaper) {
1238 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1239 }
chaviwd1c23182019-12-20 18:44:56 -08001240
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001241 void setTrustedOverlay(bool trustedOverlay) {
1242 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1243 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001244
chaviw9eaa22c2020-07-01 16:21:27 -07001245 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1246 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1247 }
1248
1249 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001250
yunho.shinf4a80b82020-11-16 21:13:57 +09001251 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1252
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001253 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001254 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001255 }
1256
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001257 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001258 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001259 }
1260
Svet Ganov5d3bc372020-01-26 23:11:07 -08001261 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001262 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001263 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1264 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001265 }
1266
1267 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001268 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001269 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1270 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001271 }
1272
1273 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001274 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001275 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1276 }
1277
1278 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1279 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001280 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001281 expectedFlags);
1282 }
1283
Svet Ganov5d3bc372020-01-26 23:11:07 -08001284 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001285 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1286 int32_t expectedFlags = 0) {
1287 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1288 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001289 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001290 }
1291
1292 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001293 int32_t expectedFlags = 0) {
1294 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1295 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001296 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001297 }
1298
1299 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001300 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001301 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001302 expectedFlags);
1303 }
1304
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001305 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1306 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001307 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001308 expectedFlags);
1309 }
1310
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001311 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1312 int32_t expectedFlags = 0) {
1313 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001314 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001315 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001316 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1317 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1318 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1319 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1320 }
1321
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001322 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1323 ASSERT_NE(mInputReceiver, nullptr)
1324 << "Cannot consume events from a window with no receiver";
1325 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1326 }
1327
Prabir Pradhan99987712020-11-10 18:43:05 -08001328 void consumeCaptureEvent(bool hasCapture) {
1329 ASSERT_NE(mInputReceiver, nullptr)
1330 << "Cannot consume events from a window with no receiver";
1331 mInputReceiver->consumeCaptureEvent(hasCapture);
1332 }
1333
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001334 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1335 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001336 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001337 ASSERT_THAT(*motionEvent, matcher);
1338 }
1339
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001340 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001341 std::optional<int32_t> expectedDisplayId,
1342 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001343 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1344 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1345 expectedFlags);
1346 }
1347
arthurhungb89ccb02020-12-30 16:19:01 +08001348 void consumeDragEvent(bool isExiting, float x, float y) {
1349 mInputReceiver->consumeDragEvent(isExiting, x, y);
1350 }
1351
Antonio Kantekf16f2832021-09-28 04:39:20 +00001352 void consumeTouchModeEvent(bool inTouchMode) {
1353 ASSERT_NE(mInputReceiver, nullptr)
1354 << "Cannot consume events from a window with no receiver";
1355 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1356 }
1357
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001358 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001359 if (mInputReceiver == nullptr) {
1360 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1361 return std::nullopt;
1362 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001363 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001364 }
1365
1366 void finishEvent(uint32_t sequenceNum) {
1367 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1368 mInputReceiver->finishEvent(sequenceNum);
1369 }
1370
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001371 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1372 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1373 mInputReceiver->sendTimeline(inputEventId, timeline);
1374 }
1375
chaviwaf87b3e2019-10-01 16:59:28 -07001376 InputEvent* consume() {
1377 if (mInputReceiver == nullptr) {
1378 return nullptr;
1379 }
1380 return mInputReceiver->consume();
1381 }
1382
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001383 MotionEvent* consumeMotion() {
1384 InputEvent* event = consume();
1385 if (event == nullptr) {
1386 ADD_FAILURE() << "Consume failed : no event";
1387 return nullptr;
1388 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001389 if (event->getType() != InputEventType::MOTION) {
1390 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001391 return nullptr;
1392 }
1393 return static_cast<MotionEvent*>(event);
1394 }
1395
Arthur Hungb92218b2018-08-14 12:00:21 +08001396 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001397 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001398 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001399 return; // Can't receive events if the window does not have input channel
1400 }
1401 ASSERT_NE(nullptr, mInputReceiver)
1402 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001403 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001404 }
1405
chaviwaf87b3e2019-10-01 16:59:28 -07001406 sp<IBinder> getToken() { return mInfo.token; }
1407
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001408 const std::string& getName() { return mName; }
1409
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001410 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1411 mInfo.ownerPid = ownerPid;
1412 mInfo.ownerUid = ownerUid;
1413 }
1414
Prabir Pradhanedd96402022-02-15 01:46:16 -08001415 int32_t getPid() const { return mInfo.ownerPid; }
1416
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001417 void destroyReceiver() { mInputReceiver = nullptr; }
1418
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001419 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1420
chaviwd1c23182019-12-20 18:44:56 -08001421private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001422 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001423 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001424 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001425};
1426
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001427std::atomic<int32_t> FakeWindowHandle::sId{1};
1428
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001429static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001430 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001431 int32_t displayId = ADISPLAY_ID_NONE,
1432 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001433 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001434 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1435 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001436 KeyEvent event;
1437 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1438
1439 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001440 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001441 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1442 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001443
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001444 if (!allowKeyRepeat) {
1445 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1446 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001447 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001448 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001449}
1450
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001451static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001452 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001453 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001454}
1455
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001456// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1457// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1458// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001459static InputEventInjectionResult injectKeyDownNoRepeat(
1460 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001461 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001463 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001464}
1465
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001466static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001467 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001468 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001469}
1470
Garfield Tandf26e862020-07-01 20:18:19 -07001471class PointerBuilder {
1472public:
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001473 PointerBuilder(int32_t id, ToolType toolType) {
Garfield Tandf26e862020-07-01 20:18:19 -07001474 mProperties.clear();
1475 mProperties.id = id;
1476 mProperties.toolType = toolType;
1477 mCoords.clear();
1478 }
1479
1480 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1481
1482 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1483
1484 PointerBuilder& axis(int32_t axis, float value) {
1485 mCoords.setAxisValue(axis, value);
1486 return *this;
1487 }
1488
1489 PointerProperties buildProperties() const { return mProperties; }
1490
1491 PointerCoords buildCoords() const { return mCoords; }
1492
1493private:
1494 PointerProperties mProperties;
1495 PointerCoords mCoords;
1496};
1497
1498class MotionEventBuilder {
1499public:
1500 MotionEventBuilder(int32_t action, int32_t source) {
1501 mAction = action;
1502 mSource = source;
1503 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001504 mDownTime = mEventTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001505 }
1506
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001507 MotionEventBuilder& deviceId(int32_t deviceId) {
1508 mDeviceId = deviceId;
1509 return *this;
1510 }
1511
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001512 MotionEventBuilder& downTime(nsecs_t downTime) {
1513 mDownTime = downTime;
1514 return *this;
1515 }
1516
Garfield Tandf26e862020-07-01 20:18:19 -07001517 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1518 mEventTime = eventTime;
1519 return *this;
1520 }
1521
1522 MotionEventBuilder& displayId(int32_t displayId) {
1523 mDisplayId = displayId;
1524 return *this;
1525 }
1526
1527 MotionEventBuilder& actionButton(int32_t actionButton) {
1528 mActionButton = actionButton;
1529 return *this;
1530 }
1531
arthurhung6d4bed92021-03-17 11:59:33 +08001532 MotionEventBuilder& buttonState(int32_t buttonState) {
1533 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001534 return *this;
1535 }
1536
1537 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1538 mRawXCursorPosition = rawXCursorPosition;
1539 return *this;
1540 }
1541
1542 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1543 mRawYCursorPosition = rawYCursorPosition;
1544 return *this;
1545 }
1546
1547 MotionEventBuilder& pointer(PointerBuilder pointer) {
1548 mPointers.push_back(pointer);
1549 return *this;
1550 }
1551
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001552 MotionEventBuilder& addFlag(uint32_t flags) {
1553 mFlags |= flags;
1554 return *this;
1555 }
1556
Garfield Tandf26e862020-07-01 20:18:19 -07001557 MotionEvent build() {
1558 std::vector<PointerProperties> pointerProperties;
1559 std::vector<PointerCoords> pointerCoords;
1560 for (const PointerBuilder& pointer : mPointers) {
1561 pointerProperties.push_back(pointer.buildProperties());
1562 pointerCoords.push_back(pointer.buildCoords());
1563 }
1564
1565 // Set mouse cursor position for the most common cases to avoid boilerplate.
1566 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou70c6ee82023-05-15 17:43:48 -07001567 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001568 mRawXCursorPosition = pointerCoords[0].getX();
1569 mRawYCursorPosition = pointerCoords[0].getY();
1570 }
1571
1572 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001573 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001574 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001575 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001576 mButtonState, MotionClassification::NONE, identityTransform,
1577 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001578 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001579 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001580
1581 return event;
1582 }
1583
1584private:
1585 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001586 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001587 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001588 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001589 nsecs_t mEventTime;
1590 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1591 int32_t mActionButton{0};
1592 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001593 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001594 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1595 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1596
1597 std::vector<PointerBuilder> mPointers;
1598};
1599
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001600class MotionArgsBuilder {
1601public:
1602 MotionArgsBuilder(int32_t action, int32_t source) {
1603 mAction = action;
1604 mSource = source;
1605 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1606 mDownTime = mEventTime;
1607 }
1608
1609 MotionArgsBuilder& deviceId(int32_t deviceId) {
1610 mDeviceId = deviceId;
1611 return *this;
1612 }
1613
1614 MotionArgsBuilder& downTime(nsecs_t downTime) {
1615 mDownTime = downTime;
1616 return *this;
1617 }
1618
1619 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
1620 mEventTime = eventTime;
1621 return *this;
1622 }
1623
1624 MotionArgsBuilder& displayId(int32_t displayId) {
1625 mDisplayId = displayId;
1626 return *this;
1627 }
1628
1629 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
1630 mPolicyFlags = policyFlags;
1631 return *this;
1632 }
1633
1634 MotionArgsBuilder& actionButton(int32_t actionButton) {
1635 mActionButton = actionButton;
1636 return *this;
1637 }
1638
1639 MotionArgsBuilder& buttonState(int32_t buttonState) {
1640 mButtonState = buttonState;
1641 return *this;
1642 }
1643
1644 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
1645 mRawXCursorPosition = rawXCursorPosition;
1646 return *this;
1647 }
1648
1649 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
1650 mRawYCursorPosition = rawYCursorPosition;
1651 return *this;
1652 }
1653
1654 MotionArgsBuilder& pointer(PointerBuilder pointer) {
1655 mPointers.push_back(pointer);
1656 return *this;
1657 }
1658
1659 MotionArgsBuilder& addFlag(uint32_t flags) {
1660 mFlags |= flags;
1661 return *this;
1662 }
1663
Harry Cuttsb166c002023-05-09 13:06:05 +00001664 MotionArgsBuilder& classification(MotionClassification classification) {
1665 mClassification = classification;
1666 return *this;
1667 }
1668
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001669 NotifyMotionArgs build() {
1670 std::vector<PointerProperties> pointerProperties;
1671 std::vector<PointerCoords> pointerCoords;
1672 for (const PointerBuilder& pointer : mPointers) {
1673 pointerProperties.push_back(pointer.buildProperties());
1674 pointerCoords.push_back(pointer.buildCoords());
1675 }
1676
1677 // Set mouse cursor position for the most common cases to avoid boilerplate.
1678 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou70c6ee82023-05-15 17:43:48 -07001679 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001680 mRawXCursorPosition = pointerCoords[0].getX();
1681 mRawYCursorPosition = pointerCoords[0].getY();
1682 }
1683
1684 NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId,
1685 mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags,
Harry Cuttsb166c002023-05-09 13:06:05 +00001686 AMETA_NONE, mButtonState, mClassification, /*edgeFlags=*/0,
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001687 mPointers.size(), pointerProperties.data(), pointerCoords.data(),
1688 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
1689 mRawYCursorPosition, mDownTime, /*videoFrames=*/{});
1690
1691 return args;
1692 }
1693
1694private:
1695 int32_t mAction;
1696 int32_t mDeviceId = DEVICE_ID;
1697 int32_t mSource;
1698 nsecs_t mDownTime;
1699 nsecs_t mEventTime;
1700 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1701 int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
1702 int32_t mActionButton{0};
1703 int32_t mButtonState{0};
1704 int32_t mFlags{0};
Harry Cuttsb166c002023-05-09 13:06:05 +00001705 MotionClassification mClassification{MotionClassification::NONE};
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001706 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1707 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1708
1709 std::vector<PointerBuilder> mPointers;
1710};
1711
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001712static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001713 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001714 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001715 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1716 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1717 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1718 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001719}
1720
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001721static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001722 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001723 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001724 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001725 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1726 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001727 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001728 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1729 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Garfield Tandf26e862020-07-01 20:18:19 -07001730 MotionEvent event = MotionEventBuilder(action, source)
1731 .displayId(displayId)
1732 .eventTime(eventTime)
1733 .rawXCursorPosition(cursorPosition.x)
1734 .rawYCursorPosition(cursorPosition.y)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001735 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Garfield Tandf26e862020-07-01 20:18:19 -07001736 .x(position.x)
1737 .y(position.y))
1738 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001739
1740 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001741 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1742 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001743}
1744
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001745static InputEventInjectionResult injectMotionDown(
1746 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1747 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001748 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001749}
1750
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001751static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001752 int32_t source, int32_t displayId,
1753 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001754 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001755}
1756
Jackal Guof9696682018-10-05 12:23:23 +08001757static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1758 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1759 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001760 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001761 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1762 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001763
1764 return args;
1765}
1766
Josep del Riob3981622023-04-18 15:49:45 +00001767static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1768 int32_t displayId = ADISPLAY_ID_NONE) {
1769 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1770 // Define a valid key event.
1771 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1772 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1773 currentTime);
1774
1775 return args;
1776}
1777
1778static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1779 int32_t displayId = ADISPLAY_ID_NONE) {
1780 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1781 // Define a valid key event.
1782 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1783 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1784 AMETA_NONE, currentTime);
1785
1786 return args;
1787}
1788
Prabir Pradhan678438e2023-04-13 19:32:51 +00001789[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1790 int32_t displayId,
1791 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001792 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001793 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1794 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1795 }
1796
chaviwd1c23182019-12-20 18:44:56 -08001797 PointerProperties pointerProperties[pointerCount];
1798 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001799
chaviwd1c23182019-12-20 18:44:56 -08001800 for (size_t i = 0; i < pointerCount; i++) {
1801 pointerProperties[i].clear();
1802 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001803 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001804
chaviwd1c23182019-12-20 18:44:56 -08001805 pointerCoords[i].clear();
1806 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1807 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1808 }
Jackal Guof9696682018-10-05 12:23:23 +08001809
1810 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1811 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001812 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001813 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1814 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001815 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1816 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001817 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1818 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001819
1820 return args;
1821}
1822
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001823static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1824 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1825}
1826
chaviwd1c23182019-12-20 18:44:56 -08001827static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1828 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1829}
1830
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001831static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1832 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001833 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001834}
1835
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001836/**
1837 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1838 * broken channel.
1839 */
1840TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1841 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1842 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001843 sp<FakeWindowHandle>::make(application, mDispatcher,
1844 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001845
1846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1847
1848 // Window closes its channel, but the window remains.
1849 window->destroyReceiver();
1850 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1851}
1852
Arthur Hungb92218b2018-08-14 12:00:21 +08001853TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001854 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001855 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1856 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001857
Arthur Hung72d8dc32020-03-28 00:48:39 +00001858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001859 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1860 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1861 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001862
1863 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001864 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001865}
1866
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001867TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1868 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001869 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1870 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001871
1872 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1873 // Inject a MotionEvent to an unknown display.
1874 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1875 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1876 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1877
1878 // Window should receive motion event.
1879 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1880}
1881
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001882/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001883 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001884 * This test serves as a sanity check for the next test, where setInputWindows is
1885 * called twice.
1886 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001887TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001888 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001889 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1890 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001891 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001892
1893 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001894 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001895 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1896 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001897 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001898
1899 // Window should receive motion event.
1900 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1901}
1902
1903/**
1904 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001905 */
1906TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001907 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001908 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1909 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001910 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001911
1912 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001914 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001915 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1916 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001917 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001918
1919 // Window should receive motion event.
1920 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1921}
1922
Arthur Hungb92218b2018-08-14 12:00:21 +08001923// The foreground window should receive the first touch down event.
1924TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001925 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001926 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001927 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001928 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001929 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001930
Arthur Hung72d8dc32020-03-28 00:48:39 +00001931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001932 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1933 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1934 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001935
1936 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001937 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001938 windowSecond->assertNoEvents();
1939}
1940
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001941/**
1942 * Two windows: A top window, and a wallpaper behind the window.
1943 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1944 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001945 * 1. foregroundWindow <-- dup touch to wallpaper
1946 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001947 */
1948TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1949 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1950 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001951 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001952 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001953 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001954 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001955 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001956
1957 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1958 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1959 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1960 {100, 200}))
1961 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1962
1963 // Both foreground window and its wallpaper should receive the touch down
1964 foregroundWindow->consumeMotionDown();
1965 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1966
1967 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1968 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1969 ADISPLAY_ID_DEFAULT, {110, 200}))
1970 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1971
1972 foregroundWindow->consumeMotionMove();
1973 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1974
1975 // Now the foreground window goes away, but the wallpaper stays
1976 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1977 foregroundWindow->consumeMotionCancel();
1978 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1979 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1980}
1981
1982/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001983 * Two fingers down on the window, and lift off the first finger.
1984 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1985 * contains a single pointer.
1986 */
1987TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1988 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1989 sp<FakeWindowHandle> window =
1990 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1991
1992 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001993 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001994 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1995 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1996 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001997 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00001998 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1999 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2000 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
2001 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002002 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002003 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2004 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2005 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
2006 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002007 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2008 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2009 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2010
2011 // Remove the window. The gesture should be canceled
2012 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2013 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
2014 window->consumeMotionEvent(
2015 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
2016}
2017
2018/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002019 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
2020 * with the following differences:
2021 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
2022 * clean up the connection.
2023 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
2024 * Ensure that there's no crash in the dispatcher.
2025 */
2026TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
2027 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2028 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002029 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002030 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002031 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002032 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002033 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002034
2035 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
2036 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2037 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2038 {100, 200}))
2039 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2040
2041 // Both foreground window and its wallpaper should receive the touch down
2042 foregroundWindow->consumeMotionDown();
2043 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2044
2045 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2046 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2047 ADISPLAY_ID_DEFAULT, {110, 200}))
2048 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2049
2050 foregroundWindow->consumeMotionMove();
2051 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2052
2053 // Wallpaper closes its channel, but the window remains.
2054 wallpaperWindow->destroyReceiver();
2055 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
2056
2057 // Now the foreground window goes away, but the wallpaper stays, even though its channel
2058 // is no longer valid.
2059 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
2060 foregroundWindow->consumeMotionCancel();
2061}
2062
Arthur Hungc539dbb2022-12-08 07:45:36 +00002063class ShouldSplitTouchFixture : public InputDispatcherTest,
2064 public ::testing::WithParamInterface<bool> {};
2065INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
2066 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002067/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002068 * A single window that receives touch (on top), and a wallpaper window underneath it.
2069 * The top window gets a multitouch gesture.
2070 * Ensure that wallpaper gets the same gesture.
2071 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002072TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002073 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002074 sp<FakeWindowHandle> foregroundWindow =
2075 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2076 foregroundWindow->setDupTouchToWallpaper(true);
2077 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002078
2079 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002080 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002081 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002082
Arthur Hungc539dbb2022-12-08 07:45:36 +00002083 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002084
2085 // Touch down on top window
2086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2087 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2088 {100, 100}))
2089 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2090
2091 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002092 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002093 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2094
2095 // Second finger down on the top window
2096 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002097 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002098 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002099 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2100 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002101 .build();
2102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2103 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2104 InputEventInjectionSync::WAIT_FOR_RESULT))
2105 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2106
Harry Cutts33476232023-01-30 19:57:29 +00002107 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2108 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002109 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002110
2111 const MotionEvent secondFingerUpEvent =
2112 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2113 .displayId(ADISPLAY_ID_DEFAULT)
2114 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002115 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2116 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002117 .build();
2118 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2119 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
2120 InputEventInjectionSync::WAIT_FOR_RESULT))
2121 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2122 foregroundWindow->consumeMotionPointerUp(0);
2123 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2124
2125 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002126 injectMotionEvent(mDispatcher,
2127 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2128 AINPUT_SOURCE_TOUCHSCREEN)
2129 .displayId(ADISPLAY_ID_DEFAULT)
2130 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2131 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002132 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002133 .x(100)
2134 .y(100))
2135 .build(),
2136 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2138 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2139 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002140}
2141
2142/**
2143 * Two windows: a window on the left and window on the right.
2144 * A third window, wallpaper, is behind both windows, and spans both top windows.
2145 * The first touch down goes to the left window. A second pointer touches down on the right window.
2146 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2147 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2148 * ACTION_POINTER_DOWN(1).
2149 */
2150TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2151 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2152 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002153 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002154 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002155 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002156
2157 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002158 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002159 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002160 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002161
2162 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002163 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002164 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002165 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002166
2167 mDispatcher->setInputWindows(
2168 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
2169
2170 // Touch down on left window
2171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2172 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2173 {100, 100}))
2174 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2175
2176 // Both foreground window and its wallpaper should receive the touch down
2177 leftWindow->consumeMotionDown();
2178 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2179
2180 // Second finger down on the right window
2181 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002182 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002183 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002184 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2185 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002186 .build();
2187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2188 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2189 InputEventInjectionSync::WAIT_FOR_RESULT))
2190 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2191
2192 leftWindow->consumeMotionMove();
2193 // Since the touch is split, right window gets ACTION_DOWN
2194 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002195 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002196 expectedWallpaperFlags);
2197
2198 // Now, leftWindow, which received the first finger, disappears.
2199 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2200 leftWindow->consumeMotionCancel();
2201 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2202 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2203
2204 // The pointer that's still down on the right window moves, and goes to the right window only.
2205 // As far as the dispatcher's concerned though, both pointers are still present.
2206 const MotionEvent secondFingerMoveEvent =
2207 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2208 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002209 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2210 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002211 .build();
2212 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2213 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2214 InputEventInjectionSync::WAIT_FOR_RESULT));
2215 rightWindow->consumeMotionMove();
2216
2217 leftWindow->assertNoEvents();
2218 rightWindow->assertNoEvents();
2219 wallpaperWindow->assertNoEvents();
2220}
2221
Arthur Hungc539dbb2022-12-08 07:45:36 +00002222/**
2223 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2224 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2225 * The right window should receive ACTION_DOWN.
2226 */
2227TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002228 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002229 sp<FakeWindowHandle> leftWindow =
2230 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2231 leftWindow->setFrame(Rect(0, 0, 200, 200));
2232 leftWindow->setDupTouchToWallpaper(true);
2233 leftWindow->setSlippery(true);
2234
2235 sp<FakeWindowHandle> rightWindow =
2236 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2237 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002238
2239 sp<FakeWindowHandle> wallpaperWindow =
2240 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2241 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002242
Arthur Hungc539dbb2022-12-08 07:45:36 +00002243 mDispatcher->setInputWindows(
2244 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002245
Arthur Hungc539dbb2022-12-08 07:45:36 +00002246 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2248 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002249 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002250 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002251
2252 // Both foreground window and its wallpaper should receive the touch down
2253 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002254 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2255
Arthur Hungc539dbb2022-12-08 07:45:36 +00002256 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002257 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002258 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2259 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002260 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2261
Arthur Hungc539dbb2022-12-08 07:45:36 +00002262 leftWindow->consumeMotionCancel();
2263 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2264 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002265}
2266
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002267/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002268 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2269 * interactive, it might stop sending this flag.
2270 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2271 * to have a consistent input stream.
2272 *
2273 * Test procedure:
2274 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2275 * DOWN (new gesture).
2276 *
2277 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2278 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2279 *
2280 * We technically just need a single window here, but we are using two windows (spy on top and a
2281 * regular window below) to emulate the actual situation where it happens on the device.
2282 */
2283TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2284 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2285 sp<FakeWindowHandle> spyWindow =
2286 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2287 spyWindow->setFrame(Rect(0, 0, 200, 200));
2288 spyWindow->setTrustedOverlay(true);
2289 spyWindow->setSpy(true);
2290
2291 sp<FakeWindowHandle> window =
2292 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2293 window->setFrame(Rect(0, 0, 200, 200));
2294
2295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2296 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002297
2298 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002299 mDispatcher->notifyMotion(
2300 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2301 .deviceId(touchDeviceId)
2302 .policyFlags(DEFAULT_POLICY_FLAGS)
2303 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2304 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002305
Prabir Pradhan678438e2023-04-13 19:32:51 +00002306 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2307 .deviceId(touchDeviceId)
2308 .policyFlags(DEFAULT_POLICY_FLAGS)
2309 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2310 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2311 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002312 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2313 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2314 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2315 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2316
2317 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002318 mDispatcher->notifyMotion(
2319 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2320 .deviceId(touchDeviceId)
2321 .policyFlags(0)
2322 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2323 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2324 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002325 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2326 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2327
2328 // We don't need to reset the device to reproduce the issue, but the reset event typically
2329 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002330 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002331
2332 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002333 mDispatcher->notifyMotion(
2334 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2335 .deviceId(touchDeviceId)
2336 .policyFlags(DEFAULT_POLICY_FLAGS)
2337 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2338 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002339 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2340 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2341
2342 // No more events
2343 spyWindow->assertNoEvents();
2344 window->assertNoEvents();
2345}
2346
2347/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002348 * Two windows: a window on the left and a window on the right.
2349 * Mouse is hovered from the right window into the left window.
2350 * Next, we tap on the left window, where the cursor was last seen.
2351 * The second tap is done onto the right window.
2352 * The mouse and tap are from two different devices.
2353 * We technically don't need to set the downtime / eventtime for these events, but setting these
2354 * explicitly helps during debugging.
2355 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2356 * In the buggy implementation, a tap on the right window would cause a crash.
2357 */
2358TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2359 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2360 sp<FakeWindowHandle> leftWindow =
2361 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2362 leftWindow->setFrame(Rect(0, 0, 200, 200));
2363
2364 sp<FakeWindowHandle> rightWindow =
2365 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2366 rightWindow->setFrame(Rect(200, 0, 400, 200));
2367
2368 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2369 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2370 // stale.
2371 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2372 const int32_t mouseDeviceId = 6;
2373 const int32_t touchDeviceId = 4;
2374 // Move the cursor from right
2375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2376 injectMotionEvent(mDispatcher,
2377 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2378 AINPUT_SOURCE_MOUSE)
2379 .deviceId(mouseDeviceId)
2380 .downTime(baseTime + 10)
2381 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002382 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002383 .x(300)
2384 .y(100))
2385 .build()));
2386 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2387
2388 // .. to the left window
2389 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2390 injectMotionEvent(mDispatcher,
2391 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2392 AINPUT_SOURCE_MOUSE)
2393 .deviceId(mouseDeviceId)
2394 .downTime(baseTime + 10)
2395 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002396 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002397 .x(110)
2398 .y(100))
2399 .build()));
2400 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2401 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2402 // Now tap the left window
2403 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2404 injectMotionEvent(mDispatcher,
2405 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2406 AINPUT_SOURCE_TOUCHSCREEN)
2407 .deviceId(touchDeviceId)
2408 .downTime(baseTime + 40)
2409 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002410 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002411 .x(100)
2412 .y(100))
2413 .build()));
2414 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2415 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2416
2417 // release tap
2418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2419 injectMotionEvent(mDispatcher,
2420 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2421 AINPUT_SOURCE_TOUCHSCREEN)
2422 .deviceId(touchDeviceId)
2423 .downTime(baseTime + 40)
2424 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002425 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002426 .x(100)
2427 .y(100))
2428 .build()));
2429 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2430
2431 // Tap the window on the right
2432 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2433 injectMotionEvent(mDispatcher,
2434 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2435 AINPUT_SOURCE_TOUCHSCREEN)
2436 .deviceId(touchDeviceId)
2437 .downTime(baseTime + 60)
2438 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002439 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002440 .x(300)
2441 .y(100))
2442 .build()));
2443 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2444
2445 // release tap
2446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2447 injectMotionEvent(mDispatcher,
2448 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2449 AINPUT_SOURCE_TOUCHSCREEN)
2450 .deviceId(touchDeviceId)
2451 .downTime(baseTime + 60)
2452 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002453 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002454 .x(300)
2455 .y(100))
2456 .build()));
2457 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2458
2459 // No more events
2460 leftWindow->assertNoEvents();
2461 rightWindow->assertNoEvents();
2462}
2463
2464/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002465 * Start hovering in a window. While this hover is still active, make another window appear on top.
2466 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2467 * While the top window is present, the hovering is stopped.
2468 * Later, hovering gets resumed again.
2469 * Ensure that new hover gesture is handled correctly.
2470 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2471 * to the window that's currently being hovered over.
2472 */
2473TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2474 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2475 sp<FakeWindowHandle> window =
2476 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2477 window->setFrame(Rect(0, 0, 200, 200));
2478
2479 // Only a single window is present at first
2480 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2481
2482 // Start hovering in the window
2483 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2484 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2485 .build());
2486 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2487
2488 // Now, an obscuring window appears!
2489 sp<FakeWindowHandle> obscuringWindow =
2490 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2491 ADISPLAY_ID_DEFAULT,
2492 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2493 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2494 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2495 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2496 obscuringWindow->setNoInputChannel(true);
2497 obscuringWindow->setFocusable(false);
2498 obscuringWindow->setAlpha(1.0);
2499 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2500
2501 // While this new obscuring window is present, the hovering is stopped
2502 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2503 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2504 .build());
2505 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2506
2507 // Now the obscuring window goes away.
2508 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2509
2510 // And a new hover gesture starts.
2511 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2512 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2513 .build());
2514 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2515}
2516
2517/**
2518 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2519 * the obscuring window.
2520 */
2521TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2522 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2523 sp<FakeWindowHandle> window =
2524 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2525 window->setFrame(Rect(0, 0, 200, 200));
2526
2527 // Only a single window is present at first
2528 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2529
2530 // Start hovering in the window
2531 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2532 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2533 .build());
2534 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2535
2536 // Now, an obscuring window appears!
2537 sp<FakeWindowHandle> obscuringWindow =
2538 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2539 ADISPLAY_ID_DEFAULT,
2540 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2541 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2542 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2543 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2544 obscuringWindow->setNoInputChannel(true);
2545 obscuringWindow->setFocusable(false);
2546 obscuringWindow->setAlpha(1.0);
2547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2548
2549 // While this new obscuring window is present, the hovering continues. The event can't go to the
2550 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2551 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2552 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2553 .build());
2554 obscuringWindow->assertNoEvents();
2555 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2556
2557 // Now the obscuring window goes away.
2558 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2559
2560 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2561 // so it should generate a HOVER_ENTER
2562 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2563 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2564 .build());
2565 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2566
2567 // Now the MOVE should be getting dispatched normally
2568 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2569 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2570 .build());
2571 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2572}
2573
2574/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002575 * Two windows: a window on the left and a window on the right.
2576 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2577 * down. Then, on the left window, also place second touch pointer down.
2578 * This test tries to reproduce a crash.
2579 * In the buggy implementation, second pointer down on the left window would cause a crash.
2580 */
2581TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2582 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2583 sp<FakeWindowHandle> leftWindow =
2584 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2585 leftWindow->setFrame(Rect(0, 0, 200, 200));
2586
2587 sp<FakeWindowHandle> rightWindow =
2588 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2589 rightWindow->setFrame(Rect(200, 0, 400, 200));
2590
2591 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2592
2593 const int32_t touchDeviceId = 4;
2594 const int32_t mouseDeviceId = 6;
2595 NotifyMotionArgs args;
2596
2597 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002598 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2599 .deviceId(mouseDeviceId)
2600 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2601 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002602 leftWindow->consumeMotionEvent(
2603 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2604
2605 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002606 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2607 .deviceId(mouseDeviceId)
2608 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2609 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2610 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002611
2612 leftWindow->consumeMotionEvent(
2613 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2614 leftWindow->consumeMotionEvent(
2615 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2616
Prabir Pradhan678438e2023-04-13 19:32:51 +00002617 mDispatcher->notifyMotion(
2618 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2619 .deviceId(mouseDeviceId)
2620 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2621 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2622 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2623 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002624 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2625
2626 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002627 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2628 .deviceId(touchDeviceId)
2629 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2630 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002631 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2632
2633 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2634
2635 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002636 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2637 .deviceId(touchDeviceId)
2638 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2639 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2640 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002641 leftWindow->consumeMotionEvent(
2642 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2643 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2644 // current implementation.
2645 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2646 rightWindow->consumeMotionEvent(
2647 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2648
2649 leftWindow->assertNoEvents();
2650 rightWindow->assertNoEvents();
2651}
2652
2653/**
2654 * On a single window, use two different devices: mouse and touch.
2655 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2656 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2657 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2658 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2659 * represent a new gesture.
2660 */
2661TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2662 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2663 sp<FakeWindowHandle> window =
2664 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2665 window->setFrame(Rect(0, 0, 400, 400));
2666
2667 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2668
2669 const int32_t touchDeviceId = 4;
2670 const int32_t mouseDeviceId = 6;
2671 NotifyMotionArgs args;
2672
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002673 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002674 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2675 .deviceId(touchDeviceId)
2676 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2677 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002678 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002679 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2680 .deviceId(touchDeviceId)
2681 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2682 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2683 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002684 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002685 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2686 .deviceId(touchDeviceId)
2687 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2688 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2689 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002690 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2691 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2692 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2693
2694 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002695 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2696 .deviceId(mouseDeviceId)
2697 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2698 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2699 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002700
2701 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002702 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002703 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2704
Prabir Pradhan678438e2023-04-13 19:32:51 +00002705 mDispatcher->notifyMotion(
2706 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2707 .deviceId(mouseDeviceId)
2708 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2709 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2710 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2711 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002712 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2713
2714 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002715 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2716 .deviceId(touchDeviceId)
2717 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2718 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2719 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002720 // The pointer_down event should be ignored
2721 window->assertNoEvents();
2722}
2723
2724/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002725 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2726 * the injected event.
2727 */
2728TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2729 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2730 sp<FakeWindowHandle> window =
2731 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2732 window->setFrame(Rect(0, 0, 400, 400));
2733
2734 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2735
2736 const int32_t touchDeviceId = 4;
2737 NotifyMotionArgs args;
2738 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2739 // completion.
2740 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2741 injectMotionEvent(mDispatcher,
2742 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2743 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002744 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002745 .x(50)
2746 .y(50))
2747 .build()));
2748 window->consumeMotionEvent(
2749 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2750
2751 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2752 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002753 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2754 .deviceId(touchDeviceId)
2755 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2756 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002757
2758 window->consumeMotionEvent(
2759 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2760 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2761}
2762
2763/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002764 * This test is similar to the test above, but the sequence of injected events is different.
2765 *
2766 * Two windows: a window on the left and a window on the right.
2767 * Mouse is hovered over the left window.
2768 * Next, we tap on the left window, where the cursor was last seen.
2769 *
2770 * After that, we inject one finger down onto the right window, and then a second finger down onto
2771 * the left window.
2772 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2773 * window (first), and then another on the left window (second).
2774 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2775 * In the buggy implementation, second finger down on the left window would cause a crash.
2776 */
2777TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2778 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2779 sp<FakeWindowHandle> leftWindow =
2780 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2781 leftWindow->setFrame(Rect(0, 0, 200, 200));
2782
2783 sp<FakeWindowHandle> rightWindow =
2784 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2785 rightWindow->setFrame(Rect(200, 0, 400, 200));
2786
2787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2788
2789 const int32_t mouseDeviceId = 6;
2790 const int32_t touchDeviceId = 4;
2791 // Hover over the left window. Keep the cursor there.
2792 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2793 injectMotionEvent(mDispatcher,
2794 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2795 AINPUT_SOURCE_MOUSE)
2796 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002797 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002798 .x(50)
2799 .y(50))
2800 .build()));
2801 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2802
2803 // Tap on left window
2804 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2805 injectMotionEvent(mDispatcher,
2806 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2807 AINPUT_SOURCE_TOUCHSCREEN)
2808 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002809 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002810 .x(100)
2811 .y(100))
2812 .build()));
2813
2814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2815 injectMotionEvent(mDispatcher,
2816 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2817 AINPUT_SOURCE_TOUCHSCREEN)
2818 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002819 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002820 .x(100)
2821 .y(100))
2822 .build()));
2823 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2824 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2825 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2826
2827 // First finger down on right window
2828 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2829 injectMotionEvent(mDispatcher,
2830 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2831 AINPUT_SOURCE_TOUCHSCREEN)
2832 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002833 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002834 .x(300)
2835 .y(100))
2836 .build()));
2837 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2838
2839 // Second finger down on the left window
2840 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2841 injectMotionEvent(mDispatcher,
2842 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2843 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002844 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002845 .x(300)
2846 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002847 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002848 .x(100)
2849 .y(100))
2850 .build()));
2851 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2852 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2853
2854 // No more events
2855 leftWindow->assertNoEvents();
2856 rightWindow->assertNoEvents();
2857}
2858
2859/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002860 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2861 * While the touch is down, new hover events from the stylus device should be ignored. After the
2862 * touch is gone, stylus hovering should start working again.
2863 */
2864TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2865 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2866 sp<FakeWindowHandle> window =
2867 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2868 window->setFrame(Rect(0, 0, 200, 200));
2869
2870 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2871
2872 const int32_t stylusDeviceId = 5;
2873 const int32_t touchDeviceId = 4;
2874 // Start hovering with stylus
2875 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2876 injectMotionEvent(mDispatcher,
2877 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2878 AINPUT_SOURCE_STYLUS)
2879 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002880 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002881 .x(50)
2882 .y(50))
2883 .build()));
2884 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2885
2886 // Finger down on the window
2887 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2888 injectMotionEvent(mDispatcher,
2889 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2890 AINPUT_SOURCE_TOUCHSCREEN)
2891 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002892 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002893 .x(100)
2894 .y(100))
2895 .build()));
2896 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2897 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2898
2899 // Try to continue hovering with stylus. Since we are already down, injection should fail
2900 ASSERT_EQ(InputEventInjectionResult::FAILED,
2901 injectMotionEvent(mDispatcher,
2902 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2903 AINPUT_SOURCE_STYLUS)
2904 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002905 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002906 .x(50)
2907 .y(50))
2908 .build()));
2909 // No event should be sent. This event should be ignored because a pointer from another device
2910 // is already down.
2911
2912 // Lift up the finger
2913 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2914 injectMotionEvent(mDispatcher,
2915 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2916 AINPUT_SOURCE_TOUCHSCREEN)
2917 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002918 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002919 .x(100)
2920 .y(100))
2921 .build()));
2922 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2923
2924 // Now that the touch is gone, stylus hovering should start working again
2925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2926 injectMotionEvent(mDispatcher,
2927 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2928 AINPUT_SOURCE_STYLUS)
2929 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002930 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002931 .x(50)
2932 .y(50))
2933 .build()));
2934 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2935 // No more events
2936 window->assertNoEvents();
2937}
2938
2939/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002940 * A spy window above a window with no input channel.
2941 * Start hovering with a stylus device, and then tap with it.
2942 * Ensure spy window receives the entire sequence.
2943 */
2944TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2945 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2946 sp<FakeWindowHandle> spyWindow =
2947 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2948 spyWindow->setFrame(Rect(0, 0, 200, 200));
2949 spyWindow->setTrustedOverlay(true);
2950 spyWindow->setSpy(true);
2951 sp<FakeWindowHandle> window =
2952 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2953 window->setNoInputChannel(true);
2954 window->setFrame(Rect(0, 0, 200, 200));
2955
2956 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2957
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002958 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002959 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2960 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2961 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002962 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2963 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002964 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2965 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2966 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002967 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2968
2969 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002970 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2971 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2972 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002973 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2974
2975 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002976 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2977 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2978 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002979 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2980
2981 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002982 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2983 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2984 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002985 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2986 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002987 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2988 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2989 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002990 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2991
2992 // No more events
2993 spyWindow->assertNoEvents();
2994 window->assertNoEvents();
2995}
2996
2997/**
2998 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
2999 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3000 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3001 * While the mouse is down, new move events from the touch device should be ignored.
3002 */
3003TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3004 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3005 sp<FakeWindowHandle> spyWindow =
3006 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3007 spyWindow->setFrame(Rect(0, 0, 200, 200));
3008 spyWindow->setTrustedOverlay(true);
3009 spyWindow->setSpy(true);
3010 sp<FakeWindowHandle> window =
3011 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3012 window->setFrame(Rect(0, 0, 200, 200));
3013
3014 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3015
3016 const int32_t mouseDeviceId = 7;
3017 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003018
3019 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003020 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3021 .deviceId(mouseDeviceId)
3022 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3023 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003024 spyWindow->consumeMotionEvent(
3025 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3026 window->consumeMotionEvent(
3027 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3028
3029 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003030 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3031 .deviceId(touchDeviceId)
3032 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3033 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003034 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3035 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3036 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3037 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3038
Prabir Pradhan678438e2023-04-13 19:32:51 +00003039 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3040 .deviceId(touchDeviceId)
3041 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3042 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003043 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3044 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3045
3046 // Pilfer the stream
3047 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3048 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3049
Prabir Pradhan678438e2023-04-13 19:32:51 +00003050 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3051 .deviceId(touchDeviceId)
3052 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3053 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003054 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3055
3056 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003057 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3058 .deviceId(mouseDeviceId)
3059 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3060 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3061 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003062
3063 spyWindow->consumeMotionEvent(
3064 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3065 spyWindow->consumeMotionEvent(
3066 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3067 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3068
Prabir Pradhan678438e2023-04-13 19:32:51 +00003069 mDispatcher->notifyMotion(
3070 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3071 .deviceId(mouseDeviceId)
3072 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3073 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3074 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3075 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003076 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3077 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3078
3079 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003080 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3081 .deviceId(mouseDeviceId)
3082 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3083 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3084 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003085 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3086 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3087
3088 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003089 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3090 .deviceId(touchDeviceId)
3091 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3092 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003093
3094 // No more events
3095 spyWindow->assertNoEvents();
3096 window->assertNoEvents();
3097}
3098
3099/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003100 * On the display, have a single window, and also an area where there's no window.
3101 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
3102 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
3103 */
3104TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3105 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3106 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003107 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003108
3109 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003110
3111 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00003112 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003113
3114 mDispatcher->waitForIdle();
3115 window->assertNoEvents();
3116
3117 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003118 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003119 mDispatcher->waitForIdle();
3120 window->consumeMotionDown();
3121}
3122
3123/**
3124 * Same test as above, but instead of touching the empty space, the first touch goes to
3125 * non-touchable window.
3126 */
3127TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3128 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3129 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003130 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003131 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3132 window1->setTouchable(false);
3133 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003134 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003135 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3136
3137 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3138
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003139 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003140 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003141
3142 mDispatcher->waitForIdle();
3143 window1->assertNoEvents();
3144 window2->assertNoEvents();
3145
3146 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003147 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003148 mDispatcher->waitForIdle();
3149 window2->consumeMotionDown();
3150}
3151
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003152/**
3153 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3154 * to the event time of the first ACTION_DOWN sent to the particular window.
3155 */
3156TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3157 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3158 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003159 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003160 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3161 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003162 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003163 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3164
3165 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3166
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003167 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003168 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003169
3170 mDispatcher->waitForIdle();
3171 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003172 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003173 window2->assertNoEvents();
3174 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
3175 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
3176 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
3177
3178 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003179 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003180 mDispatcher->waitForIdle();
3181 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003182 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003183 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
3184 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
3185 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
3186 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
3187
3188 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003189 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003190 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003191 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003192
3193 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003194 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003195 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003196 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003197
3198 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3199 window1->consumeMotionMove();
3200 window1->assertNoEvents();
3201
3202 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003203 mDispatcher->notifyMotion(
3204 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003205 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003206 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003207
Prabir Pradhan678438e2023-04-13 19:32:51 +00003208 mDispatcher->notifyMotion(
3209 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003210 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003211 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003212}
3213
Garfield Tandf26e862020-07-01 20:18:19 -07003214TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003215 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003216 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003217 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003218 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003219 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003220 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003221 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003222
3223 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3224
3225 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3226
3227 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003228 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003229 injectMotionEvent(mDispatcher,
3230 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3231 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003232 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003233 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003234 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003235
3236 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003238 injectMotionEvent(mDispatcher,
3239 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3240 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003241 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003242 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003243 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3244 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003245
3246 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003247 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003248 injectMotionEvent(mDispatcher,
3249 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3250 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003251 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003252 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003253 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3254 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003255
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003257 injectMotionEvent(mDispatcher,
3258 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3259 AINPUT_SOURCE_MOUSE)
3260 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3261 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003262 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003263 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003264 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003265
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003266 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003267 injectMotionEvent(mDispatcher,
3268 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3269 AINPUT_SOURCE_MOUSE)
3270 .buttonState(0)
3271 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003272 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003273 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003274 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003275
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003277 injectMotionEvent(mDispatcher,
3278 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3279 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003280 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003281 .build()));
3282 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3283
3284 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003286 injectMotionEvent(mDispatcher,
3287 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3288 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003289 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003290 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003291 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003292
3293 // No more events
3294 windowLeft->assertNoEvents();
3295 windowRight->assertNoEvents();
3296}
3297
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003298/**
3299 * Put two fingers down (and don't release them) and click the mouse button.
3300 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3301 * currently active gesture should be canceled, and the new one should proceed.
3302 */
3303TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3304 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3305 sp<FakeWindowHandle> window =
3306 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3307 window->setFrame(Rect(0, 0, 600, 800));
3308
3309 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3310
3311 const int32_t touchDeviceId = 4;
3312 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003313
3314 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003315 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3316 .deviceId(touchDeviceId)
3317 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3318 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003319
Prabir Pradhan678438e2023-04-13 19:32:51 +00003320 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3321 .deviceId(touchDeviceId)
3322 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3323 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3324 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003325 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3326 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3327
3328 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003329 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3330 .deviceId(mouseDeviceId)
3331 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3332 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3333 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003334 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3335 WithPointerCount(2u)));
3336 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3337
Prabir Pradhan678438e2023-04-13 19:32:51 +00003338 mDispatcher->notifyMotion(
3339 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3340 .deviceId(mouseDeviceId)
3341 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3342 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3343 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3344 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003345 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3346
3347 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3348 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003349 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3350 .deviceId(touchDeviceId)
3351 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3352 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3353 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003354 window->assertNoEvents();
3355}
3356
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003357TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3358 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3359
3360 sp<FakeWindowHandle> spyWindow =
3361 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3362 spyWindow->setFrame(Rect(0, 0, 600, 800));
3363 spyWindow->setTrustedOverlay(true);
3364 spyWindow->setSpy(true);
3365 sp<FakeWindowHandle> window =
3366 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3367 window->setFrame(Rect(0, 0, 600, 800));
3368
3369 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3371
3372 // Send mouse cursor to the window
3373 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3374 injectMotionEvent(mDispatcher,
3375 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3376 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003377 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003378 .x(100)
3379 .y(100))
3380 .build()));
3381
3382 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3383 WithSource(AINPUT_SOURCE_MOUSE)));
3384 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3385 WithSource(AINPUT_SOURCE_MOUSE)));
3386
3387 window->assertNoEvents();
3388 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003389}
3390
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003391TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3392 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3393
3394 sp<FakeWindowHandle> spyWindow =
3395 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3396 spyWindow->setFrame(Rect(0, 0, 600, 800));
3397 spyWindow->setTrustedOverlay(true);
3398 spyWindow->setSpy(true);
3399 sp<FakeWindowHandle> window =
3400 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3401 window->setFrame(Rect(0, 0, 600, 800));
3402
3403 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3404 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3405
3406 // Send mouse cursor to the window
3407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3408 injectMotionEvent(mDispatcher,
3409 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3410 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003411 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003412 .x(100)
3413 .y(100))
3414 .build()));
3415
3416 // Move mouse cursor
3417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3418 injectMotionEvent(mDispatcher,
3419 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3420 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003421 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003422 .x(110)
3423 .y(110))
3424 .build()));
3425
3426 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3427 WithSource(AINPUT_SOURCE_MOUSE)));
3428 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3429 WithSource(AINPUT_SOURCE_MOUSE)));
3430 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3431 WithSource(AINPUT_SOURCE_MOUSE)));
3432 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3433 WithSource(AINPUT_SOURCE_MOUSE)));
3434 // Touch down on the window
3435 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3436 injectMotionEvent(mDispatcher,
3437 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3438 AINPUT_SOURCE_TOUCHSCREEN)
3439 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003440 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003441 .x(200)
3442 .y(200))
3443 .build()));
3444 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3445 WithSource(AINPUT_SOURCE_MOUSE)));
3446 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3447 WithSource(AINPUT_SOURCE_MOUSE)));
3448 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3449 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3450 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3451 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3452
3453 // pilfer the motion, retaining the gesture on the spy window.
3454 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3455 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3456 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3457
3458 // Touch UP on the window
3459 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3460 injectMotionEvent(mDispatcher,
3461 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3462 AINPUT_SOURCE_TOUCHSCREEN)
3463 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003464 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003465 .x(200)
3466 .y(200))
3467 .build()));
3468 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3469 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3470
3471 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3472 // to send a new gesture. It should again go to both windows (spy and the window below), just
3473 // like the first gesture did, before pilfering. The window configuration has not changed.
3474
3475 // One more tap - DOWN
3476 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3477 injectMotionEvent(mDispatcher,
3478 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3479 AINPUT_SOURCE_TOUCHSCREEN)
3480 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003481 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003482 .x(250)
3483 .y(250))
3484 .build()));
3485 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3486 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3487 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3488 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3489
3490 // Touch UP on the window
3491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3492 injectMotionEvent(mDispatcher,
3493 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3494 AINPUT_SOURCE_TOUCHSCREEN)
3495 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003496 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003497 .x(250)
3498 .y(250))
3499 .build()));
3500 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3501 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3502 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3503 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3504
3505 window->assertNoEvents();
3506 spyWindow->assertNoEvents();
3507}
3508
Garfield Tandf26e862020-07-01 20:18:19 -07003509// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3510// directly in this test.
3511TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003512 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003513 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003514 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003515 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003516
3517 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3518
3519 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3520
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003521 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003522 injectMotionEvent(mDispatcher,
3523 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3524 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003525 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003526 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003527 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003528 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003529 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003530 injectMotionEvent(mDispatcher,
3531 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3532 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003533 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003534 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003535 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3536 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003537
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003539 injectMotionEvent(mDispatcher,
3540 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3541 AINPUT_SOURCE_MOUSE)
3542 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3543 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003544 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003545 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003546 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003547
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003549 injectMotionEvent(mDispatcher,
3550 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3551 AINPUT_SOURCE_MOUSE)
3552 .buttonState(0)
3553 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003554 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003555 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003556 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003557
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003558 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003559 injectMotionEvent(mDispatcher,
3560 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3561 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003562 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003563 .build()));
3564 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3565
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003566 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3567 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3568 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003569 injectMotionEvent(mDispatcher,
3570 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3571 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003572 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003573 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003574 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003575}
3576
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003577/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003578 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3579 * is generated.
3580 */
3581TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3582 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3583 sp<FakeWindowHandle> window =
3584 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3585 window->setFrame(Rect(0, 0, 1200, 800));
3586
3587 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3588
3589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3590
3591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3592 injectMotionEvent(mDispatcher,
3593 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3594 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003595 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003596 .x(300)
3597 .y(400))
3598 .build()));
3599 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3600
3601 // Remove the window, but keep the channel.
3602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3603 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3604}
3605
3606/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003607 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3608 */
3609TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3610 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3611 sp<FakeWindowHandle> window =
3612 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3613 window->setFrame(Rect(0, 0, 100, 100));
3614
3615 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3616
3617 const int32_t mouseDeviceId = 7;
3618 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003619
3620 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003621 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3622 .deviceId(mouseDeviceId)
3623 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3624 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003625 window->consumeMotionEvent(
3626 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3627
3628 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003629 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3630 .deviceId(touchDeviceId)
3631 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3632 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003633
3634 window->consumeMotionEvent(
3635 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3636 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3637}
3638
3639/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003640 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003641 * The tap causes a HOVER_EXIT event to be generated because the current event
3642 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003643 */
3644TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3645 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3646 sp<FakeWindowHandle> window =
3647 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3648 window->setFrame(Rect(0, 0, 100, 100));
3649
3650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3651
3652 // Inject a hover_move from mouse.
3653 NotifyMotionArgs motionArgs =
3654 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3655 ADISPLAY_ID_DEFAULT, {{50, 50}});
3656 motionArgs.xCursorPosition = 50;
3657 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003658 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003659 ASSERT_NO_FATAL_FAILURE(
3660 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3661 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003662
3663 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003664 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3665 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3666 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003667 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003668 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3669 WithSource(AINPUT_SOURCE_MOUSE))));
3670
3671 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003672 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3673 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3674
Prabir Pradhan678438e2023-04-13 19:32:51 +00003675 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3676 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003677 ASSERT_NO_FATAL_FAILURE(
3678 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3679 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3680}
3681
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003682TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3683 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3684 sp<FakeWindowHandle> windowDefaultDisplay =
3685 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3686 ADISPLAY_ID_DEFAULT);
3687 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3688 sp<FakeWindowHandle> windowSecondDisplay =
3689 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3690 SECOND_DISPLAY_ID);
3691 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3692
3693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3694 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3695
3696 // Set cursor position in window in default display and check that hover enter and move
3697 // events are generated.
3698 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3699 injectMotionEvent(mDispatcher,
3700 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3701 AINPUT_SOURCE_MOUSE)
3702 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003703 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003704 .x(300)
3705 .y(600))
3706 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003707 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003708
3709 // Remove all windows in secondary display and check that no event happens on window in
3710 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003711 mDispatcher->setInputWindows(
3712 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003713 windowDefaultDisplay->assertNoEvents();
3714
3715 // Move cursor position in window in default display and check that only hover move
3716 // event is generated and not hover enter event.
3717 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3718 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3719 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3720 injectMotionEvent(mDispatcher,
3721 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3722 AINPUT_SOURCE_MOUSE)
3723 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003724 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003725 .x(400)
3726 .y(700))
3727 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003728 windowDefaultDisplay->consumeMotionEvent(
3729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3730 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003731 windowDefaultDisplay->assertNoEvents();
3732}
3733
Garfield Tan00f511d2019-06-12 16:55:40 -07003734TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003735 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003736
3737 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003738 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003739 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003740 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003741 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003742 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003743
3744 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3745
Arthur Hung72d8dc32020-03-28 00:48:39 +00003746 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003747
3748 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3749 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003750 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003751 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003752 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003753 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003754 windowRight->assertNoEvents();
3755}
3756
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003757TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003758 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003759 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3760 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003761 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003762
Arthur Hung72d8dc32020-03-28 00:48:39 +00003763 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003764 setFocusedWindow(window);
3765
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003766 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003767
Prabir Pradhan678438e2023-04-13 19:32:51 +00003768 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003769
3770 // Window should receive key down event.
3771 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3772
3773 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3774 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003775 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003776 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003777 AKEY_EVENT_FLAG_CANCELED);
3778}
3779
3780TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003781 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003782 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3783 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003784
Arthur Hung72d8dc32020-03-28 00:48:39 +00003785 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003786
Prabir Pradhan678438e2023-04-13 19:32:51 +00003787 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3788 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003789
3790 // Window should receive motion down event.
3791 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3792
3793 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3794 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003795 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003796 window->consumeMotionEvent(
3797 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003798}
3799
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003800TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3801 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3802 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3803 "Fake Window", ADISPLAY_ID_DEFAULT);
3804
3805 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3806
3807 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3808 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3809 .build());
3810
3811 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3812
3813 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3814 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3815 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3816
3817 // After the device has been reset, a new hovering stream can be sent to the window
3818 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3819 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3820 .build());
3821 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3822}
3823
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003824TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3825 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003826 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3827 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003828 window->setFocusable(true);
3829
3830 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3831 setFocusedWindow(window);
3832
3833 window->consumeFocusEvent(true);
3834
Prabir Pradhan678438e2023-04-13 19:32:51 +00003835 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003836 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3837 const nsecs_t injectTime = keyArgs.eventTime;
3838 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003839 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003840 // The dispatching time should be always greater than or equal to intercept key timeout.
3841 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3842 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3843 std::chrono::nanoseconds(interceptKeyTimeout).count());
3844}
3845
3846TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3847 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003848 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3849 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003850 window->setFocusable(true);
3851
3852 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3853 setFocusedWindow(window);
3854
3855 window->consumeFocusEvent(true);
3856
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003857 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003858 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3859 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003860
3861 // Window should receive key event immediately when same key up.
3862 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3863 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3864}
3865
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003866/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003867 * Two windows. First is a regular window. Second does not overlap with the first, and has
3868 * WATCH_OUTSIDE_TOUCH.
3869 * Both windows are owned by the same UID.
3870 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3871 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3872 */
3873TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3874 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3875 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3876 "First Window", ADISPLAY_ID_DEFAULT);
3877 window->setFrame(Rect{0, 0, 100, 100});
3878
3879 sp<FakeWindowHandle> outsideWindow =
3880 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3881 ADISPLAY_ID_DEFAULT);
3882 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3883 outsideWindow->setWatchOutsideTouch(true);
3884 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3885 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3886
3887 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003888 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3889 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3890 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003891 window->consumeMotionDown();
3892 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3893 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3894 outsideWindow->consumeMotionEvent(
3895 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3896}
3897
3898/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003899 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3900 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3901 * ACTION_OUTSIDE event is sent per gesture.
3902 */
3903TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3904 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3905 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003906 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3907 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003908 window->setWatchOutsideTouch(true);
3909 window->setFrame(Rect{0, 0, 100, 100});
3910 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003911 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3912 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003913 secondWindow->setFrame(Rect{100, 100, 200, 200});
3914 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003915 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3916 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003917 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3918 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3919
3920 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003921 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3922 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3923 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003924 window->assertNoEvents();
3925 secondWindow->assertNoEvents();
3926
3927 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3928 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003929 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3930 ADISPLAY_ID_DEFAULT,
3931 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003932 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3933 window->consumeMotionEvent(
3934 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003935 secondWindow->consumeMotionDown();
3936 thirdWindow->assertNoEvents();
3937
3938 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3939 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003940 mDispatcher->notifyMotion(
3941 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3942 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003943 window->assertNoEvents();
3944 secondWindow->consumeMotionMove();
3945 thirdWindow->consumeMotionDown();
3946}
3947
Prabir Pradhan814fe082022-07-22 20:22:18 +00003948TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3949 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003950 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3951 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003952 window->setFocusable(true);
3953
Patrick Williamsd828f302023-04-28 17:52:08 -05003954 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003955 setFocusedWindow(window);
3956
3957 window->consumeFocusEvent(true);
3958
Prabir Pradhan678438e2023-04-13 19:32:51 +00003959 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3960 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3961 mDispatcher->notifyKey(keyDown);
3962 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003963
3964 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3965 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3966
3967 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003968 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003969
3970 window->consumeFocusEvent(false);
3971
Prabir Pradhan678438e2023-04-13 19:32:51 +00003972 mDispatcher->notifyKey(keyDown);
3973 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003974 window->assertNoEvents();
3975}
3976
Arthur Hung96483742022-11-15 03:30:48 +00003977TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3978 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3979 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3980 "Fake Window", ADISPLAY_ID_DEFAULT);
3981 // Ensure window is non-split and have some transform.
3982 window->setPreventSplitting(true);
3983 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003984 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003985
3986 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3987 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3988 {50, 50}))
3989 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3990 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3991
3992 const MotionEvent secondFingerDownEvent =
3993 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3994 .displayId(ADISPLAY_ID_DEFAULT)
3995 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003996 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3997 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00003998 .build();
3999 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4000 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
4001 InputEventInjectionSync::WAIT_FOR_RESULT))
4002 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4003
4004 const MotionEvent* event = window->consumeMotion();
4005 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4006 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4007 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4008 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4009 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
4010}
4011
Harry Cuttsb166c002023-05-09 13:06:05 +00004012TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4013 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4014 sp<FakeWindowHandle> window =
4015 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4016 window->setFrame(Rect(0, 0, 400, 400));
4017 sp<FakeWindowHandle> trustedOverlay =
4018 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4019 ADISPLAY_ID_DEFAULT);
4020 trustedOverlay->setSpy(true);
4021 trustedOverlay->setTrustedOverlay(true);
4022
4023 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
4024
4025 // Start a three-finger touchpad swipe
4026 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4027 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4028 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4029 .build());
4030 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4031 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4032 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4033 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4034 .build());
4035 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4036 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4037 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4038 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4039 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4040 .build());
4041
4042 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4043 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4044 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4045
4046 // Move the swipe a bit
4047 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4048 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4049 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4050 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4051 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4052 .build());
4053
4054 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4055
4056 // End the swipe
4057 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4058 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4059 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4060 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4061 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4062 .build());
4063 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4064 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4065 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4066 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4067 .build());
4068 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4069 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4070 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4071 .build());
4072
4073 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4074 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4075 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4076
4077 window->assertNoEvents();
4078}
4079
4080TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4081 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4082 sp<FakeWindowHandle> window =
4083 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4084 window->setFrame(Rect(0, 0, 400, 400));
4085 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4086
4087 // Start a three-finger touchpad swipe
4088 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4089 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4090 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4091 .build());
4092 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4093 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4094 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4095 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4096 .build());
4097 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4098 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4099 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4100 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4101 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4102 .build());
4103
4104 // Move the swipe a bit
4105 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4106 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4107 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4108 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4109 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4110 .build());
4111
4112 // End the swipe
4113 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4114 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4115 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4116 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4117 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4118 .build());
4119 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4120 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4121 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4122 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4123 .build());
4124 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4125 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4126 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4127 .build());
4128
4129 window->assertNoEvents();
4130}
4131
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004132/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004133 * Ensure the correct coordinate spaces are used by InputDispatcher.
4134 *
4135 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4136 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4137 * space.
4138 */
4139class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4140public:
4141 void SetUp() override {
4142 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004143 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004144 }
4145
4146 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4147 gui::DisplayInfo info;
4148 info.displayId = displayId;
4149 info.transform = transform;
4150 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004151 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004152 }
4153
4154 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4155 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004156 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004157 }
4158
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004159 void removeAllWindowsAndDisplays() {
4160 mDisplayInfos.clear();
4161 mWindowInfos.clear();
4162 }
4163
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004164 // Set up a test scenario where the display has a scaled projection and there are two windows
4165 // on the display.
4166 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4167 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4168 // respectively.
4169 ui::Transform displayTransform;
4170 displayTransform.set(2, 0, 0, 4);
4171 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4172
4173 std::shared_ptr<FakeApplicationHandle> application =
4174 std::make_shared<FakeApplicationHandle>();
4175
4176 // Add two windows to the display. Their frames are represented in the display space.
4177 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004178 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4179 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004180 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4181 addWindow(firstWindow);
4182
4183 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004184 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4185 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004186 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4187 addWindow(secondWindow);
4188 return {std::move(firstWindow), std::move(secondWindow)};
4189 }
4190
4191private:
4192 std::vector<gui::DisplayInfo> mDisplayInfos;
4193 std::vector<gui::WindowInfo> mWindowInfos;
4194};
4195
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004196TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004197 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4198 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004199 // selected so that if the hit test was performed with the point and the bounds being in
4200 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004201 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4202 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4203 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004204
4205 firstWindow->consumeMotionDown();
4206 secondWindow->assertNoEvents();
4207}
4208
4209// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4210// the event should be treated as being in the logical display space.
4211TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4212 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4213 // Send down to the first window. The point is represented in the logical display space. The
4214 // point is selected so that if the hit test was done in logical display space, then it would
4215 // end up in the incorrect window.
4216 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4217 PointF{75 * 2, 55 * 4});
4218
4219 firstWindow->consumeMotionDown();
4220 secondWindow->assertNoEvents();
4221}
4222
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004223// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4224// event should be treated as being in the logical display space.
4225TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4226 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4227
4228 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4229 ui::Transform injectedEventTransform;
4230 injectedEventTransform.set(matrix);
4231 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4232 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4233
4234 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4235 .displayId(ADISPLAY_ID_DEFAULT)
4236 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004237 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004238 .x(untransformedPoint.x)
4239 .y(untransformedPoint.y))
4240 .build();
4241 event.transform(matrix);
4242
4243 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4244 InputEventInjectionSync::WAIT_FOR_RESULT);
4245
4246 firstWindow->consumeMotionDown();
4247 secondWindow->assertNoEvents();
4248}
4249
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004250TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4251 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4252
4253 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004254 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4255 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4256 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004257
4258 firstWindow->assertNoEvents();
4259 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004260 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004261 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4262
4263 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4264 EXPECT_EQ(300, event->getRawX(0));
4265 EXPECT_EQ(880, event->getRawY(0));
4266
4267 // Ensure that the x and y values are in the window's coordinate space.
4268 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4269 // the logical display space. This will be the origin of the window space.
4270 EXPECT_EQ(100, event->getX(0));
4271 EXPECT_EQ(80, event->getY(0));
4272}
4273
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004274/** Ensure consistent behavior of InputDispatcher in all orientations. */
4275class InputDispatcherDisplayOrientationFixture
4276 : public InputDispatcherDisplayProjectionTest,
4277 public ::testing::WithParamInterface<ui::Rotation> {};
4278
4279// This test verifies the touchable region of a window for all rotations of the display by tapping
4280// in different locations on the display, specifically points close to the four corners of a
4281// window.
4282TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4283 constexpr static int32_t displayWidth = 400;
4284 constexpr static int32_t displayHeight = 800;
4285
4286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4287
4288 const auto rotation = GetParam();
4289
4290 // Set up the display with the specified rotation.
4291 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4292 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4293 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4294 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4295 logicalDisplayWidth, logicalDisplayHeight);
4296 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4297
4298 // Create a window with its bounds determined in the logical display.
4299 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4300 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4301 sp<FakeWindowHandle> window =
4302 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4303 window->setFrame(frameInDisplay, displayTransform);
4304 addWindow(window);
4305
4306 // The following points in logical display space should be inside the window.
4307 static const std::array<vec2, 4> insidePoints{
4308 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4309 for (const auto pointInsideWindow : insidePoints) {
4310 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4311 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004312 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4313 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4314 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004315 window->consumeMotionDown();
4316
Prabir Pradhan678438e2023-04-13 19:32:51 +00004317 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4318 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4319 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004320 window->consumeMotionUp();
4321 }
4322
4323 // The following points in logical display space should be outside the window.
4324 static const std::array<vec2, 5> outsidePoints{
4325 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4326 for (const auto pointOutsideWindow : outsidePoints) {
4327 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4328 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004329 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4330 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4331 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004332
Prabir Pradhan678438e2023-04-13 19:32:51 +00004333 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4334 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4335 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004336 }
4337 window->assertNoEvents();
4338}
4339
4340// Run the precision tests for all rotations.
4341INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4342 InputDispatcherDisplayOrientationFixture,
4343 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4344 ui::ROTATION_270),
4345 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4346 return ftl::enum_string(testParamInfo.param);
4347 });
4348
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004349using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4350 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004351
4352class TransferTouchFixture : public InputDispatcherTest,
4353 public ::testing::WithParamInterface<TransferFunction> {};
4354
4355TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004356 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004357
4358 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004359 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004360 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4361 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004362 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004363 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004364 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4365 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004366 sp<FakeWindowHandle> wallpaper =
4367 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4368 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004369 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004371
4372 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004373 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4374 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004375
Svet Ganov5d3bc372020-01-26 23:11:07 -08004376 // Only the first window should get the down event
4377 firstWindow->consumeMotionDown();
4378 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004379 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004380
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004381 // Transfer touch to the second window
4382 TransferFunction f = GetParam();
4383 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4384 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004385 // The first window gets cancel and the second gets down
4386 firstWindow->consumeMotionCancel();
4387 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004388 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004389
4390 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004391 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4392 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004393 // The first window gets no events and the second gets up
4394 firstWindow->assertNoEvents();
4395 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004396 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004397}
4398
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004399/**
4400 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4401 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4402 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4403 * natural to the user.
4404 * In this test, we are sending a pointer to both spy window and first window. We then try to
4405 * transfer touch to the second window. The dispatcher should identify the first window as the
4406 * one that should lose the gesture, and therefore the action should be to move the gesture from
4407 * the first window to the second.
4408 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4409 * the other API, as well.
4410 */
4411TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4412 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4413
4414 // Create a couple of windows + a spy window
4415 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004416 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004417 spyWindow->setTrustedOverlay(true);
4418 spyWindow->setSpy(true);
4419 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004420 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004421 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004422 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004423
4424 // Add the windows to the dispatcher
4425 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4426
4427 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004428 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4429 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004430 // Only the first window and spy should get the down event
4431 spyWindow->consumeMotionDown();
4432 firstWindow->consumeMotionDown();
4433
4434 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4435 // if f === 'transferTouch'.
4436 TransferFunction f = GetParam();
4437 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4438 ASSERT_TRUE(success);
4439 // The first window gets cancel and the second gets down
4440 firstWindow->consumeMotionCancel();
4441 secondWindow->consumeMotionDown();
4442
4443 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004444 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4445 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004446 // The first window gets no events and the second+spy get up
4447 firstWindow->assertNoEvents();
4448 spyWindow->consumeMotionUp();
4449 secondWindow->consumeMotionUp();
4450}
4451
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004452TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004453 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004454
4455 PointF touchPoint = {10, 10};
4456
4457 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004458 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004459 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4460 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004461 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004462 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004463 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4464 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004465 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004466
4467 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004469
4470 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004471 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4472 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4473 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004474 // Only the first window should get the down event
4475 firstWindow->consumeMotionDown();
4476 secondWindow->assertNoEvents();
4477
4478 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004479 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4480 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004481 // Only the first window should get the pointer down event
4482 firstWindow->consumeMotionPointerDown(1);
4483 secondWindow->assertNoEvents();
4484
4485 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004486 TransferFunction f = GetParam();
4487 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4488 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004489 // The first window gets cancel and the second gets down and pointer down
4490 firstWindow->consumeMotionCancel();
4491 secondWindow->consumeMotionDown();
4492 secondWindow->consumeMotionPointerDown(1);
4493
4494 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004495 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4496 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004497 // The first window gets nothing and the second gets pointer up
4498 firstWindow->assertNoEvents();
4499 secondWindow->consumeMotionPointerUp(1);
4500
4501 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004502 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4503 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004504 // The first window gets nothing and the second gets up
4505 firstWindow->assertNoEvents();
4506 secondWindow->consumeMotionUp();
4507}
4508
Arthur Hungc539dbb2022-12-08 07:45:36 +00004509TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4510 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4511
4512 // Create a couple of windows
4513 sp<FakeWindowHandle> firstWindow =
4514 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4515 ADISPLAY_ID_DEFAULT);
4516 firstWindow->setDupTouchToWallpaper(true);
4517 sp<FakeWindowHandle> secondWindow =
4518 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4519 ADISPLAY_ID_DEFAULT);
4520 secondWindow->setDupTouchToWallpaper(true);
4521
4522 sp<FakeWindowHandle> wallpaper1 =
4523 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4524 wallpaper1->setIsWallpaper(true);
4525
4526 sp<FakeWindowHandle> wallpaper2 =
4527 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4528 wallpaper2->setIsWallpaper(true);
4529 // Add the windows to the dispatcher
4530 mDispatcher->setInputWindows(
4531 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4532
4533 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004534 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4535 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004536
4537 // Only the first window should get the down event
4538 firstWindow->consumeMotionDown();
4539 secondWindow->assertNoEvents();
4540 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4541 wallpaper2->assertNoEvents();
4542
4543 // Transfer touch focus to the second window
4544 TransferFunction f = GetParam();
4545 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4546 ASSERT_TRUE(success);
4547
4548 // The first window gets cancel and the second gets down
4549 firstWindow->consumeMotionCancel();
4550 secondWindow->consumeMotionDown();
4551 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4552 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4553
4554 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004555 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4556 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004557 // The first window gets no events and the second gets up
4558 firstWindow->assertNoEvents();
4559 secondWindow->consumeMotionUp();
4560 wallpaper1->assertNoEvents();
4561 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4562}
4563
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004564// For the cases of single pointer touch and two pointers non-split touch, the api's
4565// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4566// for the case where there are multiple pointers split across several windows.
4567INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4568 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004569 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4570 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004571 return dispatcher->transferTouch(destChannelToken,
4572 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004573 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004574 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4575 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004576 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004577 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004578 }));
4579
Svet Ganov5d3bc372020-01-26 23:11:07 -08004580TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004581 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004582
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004583 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004584 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4585 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004586 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004587
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004588 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004589 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4590 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004591 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004592
4593 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004594 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004595
4596 PointF pointInFirst = {300, 200};
4597 PointF pointInSecond = {300, 600};
4598
4599 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004600 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4601 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4602 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004603 // Only the first window should get the down event
4604 firstWindow->consumeMotionDown();
4605 secondWindow->assertNoEvents();
4606
4607 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004608 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4609 ADISPLAY_ID_DEFAULT,
4610 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004611 // The first window gets a move and the second a down
4612 firstWindow->consumeMotionMove();
4613 secondWindow->consumeMotionDown();
4614
4615 // Transfer touch focus to the second window
4616 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4617 // The first window gets cancel and the new gets pointer down (it already saw down)
4618 firstWindow->consumeMotionCancel();
4619 secondWindow->consumeMotionPointerDown(1);
4620
4621 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004622 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4623 ADISPLAY_ID_DEFAULT,
4624 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004625 // The first window gets nothing and the second gets pointer up
4626 firstWindow->assertNoEvents();
4627 secondWindow->consumeMotionPointerUp(1);
4628
4629 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004630 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4631 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004632 // The first window gets nothing and the second gets up
4633 firstWindow->assertNoEvents();
4634 secondWindow->consumeMotionUp();
4635}
4636
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004637// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4638// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4639// touch is not supported, so the touch should continue on those windows and the transferred-to
4640// window should get nothing.
4641TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4642 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4643
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004644 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004645 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4646 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004647 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004648
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004649 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004650 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4651 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004652 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004653
4654 // Add the windows to the dispatcher
4655 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4656
4657 PointF pointInFirst = {300, 200};
4658 PointF pointInSecond = {300, 600};
4659
4660 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004661 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4662 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4663 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004664 // Only the first window should get the down event
4665 firstWindow->consumeMotionDown();
4666 secondWindow->assertNoEvents();
4667
4668 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004669 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4670 ADISPLAY_ID_DEFAULT,
4671 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004672 // The first window gets a move and the second a down
4673 firstWindow->consumeMotionMove();
4674 secondWindow->consumeMotionDown();
4675
4676 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004677 const bool transferred =
4678 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004679 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4680 ASSERT_FALSE(transferred);
4681 firstWindow->assertNoEvents();
4682 secondWindow->assertNoEvents();
4683
4684 // The rest of the dispatch should proceed as normal
4685 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004686 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4687 ADISPLAY_ID_DEFAULT,
4688 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004689 // The first window gets MOVE and the second gets pointer up
4690 firstWindow->consumeMotionMove();
4691 secondWindow->consumeMotionUp();
4692
4693 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004694 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4695 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004696 // The first window gets nothing and the second gets up
4697 firstWindow->consumeMotionUp();
4698 secondWindow->assertNoEvents();
4699}
4700
Arthur Hungabbb9d82021-09-01 14:52:30 +00004701// This case will create two windows and one mirrored window on the default display and mirror
4702// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4703// the windows info of second display before default display.
4704TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4705 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4706 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004707 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004708 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004709 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004710 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004711 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004712
4713 sp<FakeWindowHandle> mirrorWindowInPrimary =
4714 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4715 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004716
4717 sp<FakeWindowHandle> firstWindowInSecondary =
4718 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4719 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004720
4721 sp<FakeWindowHandle> secondWindowInSecondary =
4722 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4723 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004724
4725 // Update window info, let it find window handle of second display first.
4726 mDispatcher->setInputWindows(
4727 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4728 {ADISPLAY_ID_DEFAULT,
4729 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4730
4731 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4732 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4733 {50, 50}))
4734 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4735
4736 // Window should receive motion event.
4737 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4738
4739 // Transfer touch focus
4740 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4741 secondWindowInPrimary->getToken()));
4742 // The first window gets cancel.
4743 firstWindowInPrimary->consumeMotionCancel();
4744 secondWindowInPrimary->consumeMotionDown();
4745
4746 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4747 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4748 ADISPLAY_ID_DEFAULT, {150, 50}))
4749 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4750 firstWindowInPrimary->assertNoEvents();
4751 secondWindowInPrimary->consumeMotionMove();
4752
4753 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4754 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4755 {150, 50}))
4756 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4757 firstWindowInPrimary->assertNoEvents();
4758 secondWindowInPrimary->consumeMotionUp();
4759}
4760
4761// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4762// 'transferTouch' api.
4763TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4764 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4765 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004766 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004767 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004768 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004769 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004770 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004771
4772 sp<FakeWindowHandle> mirrorWindowInPrimary =
4773 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4774 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004775
4776 sp<FakeWindowHandle> firstWindowInSecondary =
4777 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4778 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004779
4780 sp<FakeWindowHandle> secondWindowInSecondary =
4781 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4782 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004783
4784 // Update window info, let it find window handle of second display first.
4785 mDispatcher->setInputWindows(
4786 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4787 {ADISPLAY_ID_DEFAULT,
4788 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4789
4790 // Touch on second display.
4791 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4792 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4793 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4794
4795 // Window should receive motion event.
4796 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4797
4798 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004799 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004800
4801 // The first window gets cancel.
4802 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4803 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4804
4805 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4806 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4807 SECOND_DISPLAY_ID, {150, 50}))
4808 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4809 firstWindowInPrimary->assertNoEvents();
4810 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4811
4812 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4813 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4814 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4815 firstWindowInPrimary->assertNoEvents();
4816 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4817}
4818
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004819TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004820 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004821 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4822 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004823
Vishnu Nair47074b82020-08-14 11:54:47 -07004824 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004825 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004826 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004827
4828 window->consumeFocusEvent(true);
4829
Prabir Pradhan678438e2023-04-13 19:32:51 +00004830 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004831
4832 // Window should receive key down event.
4833 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004834
4835 // Should have poked user activity
4836 mFakePolicy->assertUserActivityPoked();
4837}
4838
4839TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4840 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4841 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4842 "Fake Window", ADISPLAY_ID_DEFAULT);
4843
4844 window->setDisableUserActivity(true);
4845 window->setFocusable(true);
4846 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4847 setFocusedWindow(window);
4848
4849 window->consumeFocusEvent(true);
4850
4851 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4852
4853 // Window should receive key down event.
4854 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4855
4856 // Should have poked user activity
4857 mFakePolicy->assertUserActivityNotPoked();
4858}
4859
4860TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4861 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4862 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4863 "Fake Window", ADISPLAY_ID_DEFAULT);
4864
4865 window->setFocusable(true);
4866 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4867 setFocusedWindow(window);
4868
4869 window->consumeFocusEvent(true);
4870
4871 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4872 mDispatcher->waitForIdle();
4873
4874 // System key is not passed down
4875 window->assertNoEvents();
4876
4877 // Should have poked user activity
4878 mFakePolicy->assertUserActivityPoked();
4879}
4880
4881TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4882 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4883 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4884 "Fake Window", ADISPLAY_ID_DEFAULT);
4885
4886 window->setFocusable(true);
4887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4888 setFocusedWindow(window);
4889
4890 window->consumeFocusEvent(true);
4891
4892 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4893 mDispatcher->waitForIdle();
4894
4895 // System key is not passed down
4896 window->assertNoEvents();
4897
4898 // Should have poked user activity
4899 mFakePolicy->assertUserActivityPoked();
4900}
4901
4902TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4903 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4904 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4905 "Fake Window", ADISPLAY_ID_DEFAULT);
4906
4907 window->setDisableUserActivity(true);
4908 window->setFocusable(true);
4909 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4910 setFocusedWindow(window);
4911
4912 window->consumeFocusEvent(true);
4913
4914 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4915 mDispatcher->waitForIdle();
4916
4917 // System key is not passed down
4918 window->assertNoEvents();
4919
4920 // Should have poked user activity
4921 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004922}
4923
4924TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004925 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004926 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4927 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004928
Arthur Hung72d8dc32020-03-28 00:48:39 +00004929 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004930
Prabir Pradhan678438e2023-04-13 19:32:51 +00004931 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004932 mDispatcher->waitForIdle();
4933
4934 window->assertNoEvents();
4935}
4936
4937// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4938TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004939 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004940 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4941 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004942
Arthur Hung72d8dc32020-03-28 00:48:39 +00004943 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004944
4945 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004946 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004947 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004948 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4949 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004950
4951 // Window should receive only the motion event
4952 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4953 window->assertNoEvents(); // Key event or focus event will not be received
4954}
4955
arthurhungea3f4fc2020-12-21 23:18:53 +08004956TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4957 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4958
arthurhungea3f4fc2020-12-21 23:18:53 +08004959 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004960 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4961 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004962 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004963
arthurhungea3f4fc2020-12-21 23:18:53 +08004964 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004965 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4966 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004967 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004968
4969 // Add the windows to the dispatcher
4970 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4971
4972 PointF pointInFirst = {300, 200};
4973 PointF pointInSecond = {300, 600};
4974
4975 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004976 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4977 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4978 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004979 // Only the first window should get the down event
4980 firstWindow->consumeMotionDown();
4981 secondWindow->assertNoEvents();
4982
4983 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004984 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4985 ADISPLAY_ID_DEFAULT,
4986 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004987 // The first window gets a move and the second a down
4988 firstWindow->consumeMotionMove();
4989 secondWindow->consumeMotionDown();
4990
4991 // Send pointer cancel to the second window
4992 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004993 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004994 {pointInFirst, pointInSecond});
4995 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004996 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004997 // The first window gets move and the second gets cancel.
4998 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
4999 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5000
5001 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005002 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5003 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08005004 // The first window gets up and the second gets nothing.
5005 firstWindow->consumeMotionUp();
5006 secondWindow->assertNoEvents();
5007}
5008
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005009TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
5010 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5011
5012 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005013 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005014 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5015 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
5016 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
5017 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
5018
Harry Cutts33476232023-01-30 19:57:29 +00005019 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005020 window->assertNoEvents();
5021 mDispatcher->waitForIdle();
5022}
5023
chaviwd1c23182019-12-20 18:44:56 -08005024class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00005025public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005026 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005027 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07005028 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005029 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07005030 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00005031 }
5032
chaviwd1c23182019-12-20 18:44:56 -08005033 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
5034
5035 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005036 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
5037 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08005038 }
5039
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005040 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
5041
5042 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
5043
chaviwd1c23182019-12-20 18:44:56 -08005044 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005045 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08005046 expectedDisplayId, expectedFlags);
5047 }
5048
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005049 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005050 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005051 expectedDisplayId, expectedFlags);
5052 }
5053
chaviwd1c23182019-12-20 18:44:56 -08005054 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005055 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08005056 expectedDisplayId, expectedFlags);
5057 }
5058
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005059 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005060 mInputReceiver->consumeMotionEvent(
5061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
5062 WithDisplayId(expectedDisplayId),
5063 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005064 }
5065
Arthur Hungfbfa5722021-11-16 02:45:54 +00005066 void consumeMotionPointerDown(int32_t pointerIdx) {
5067 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
5068 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005069 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005070 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00005071 }
5072
Evan Rosky84f07f02021-04-16 10:42:42 -07005073 MotionEvent* consumeMotion() {
5074 InputEvent* event = mInputReceiver->consume();
5075 if (!event) {
5076 ADD_FAILURE() << "No event was produced";
5077 return nullptr;
5078 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005079 if (event->getType() != InputEventType::MOTION) {
5080 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07005081 return nullptr;
5082 }
5083 return static_cast<MotionEvent*>(event);
5084 }
5085
chaviwd1c23182019-12-20 18:44:56 -08005086 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
5087
5088private:
5089 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00005090};
5091
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005092using InputDispatcherMonitorTest = InputDispatcherTest;
5093
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005094/**
5095 * Two entities that receive touch: A window, and a global monitor.
5096 * The touch goes to the window, and then the window disappears.
5097 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
5098 * for the monitor, as well.
5099 * 1. foregroundWindow
5100 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
5101 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005102TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005103 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5104 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005105 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005106
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005107 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005108
5109 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5110 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5111 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5112 {100, 200}))
5113 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5114
5115 // Both the foreground window and the global monitor should receive the touch down
5116 window->consumeMotionDown();
5117 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5118
5119 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5120 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5121 ADISPLAY_ID_DEFAULT, {110, 200}))
5122 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5123
5124 window->consumeMotionMove();
5125 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5126
5127 // Now the foreground window goes away
5128 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5129 window->consumeMotionCancel();
5130 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5131
5132 // If more events come in, there will be no more foreground window to send them to. This will
5133 // cause a cancel for the monitor, as well.
5134 ASSERT_EQ(InputEventInjectionResult::FAILED,
5135 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5136 ADISPLAY_ID_DEFAULT, {120, 200}))
5137 << "Injection should fail because the window was removed";
5138 window->assertNoEvents();
5139 // Global monitor now gets the cancel
5140 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5141}
5142
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005143TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005144 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005145 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5146 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005147 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005148
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005149 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005150
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005151 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005152 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005153 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005154 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005155 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005156}
5157
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005158TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5159 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005160
Chris Yea209fde2020-07-22 13:54:51 -07005161 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005162 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5163 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005165
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005167 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005168 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005169 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005170 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005171
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005172 // Pilfer pointers from the monitor.
5173 // This should not do anything and the window should continue to receive events.
5174 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005175
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005177 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5178 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005179 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005180
5181 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5182 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005183}
5184
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005185TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005186 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005187 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5188 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005189 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5190 window->setWindowOffset(20, 40);
5191 window->setWindowTransform(0, 1, -1, 0);
5192
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005193 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005194
5195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5196 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5197 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5198 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5199 MotionEvent* event = monitor.consumeMotion();
5200 // Even though window has transform, gesture monitor must not.
5201 ASSERT_EQ(ui::Transform(), event->getTransform());
5202}
5203
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005204TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005205 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005206 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005207
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005208 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005209 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005210 << "Injection should fail if there is a monitor, but no touchable window";
5211 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005212}
5213
chaviw81e2bb92019-12-18 15:03:51 -08005214TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005215 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005216 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5217 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005218
Arthur Hung72d8dc32020-03-28 00:48:39 +00005219 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005220
5221 NotifyMotionArgs motionArgs =
5222 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5223 ADISPLAY_ID_DEFAULT);
5224
Prabir Pradhan678438e2023-04-13 19:32:51 +00005225 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005226 // Window should receive motion down event.
5227 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5228
5229 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005230 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005231 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5232 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5233 motionArgs.pointerCoords[0].getX() - 10);
5234
Prabir Pradhan678438e2023-04-13 19:32:51 +00005235 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005236 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005237 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005238}
5239
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005240/**
5241 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5242 * the device default right away. In the test scenario, we check both the default value,
5243 * and the action of enabling / disabling.
5244 */
5245TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005246 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005247 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5248 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005249 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005250
5251 // Set focused application.
5252 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005253 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005254
5255 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005256 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005257 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005258 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005259
5260 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005261 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005262 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005263 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005264
5265 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005266 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005267 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005268 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005269 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005270 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005271 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005272 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005273
5274 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005275 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005276 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005277 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005278
5279 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005280 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005281 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005282 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005283 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005284 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005285 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005286 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005287
5288 window->assertNoEvents();
5289}
5290
Gang Wange9087892020-01-07 12:17:14 -05005291TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005292 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005293 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5294 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005295
5296 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005297 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005298
Arthur Hung72d8dc32020-03-28 00:48:39 +00005299 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005300 setFocusedWindow(window);
5301
Harry Cutts33476232023-01-30 19:57:29 +00005302 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005303
Prabir Pradhan678438e2023-04-13 19:32:51 +00005304 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5305 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005306
5307 InputEvent* event = window->consume();
5308 ASSERT_NE(event, nullptr);
5309
5310 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5311 ASSERT_NE(verified, nullptr);
5312 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5313
5314 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5315 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5316 ASSERT_EQ(keyArgs.source, verified->source);
5317 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5318
5319 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5320
5321 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005322 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005323 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005324 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5325 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5326 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5327 ASSERT_EQ(0, verifiedKey.repeatCount);
5328}
5329
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005330TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005331 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005332 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5333 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005334
5335 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5336
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005337 ui::Transform transform;
5338 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5339
5340 gui::DisplayInfo displayInfo;
5341 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5342 displayInfo.transform = transform;
5343
Patrick Williamsd828f302023-04-28 17:52:08 -05005344 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005345
Prabir Pradhan678438e2023-04-13 19:32:51 +00005346 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005347 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5348 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005349 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005350
5351 InputEvent* event = window->consume();
5352 ASSERT_NE(event, nullptr);
5353
5354 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5355 ASSERT_NE(verified, nullptr);
5356 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5357
5358 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5359 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5360 EXPECT_EQ(motionArgs.source, verified->source);
5361 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5362
5363 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5364
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005365 const vec2 rawXY =
5366 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5367 motionArgs.pointerCoords[0].getXYValue());
5368 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5369 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005370 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005371 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005372 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005373 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5374 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5375}
5376
chaviw09c8d2d2020-08-24 15:48:26 -07005377/**
5378 * Ensure that separate calls to sign the same data are generating the same key.
5379 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5380 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5381 * tests.
5382 */
5383TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5384 KeyEvent event = getTestKeyEvent();
5385 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5386
5387 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5388 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5389 ASSERT_EQ(hmac1, hmac2);
5390}
5391
5392/**
5393 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5394 */
5395TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5396 KeyEvent event = getTestKeyEvent();
5397 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5398 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5399
5400 verifiedEvent.deviceId += 1;
5401 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5402
5403 verifiedEvent.source += 1;
5404 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5405
5406 verifiedEvent.eventTimeNanos += 1;
5407 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5408
5409 verifiedEvent.displayId += 1;
5410 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5411
5412 verifiedEvent.action += 1;
5413 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5414
5415 verifiedEvent.downTimeNanos += 1;
5416 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5417
5418 verifiedEvent.flags += 1;
5419 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5420
5421 verifiedEvent.keyCode += 1;
5422 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5423
5424 verifiedEvent.scanCode += 1;
5425 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5426
5427 verifiedEvent.metaState += 1;
5428 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5429
5430 verifiedEvent.repeatCount += 1;
5431 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5432}
5433
Vishnu Nair958da932020-08-21 17:12:37 -07005434TEST_F(InputDispatcherTest, SetFocusedWindow) {
5435 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5436 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005437 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005438 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005439 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005440 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5441
5442 // Top window is also focusable but is not granted focus.
5443 windowTop->setFocusable(true);
5444 windowSecond->setFocusable(true);
5445 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5446 setFocusedWindow(windowSecond);
5447
5448 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005449 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5450 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005451
5452 // Focused window should receive event.
5453 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5454 windowTop->assertNoEvents();
5455}
5456
5457TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5458 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5459 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005460 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005461 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5462
5463 window->setFocusable(true);
5464 // Release channel for window is no longer valid.
5465 window->releaseChannel();
5466 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5467 setFocusedWindow(window);
5468
5469 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005470 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5471 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005472
5473 // window channel is invalid, so it should not receive any input event.
5474 window->assertNoEvents();
5475}
5476
5477TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5478 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5479 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005480 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005481 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005482 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5483
Vishnu Nair958da932020-08-21 17:12:37 -07005484 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5485 setFocusedWindow(window);
5486
5487 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005488 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5489 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005490
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005491 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005492 window->assertNoEvents();
5493}
5494
5495TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5496 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5497 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005498 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005499 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005500 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005501 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5502
5503 windowTop->setFocusable(true);
5504 windowSecond->setFocusable(true);
5505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5506 setFocusedWindow(windowTop);
5507 windowTop->consumeFocusEvent(true);
5508
Chavi Weingarten847e8512023-03-29 00:26:09 +00005509 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5510 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005511 windowSecond->consumeFocusEvent(true);
5512 windowTop->consumeFocusEvent(false);
5513
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005514 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5515 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005516
5517 // Focused window should receive event.
5518 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5519}
5520
Chavi Weingarten847e8512023-03-29 00:26:09 +00005521TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005522 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5523 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005524 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005525 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005526 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005527 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5528
5529 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005530 windowSecond->setFocusable(false);
5531 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005532 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005533 setFocusedWindow(windowTop);
5534 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005535
Chavi Weingarten847e8512023-03-29 00:26:09 +00005536 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5537 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005538
5539 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005540 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005541 windowSecond->assertNoEvents();
5542}
5543
5544TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5545 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5546 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005547 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005548 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005549 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5550 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005551 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5552
5553 window->setFocusable(true);
5554 previousFocusedWindow->setFocusable(true);
5555 window->setVisible(false);
5556 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5557 setFocusedWindow(previousFocusedWindow);
5558 previousFocusedWindow->consumeFocusEvent(true);
5559
5560 // Requesting focus on invisible window takes focus from currently focused window.
5561 setFocusedWindow(window);
5562 previousFocusedWindow->consumeFocusEvent(false);
5563
5564 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005565 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005566 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5567 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005568
5569 // Window does not get focus event or key down.
5570 window->assertNoEvents();
5571
5572 // Window becomes visible.
5573 window->setVisible(true);
5574 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5575
5576 // Window receives focus event.
5577 window->consumeFocusEvent(true);
5578 // Focused window receives key down.
5579 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5580}
5581
Vishnu Nair599f1412021-06-21 10:39:58 -07005582TEST_F(InputDispatcherTest, DisplayRemoved) {
5583 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5584 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005585 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005586 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5587
5588 // window is granted focus.
5589 window->setFocusable(true);
5590 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5591 setFocusedWindow(window);
5592 window->consumeFocusEvent(true);
5593
5594 // When a display is removed window loses focus.
5595 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5596 window->consumeFocusEvent(false);
5597}
5598
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005599/**
5600 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5601 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5602 * of the 'slipperyEnterWindow'.
5603 *
5604 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5605 * a way so that the touched location is no longer covered by the top window.
5606 *
5607 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5608 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5609 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5610 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5611 * with ACTION_DOWN).
5612 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5613 * window moved itself away from the touched location and had Flag::SLIPPERY.
5614 *
5615 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5616 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5617 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5618 *
5619 * In this test, we ensure that the event received by the bottom window has
5620 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5621 */
5622TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005623 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5624 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005625
5626 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5627 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5628
5629 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005630 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005631 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005632 // Make sure this one overlaps the bottom window
5633 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5634 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5635 // one. Windows with the same owner are not considered to be occluding each other.
5636 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5637
5638 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005639 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005640 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5641
5642 mDispatcher->setInputWindows(
5643 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5644
5645 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005646 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5647 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5648 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005649 slipperyExitWindow->consumeMotionDown();
5650 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5651 mDispatcher->setInputWindows(
5652 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5653
Prabir Pradhan678438e2023-04-13 19:32:51 +00005654 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5655 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5656 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005657
5658 slipperyExitWindow->consumeMotionCancel();
5659
5660 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5661 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5662}
5663
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005664/**
5665 * Two windows, one on the left and another on the right. The left window is slippery. The right
5666 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5667 * touch moves from the left window into the right window, the gesture should continue to go to the
5668 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5669 * reproduces a crash.
5670 */
5671TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5672 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5673
5674 sp<FakeWindowHandle> leftSlipperyWindow =
5675 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5676 leftSlipperyWindow->setSlippery(true);
5677 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5678
5679 sp<FakeWindowHandle> rightDropTouchesWindow =
5680 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5681 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5682 rightDropTouchesWindow->setDropInput(true);
5683
5684 mDispatcher->setInputWindows(
5685 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5686
5687 // Start touch in the left window
5688 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5689 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5690 .build());
5691 leftSlipperyWindow->consumeMotionDown();
5692
5693 // And move it into the right window
5694 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5695 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5696 .build());
5697
5698 // Since the right window isn't eligible to receive input, touch does not slip.
5699 // The left window continues to receive the gesture.
5700 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5701 rightDropTouchesWindow->assertNoEvents();
5702}
5703
Garfield Tan1c7bc862020-01-28 13:24:04 -08005704class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5705protected:
5706 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5707 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5708
Chris Yea209fde2020-07-22 13:54:51 -07005709 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005710 sp<FakeWindowHandle> mWindow;
5711
5712 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005713 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005714 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005715 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005716 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005717 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5718 ASSERT_EQ(OK, mDispatcher->start());
5719
5720 setUpWindow();
5721 }
5722
5723 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005724 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005725 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005726
Vishnu Nair47074b82020-08-14 11:54:47 -07005727 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005728 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005729 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005730 mWindow->consumeFocusEvent(true);
5731 }
5732
Chris Ye2ad95392020-09-01 13:44:44 -07005733 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005734 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005735 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005736 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005737 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005738
5739 // Window should receive key down event.
5740 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5741 }
5742
5743 void expectKeyRepeatOnce(int32_t repeatCount) {
5744 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5745 InputEvent* repeatEvent = mWindow->consume();
5746 ASSERT_NE(nullptr, repeatEvent);
5747
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005748 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005749
5750 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5751 uint32_t eventAction = repeatKeyEvent->getAction();
5752 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5753 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5754 }
5755
Chris Ye2ad95392020-09-01 13:44:44 -07005756 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005757 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005758 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005759 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005760 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005761
5762 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005763 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005764 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005765 }
5766};
5767
5768TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005769 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005770 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5771 expectKeyRepeatOnce(repeatCount);
5772 }
5773}
5774
5775TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005776 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005777 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5778 expectKeyRepeatOnce(repeatCount);
5779 }
Harry Cutts33476232023-01-30 19:57:29 +00005780 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005781 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005782 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5783 expectKeyRepeatOnce(repeatCount);
5784 }
5785}
5786
5787TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005788 sendAndConsumeKeyDown(/*deviceId=*/1);
5789 expectKeyRepeatOnce(/*repeatCount=*/1);
5790 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005791 mWindow->assertNoEvents();
5792}
5793
5794TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005795 sendAndConsumeKeyDown(/*deviceId=*/1);
5796 expectKeyRepeatOnce(/*repeatCount=*/1);
5797 sendAndConsumeKeyDown(/*deviceId=*/2);
5798 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005799 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005800 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005801 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005802 expectKeyRepeatOnce(/*repeatCount=*/2);
5803 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005804 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005805 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005806 mWindow->assertNoEvents();
5807}
5808
5809TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005810 sendAndConsumeKeyDown(/*deviceId=*/1);
5811 expectKeyRepeatOnce(/*repeatCount=*/1);
5812 sendAndConsumeKeyDown(/*deviceId=*/2);
5813 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005814 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005815 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005816 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005817 mWindow->assertNoEvents();
5818}
5819
liushenxiang42232912021-05-21 20:24:09 +08005820TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5821 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005822 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005823 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005824 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5825 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5826 mWindow->assertNoEvents();
5827}
5828
Garfield Tan1c7bc862020-01-28 13:24:04 -08005829TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005830 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005831 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005832 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5833 InputEvent* repeatEvent = mWindow->consume();
5834 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5835 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5836 IdGenerator::getSource(repeatEvent->getId()));
5837 }
5838}
5839
5840TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005841 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005842 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005843
5844 std::unordered_set<int32_t> idSet;
5845 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5846 InputEvent* repeatEvent = mWindow->consume();
5847 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5848 int32_t id = repeatEvent->getId();
5849 EXPECT_EQ(idSet.end(), idSet.find(id));
5850 idSet.insert(id);
5851 }
5852}
5853
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005854/* Test InputDispatcher for MultiDisplay */
5855class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5856public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005857 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005858 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005859
Chris Yea209fde2020-07-22 13:54:51 -07005860 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005861 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005862 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005863
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005864 // Set focus window for primary display, but focused display would be second one.
5865 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005866 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005868 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005869 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005870
Chris Yea209fde2020-07-22 13:54:51 -07005871 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005872 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005873 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005874 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005875 // Set focus display to second one.
5876 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5877 // Set focus window for second display.
5878 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005879 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005880 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005881 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005882 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005883 }
5884
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005885 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005886 InputDispatcherTest::TearDown();
5887
Chris Yea209fde2020-07-22 13:54:51 -07005888 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005889 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005890 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005891 windowInSecondary.clear();
5892 }
5893
5894protected:
Chris Yea209fde2020-07-22 13:54:51 -07005895 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005896 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005897 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005898 sp<FakeWindowHandle> windowInSecondary;
5899};
5900
5901TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5902 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5904 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5905 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005906 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005907 windowInSecondary->assertNoEvents();
5908
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005909 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005910 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5911 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5912 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005913 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005914 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005915}
5916
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005917TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005918 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005919 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5920 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005921 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005922 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005923 windowInSecondary->assertNoEvents();
5924
5925 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005927 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005928 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005929 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005930
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005931 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005932 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005933
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005934 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005935 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005936 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005937
5938 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005939 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005940 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005941 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005942 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005943 windowInSecondary->assertNoEvents();
5944}
5945
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005946// Test per-display input monitors for motion event.
5947TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005948 FakeMonitorReceiver monitorInPrimary =
5949 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5950 FakeMonitorReceiver monitorInSecondary =
5951 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005952
5953 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005954 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5955 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5956 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005957 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005958 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005959 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005960 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005961
5962 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005963 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5964 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5965 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005966 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005967 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005968 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005969 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005970
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005971 // Lift up the touch from the second display
5972 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5973 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5974 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5975 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5976 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5977
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005978 // Test inject a non-pointer motion event.
5979 // If specific a display, it will dispatch to the focused window of particular display,
5980 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005981 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5982 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5983 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005984 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005985 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005986 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005987 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005988}
5989
5990// Test per-display input monitors for key event.
5991TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005992 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005993 FakeMonitorReceiver monitorInPrimary =
5994 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5995 FakeMonitorReceiver monitorInSecondary =
5996 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005997
5998 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005999 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6000 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006001 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006002 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006003 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006004 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006005}
6006
Vishnu Nair958da932020-08-21 17:12:37 -07006007TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6008 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006009 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006010 secondWindowInPrimary->setFocusable(true);
6011 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
6012 setFocusedWindow(secondWindowInPrimary);
6013 windowInPrimary->consumeFocusEvent(false);
6014 secondWindowInPrimary->consumeFocusEvent(true);
6015
6016 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006017 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6018 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006019 windowInPrimary->assertNoEvents();
6020 windowInSecondary->assertNoEvents();
6021 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6022}
6023
Arthur Hungdfd528e2021-12-08 13:23:04 +00006024TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6025 FakeMonitorReceiver monitorInPrimary =
6026 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6027 FakeMonitorReceiver monitorInSecondary =
6028 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6029
6030 // Test touch down on primary display.
6031 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6032 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6033 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6034 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6035 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6036
6037 // Test touch down on second display.
6038 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6039 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6040 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6041 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6042 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6043
6044 // Trigger cancel touch.
6045 mDispatcher->cancelCurrentTouch();
6046 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6047 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6048 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6049 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6050
6051 // Test inject a move motion event, no window/monitor should receive the event.
6052 ASSERT_EQ(InputEventInjectionResult::FAILED,
6053 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6054 ADISPLAY_ID_DEFAULT, {110, 200}))
6055 << "Inject motion event should return InputEventInjectionResult::FAILED";
6056 windowInPrimary->assertNoEvents();
6057 monitorInPrimary.assertNoEvents();
6058
6059 ASSERT_EQ(InputEventInjectionResult::FAILED,
6060 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6061 SECOND_DISPLAY_ID, {110, 200}))
6062 << "Inject motion event should return InputEventInjectionResult::FAILED";
6063 windowInSecondary->assertNoEvents();
6064 monitorInSecondary.assertNoEvents();
6065}
6066
Jackal Guof9696682018-10-05 12:23:23 +08006067class InputFilterTest : public InputDispatcherTest {
6068protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006069 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6070 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006071 NotifyMotionArgs motionArgs;
6072
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006073 motionArgs =
6074 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006075 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006076 motionArgs =
6077 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006078 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006079 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006080 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006081 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6082 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006083 } else {
6084 mFakePolicy->assertFilterInputEventWasNotCalled();
6085 }
6086 }
6087
6088 void testNotifyKey(bool expectToBeFiltered) {
6089 NotifyKeyArgs keyArgs;
6090
6091 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006092 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006093 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006094 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006095 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006096
6097 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006098 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006099 } else {
6100 mFakePolicy->assertFilterInputEventWasNotCalled();
6101 }
6102 }
6103};
6104
6105// Test InputFilter for MotionEvent
6106TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6107 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6108 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6109 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6110
6111 // Enable InputFilter
6112 mDispatcher->setInputFilterEnabled(true);
6113 // Test touch on both primary and second display, and check if both events are filtered.
6114 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6115 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6116
6117 // Disable InputFilter
6118 mDispatcher->setInputFilterEnabled(false);
6119 // Test touch on both primary and second display, and check if both events aren't filtered.
6120 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6121 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6122}
6123
6124// Test InputFilter for KeyEvent
6125TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6126 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6127 testNotifyKey(/*expectToBeFiltered*/ false);
6128
6129 // Enable InputFilter
6130 mDispatcher->setInputFilterEnabled(true);
6131 // Send a key event, and check if it is filtered.
6132 testNotifyKey(/*expectToBeFiltered*/ true);
6133
6134 // Disable InputFilter
6135 mDispatcher->setInputFilterEnabled(false);
6136 // Send a key event, and check if it isn't filtered.
6137 testNotifyKey(/*expectToBeFiltered*/ false);
6138}
6139
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006140// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6141// logical display coordinate space.
6142TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6143 ui::Transform firstDisplayTransform;
6144 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6145 ui::Transform secondDisplayTransform;
6146 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6147
6148 std::vector<gui::DisplayInfo> displayInfos(2);
6149 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6150 displayInfos[0].transform = firstDisplayTransform;
6151 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6152 displayInfos[1].transform = secondDisplayTransform;
6153
Patrick Williamsd828f302023-04-28 17:52:08 -05006154 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006155
6156 // Enable InputFilter
6157 mDispatcher->setInputFilterEnabled(true);
6158
6159 // Ensure the correct transforms are used for the displays.
6160 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6161 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6162}
6163
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006164class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6165protected:
6166 virtual void SetUp() override {
6167 InputDispatcherTest::SetUp();
6168
6169 /**
6170 * We don't need to enable input filter to test the injected event policy, but we enabled it
6171 * here to make the tests more realistic, since this policy only matters when inputfilter is
6172 * on.
6173 */
6174 mDispatcher->setInputFilterEnabled(true);
6175
6176 std::shared_ptr<InputApplicationHandle> application =
6177 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006178 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6179 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006180
6181 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6182 mWindow->setFocusable(true);
6183 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6184 setFocusedWindow(mWindow);
6185 mWindow->consumeFocusEvent(true);
6186 }
6187
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006188 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6189 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006190 KeyEvent event;
6191
6192 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6193 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6194 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006195 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006196 const int32_t additionalPolicyFlags =
6197 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006199 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006200 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6201 policyFlags | additionalPolicyFlags));
6202
6203 InputEvent* received = mWindow->consume();
6204 ASSERT_NE(nullptr, received);
6205 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006206 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006207 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6208 ASSERT_EQ(flags, keyEvent.getFlags());
6209 }
6210
6211 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6212 int32_t flags) {
6213 MotionEvent event;
6214 PointerProperties pointerProperties[1];
6215 PointerCoords pointerCoords[1];
6216 pointerProperties[0].clear();
6217 pointerProperties[0].id = 0;
6218 pointerCoords[0].clear();
6219 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6220 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6221
6222 ui::Transform identityTransform;
6223 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6224 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6225 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6226 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6227 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006228 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006229 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006230 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6231
6232 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6233 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006234 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006235 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6236 policyFlags | additionalPolicyFlags));
6237
6238 InputEvent* received = mWindow->consume();
6239 ASSERT_NE(nullptr, received);
6240 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006241 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006242 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6243 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006244 }
6245
6246private:
6247 sp<FakeWindowHandle> mWindow;
6248};
6249
6250TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006251 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6252 // filter. Without it, the event will no different from a regularly injected event, and the
6253 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006254 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6255 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006256}
6257
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006258TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006259 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006260 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006261 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6262}
6263
6264TEST_F(InputFilterInjectionPolicyTest,
6265 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6266 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006267 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006268 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006269}
6270
6271TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006272 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6273 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006274}
6275
chaviwfd6d3512019-03-25 13:23:49 -07006276class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006277 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006278 InputDispatcherTest::SetUp();
6279
Chris Yea209fde2020-07-22 13:54:51 -07006280 std::shared_ptr<FakeApplicationHandle> application =
6281 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006282 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006283 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006284 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006285
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006286 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006287 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006288 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006289
6290 // Set focused application.
6291 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006292 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006293
6294 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006296 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006297 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006298 }
6299
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006300 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006301 InputDispatcherTest::TearDown();
6302
6303 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006304 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006305 }
6306
6307protected:
6308 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006309 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006310 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006311};
6312
6313// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6314// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6315// the onPointerDownOutsideFocus callback.
6316TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006317 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006318 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6319 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006320 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006321 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006322
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006323 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006324 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6325}
6326
6327// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6328// DOWN on the window that doesn't have focus. Ensure no window received the
6329// onPointerDownOutsideFocus callback.
6330TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006331 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006332 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006333 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006334 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006335
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006336 ASSERT_TRUE(mDispatcher->waitForIdle());
6337 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006338}
6339
6340// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6341// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6342TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006343 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6344 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006345 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006346 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006347
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006348 ASSERT_TRUE(mDispatcher->waitForIdle());
6349 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006350}
6351
6352// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6353// DOWN on the window that already has focus. Ensure no window received the
6354// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006355TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006357 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006358 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006359 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006360 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006361
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006362 ASSERT_TRUE(mDispatcher->waitForIdle());
6363 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006364}
6365
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006366// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6367// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6368TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6369 const MotionEvent event =
6370 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6371 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006372 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006373 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6374 .build();
6375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6376 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6377 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6378
6379 ASSERT_TRUE(mDispatcher->waitForIdle());
6380 mFakePolicy->assertOnPointerDownWasNotCalled();
6381 // Ensure that the unfocused window did not receive any FOCUS events.
6382 mUnfocusedWindow->assertNoEvents();
6383}
6384
chaviwaf87b3e2019-10-01 16:59:28 -07006385// These tests ensures we can send touch events to a single client when there are multiple input
6386// windows that point to the same client token.
6387class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6388 virtual void SetUp() override {
6389 InputDispatcherTest::SetUp();
6390
Chris Yea209fde2020-07-22 13:54:51 -07006391 std::shared_ptr<FakeApplicationHandle> application =
6392 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006393 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6394 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006395 mWindow1->setFrame(Rect(0, 0, 100, 100));
6396
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006397 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6398 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006399 mWindow2->setFrame(Rect(100, 100, 200, 200));
6400
Arthur Hung72d8dc32020-03-28 00:48:39 +00006401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006402 }
6403
6404protected:
6405 sp<FakeWindowHandle> mWindow1;
6406 sp<FakeWindowHandle> mWindow2;
6407
6408 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006409 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006410 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6411 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006412 }
6413
6414 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6415 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006416 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006417 InputEvent* event = window->consume();
6418
6419 ASSERT_NE(nullptr, event) << name.c_str()
6420 << ": consumer should have returned non-NULL event.";
6421
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006422 ASSERT_EQ(InputEventType::MOTION, event->getType())
6423 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006424
6425 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006426 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006427 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006428
6429 for (size_t i = 0; i < points.size(); i++) {
6430 float expectedX = points[i].x;
6431 float expectedY = points[i].y;
6432
6433 EXPECT_EQ(expectedX, motionEvent.getX(i))
6434 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6435 << ", got " << motionEvent.getX(i);
6436 EXPECT_EQ(expectedY, motionEvent.getY(i))
6437 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6438 << ", got " << motionEvent.getY(i);
6439 }
6440 }
chaviw9eaa22c2020-07-01 16:21:27 -07006441
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006442 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006443 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006444 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6445 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006446
6447 // Always consume from window1 since it's the window that has the InputReceiver
6448 consumeMotionEvent(mWindow1, action, expectedPoints);
6449 }
chaviwaf87b3e2019-10-01 16:59:28 -07006450};
6451
6452TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6453 // Touch Window 1
6454 PointF touchedPoint = {10, 10};
6455 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006456 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006457
6458 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006459 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006460
6461 // Touch Window 2
6462 touchedPoint = {150, 150};
6463 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006464 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006465}
6466
chaviw9eaa22c2020-07-01 16:21:27 -07006467TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6468 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006469 mWindow2->setWindowScale(0.5f, 0.5f);
6470
6471 // Touch Window 1
6472 PointF touchedPoint = {10, 10};
6473 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006474 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006475 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006476 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006477
6478 // Touch Window 2
6479 touchedPoint = {150, 150};
6480 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006481 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6482 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006483
chaviw9eaa22c2020-07-01 16:21:27 -07006484 // Update the transform so rotation is set
6485 mWindow2->setWindowTransform(0, -1, 1, 0);
6486 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6487 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006488}
6489
chaviw9eaa22c2020-07-01 16:21:27 -07006490TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006491 mWindow2->setWindowScale(0.5f, 0.5f);
6492
6493 // Touch Window 1
6494 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6495 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006496 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006497
6498 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006499 touchedPoints.push_back(PointF{150, 150});
6500 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006501 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006502
chaviw9eaa22c2020-07-01 16:21:27 -07006503 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006504 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006505 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006506
chaviw9eaa22c2020-07-01 16:21:27 -07006507 // Update the transform so rotation is set for Window 2
6508 mWindow2->setWindowTransform(0, -1, 1, 0);
6509 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006510 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006511}
6512
chaviw9eaa22c2020-07-01 16:21:27 -07006513TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006514 mWindow2->setWindowScale(0.5f, 0.5f);
6515
6516 // Touch Window 1
6517 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6518 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006519 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006520
6521 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006522 touchedPoints.push_back(PointF{150, 150});
6523 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006524
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006525 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006526
6527 // Move both windows
6528 touchedPoints = {{20, 20}, {175, 175}};
6529 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6530 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6531
chaviw9eaa22c2020-07-01 16:21:27 -07006532 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006533
chaviw9eaa22c2020-07-01 16:21:27 -07006534 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006535 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006536 expectedPoints.pop_back();
6537
6538 // Touch Window 2
6539 mWindow2->setWindowTransform(0, -1, 1, 0);
6540 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006541 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006542
6543 // Move both windows
6544 touchedPoints = {{20, 20}, {175, 175}};
6545 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6546 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6547
6548 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006549}
6550
6551TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6552 mWindow1->setWindowScale(0.5f, 0.5f);
6553
6554 // Touch Window 1
6555 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6556 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006557 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006558
6559 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006560 touchedPoints.push_back(PointF{150, 150});
6561 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006562
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006563 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006564
6565 // Move both windows
6566 touchedPoints = {{20, 20}, {175, 175}};
6567 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6568 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6569
chaviw9eaa22c2020-07-01 16:21:27 -07006570 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006571}
6572
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006573/**
6574 * When one of the windows is slippery, the touch should not slip into the other window with the
6575 * same input channel.
6576 */
6577TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6578 mWindow1->setSlippery(true);
6579 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6580
6581 // Touch down in window 1
6582 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6583 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6584 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6585
6586 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6587 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6588 // getting generated.
6589 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6590 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6591
6592 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6593}
6594
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006595/**
6596 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6597 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6598 * that the pointer is hovering over may have a different transform.
6599 */
6600TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6601 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6602
6603 // Start hover in window 1
6604 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6605 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6606 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6607 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6608
6609 // Move hover to window 2.
6610 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6611 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6612
6613 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6614 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6615 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6616}
6617
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006618class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6619 virtual void SetUp() override {
6620 InputDispatcherTest::SetUp();
6621
Chris Yea209fde2020-07-22 13:54:51 -07006622 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006623 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006624 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6625 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006626 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006627 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006628 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006629
6630 // Set focused application.
6631 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6632
6633 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006634 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006635 mWindow->consumeFocusEvent(true);
6636 }
6637
6638 virtual void TearDown() override {
6639 InputDispatcherTest::TearDown();
6640 mWindow.clear();
6641 }
6642
6643protected:
Chris Yea209fde2020-07-22 13:54:51 -07006644 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006645 sp<FakeWindowHandle> mWindow;
6646 static constexpr PointF WINDOW_LOCATION = {20, 20};
6647
6648 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006649 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006650 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6651 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006653 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6654 WINDOW_LOCATION));
6655 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006656
6657 sp<FakeWindowHandle> addSpyWindow() {
6658 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006659 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006660 spy->setTrustedOverlay(true);
6661 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006662 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006663 spy->setDispatchingTimeout(30ms);
6664 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6665 return spy;
6666 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006667};
6668
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006669// Send a tap and respond, which should not cause an ANR.
6670TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6671 tapOnWindow();
6672 mWindow->consumeMotionDown();
6673 mWindow->consumeMotionUp();
6674 ASSERT_TRUE(mDispatcher->waitForIdle());
6675 mFakePolicy->assertNotifyAnrWasNotCalled();
6676}
6677
6678// Send a regular key and respond, which should not cause an ANR.
6679TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006680 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006681 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6682 ASSERT_TRUE(mDispatcher->waitForIdle());
6683 mFakePolicy->assertNotifyAnrWasNotCalled();
6684}
6685
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006686TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6687 mWindow->setFocusable(false);
6688 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6689 mWindow->consumeFocusEvent(false);
6690
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006691 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006692 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6693 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6694 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006695 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006696 // Key will not go to window because we have no focused window.
6697 // The 'no focused window' ANR timer should start instead.
6698
6699 // Now, the focused application goes away.
6700 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6701 // The key should get dropped and there should be no ANR.
6702
6703 ASSERT_TRUE(mDispatcher->waitForIdle());
6704 mFakePolicy->assertNotifyAnrWasNotCalled();
6705}
6706
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006707// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006708// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6709// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006710TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006711 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006712 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6713 WINDOW_LOCATION));
6714
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006715 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6716 ASSERT_TRUE(sequenceNum);
6717 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006718 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006719
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006720 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006721 mWindow->consumeMotionEvent(
6722 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006723 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006724 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006725}
6726
6727// Send a key to the app and have the app not respond right away.
6728TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6729 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006730 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006731 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6732 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006733 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006734 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006735 ASSERT_TRUE(mDispatcher->waitForIdle());
6736}
6737
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006738// We have a focused application, but no focused window
6739TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006740 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006741 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6742 mWindow->consumeFocusEvent(false);
6743
6744 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006745 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006746 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6747 WINDOW_LOCATION));
6748 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6749 mDispatcher->waitForIdle();
6750 mFakePolicy->assertNotifyAnrWasNotCalled();
6751
6752 // Once a focused event arrives, we get an ANR for this application
6753 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6754 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006755 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006756 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6757 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006758 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006759 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006760 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006761 ASSERT_TRUE(mDispatcher->waitForIdle());
6762}
6763
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006764/**
6765 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6766 * there will not be an ANR.
6767 */
6768TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6769 mWindow->setFocusable(false);
6770 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6771 mWindow->consumeFocusEvent(false);
6772
6773 KeyEvent event;
6774 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6775 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6776
6777 // Define a valid key down event that is stale (too old).
6778 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6779 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006780 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006781
6782 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6783
6784 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006785 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006786 InputEventInjectionSync::WAIT_FOR_RESULT,
6787 INJECT_EVENT_TIMEOUT, policyFlags);
6788 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6789 << "Injection should fail because the event is stale";
6790
6791 ASSERT_TRUE(mDispatcher->waitForIdle());
6792 mFakePolicy->assertNotifyAnrWasNotCalled();
6793 mWindow->assertNoEvents();
6794}
6795
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006796// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006797// Make sure that we don't notify policy twice about the same ANR.
6798TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006799 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006800 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6801 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006802
6803 // Once a focused event arrives, we get an ANR for this application
6804 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6805 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006806 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006807 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6808 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006809 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006810 const std::chrono::duration appTimeout =
6811 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6812 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006813
Vishnu Naire4df8752022-09-08 09:17:55 -07006814 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006815 // ANR should not be raised again. It is up to policy to do that if it desires.
6816 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006817
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006818 // If we now get a focused window, the ANR should stop, but the policy handles that via
6819 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006820 ASSERT_TRUE(mDispatcher->waitForIdle());
6821}
6822
6823// We have a focused application, but no focused window
6824TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006825 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006826 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6827 mWindow->consumeFocusEvent(false);
6828
6829 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006830 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006831 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006832 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6833 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006834
Vishnu Naire4df8752022-09-08 09:17:55 -07006835 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6836 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006837
6838 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006839 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006840 ASSERT_TRUE(mDispatcher->waitForIdle());
6841 mWindow->assertNoEvents();
6842}
6843
6844/**
6845 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6846 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6847 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6848 * the ANR mechanism should still work.
6849 *
6850 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6851 * DOWN event, while not responding on the second one.
6852 */
6853TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6854 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6855 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6856 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6857 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6858 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006859 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006860
6861 // Now send ACTION_UP, with identical timestamp
6862 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6863 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6864 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6865 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006866 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006867
6868 // We have now sent down and up. Let's consume first event and then ANR on the second.
6869 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6870 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006871 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006872}
6873
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006874// A spy window can receive an ANR
6875TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6876 sp<FakeWindowHandle> spy = addSpyWindow();
6877
6878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6880 WINDOW_LOCATION));
6881 mWindow->consumeMotionDown();
6882
6883 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6884 ASSERT_TRUE(sequenceNum);
6885 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006886 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006887
6888 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006889 spy->consumeMotionEvent(
6890 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006891 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006892 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006893}
6894
6895// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006896// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006897TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6898 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006899
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006900 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6901 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006902 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006903 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006904
6905 // Stuck on the ACTION_UP
6906 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006907 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006908
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006909 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006910 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006911 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6912 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006913
6914 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6915 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006916 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006917 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006918 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006919}
6920
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006921// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006922// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006923TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6924 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006925
6926 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006927 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6928 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006929
6930 mWindow->consumeMotionDown();
6931 // Stuck on the ACTION_UP
6932 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006933 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006934
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006935 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006936 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006937 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6938 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006939
6940 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6941 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006942 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006943 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006944 spy->assertNoEvents();
6945}
6946
6947TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6948 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6949
6950 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6951
6952 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6953 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6954 WINDOW_LOCATION));
6955
6956 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6957 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6958 ASSERT_TRUE(consumeSeq);
6959
Prabir Pradhanedd96402022-02-15 01:46:16 -08006960 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006961
6962 monitor.finishEvent(*consumeSeq);
6963 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6964
6965 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006966 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006967}
6968
6969// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6970// process events, you don't get an anr. When the window later becomes unresponsive again, you
6971// get an ANR again.
6972// 1. tap -> block on ACTION_UP -> receive ANR
6973// 2. consume all pending events (= queue becomes healthy again)
6974// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6975TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6976 tapOnWindow();
6977
6978 mWindow->consumeMotionDown();
6979 // Block on ACTION_UP
6980 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006981 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006982 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6983 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006984 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006985 mWindow->assertNoEvents();
6986
6987 tapOnWindow();
6988 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006989 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006990 mWindow->consumeMotionUp();
6991
6992 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006993 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006994 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006995 mWindow->assertNoEvents();
6996}
6997
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006998// If a connection remains unresponsive for a while, make sure policy is only notified once about
6999// it.
7000TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007002 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7003 WINDOW_LOCATION));
7004
7005 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007006 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007007 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007008 // 'notifyConnectionUnresponsive' should only be called once per connection
7009 mFakePolicy->assertNotifyAnrWasNotCalled();
7010 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007011 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007012 mWindow->consumeMotionEvent(
7013 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007014 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007015 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007016 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007017 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007018}
7019
7020/**
7021 * If a window is processing a motion event, and then a key event comes in, the key event should
7022 * not to to the focused window until the motion is processed.
7023 *
7024 * Warning!!!
7025 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7026 * and the injection timeout that we specify when injecting the key.
7027 * We must have the injection timeout (10ms) be smaller than
7028 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7029 *
7030 * If that value changes, this test should also change.
7031 */
7032TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7033 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7035
7036 tapOnWindow();
7037 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7038 ASSERT_TRUE(downSequenceNum);
7039 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7040 ASSERT_TRUE(upSequenceNum);
7041 // Don't finish the events yet, and send a key
7042 // Injection will "succeed" because we will eventually give up and send the key to the focused
7043 // window even if motions are still being processed. But because the injection timeout is short,
7044 // we will receive INJECTION_TIMED_OUT as the result.
7045
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007046 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007047 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007048 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7049 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007050 // Key will not be sent to the window, yet, because the window is still processing events
7051 // and the key remains pending, waiting for the touch events to be processed
7052 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7053 ASSERT_FALSE(keySequenceNum);
7054
7055 std::this_thread::sleep_for(500ms);
7056 // if we wait long enough though, dispatcher will give up, and still send the key
7057 // to the focused window, even though we have not yet finished the motion event
7058 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7059 mWindow->finishEvent(*downSequenceNum);
7060 mWindow->finishEvent(*upSequenceNum);
7061}
7062
7063/**
7064 * If a window is processing a motion event, and then a key event comes in, the key event should
7065 * not go to the focused window until the motion is processed.
7066 * If then a new motion comes in, then the pending key event should be going to the currently
7067 * focused window right away.
7068 */
7069TEST_F(InputDispatcherSingleWindowAnr,
7070 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7071 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7073
7074 tapOnWindow();
7075 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7076 ASSERT_TRUE(downSequenceNum);
7077 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7078 ASSERT_TRUE(upSequenceNum);
7079 // Don't finish the events yet, and send a key
7080 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007081 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007082 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7083 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007084 // At this point, key is still pending, and should not be sent to the application yet.
7085 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7086 ASSERT_FALSE(keySequenceNum);
7087
7088 // Now tap down again. It should cause the pending key to go to the focused window right away.
7089 tapOnWindow();
7090 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7091 // the other events yet. We can finish events in any order.
7092 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7093 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7094 mWindow->consumeMotionDown();
7095 mWindow->consumeMotionUp();
7096 mWindow->assertNoEvents();
7097}
7098
7099class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7100 virtual void SetUp() override {
7101 InputDispatcherTest::SetUp();
7102
Chris Yea209fde2020-07-22 13:54:51 -07007103 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007104 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007105 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7106 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007107 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007108 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007109 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007110
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007111 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7112 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007113 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007114 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007115
7116 // Set focused application.
7117 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007118 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007119
7120 // Expect one focus window exist in display.
7121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007122 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007123 mFocusedWindow->consumeFocusEvent(true);
7124 }
7125
7126 virtual void TearDown() override {
7127 InputDispatcherTest::TearDown();
7128
7129 mUnfocusedWindow.clear();
7130 mFocusedWindow.clear();
7131 }
7132
7133protected:
Chris Yea209fde2020-07-22 13:54:51 -07007134 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007135 sp<FakeWindowHandle> mUnfocusedWindow;
7136 sp<FakeWindowHandle> mFocusedWindow;
7137 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7138 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7139 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7140
7141 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7142
7143 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7144
7145private:
7146 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007147 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007148 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7149 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007150 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007151 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7152 location));
7153 }
7154};
7155
7156// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7157// should be ANR'd first.
7158TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007159 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007160 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7161 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007162 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007163 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007164 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007165 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007166 // We consumed all events, so no ANR
7167 ASSERT_TRUE(mDispatcher->waitForIdle());
7168 mFakePolicy->assertNotifyAnrWasNotCalled();
7169
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007170 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007171 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7172 FOCUSED_WINDOW_LOCATION));
7173 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7174 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007175
7176 const std::chrono::duration timeout =
7177 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007178 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007179 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7180 // sequence to make it consistent
7181 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007182 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007183 mFocusedWindow->consumeMotionDown();
7184 // This cancel is generated because the connection was unresponsive
7185 mFocusedWindow->consumeMotionCancel();
7186 mFocusedWindow->assertNoEvents();
7187 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007188 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007189 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7190 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007191 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007192}
7193
7194// If we have 2 windows with identical timeouts that are both unresponsive,
7195// it doesn't matter which order they should have ANR.
7196// But we should receive ANR for both.
7197TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7198 // Set the timeout for unfocused window to match the focused window
7199 mUnfocusedWindow->setDispatchingTimeout(10ms);
7200 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7201
7202 tapOnFocusedWindow();
7203 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007204 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7205 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7206 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007207
7208 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007209 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7210 mFocusedWindow->getToken() == anrConnectionToken2);
7211 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7212 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007213
7214 ASSERT_TRUE(mDispatcher->waitForIdle());
7215 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007216
7217 mFocusedWindow->consumeMotionDown();
7218 mFocusedWindow->consumeMotionUp();
7219 mUnfocusedWindow->consumeMotionOutside();
7220
Prabir Pradhanedd96402022-02-15 01:46:16 -08007221 sp<IBinder> responsiveToken1, responsiveToken2;
7222 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7223 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007224
7225 // Both applications should be marked as responsive, in any order
7226 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7227 mFocusedWindow->getToken() == responsiveToken2);
7228 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7229 mUnfocusedWindow->getToken() == responsiveToken2);
7230 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007231}
7232
7233// If a window is already not responding, the second tap on the same window should be ignored.
7234// We should also log an error to account for the dropped event (not tested here).
7235// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7236TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7237 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007238 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007239 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007240 // Receive the events, but don't respond
7241 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7242 ASSERT_TRUE(downEventSequenceNum);
7243 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7244 ASSERT_TRUE(upEventSequenceNum);
7245 const std::chrono::duration timeout =
7246 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007247 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007248
7249 // Tap once again
7250 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007251 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007252 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7253 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007254 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007255 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7256 FOCUSED_WINDOW_LOCATION));
7257 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7258 // valid touch target
7259 mUnfocusedWindow->assertNoEvents();
7260
7261 // Consume the first tap
7262 mFocusedWindow->finishEvent(*downEventSequenceNum);
7263 mFocusedWindow->finishEvent(*upEventSequenceNum);
7264 ASSERT_TRUE(mDispatcher->waitForIdle());
7265 // The second tap did not go to the focused window
7266 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007267 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007268 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7269 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007270 mFakePolicy->assertNotifyAnrWasNotCalled();
7271}
7272
7273// If you tap outside of all windows, there will not be ANR
7274TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007275 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007276 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7277 LOCATION_OUTSIDE_ALL_WINDOWS));
7278 ASSERT_TRUE(mDispatcher->waitForIdle());
7279 mFakePolicy->assertNotifyAnrWasNotCalled();
7280}
7281
7282// Since the focused window is paused, tapping on it should not produce any events
7283TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7284 mFocusedWindow->setPaused(true);
7285 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7286
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007287 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007288 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7289 FOCUSED_WINDOW_LOCATION));
7290
7291 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7292 ASSERT_TRUE(mDispatcher->waitForIdle());
7293 // Should not ANR because the window is paused, and touches shouldn't go to it
7294 mFakePolicy->assertNotifyAnrWasNotCalled();
7295
7296 mFocusedWindow->assertNoEvents();
7297 mUnfocusedWindow->assertNoEvents();
7298}
7299
7300/**
7301 * If a window is processing a motion event, and then a key event comes in, the key event should
7302 * not to to the focused window until the motion is processed.
7303 * If a different window becomes focused at this time, the key should go to that window instead.
7304 *
7305 * Warning!!!
7306 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7307 * and the injection timeout that we specify when injecting the key.
7308 * We must have the injection timeout (10ms) be smaller than
7309 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7310 *
7311 * If that value changes, this test should also change.
7312 */
7313TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7314 // Set a long ANR timeout to prevent it from triggering
7315 mFocusedWindow->setDispatchingTimeout(2s);
7316 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7317
7318 tapOnUnfocusedWindow();
7319 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7320 ASSERT_TRUE(downSequenceNum);
7321 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7322 ASSERT_TRUE(upSequenceNum);
7323 // Don't finish the events yet, and send a key
7324 // Injection will succeed because we will eventually give up and send the key to the focused
7325 // window even if motions are still being processed.
7326
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007327 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007328 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7329 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007330 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007331 // Key will not be sent to the window, yet, because the window is still processing events
7332 // and the key remains pending, waiting for the touch events to be processed
7333 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7334 ASSERT_FALSE(keySequenceNum);
7335
7336 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007337 mFocusedWindow->setFocusable(false);
7338 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007339 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007340 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007341
7342 // Focus events should precede the key events
7343 mUnfocusedWindow->consumeFocusEvent(true);
7344 mFocusedWindow->consumeFocusEvent(false);
7345
7346 // Finish the tap events, which should unblock dispatcher
7347 mUnfocusedWindow->finishEvent(*downSequenceNum);
7348 mUnfocusedWindow->finishEvent(*upSequenceNum);
7349
7350 // Now that all queues are cleared and no backlog in the connections, the key event
7351 // can finally go to the newly focused "mUnfocusedWindow".
7352 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7353 mFocusedWindow->assertNoEvents();
7354 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007355 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007356}
7357
7358// When the touch stream is split across 2 windows, and one of them does not respond,
7359// then ANR should be raised and the touch should be canceled for the unresponsive window.
7360// The other window should not be affected by that.
7361TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7362 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007363 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7364 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7365 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007366 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007367 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007368
7369 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007370 mDispatcher->notifyMotion(
7371 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7372 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007373
7374 const std::chrono::duration timeout =
7375 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007376 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007377
7378 mUnfocusedWindow->consumeMotionDown();
7379 mFocusedWindow->consumeMotionDown();
7380 // Focused window may or may not receive ACTION_MOVE
7381 // But it should definitely receive ACTION_CANCEL due to the ANR
7382 InputEvent* event;
7383 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7384 ASSERT_TRUE(moveOrCancelSequenceNum);
7385 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7386 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007387 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007388 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7389 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7390 mFocusedWindow->consumeMotionCancel();
7391 } else {
7392 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7393 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007394 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007395 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7396 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007397
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007398 mUnfocusedWindow->assertNoEvents();
7399 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007400 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007401}
7402
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007403/**
7404 * If we have no focused window, and a key comes in, we start the ANR timer.
7405 * The focused application should add a focused window before the timer runs out to prevent ANR.
7406 *
7407 * If the user touches another application during this time, the key should be dropped.
7408 * Next, if a new focused window comes in, without toggling the focused application,
7409 * then no ANR should occur.
7410 *
7411 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7412 * but in some cases the policy may not update the focused application.
7413 */
7414TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7415 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7416 std::make_shared<FakeApplicationHandle>();
7417 focusedApplication->setDispatchingTimeout(60ms);
7418 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7419 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7420 mFocusedWindow->setFocusable(false);
7421
7422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7423 mFocusedWindow->consumeFocusEvent(false);
7424
7425 // Send a key. The ANR timer should start because there is no focused window.
7426 // 'focusedApplication' will get blamed if this timer completes.
7427 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007428 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007429 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7430 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7431 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007432 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007433
7434 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7435 // then the injected touches won't cause the focused event to get dropped.
7436 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7437 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7438 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7439 // For this test, it means that the key would get delivered to the window once it becomes
7440 // focused.
7441 std::this_thread::sleep_for(10ms);
7442
7443 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007444 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7445 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7446 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007447
7448 // We do not consume the motion right away, because that would require dispatcher to first
7449 // process (== drop) the key event, and by that time, ANR will be raised.
7450 // Set the focused window first.
7451 mFocusedWindow->setFocusable(true);
7452 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7453 setFocusedWindow(mFocusedWindow);
7454 mFocusedWindow->consumeFocusEvent(true);
7455 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7456 // to another application. This could be a bug / behaviour in the policy.
7457
7458 mUnfocusedWindow->consumeMotionDown();
7459
7460 ASSERT_TRUE(mDispatcher->waitForIdle());
7461 // Should not ANR because we actually have a focused window. It was just added too slowly.
7462 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7463}
7464
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007465// These tests ensure we cannot send touch events to a window that's positioned behind a window
7466// that has feature NO_INPUT_CHANNEL.
7467// Layout:
7468// Top (closest to user)
7469// mNoInputWindow (above all windows)
7470// mBottomWindow
7471// Bottom (furthest from user)
7472class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7473 virtual void SetUp() override {
7474 InputDispatcherTest::SetUp();
7475
7476 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007477 mNoInputWindow =
7478 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7479 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007480 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007481 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007482 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7483 // It's perfectly valid for this window to not have an associated input channel
7484
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007485 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7486 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007487 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7488
7489 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7490 }
7491
7492protected:
7493 std::shared_ptr<FakeApplicationHandle> mApplication;
7494 sp<FakeWindowHandle> mNoInputWindow;
7495 sp<FakeWindowHandle> mBottomWindow;
7496};
7497
7498TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7499 PointF touchedPoint = {10, 10};
7500
Prabir Pradhan678438e2023-04-13 19:32:51 +00007501 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7502 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7503 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007504
7505 mNoInputWindow->assertNoEvents();
7506 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7507 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7508 // and therefore should prevent mBottomWindow from receiving touches
7509 mBottomWindow->assertNoEvents();
7510}
7511
7512/**
7513 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7514 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7515 */
7516TEST_F(InputDispatcherMultiWindowOcclusionTests,
7517 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007518 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7519 "Window with input channel and NO_INPUT_CHANNEL",
7520 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007521
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007522 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007523 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7524 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7525
7526 PointF touchedPoint = {10, 10};
7527
Prabir Pradhan678438e2023-04-13 19:32:51 +00007528 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7529 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7530 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007531
7532 mNoInputWindow->assertNoEvents();
7533 mBottomWindow->assertNoEvents();
7534}
7535
Vishnu Nair958da932020-08-21 17:12:37 -07007536class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7537protected:
7538 std::shared_ptr<FakeApplicationHandle> mApp;
7539 sp<FakeWindowHandle> mWindow;
7540 sp<FakeWindowHandle> mMirror;
7541
7542 virtual void SetUp() override {
7543 InputDispatcherTest::SetUp();
7544 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007545 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7546 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7547 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007548 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7549 mWindow->setFocusable(true);
7550 mMirror->setFocusable(true);
7551 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7552 }
7553};
7554
7555TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7556 // Request focus on a mirrored window
7557 setFocusedWindow(mMirror);
7558
7559 // window gets focused
7560 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007561 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7562 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007563 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7564}
7565
7566// A focused & mirrored window remains focused only if the window and its mirror are both
7567// focusable.
7568TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7569 setFocusedWindow(mMirror);
7570
7571 // window gets focused
7572 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007573 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7574 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007575 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7577 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007578 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7579
7580 mMirror->setFocusable(false);
7581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7582
7583 // window loses focus since one of the windows associated with the token in not focusable
7584 mWindow->consumeFocusEvent(false);
7585
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007586 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7587 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007588 mWindow->assertNoEvents();
7589}
7590
7591// A focused & mirrored window remains focused until the window and its mirror both become
7592// invisible.
7593TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7594 setFocusedWindow(mMirror);
7595
7596 // window gets focused
7597 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007598 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7599 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007600 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007601 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7602 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007603 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7604
7605 mMirror->setVisible(false);
7606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7607
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007608 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7609 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007610 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7612 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007613 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7614
7615 mWindow->setVisible(false);
7616 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7617
7618 // window loses focus only after all windows associated with the token become invisible.
7619 mWindow->consumeFocusEvent(false);
7620
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007621 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7622 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007623 mWindow->assertNoEvents();
7624}
7625
7626// A focused & mirrored window remains focused until both windows are removed.
7627TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7628 setFocusedWindow(mMirror);
7629
7630 // window gets focused
7631 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7633 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007634 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007635 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7636 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007637 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7638
7639 // single window is removed but the window token remains focused
7640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7641
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007642 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7643 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007644 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007645 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7646 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007647 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7648
7649 // Both windows are removed
7650 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7651 mWindow->consumeFocusEvent(false);
7652
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007653 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7654 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007655 mWindow->assertNoEvents();
7656}
7657
7658// Focus request can be pending until one window becomes visible.
7659TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7660 // Request focus on an invisible mirror.
7661 mWindow->setVisible(false);
7662 mMirror->setVisible(false);
7663 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7664 setFocusedWindow(mMirror);
7665
7666 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007668 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7669 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007670
7671 mMirror->setVisible(true);
7672 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7673
7674 // window gets focused
7675 mWindow->consumeFocusEvent(true);
7676 // window gets the pending key event
7677 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7678}
Prabir Pradhan99987712020-11-10 18:43:05 -08007679
7680class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7681protected:
7682 std::shared_ptr<FakeApplicationHandle> mApp;
7683 sp<FakeWindowHandle> mWindow;
7684 sp<FakeWindowHandle> mSecondWindow;
7685
7686 void SetUp() override {
7687 InputDispatcherTest::SetUp();
7688 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007689 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007690 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007691 mSecondWindow =
7692 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007693 mSecondWindow->setFocusable(true);
7694
7695 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7696 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7697
7698 setFocusedWindow(mWindow);
7699 mWindow->consumeFocusEvent(true);
7700 }
7701
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007702 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007703 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007704 }
7705
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007706 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7707 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007708 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007709 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7710 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007711 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007712 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007713 }
7714};
7715
7716TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7717 // Ensure that capture cannot be obtained for unfocused windows.
7718 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7719 mFakePolicy->assertSetPointerCaptureNotCalled();
7720 mSecondWindow->assertNoEvents();
7721
7722 // Ensure that capture can be enabled from the focus window.
7723 requestAndVerifyPointerCapture(mWindow, true);
7724
7725 // Ensure that capture cannot be disabled from a window that does not have capture.
7726 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7727 mFakePolicy->assertSetPointerCaptureNotCalled();
7728
7729 // Ensure that capture can be disabled from the window with capture.
7730 requestAndVerifyPointerCapture(mWindow, false);
7731}
7732
7733TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007734 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007735
7736 setFocusedWindow(mSecondWindow);
7737
7738 // Ensure that the capture disabled event was sent first.
7739 mWindow->consumeCaptureEvent(false);
7740 mWindow->consumeFocusEvent(false);
7741 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007742 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007743
7744 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007745 notifyPointerCaptureChanged({});
7746 notifyPointerCaptureChanged(request);
7747 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007748 mWindow->assertNoEvents();
7749 mSecondWindow->assertNoEvents();
7750 mFakePolicy->assertSetPointerCaptureNotCalled();
7751}
7752
7753TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007754 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007755
7756 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007757 notifyPointerCaptureChanged({});
7758 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007759
7760 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007761 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007762 mWindow->consumeCaptureEvent(false);
7763 mWindow->assertNoEvents();
7764}
7765
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007766TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7767 requestAndVerifyPointerCapture(mWindow, true);
7768
7769 // The first window loses focus.
7770 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007771 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007772 mWindow->consumeCaptureEvent(false);
7773
7774 // Request Pointer Capture from the second window before the notification from InputReader
7775 // arrives.
7776 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007777 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007778
7779 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007780 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007781
7782 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007783 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007784
7785 mSecondWindow->consumeFocusEvent(true);
7786 mSecondWindow->consumeCaptureEvent(true);
7787}
7788
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007789TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7790 // App repeatedly enables and disables capture.
7791 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7792 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7793 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7794 mFakePolicy->assertSetPointerCaptureCalled(false);
7795 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7796 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7797
7798 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7799 // first request is now stale, this should do nothing.
7800 notifyPointerCaptureChanged(firstRequest);
7801 mWindow->assertNoEvents();
7802
7803 // InputReader notifies that the second request was enabled.
7804 notifyPointerCaptureChanged(secondRequest);
7805 mWindow->consumeCaptureEvent(true);
7806}
7807
Prabir Pradhan7092e262022-05-03 16:51:09 +00007808TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7809 requestAndVerifyPointerCapture(mWindow, true);
7810
7811 // App toggles pointer capture off and on.
7812 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7813 mFakePolicy->assertSetPointerCaptureCalled(false);
7814
7815 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7816 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7817
7818 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7819 // preceding "disable" request.
7820 notifyPointerCaptureChanged(enableRequest);
7821
7822 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7823 // any notifications.
7824 mWindow->assertNoEvents();
7825}
7826
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007827class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7828protected:
7829 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007830
7831 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7832 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7833
7834 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7835 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7836
7837 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7838 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7839 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7840 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7841 MAXIMUM_OBSCURING_OPACITY);
7842
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007843 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007844 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007845 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007846
7847 sp<FakeWindowHandle> mTouchWindow;
7848
7849 virtual void SetUp() override {
7850 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007851 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007852 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7853 }
7854
7855 virtual void TearDown() override {
7856 InputDispatcherTest::TearDown();
7857 mTouchWindow.clear();
7858 }
7859
chaviw3277faf2021-05-19 16:45:23 -05007860 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7861 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007862 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007863 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007864 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007865 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007866 return window;
7867 }
7868
7869 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7870 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7871 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007872 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007873 // Generate an arbitrary PID based on the UID
7874 window->setOwnerInfo(1777 + (uid % 10000), uid);
7875 return window;
7876 }
7877
7878 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007879 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7880 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7881 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007882 }
7883};
7884
7885TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007886 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007887 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007889
7890 touch();
7891
7892 mTouchWindow->assertNoEvents();
7893}
7894
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007895TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007896 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7897 const sp<FakeWindowHandle>& w =
7898 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7900
7901 touch();
7902
7903 mTouchWindow->assertNoEvents();
7904}
7905
7906TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007907 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7908 const sp<FakeWindowHandle>& w =
7909 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7910 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7911
7912 touch();
7913
7914 w->assertNoEvents();
7915}
7916
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007917TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007918 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7919 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007920
7921 touch();
7922
7923 mTouchWindow->consumeAnyMotionDown();
7924}
7925
7926TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007927 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007928 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007929 w->setFrame(Rect(0, 0, 50, 50));
7930 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007931
7932 touch({PointF{100, 100}});
7933
7934 mTouchWindow->consumeAnyMotionDown();
7935}
7936
7937TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007938 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007939 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007940 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7941
7942 touch();
7943
7944 mTouchWindow->consumeAnyMotionDown();
7945}
7946
7947TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7948 const sp<FakeWindowHandle>& w =
7949 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7950 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007951
7952 touch();
7953
7954 mTouchWindow->consumeAnyMotionDown();
7955}
7956
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007957TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7958 const sp<FakeWindowHandle>& w =
7959 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7961
7962 touch();
7963
7964 w->assertNoEvents();
7965}
7966
7967/**
7968 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7969 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7970 * window, the occluding window will still receive ACTION_OUTSIDE event.
7971 */
7972TEST_F(InputDispatcherUntrustedTouchesTest,
7973 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7974 const sp<FakeWindowHandle>& w =
7975 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007976 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007977 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7978
7979 touch();
7980
7981 w->consumeMotionOutside();
7982}
7983
7984TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7985 const sp<FakeWindowHandle>& w =
7986 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007987 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007988 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7989
7990 touch();
7991
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007992 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007993}
7994
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007995TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007996 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007997 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7998 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007999 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8000
8001 touch();
8002
8003 mTouchWindow->consumeAnyMotionDown();
8004}
8005
8006TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8007 const sp<FakeWindowHandle>& w =
8008 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8009 MAXIMUM_OBSCURING_OPACITY);
8010 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008011
8012 touch();
8013
8014 mTouchWindow->consumeAnyMotionDown();
8015}
8016
8017TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008018 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008019 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8020 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008021 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8022
8023 touch();
8024
8025 mTouchWindow->assertNoEvents();
8026}
8027
8028TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8029 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8030 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008031 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8032 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008033 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008034 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8035 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008036 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8037
8038 touch();
8039
8040 mTouchWindow->assertNoEvents();
8041}
8042
8043TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8044 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8045 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008046 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8047 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008048 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008049 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8050 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8052
8053 touch();
8054
8055 mTouchWindow->consumeAnyMotionDown();
8056}
8057
8058TEST_F(InputDispatcherUntrustedTouchesTest,
8059 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8060 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008061 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8062 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008063 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008064 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8065 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008066 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8067
8068 touch();
8069
8070 mTouchWindow->consumeAnyMotionDown();
8071}
8072
8073TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8074 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008075 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8076 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008077 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008078 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8079 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008080 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008081
8082 touch();
8083
8084 mTouchWindow->assertNoEvents();
8085}
8086
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008087TEST_F(InputDispatcherUntrustedTouchesTest,
8088 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8089 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008090 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8091 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008092 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008093 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8094 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008095 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8096
8097 touch();
8098
8099 mTouchWindow->assertNoEvents();
8100}
8101
8102TEST_F(InputDispatcherUntrustedTouchesTest,
8103 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8104 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008105 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8106 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008107 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008108 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8109 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008110 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8111
8112 touch();
8113
8114 mTouchWindow->consumeAnyMotionDown();
8115}
8116
8117TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8118 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008119 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8120 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8122
8123 touch();
8124
8125 mTouchWindow->consumeAnyMotionDown();
8126}
8127
8128TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8129 const sp<FakeWindowHandle>& w =
8130 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8131 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8132
8133 touch();
8134
8135 mTouchWindow->consumeAnyMotionDown();
8136}
8137
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008138TEST_F(InputDispatcherUntrustedTouchesTest,
8139 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8140 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8141 const sp<FakeWindowHandle>& w =
8142 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8143 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8144
8145 touch();
8146
8147 mTouchWindow->assertNoEvents();
8148}
8149
8150TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8151 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8152 const sp<FakeWindowHandle>& w =
8153 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8154 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8155
8156 touch();
8157
8158 mTouchWindow->consumeAnyMotionDown();
8159}
8160
8161TEST_F(InputDispatcherUntrustedTouchesTest,
8162 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8163 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8164 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008165 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8166 OPACITY_ABOVE_THRESHOLD);
8167 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8168
8169 touch();
8170
8171 mTouchWindow->consumeAnyMotionDown();
8172}
8173
8174TEST_F(InputDispatcherUntrustedTouchesTest,
8175 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8176 const sp<FakeWindowHandle>& w1 =
8177 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8178 OPACITY_BELOW_THRESHOLD);
8179 const sp<FakeWindowHandle>& w2 =
8180 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8181 OPACITY_BELOW_THRESHOLD);
8182 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8183
8184 touch();
8185
8186 mTouchWindow->assertNoEvents();
8187}
8188
8189/**
8190 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8191 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8192 * (which alone would result in allowing touches) does not affect the blocking behavior.
8193 */
8194TEST_F(InputDispatcherUntrustedTouchesTest,
8195 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8196 const sp<FakeWindowHandle>& wB =
8197 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8198 OPACITY_BELOW_THRESHOLD);
8199 const sp<FakeWindowHandle>& wC =
8200 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8201 OPACITY_BELOW_THRESHOLD);
8202 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8203
8204 touch();
8205
8206 mTouchWindow->assertNoEvents();
8207}
8208
8209/**
8210 * This test is testing that a window from a different UID but with same application token doesn't
8211 * block the touch. Apps can share the application token for close UI collaboration for example.
8212 */
8213TEST_F(InputDispatcherUntrustedTouchesTest,
8214 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8215 const sp<FakeWindowHandle>& w =
8216 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8217 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008218 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8219
8220 touch();
8221
8222 mTouchWindow->consumeAnyMotionDown();
8223}
8224
arthurhungb89ccb02020-12-30 16:19:01 +08008225class InputDispatcherDragTests : public InputDispatcherTest {
8226protected:
8227 std::shared_ptr<FakeApplicationHandle> mApp;
8228 sp<FakeWindowHandle> mWindow;
8229 sp<FakeWindowHandle> mSecondWindow;
8230 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008231 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008232 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8233 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008234
8235 void SetUp() override {
8236 InputDispatcherTest::SetUp();
8237 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008238 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008239 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008240
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008241 mSecondWindow =
8242 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008243 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008244
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008245 mSpyWindow =
8246 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008247 mSpyWindow->setSpy(true);
8248 mSpyWindow->setTrustedOverlay(true);
8249 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8250
arthurhungb89ccb02020-12-30 16:19:01 +08008251 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008252 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008253 }
8254
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008255 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8256 switch (fromSource) {
8257 case AINPUT_SOURCE_TOUCHSCREEN:
8258 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8259 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8260 ADISPLAY_ID_DEFAULT, {50, 50}))
8261 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8262 break;
8263 case AINPUT_SOURCE_STYLUS:
8264 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8265 injectMotionEvent(
8266 mDispatcher,
8267 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8268 AINPUT_SOURCE_STYLUS)
8269 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008270 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008271 .x(50)
8272 .y(50))
8273 .build()));
8274 break;
8275 case AINPUT_SOURCE_MOUSE:
8276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8277 injectMotionEvent(
8278 mDispatcher,
8279 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8280 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8281 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008282 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008283 .x(50)
8284 .y(50))
8285 .build()));
8286 break;
8287 default:
8288 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8289 }
arthurhungb89ccb02020-12-30 16:19:01 +08008290
8291 // Window should receive motion event.
8292 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008293 // Spy window should also receive motion event
8294 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008295 }
8296
8297 // Start performing drag, we will create a drag window and transfer touch to it.
8298 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8299 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008300 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008301 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008302 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008303 }
arthurhungb89ccb02020-12-30 16:19:01 +08008304
8305 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008306 mDragWindow =
8307 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008308 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008309 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008310 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008311
8312 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008313 bool transferred =
8314 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008315 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008316 if (transferred) {
8317 mWindow->consumeMotionCancel();
8318 mDragWindow->consumeMotionDown();
8319 }
8320 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008321 }
8322};
8323
8324TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008325 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008326
8327 // Move on window.
8328 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8329 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8330 ADISPLAY_ID_DEFAULT, {50, 50}))
8331 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8332 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8333 mWindow->consumeDragEvent(false, 50, 50);
8334 mSecondWindow->assertNoEvents();
8335
8336 // Move to another window.
8337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8338 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8339 ADISPLAY_ID_DEFAULT, {150, 50}))
8340 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8341 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8342 mWindow->consumeDragEvent(true, 150, 50);
8343 mSecondWindow->consumeDragEvent(false, 50, 50);
8344
8345 // Move back to original window.
8346 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8347 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8348 ADISPLAY_ID_DEFAULT, {50, 50}))
8349 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8350 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8351 mWindow->consumeDragEvent(false, 50, 50);
8352 mSecondWindow->consumeDragEvent(true, -50, 50);
8353
8354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8355 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8356 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8357 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8358 mWindow->assertNoEvents();
8359 mSecondWindow->assertNoEvents();
8360}
8361
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008362TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008363 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008364
8365 // No cancel event after drag start
8366 mSpyWindow->assertNoEvents();
8367
8368 const MotionEvent secondFingerDownEvent =
8369 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8370 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008371 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8372 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008373 .build();
8374 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8375 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8376 InputEventInjectionSync::WAIT_FOR_RESULT))
8377 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8378
8379 // Receives cancel for first pointer after next pointer down
8380 mSpyWindow->consumeMotionCancel();
8381 mSpyWindow->consumeMotionDown();
8382
8383 mSpyWindow->assertNoEvents();
8384}
8385
arthurhungf452d0b2021-01-06 00:19:52 +08008386TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008387 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008388
8389 // Move on window.
8390 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8391 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8392 ADISPLAY_ID_DEFAULT, {50, 50}))
8393 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8394 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8395 mWindow->consumeDragEvent(false, 50, 50);
8396 mSecondWindow->assertNoEvents();
8397
8398 // Move to another window.
8399 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8400 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8401 ADISPLAY_ID_DEFAULT, {150, 50}))
8402 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8403 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8404 mWindow->consumeDragEvent(true, 150, 50);
8405 mSecondWindow->consumeDragEvent(false, 50, 50);
8406
8407 // drop to another window.
8408 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8409 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8410 {150, 50}))
8411 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8412 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8413 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8414 mWindow->assertNoEvents();
8415 mSecondWindow->assertNoEvents();
8416}
8417
arthurhung6d4bed92021-03-17 11:59:33 +08008418TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008419 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008420
8421 // Move on window and keep button pressed.
8422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8423 injectMotionEvent(mDispatcher,
8424 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8425 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008426 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008427 .build()))
8428 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8429 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8430 mWindow->consumeDragEvent(false, 50, 50);
8431 mSecondWindow->assertNoEvents();
8432
8433 // Move to another window and release button, expect to drop item.
8434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8435 injectMotionEvent(mDispatcher,
8436 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8437 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008438 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008439 .build()))
8440 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8441 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8442 mWindow->assertNoEvents();
8443 mSecondWindow->assertNoEvents();
8444 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8445
8446 // nothing to the window.
8447 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8448 injectMotionEvent(mDispatcher,
8449 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8450 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008451 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008452 .build()))
8453 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8454 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8455 mWindow->assertNoEvents();
8456 mSecondWindow->assertNoEvents();
8457}
8458
Arthur Hung54745652022-04-20 07:17:41 +00008459TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008460 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008461
8462 // Set second window invisible.
8463 mSecondWindow->setVisible(false);
8464 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8465
8466 // Move on window.
8467 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8468 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8469 ADISPLAY_ID_DEFAULT, {50, 50}))
8470 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8471 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8472 mWindow->consumeDragEvent(false, 50, 50);
8473 mSecondWindow->assertNoEvents();
8474
8475 // Move to another window.
8476 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8477 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8478 ADISPLAY_ID_DEFAULT, {150, 50}))
8479 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8480 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8481 mWindow->consumeDragEvent(true, 150, 50);
8482 mSecondWindow->assertNoEvents();
8483
8484 // drop to another window.
8485 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8486 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8487 {150, 50}))
8488 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8489 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8490 mFakePolicy->assertDropTargetEquals(nullptr);
8491 mWindow->assertNoEvents();
8492 mSecondWindow->assertNoEvents();
8493}
8494
Arthur Hung54745652022-04-20 07:17:41 +00008495TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008496 // Ensure window could track pointerIds if it didn't support split touch.
8497 mWindow->setPreventSplitting(true);
8498
Arthur Hung54745652022-04-20 07:17:41 +00008499 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8500 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8501 {50, 50}))
8502 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8503 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8504
8505 const MotionEvent secondFingerDownEvent =
8506 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8507 .displayId(ADISPLAY_ID_DEFAULT)
8508 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008509 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8510 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008511 .build();
8512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8513 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8514 InputEventInjectionSync::WAIT_FOR_RESULT))
8515 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008516 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008517
8518 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008519 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008520}
8521
8522TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8523 // First down on second window.
8524 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8525 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8526 {150, 50}))
8527 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8528
8529 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8530
8531 // Second down on first window.
8532 const MotionEvent secondFingerDownEvent =
8533 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8534 .displayId(ADISPLAY_ID_DEFAULT)
8535 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008536 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8537 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008538 .build();
8539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8540 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8541 InputEventInjectionSync::WAIT_FOR_RESULT))
8542 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8543 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8544
8545 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008546 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008547
8548 // Move on window.
8549 const MotionEvent secondFingerMoveEvent =
8550 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8551 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008552 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8553 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008554 .build();
8555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8556 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8557 InputEventInjectionSync::WAIT_FOR_RESULT));
8558 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8559 mWindow->consumeDragEvent(false, 50, 50);
8560 mSecondWindow->consumeMotionMove();
8561
8562 // Release the drag pointer should perform drop.
8563 const MotionEvent secondFingerUpEvent =
8564 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8565 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008566 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8567 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008568 .build();
8569 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8570 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8571 InputEventInjectionSync::WAIT_FOR_RESULT));
8572 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8573 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8574 mWindow->assertNoEvents();
8575 mSecondWindow->consumeMotionMove();
8576}
8577
Arthur Hung3915c1f2022-05-31 07:17:17 +00008578TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008579 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008580
8581 // Update window of second display.
8582 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008583 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008584 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8585
8586 // Let second display has a touch state.
8587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8588 injectMotionEvent(mDispatcher,
8589 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8590 AINPUT_SOURCE_TOUCHSCREEN)
8591 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008592 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008593 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008594 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008595 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008596 // Update window again.
8597 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8598
8599 // Move on window.
8600 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8601 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8602 ADISPLAY_ID_DEFAULT, {50, 50}))
8603 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8604 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8605 mWindow->consumeDragEvent(false, 50, 50);
8606 mSecondWindow->assertNoEvents();
8607
8608 // Move to another window.
8609 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8610 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8611 ADISPLAY_ID_DEFAULT, {150, 50}))
8612 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8613 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8614 mWindow->consumeDragEvent(true, 150, 50);
8615 mSecondWindow->consumeDragEvent(false, 50, 50);
8616
8617 // drop to another window.
8618 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8619 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8620 {150, 50}))
8621 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8622 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8623 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8624 mWindow->assertNoEvents();
8625 mSecondWindow->assertNoEvents();
8626}
8627
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008628TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8629 startDrag(true, AINPUT_SOURCE_MOUSE);
8630 // Move on window.
8631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8632 injectMotionEvent(mDispatcher,
8633 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8634 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8635 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008636 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008637 .x(50)
8638 .y(50))
8639 .build()))
8640 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8641 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8642 mWindow->consumeDragEvent(false, 50, 50);
8643 mSecondWindow->assertNoEvents();
8644
8645 // Move to another window.
8646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8647 injectMotionEvent(mDispatcher,
8648 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8649 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8650 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008651 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008652 .x(150)
8653 .y(50))
8654 .build()))
8655 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8656 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8657 mWindow->consumeDragEvent(true, 150, 50);
8658 mSecondWindow->consumeDragEvent(false, 50, 50);
8659
8660 // drop to another window.
8661 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8662 injectMotionEvent(mDispatcher,
8663 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8664 .buttonState(0)
8665 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008666 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008667 .x(150)
8668 .y(50))
8669 .build()))
8670 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8671 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8672 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8673 mWindow->assertNoEvents();
8674 mSecondWindow->assertNoEvents();
8675}
8676
Vishnu Nair062a8672021-09-03 16:07:44 -07008677class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8678
8679TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8680 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008681 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8682 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008683 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008684 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8685 window->setFocusable(true);
8686 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8687 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008688 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008689
8690 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008691 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008692 window->assertNoEvents();
8693
Prabir Pradhan678438e2023-04-13 19:32:51 +00008694 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8695 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008696 window->assertNoEvents();
8697
8698 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008699 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008700 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8701
Prabir Pradhan678438e2023-04-13 19:32:51 +00008702 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008703 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8704
Prabir Pradhan678438e2023-04-13 19:32:51 +00008705 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8706 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008707 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8708 window->assertNoEvents();
8709}
8710
8711TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8712 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8713 std::make_shared<FakeApplicationHandle>();
8714 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008715 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8716 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008717 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8718 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008719 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008720 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008721 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8722 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008723 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008724 window->setOwnerInfo(222, 222);
8725 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8726 window->setFocusable(true);
8727 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8728 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008729 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008730
8731 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008732 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008733 window->assertNoEvents();
8734
Prabir Pradhan678438e2023-04-13 19:32:51 +00008735 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8736 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008737 window->assertNoEvents();
8738
8739 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008740 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008741 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8742
Prabir Pradhan678438e2023-04-13 19:32:51 +00008743 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008744 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8745
Prabir Pradhan678438e2023-04-13 19:32:51 +00008746 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8747 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008748 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8749 window->assertNoEvents();
8750}
8751
8752TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8753 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8754 std::make_shared<FakeApplicationHandle>();
8755 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008756 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8757 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008758 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8759 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008760 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008761 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008762 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8763 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008764 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008765 window->setOwnerInfo(222, 222);
8766 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8767 window->setFocusable(true);
8768 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8769 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008770 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008771
8772 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008773 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008774 window->assertNoEvents();
8775
Prabir Pradhan678438e2023-04-13 19:32:51 +00008776 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8777 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008778 window->assertNoEvents();
8779
8780 // When the window is no longer obscured because it went on top, it should get input
8781 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8782
Prabir Pradhan678438e2023-04-13 19:32:51 +00008783 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008784 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8785
Prabir Pradhan678438e2023-04-13 19:32:51 +00008786 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8787 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008788 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8789 window->assertNoEvents();
8790}
8791
Antonio Kantekf16f2832021-09-28 04:39:20 +00008792class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8793protected:
8794 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008795 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008796 sp<FakeWindowHandle> mWindow;
8797 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008798 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008799
8800 void SetUp() override {
8801 InputDispatcherTest::SetUp();
8802
8803 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008804 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008805 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008806 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008807 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008808 mSecondWindow =
8809 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008810 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008811 mThirdWindow =
8812 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8813 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8814 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008815
8816 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008817 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8818 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8819 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008820 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008821
Antonio Kantek15beb512022-06-13 22:35:41 +00008822 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008823 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008824 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008825 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8826 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008827 mThirdWindow->assertNoEvents();
8828 }
8829
8830 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8831 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008832 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008833 SECOND_DISPLAY_ID)) {
8834 mWindow->assertNoEvents();
8835 mSecondWindow->assertNoEvents();
8836 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008837 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008838 }
8839
Antonio Kantek15beb512022-06-13 22:35:41 +00008840 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8841 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008842 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8843 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008844 mWindow->consumeTouchModeEvent(inTouchMode);
8845 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008846 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008847 }
8848};
8849
Antonio Kantek26defcf2022-02-08 01:12:27 +00008850TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008851 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008852 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8853 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008854 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008855}
8856
Antonio Kantek26defcf2022-02-08 01:12:27 +00008857TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8858 const WindowInfo& windowInfo = *mWindow->getInfo();
8859 int32_t ownerPid = windowInfo.ownerPid;
8860 int32_t ownerUid = windowInfo.ownerUid;
8861 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8862 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008863 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008864 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008865 mWindow->assertNoEvents();
8866 mSecondWindow->assertNoEvents();
8867}
8868
8869TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8870 const WindowInfo& windowInfo = *mWindow->getInfo();
8871 int32_t ownerPid = windowInfo.ownerPid;
8872 int32_t ownerUid = windowInfo.ownerUid;
8873 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008874 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008875 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008876}
8877
Antonio Kantekf16f2832021-09-28 04:39:20 +00008878TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008879 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008880 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8881 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008882 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008883 mWindow->assertNoEvents();
8884 mSecondWindow->assertNoEvents();
8885}
8886
Antonio Kantek15beb512022-06-13 22:35:41 +00008887TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8888 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8889 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8890 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008891 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008892 mWindow->assertNoEvents();
8893 mSecondWindow->assertNoEvents();
8894 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8895}
8896
Antonio Kantek48710e42022-03-24 14:19:30 -07008897TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8898 // Interact with the window first.
8899 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8900 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8901 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8902
8903 // Then remove focus.
8904 mWindow->setFocusable(false);
8905 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8906
8907 // Assert that caller can switch touch mode by owning one of the last interacted window.
8908 const WindowInfo& windowInfo = *mWindow->getInfo();
8909 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8910 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008911 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008912}
8913
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008914class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8915public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008916 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008917 std::shared_ptr<FakeApplicationHandle> application =
8918 std::make_shared<FakeApplicationHandle>();
8919 std::string name = "Fake Spy ";
8920 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008921 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8922 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008923 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008924 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008925 return spy;
8926 }
8927
8928 sp<FakeWindowHandle> createForeground() {
8929 std::shared_ptr<FakeApplicationHandle> application =
8930 std::make_shared<FakeApplicationHandle>();
8931 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008932 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8933 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008934 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008935 return window;
8936 }
8937
8938private:
8939 int mSpyCount{0};
8940};
8941
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008942using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008943/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008944 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8945 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008946TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8947 ScopedSilentDeath _silentDeath;
8948
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008949 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008950 spy->setTrustedOverlay(false);
8951 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8952 ".* not a trusted overlay");
8953}
8954
8955/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008956 * Input injection into a display with a spy window but no foreground windows should succeed.
8957 */
8958TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008959 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8961
8962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8963 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8964 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8965 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8966}
8967
8968/**
8969 * Verify the order in which different input windows receive events. The touched foreground window
8970 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8971 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8972 * receive events before ones belows it.
8973 *
8974 * Here, we set up a scenario with four windows in the following Z order from the top:
8975 * spy1, spy2, window, spy3.
8976 * We then inject an event and verify that the foreground "window" receives it first, followed by
8977 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8978 * window.
8979 */
8980TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8981 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008982 auto spy1 = createSpy();
8983 auto spy2 = createSpy();
8984 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008985 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8986 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8987 const size_t numChannels = channels.size();
8988
Michael Wright8e9a8562022-02-09 13:44:29 +00008989 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008990 if (!epollFd.ok()) {
8991 FAIL() << "Failed to create epoll fd";
8992 }
8993
8994 for (size_t i = 0; i < numChannels; i++) {
8995 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8996 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8997 FAIL() << "Failed to add fd to epoll";
8998 }
8999 }
9000
9001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9002 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9003 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9004
9005 std::vector<size_t> eventOrder;
9006 std::vector<struct epoll_event> events(numChannels);
9007 for (;;) {
9008 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9009 (100ms).count());
9010 if (nFds < 0) {
9011 FAIL() << "Failed to call epoll_wait";
9012 }
9013 if (nFds == 0) {
9014 break; // epoll_wait timed out
9015 }
9016 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009017 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009018 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009019 channels[i]->consumeMotionDown();
9020 }
9021 }
9022
9023 // Verify the order in which the events were received.
9024 EXPECT_EQ(3u, eventOrder.size());
9025 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9026 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9027 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9028}
9029
9030/**
9031 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9032 */
9033TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9034 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009035 auto spy = createSpy();
9036 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9038
9039 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9040 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9041 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9042 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9043 spy->assertNoEvents();
9044}
9045
9046/**
9047 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9048 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9049 * to the window.
9050 */
9051TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9052 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009053 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009054 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9055 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9056
9057 // Inject an event outside the spy window's touchable region.
9058 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9059 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9060 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9061 window->consumeMotionDown();
9062 spy->assertNoEvents();
9063 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9064 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9065 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9066 window->consumeMotionUp();
9067 spy->assertNoEvents();
9068
9069 // Inject an event inside the spy window's touchable region.
9070 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9071 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9072 {5, 10}))
9073 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9074 window->consumeMotionDown();
9075 spy->consumeMotionDown();
9076}
9077
9078/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009079 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009080 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009081 */
9082TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9083 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009084 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009085 auto spy = createSpy();
9086 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009087 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009088 spy->setFrame(Rect{0, 0, 20, 20});
9089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9090
9091 // Inject an event outside the spy window's frame and touchable region.
9092 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009093 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9094 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009095 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9096 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009097 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009098}
9099
9100/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009101 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9102 * pointers that are down within its bounds.
9103 */
9104TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9105 auto windowLeft = createForeground();
9106 windowLeft->setFrame({0, 0, 100, 200});
9107 auto windowRight = createForeground();
9108 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009109 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009110 spy->setFrame({0, 0, 200, 200});
9111 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9112
9113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9114 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9115 {50, 50}))
9116 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9117 windowLeft->consumeMotionDown();
9118 spy->consumeMotionDown();
9119
9120 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009121 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009122 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009123 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9124 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009125 .build();
9126 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9127 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9128 InputEventInjectionSync::WAIT_FOR_RESULT))
9129 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9130 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009131 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009132}
9133
9134/**
9135 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9136 * the spy should receive the second pointer with ACTION_DOWN.
9137 */
9138TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9139 auto window = createForeground();
9140 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009141 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009142 spyRight->setFrame({100, 0, 200, 200});
9143 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9144
9145 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9146 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9147 {50, 50}))
9148 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9149 window->consumeMotionDown();
9150 spyRight->assertNoEvents();
9151
9152 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009153 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009154 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009155 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9156 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009157 .build();
9158 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9159 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9160 InputEventInjectionSync::WAIT_FOR_RESULT))
9161 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009162 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009163 spyRight->consumeMotionDown();
9164}
9165
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009166/**
9167 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9168 * windows should be allowed to control split touch.
9169 */
9170TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009171 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009172 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009173 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009174 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009175
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009176 auto window = createForeground();
9177 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009178
9179 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9180
9181 // First finger down, no window touched.
9182 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9183 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9184 {100, 200}))
9185 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9186 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9187 window->assertNoEvents();
9188
9189 // Second finger down on window, the window should receive touch down.
9190 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009191 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009192 .displayId(ADISPLAY_ID_DEFAULT)
9193 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009194 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9195 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009196 .build();
9197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9198 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9199 InputEventInjectionSync::WAIT_FOR_RESULT))
9200 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9201
9202 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009203 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009204}
9205
9206/**
9207 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9208 * do not receive key events.
9209 */
9210TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009211 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009212 spy->setFocusable(false);
9213
9214 auto window = createForeground();
9215 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9216 setFocusedWindow(window);
9217 window->consumeFocusEvent(true);
9218
9219 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9220 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9221 window->consumeKeyDown(ADISPLAY_ID_NONE);
9222
9223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9224 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9225 window->consumeKeyUp(ADISPLAY_ID_NONE);
9226
9227 spy->assertNoEvents();
9228}
9229
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009230using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9231
9232/**
9233 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9234 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9235 */
9236TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9237 auto window = createForeground();
9238 auto spy1 = createSpy();
9239 auto spy2 = createSpy();
9240 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9241
9242 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9243 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9244 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9245 window->consumeMotionDown();
9246 spy1->consumeMotionDown();
9247 spy2->consumeMotionDown();
9248
9249 // Pilfer pointers from the second spy window.
9250 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9251 spy2->assertNoEvents();
9252 spy1->consumeMotionCancel();
9253 window->consumeMotionCancel();
9254
9255 // The rest of the gesture should only be sent to the second spy window.
9256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9257 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9258 ADISPLAY_ID_DEFAULT))
9259 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9260 spy2->consumeMotionMove();
9261 spy1->assertNoEvents();
9262 window->assertNoEvents();
9263}
9264
9265/**
9266 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9267 * in the middle of the gesture.
9268 */
9269TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9270 auto window = createForeground();
9271 auto spy = createSpy();
9272 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9273
9274 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9275 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9276 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9277 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9278 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9279
9280 window->releaseChannel();
9281
9282 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9283
9284 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9285 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9286 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9287 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9288}
9289
9290/**
9291 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9292 * the spy, but not to any other windows.
9293 */
9294TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9295 auto spy = createSpy();
9296 auto window = createForeground();
9297
9298 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9299
9300 // First finger down on the window and the spy.
9301 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9302 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9303 {100, 200}))
9304 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9305 spy->consumeMotionDown();
9306 window->consumeMotionDown();
9307
9308 // Spy window pilfers the pointers.
9309 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9310 window->consumeMotionCancel();
9311
9312 // Second finger down on the window and spy, but the window should not receive the pointer down.
9313 const MotionEvent secondFingerDownEvent =
9314 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9315 .displayId(ADISPLAY_ID_DEFAULT)
9316 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009317 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9318 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009319 .build();
9320 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9321 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9322 InputEventInjectionSync::WAIT_FOR_RESULT))
9323 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9324
Harry Cutts33476232023-01-30 19:57:29 +00009325 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009326
9327 // Third finger goes down outside all windows, so injection should fail.
9328 const MotionEvent thirdFingerDownEvent =
9329 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9330 .displayId(ADISPLAY_ID_DEFAULT)
9331 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009332 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9333 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9334 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009335 .build();
9336 ASSERT_EQ(InputEventInjectionResult::FAILED,
9337 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9338 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009339 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009340
9341 spy->assertNoEvents();
9342 window->assertNoEvents();
9343}
9344
9345/**
9346 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9347 */
9348TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9349 auto spy = createSpy();
9350 spy->setFrame(Rect(0, 0, 100, 100));
9351 auto window = createForeground();
9352 window->setFrame(Rect(0, 0, 200, 200));
9353
9354 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9355
9356 // First finger down on the window only
9357 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9358 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9359 {150, 150}))
9360 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9361 window->consumeMotionDown();
9362
9363 // Second finger down on the spy and window
9364 const MotionEvent secondFingerDownEvent =
9365 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9366 .displayId(ADISPLAY_ID_DEFAULT)
9367 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009368 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9369 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009370 .build();
9371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9372 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9373 InputEventInjectionSync::WAIT_FOR_RESULT))
9374 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9375 spy->consumeMotionDown();
9376 window->consumeMotionPointerDown(1);
9377
9378 // Third finger down on the spy and window
9379 const MotionEvent thirdFingerDownEvent =
9380 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9381 .displayId(ADISPLAY_ID_DEFAULT)
9382 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009383 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9384 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9385 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009386 .build();
9387 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9388 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9389 InputEventInjectionSync::WAIT_FOR_RESULT))
9390 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9391 spy->consumeMotionPointerDown(1);
9392 window->consumeMotionPointerDown(2);
9393
9394 // Spy window pilfers the pointers.
9395 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9396 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9397 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9398
9399 spy->assertNoEvents();
9400 window->assertNoEvents();
9401}
9402
9403/**
9404 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9405 * other windows should be canceled. If this results in the cancellation of all pointers for some
9406 * window, then that window should receive ACTION_CANCEL.
9407 */
9408TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9409 auto spy = createSpy();
9410 spy->setFrame(Rect(0, 0, 100, 100));
9411 auto window = createForeground();
9412 window->setFrame(Rect(0, 0, 200, 200));
9413
9414 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9415
9416 // First finger down on both spy and window
9417 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9418 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9419 {10, 10}))
9420 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9421 window->consumeMotionDown();
9422 spy->consumeMotionDown();
9423
9424 // Second finger down on the spy and window
9425 const MotionEvent secondFingerDownEvent =
9426 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9427 .displayId(ADISPLAY_ID_DEFAULT)
9428 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009429 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9430 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009431 .build();
9432 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9433 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9434 InputEventInjectionSync::WAIT_FOR_RESULT))
9435 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9436 spy->consumeMotionPointerDown(1);
9437 window->consumeMotionPointerDown(1);
9438
9439 // Spy window pilfers the pointers.
9440 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9441 window->consumeMotionCancel();
9442
9443 spy->assertNoEvents();
9444 window->assertNoEvents();
9445}
9446
9447/**
9448 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9449 * be sent to other windows
9450 */
9451TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9452 auto spy = createSpy();
9453 spy->setFrame(Rect(0, 0, 100, 100));
9454 auto window = createForeground();
9455 window->setFrame(Rect(0, 0, 200, 200));
9456
9457 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9458
9459 // First finger down on both window and spy
9460 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9461 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9462 {10, 10}))
9463 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9464 window->consumeMotionDown();
9465 spy->consumeMotionDown();
9466
9467 // Spy window pilfers the pointers.
9468 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9469 window->consumeMotionCancel();
9470
9471 // Second finger down on the window only
9472 const MotionEvent secondFingerDownEvent =
9473 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9474 .displayId(ADISPLAY_ID_DEFAULT)
9475 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009476 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9477 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009478 .build();
9479 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9480 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9481 InputEventInjectionSync::WAIT_FOR_RESULT))
9482 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9483 window->consumeMotionDown();
9484 window->assertNoEvents();
9485
9486 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9487 spy->consumeMotionMove();
9488 spy->assertNoEvents();
9489}
9490
Prabir Pradhand65552b2021-10-07 11:23:50 -07009491class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9492public:
9493 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9494 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9495 std::make_shared<FakeApplicationHandle>();
9496 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009497 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9498 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009499 overlay->setFocusable(false);
9500 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009501 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009502 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009503 overlay->setTrustedOverlay(true);
9504
9505 std::shared_ptr<FakeApplicationHandle> application =
9506 std::make_shared<FakeApplicationHandle>();
9507 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009508 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9509 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009510 window->setFocusable(true);
9511 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009512
9513 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9514 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9515 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009516 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009517 return {std::move(overlay), std::move(window)};
9518 }
9519
9520 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009521 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009522 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009523 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009524 }
9525
9526 void sendStylusEvent(int32_t action) {
9527 NotifyMotionArgs motionArgs =
9528 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9529 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009530 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009531 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009532 }
9533};
9534
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009535using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9536
9537TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9538 ScopedSilentDeath _silentDeath;
9539
Prabir Pradhand65552b2021-10-07 11:23:50 -07009540 auto [overlay, window] = setupStylusOverlayScenario();
9541 overlay->setTrustedOverlay(false);
9542 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9543 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9544 ".* not a trusted overlay");
9545}
9546
9547TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9548 auto [overlay, window] = setupStylusOverlayScenario();
9549 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9550
9551 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9552 overlay->consumeMotionDown();
9553 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9554 overlay->consumeMotionUp();
9555
9556 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9557 window->consumeMotionDown();
9558 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9559 window->consumeMotionUp();
9560
9561 overlay->assertNoEvents();
9562 window->assertNoEvents();
9563}
9564
9565TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9566 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009567 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009568 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9569
9570 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9571 overlay->consumeMotionDown();
9572 window->consumeMotionDown();
9573 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9574 overlay->consumeMotionUp();
9575 window->consumeMotionUp();
9576
9577 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9578 window->consumeMotionDown();
9579 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9580 window->consumeMotionUp();
9581
9582 overlay->assertNoEvents();
9583 window->assertNoEvents();
9584}
9585
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009586/**
9587 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9588 * The scenario is as follows:
9589 * - The stylus interceptor overlay is configured as a spy window.
9590 * - The stylus interceptor spy receives the start of a new stylus gesture.
9591 * - It pilfers pointers and then configures itself to no longer be a spy.
9592 * - The stylus interceptor continues to receive the rest of the gesture.
9593 */
9594TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9595 auto [overlay, window] = setupStylusOverlayScenario();
9596 overlay->setSpy(true);
9597 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9598
9599 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9600 overlay->consumeMotionDown();
9601 window->consumeMotionDown();
9602
9603 // The interceptor pilfers the pointers.
9604 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9605 window->consumeMotionCancel();
9606
9607 // The interceptor configures itself so that it is no longer a spy.
9608 overlay->setSpy(false);
9609 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9610
9611 // It continues to receive the rest of the stylus gesture.
9612 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9613 overlay->consumeMotionMove();
9614 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9615 overlay->consumeMotionUp();
9616
9617 window->assertNoEvents();
9618}
9619
Prabir Pradhan5735a322022-04-11 17:23:34 +00009620struct User {
9621 int32_t mPid;
9622 int32_t mUid;
9623 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9624 std::unique_ptr<InputDispatcher>& mDispatcher;
9625
9626 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9627 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9628
9629 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9630 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9631 ADISPLAY_ID_DEFAULT, {100, 200},
9632 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9633 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9634 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9635 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9636 }
9637
9638 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009639 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009640 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009641 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009642 mPolicyFlags);
9643 }
9644
9645 sp<FakeWindowHandle> createWindow() const {
9646 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9647 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009648 sp<FakeWindowHandle> window =
9649 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9650 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009651 window->setOwnerInfo(mPid, mUid);
9652 return window;
9653 }
9654};
9655
9656using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9657
9658TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9659 auto owner = User(mDispatcher, 10, 11);
9660 auto window = owner.createWindow();
9661 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9662
9663 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9664 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9665 window->consumeMotionDown();
9666
9667 setFocusedWindow(window);
9668 window->consumeFocusEvent(true);
9669
9670 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9671 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9672 window->consumeKeyDown(ADISPLAY_ID_NONE);
9673}
9674
9675TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9676 auto owner = User(mDispatcher, 10, 11);
9677 auto window = owner.createWindow();
9678 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9679
9680 auto rando = User(mDispatcher, 20, 21);
9681 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9682 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9683
9684 setFocusedWindow(window);
9685 window->consumeFocusEvent(true);
9686
9687 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9688 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9689 window->assertNoEvents();
9690}
9691
9692TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9693 auto owner = User(mDispatcher, 10, 11);
9694 auto window = owner.createWindow();
9695 auto spy = owner.createWindow();
9696 spy->setSpy(true);
9697 spy->setTrustedOverlay(true);
9698 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9699
9700 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9701 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9702 spy->consumeMotionDown();
9703 window->consumeMotionDown();
9704}
9705
9706TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9707 auto owner = User(mDispatcher, 10, 11);
9708 auto window = owner.createWindow();
9709
9710 auto rando = User(mDispatcher, 20, 21);
9711 auto randosSpy = rando.createWindow();
9712 randosSpy->setSpy(true);
9713 randosSpy->setTrustedOverlay(true);
9714 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9715
9716 // The event is targeted at owner's window, so injection should succeed, but the spy should
9717 // not receive the event.
9718 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9719 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9720 randosSpy->assertNoEvents();
9721 window->consumeMotionDown();
9722}
9723
9724TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9725 auto owner = User(mDispatcher, 10, 11);
9726 auto window = owner.createWindow();
9727
9728 auto rando = User(mDispatcher, 20, 21);
9729 auto randosSpy = rando.createWindow();
9730 randosSpy->setSpy(true);
9731 randosSpy->setTrustedOverlay(true);
9732 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9733
9734 // A user that has injection permission can inject into any window.
9735 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9736 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9737 ADISPLAY_ID_DEFAULT));
9738 randosSpy->consumeMotionDown();
9739 window->consumeMotionDown();
9740
9741 setFocusedWindow(randosSpy);
9742 randosSpy->consumeFocusEvent(true);
9743
9744 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9745 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9746 window->assertNoEvents();
9747}
9748
9749TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9750 auto owner = User(mDispatcher, 10, 11);
9751 auto window = owner.createWindow();
9752
9753 auto rando = User(mDispatcher, 20, 21);
9754 auto randosWindow = rando.createWindow();
9755 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9756 randosWindow->setWatchOutsideTouch(true);
9757 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9758
9759 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9760 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9761 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9762 window->consumeMotionDown();
9763 randosWindow->consumeMotionOutside();
9764}
9765
Garfield Tane84e6f92019-08-29 17:28:41 -07009766} // namespace android::inputdispatcher