blob: 655931cd42d98483e699cde2d70abcffeca7f925 [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 &&
1567 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1568 mPointers.size() == 1) {
1569 mRawXCursorPosition = pointerCoords[0].getX();
1570 mRawYCursorPosition = pointerCoords[0].getY();
1571 }
1572
1573 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001574 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001575 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001576 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001577 mButtonState, MotionClassification::NONE, identityTransform,
1578 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001579 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001580 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001581
1582 return event;
1583 }
1584
1585private:
1586 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001587 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001588 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001589 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001590 nsecs_t mEventTime;
1591 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1592 int32_t mActionButton{0};
1593 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001594 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001595 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1596 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1597
1598 std::vector<PointerBuilder> mPointers;
1599};
1600
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001601class MotionArgsBuilder {
1602public:
1603 MotionArgsBuilder(int32_t action, int32_t source) {
1604 mAction = action;
1605 mSource = source;
1606 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1607 mDownTime = mEventTime;
1608 }
1609
1610 MotionArgsBuilder& deviceId(int32_t deviceId) {
1611 mDeviceId = deviceId;
1612 return *this;
1613 }
1614
1615 MotionArgsBuilder& downTime(nsecs_t downTime) {
1616 mDownTime = downTime;
1617 return *this;
1618 }
1619
1620 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
1621 mEventTime = eventTime;
1622 return *this;
1623 }
1624
1625 MotionArgsBuilder& displayId(int32_t displayId) {
1626 mDisplayId = displayId;
1627 return *this;
1628 }
1629
1630 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
1631 mPolicyFlags = policyFlags;
1632 return *this;
1633 }
1634
1635 MotionArgsBuilder& actionButton(int32_t actionButton) {
1636 mActionButton = actionButton;
1637 return *this;
1638 }
1639
1640 MotionArgsBuilder& buttonState(int32_t buttonState) {
1641 mButtonState = buttonState;
1642 return *this;
1643 }
1644
1645 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
1646 mRawXCursorPosition = rawXCursorPosition;
1647 return *this;
1648 }
1649
1650 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
1651 mRawYCursorPosition = rawYCursorPosition;
1652 return *this;
1653 }
1654
1655 MotionArgsBuilder& pointer(PointerBuilder pointer) {
1656 mPointers.push_back(pointer);
1657 return *this;
1658 }
1659
1660 MotionArgsBuilder& addFlag(uint32_t flags) {
1661 mFlags |= flags;
1662 return *this;
1663 }
1664
Harry Cuttsb166c002023-05-09 13:06:05 +00001665 MotionArgsBuilder& classification(MotionClassification classification) {
1666 mClassification = classification;
1667 return *this;
1668 }
1669
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001670 NotifyMotionArgs build() {
1671 std::vector<PointerProperties> pointerProperties;
1672 std::vector<PointerCoords> pointerCoords;
1673 for (const PointerBuilder& pointer : mPointers) {
1674 pointerProperties.push_back(pointer.buildProperties());
1675 pointerCoords.push_back(pointer.buildCoords());
1676 }
1677
1678 // Set mouse cursor position for the most common cases to avoid boilerplate.
1679 if (mSource == AINPUT_SOURCE_MOUSE &&
1680 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) &&
1681 mPointers.size() == 1) {
1682 mRawXCursorPosition = pointerCoords[0].getX();
1683 mRawYCursorPosition = pointerCoords[0].getY();
1684 }
1685
1686 NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId,
1687 mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags,
Harry Cuttsb166c002023-05-09 13:06:05 +00001688 AMETA_NONE, mButtonState, mClassification, /*edgeFlags=*/0,
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001689 mPointers.size(), pointerProperties.data(), pointerCoords.data(),
1690 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
1691 mRawYCursorPosition, mDownTime, /*videoFrames=*/{});
1692
1693 return args;
1694 }
1695
1696private:
1697 int32_t mAction;
1698 int32_t mDeviceId = DEVICE_ID;
1699 int32_t mSource;
1700 nsecs_t mDownTime;
1701 nsecs_t mEventTime;
1702 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1703 int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
1704 int32_t mActionButton{0};
1705 int32_t mButtonState{0};
1706 int32_t mFlags{0};
Harry Cuttsb166c002023-05-09 13:06:05 +00001707 MotionClassification mClassification{MotionClassification::NONE};
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001708 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1709 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1710
1711 std::vector<PointerBuilder> mPointers;
1712};
1713
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001714static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001715 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001716 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001717 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1718 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1719 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1720 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001721}
1722
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001723static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001724 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001725 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001726 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001727 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1728 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001729 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001730 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1731 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001732 MotionEventBuilder motionBuilder =
1733 MotionEventBuilder(action, source)
1734 .displayId(displayId)
1735 .eventTime(eventTime)
1736 .rawXCursorPosition(cursorPosition.x)
1737 .rawYCursorPosition(cursorPosition.y)
1738 .pointer(
1739 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1740 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1741 motionBuilder.downTime(eventTime);
1742 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001743
1744 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001745 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1746 targetUid, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001747}
1748
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001749static InputEventInjectionResult injectMotionDown(
1750 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1751 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001752 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001753}
1754
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001755static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001756 int32_t source, int32_t displayId,
1757 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001758 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001759}
1760
Jackal Guof9696682018-10-05 12:23:23 +08001761static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1762 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1763 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001764 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001765 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1766 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001767
1768 return args;
1769}
1770
Josep del Riob3981622023-04-18 15:49:45 +00001771static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1772 int32_t displayId = ADISPLAY_ID_NONE) {
1773 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1774 // Define a valid key event.
1775 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1776 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1777 currentTime);
1778
1779 return args;
1780}
1781
1782static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1783 int32_t displayId = ADISPLAY_ID_NONE) {
1784 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1785 // Define a valid key event.
1786 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1787 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1788 AMETA_NONE, currentTime);
1789
1790 return args;
1791}
1792
Prabir Pradhan678438e2023-04-13 19:32:51 +00001793[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1794 int32_t displayId,
1795 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001796 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001797 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1798 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1799 }
1800
chaviwd1c23182019-12-20 18:44:56 -08001801 PointerProperties pointerProperties[pointerCount];
1802 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001803
chaviwd1c23182019-12-20 18:44:56 -08001804 for (size_t i = 0; i < pointerCount; i++) {
1805 pointerProperties[i].clear();
1806 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001807 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001808
chaviwd1c23182019-12-20 18:44:56 -08001809 pointerCoords[i].clear();
1810 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1811 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1812 }
Jackal Guof9696682018-10-05 12:23:23 +08001813
1814 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1815 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001816 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001817 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1818 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001819 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1820 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001821 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1822 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001823
1824 return args;
1825}
1826
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001827static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1828 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1829}
1830
chaviwd1c23182019-12-20 18:44:56 -08001831static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1832 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1833}
1834
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001835static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1836 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001837 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001838}
1839
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001840/**
1841 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1842 * broken channel.
1843 */
1844TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1845 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1846 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001847 sp<FakeWindowHandle>::make(application, mDispatcher,
1848 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001849
1850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1851
1852 // Window closes its channel, but the window remains.
1853 window->destroyReceiver();
1854 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1855}
1856
Arthur Hungb92218b2018-08-14 12:00:21 +08001857TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001858 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001859 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1860 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001861
Arthur Hung72d8dc32020-03-28 00:48:39 +00001862 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001863 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1864 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1865 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001866
1867 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001868 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001869}
1870
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001871TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1872 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001873 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1874 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001875
1876 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1877 // Inject a MotionEvent to an unknown display.
1878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1879 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1880 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1881
1882 // Window should receive motion event.
1883 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1884}
1885
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001886/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001887 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001888 * This test serves as a sanity check for the next test, where setInputWindows is
1889 * called twice.
1890 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001891TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001892 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001893 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1894 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001895 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001896
1897 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001898 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001899 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1900 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001901 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001902
1903 // Window should receive motion event.
1904 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1905}
1906
1907/**
1908 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001909 */
1910TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001911 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001912 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1913 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001914 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001915
1916 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1917 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001918 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001919 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1920 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001921 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001922
1923 // Window should receive motion event.
1924 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1925}
1926
Arthur Hungb92218b2018-08-14 12:00:21 +08001927// The foreground window should receive the first touch down event.
1928TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001929 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001930 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001931 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001932 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001933 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001934
Arthur Hung72d8dc32020-03-28 00:48:39 +00001935 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001936 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1937 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1938 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001939
1940 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001941 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001942 windowSecond->assertNoEvents();
1943}
1944
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001945/**
1946 * Two windows: A top window, and a wallpaper behind the window.
1947 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1948 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001949 * 1. foregroundWindow <-- dup touch to wallpaper
1950 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001951 */
1952TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1953 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1954 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001955 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001956 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001957 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001958 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001959 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001960
1961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1962 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1963 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1964 {100, 200}))
1965 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1966
1967 // Both foreground window and its wallpaper should receive the touch down
1968 foregroundWindow->consumeMotionDown();
1969 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1970
1971 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1972 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1973 ADISPLAY_ID_DEFAULT, {110, 200}))
1974 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1975
1976 foregroundWindow->consumeMotionMove();
1977 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1978
1979 // Now the foreground window goes away, but the wallpaper stays
1980 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1981 foregroundWindow->consumeMotionCancel();
1982 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1983 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1984}
1985
1986/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001987 * Two fingers down on the window, and lift off the first finger.
1988 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1989 * contains a single pointer.
1990 */
1991TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1992 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1993 sp<FakeWindowHandle> window =
1994 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1995
1996 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001997 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001998 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1999 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2000 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002001 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002002 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2003 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2004 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
2005 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002006 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002007 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2008 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2009 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
2010 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002011 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2012 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2013 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2014
2015 // Remove the window. The gesture should be canceled
2016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2017 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
2018 window->consumeMotionEvent(
2019 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
2020}
2021
2022/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002023 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
2024 * with the following differences:
2025 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
2026 * clean up the connection.
2027 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
2028 * Ensure that there's no crash in the dispatcher.
2029 */
2030TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
2031 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2032 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002033 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002034 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002035 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002036 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002037 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002038
2039 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
2040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2041 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2042 {100, 200}))
2043 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2044
2045 // Both foreground window and its wallpaper should receive the touch down
2046 foregroundWindow->consumeMotionDown();
2047 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2048
2049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2050 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2051 ADISPLAY_ID_DEFAULT, {110, 200}))
2052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2053
2054 foregroundWindow->consumeMotionMove();
2055 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2056
2057 // Wallpaper closes its channel, but the window remains.
2058 wallpaperWindow->destroyReceiver();
2059 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
2060
2061 // Now the foreground window goes away, but the wallpaper stays, even though its channel
2062 // is no longer valid.
2063 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
2064 foregroundWindow->consumeMotionCancel();
2065}
2066
Arthur Hungc539dbb2022-12-08 07:45:36 +00002067class ShouldSplitTouchFixture : public InputDispatcherTest,
2068 public ::testing::WithParamInterface<bool> {};
2069INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
2070 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002071/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002072 * A single window that receives touch (on top), and a wallpaper window underneath it.
2073 * The top window gets a multitouch gesture.
2074 * Ensure that wallpaper gets the same gesture.
2075 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002076TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002077 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002078 sp<FakeWindowHandle> foregroundWindow =
2079 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2080 foregroundWindow->setDupTouchToWallpaper(true);
2081 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002082
2083 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002084 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002085 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002086
Arthur Hungc539dbb2022-12-08 07:45:36 +00002087 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002088
2089 // Touch down on top window
2090 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2091 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2092 {100, 100}))
2093 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2094
2095 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002096 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002097 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2098
2099 // Second finger down on the top window
2100 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002101 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002102 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002103 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2104 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002105 .build();
2106 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2107 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2108 InputEventInjectionSync::WAIT_FOR_RESULT))
2109 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2110
Harry Cutts33476232023-01-30 19:57:29 +00002111 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2112 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002113 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002114
2115 const MotionEvent secondFingerUpEvent =
2116 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2117 .displayId(ADISPLAY_ID_DEFAULT)
2118 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002119 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2120 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002121 .build();
2122 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2123 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
2124 InputEventInjectionSync::WAIT_FOR_RESULT))
2125 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2126 foregroundWindow->consumeMotionPointerUp(0);
2127 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2128
2129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002130 injectMotionEvent(mDispatcher,
2131 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2132 AINPUT_SOURCE_TOUCHSCREEN)
2133 .displayId(ADISPLAY_ID_DEFAULT)
2134 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2135 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002136 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002137 .x(100)
2138 .y(100))
2139 .build(),
2140 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002141 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2142 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2143 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002144}
2145
2146/**
2147 * Two windows: a window on the left and window on the right.
2148 * A third window, wallpaper, is behind both windows, and spans both top windows.
2149 * The first touch down goes to the left window. A second pointer touches down on the right window.
2150 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2151 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2152 * ACTION_POINTER_DOWN(1).
2153 */
2154TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2155 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2156 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002157 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002158 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002159 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002160
2161 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002162 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002163 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002164 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002165
2166 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002167 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002168 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002169 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002170
2171 mDispatcher->setInputWindows(
2172 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
2173
2174 // Touch down on left window
2175 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2176 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2177 {100, 100}))
2178 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2179
2180 // Both foreground window and its wallpaper should receive the touch down
2181 leftWindow->consumeMotionDown();
2182 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2183
2184 // Second finger down on the right window
2185 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002186 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002187 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002188 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2189 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002190 .build();
2191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2192 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2193 InputEventInjectionSync::WAIT_FOR_RESULT))
2194 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2195
2196 leftWindow->consumeMotionMove();
2197 // Since the touch is split, right window gets ACTION_DOWN
2198 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002199 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002200 expectedWallpaperFlags);
2201
2202 // Now, leftWindow, which received the first finger, disappears.
2203 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2204 leftWindow->consumeMotionCancel();
2205 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2206 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2207
2208 // The pointer that's still down on the right window moves, and goes to the right window only.
2209 // As far as the dispatcher's concerned though, both pointers are still present.
2210 const MotionEvent secondFingerMoveEvent =
2211 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2212 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002213 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2214 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002215 .build();
2216 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2217 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2218 InputEventInjectionSync::WAIT_FOR_RESULT));
2219 rightWindow->consumeMotionMove();
2220
2221 leftWindow->assertNoEvents();
2222 rightWindow->assertNoEvents();
2223 wallpaperWindow->assertNoEvents();
2224}
2225
Arthur Hungc539dbb2022-12-08 07:45:36 +00002226/**
2227 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2228 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2229 * The right window should receive ACTION_DOWN.
2230 */
2231TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002232 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002233 sp<FakeWindowHandle> leftWindow =
2234 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2235 leftWindow->setFrame(Rect(0, 0, 200, 200));
2236 leftWindow->setDupTouchToWallpaper(true);
2237 leftWindow->setSlippery(true);
2238
2239 sp<FakeWindowHandle> rightWindow =
2240 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2241 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002242
2243 sp<FakeWindowHandle> wallpaperWindow =
2244 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2245 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002246
Arthur Hungc539dbb2022-12-08 07:45:36 +00002247 mDispatcher->setInputWindows(
2248 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002249
Arthur Hungc539dbb2022-12-08 07:45:36 +00002250 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2252 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002253 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002254 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002255
2256 // Both foreground window and its wallpaper should receive the touch down
2257 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002258 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2259
Arthur Hungc539dbb2022-12-08 07:45:36 +00002260 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002261 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002262 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2263 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002264 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2265
Arthur Hungc539dbb2022-12-08 07:45:36 +00002266 leftWindow->consumeMotionCancel();
2267 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2268 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002269}
2270
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002271/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002272 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2273 * interactive, it might stop sending this flag.
2274 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2275 * to have a consistent input stream.
2276 *
2277 * Test procedure:
2278 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2279 * DOWN (new gesture).
2280 *
2281 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2282 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2283 *
2284 * We technically just need a single window here, but we are using two windows (spy on top and a
2285 * regular window below) to emulate the actual situation where it happens on the device.
2286 */
2287TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2288 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2289 sp<FakeWindowHandle> spyWindow =
2290 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2291 spyWindow->setFrame(Rect(0, 0, 200, 200));
2292 spyWindow->setTrustedOverlay(true);
2293 spyWindow->setSpy(true);
2294
2295 sp<FakeWindowHandle> window =
2296 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2297 window->setFrame(Rect(0, 0, 200, 200));
2298
2299 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2300 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002301
2302 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002303 mDispatcher->notifyMotion(
2304 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2305 .deviceId(touchDeviceId)
2306 .policyFlags(DEFAULT_POLICY_FLAGS)
2307 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2308 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002309
Prabir Pradhan678438e2023-04-13 19:32:51 +00002310 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2311 .deviceId(touchDeviceId)
2312 .policyFlags(DEFAULT_POLICY_FLAGS)
2313 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2314 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2315 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002316 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2317 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2318 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2319 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2320
2321 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002322 mDispatcher->notifyMotion(
2323 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2324 .deviceId(touchDeviceId)
2325 .policyFlags(0)
2326 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2327 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2328 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002329 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2330 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2331
2332 // We don't need to reset the device to reproduce the issue, but the reset event typically
2333 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002334 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002335
2336 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002337 mDispatcher->notifyMotion(
2338 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2339 .deviceId(touchDeviceId)
2340 .policyFlags(DEFAULT_POLICY_FLAGS)
2341 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2342 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002343 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2344 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2345
2346 // No more events
2347 spyWindow->assertNoEvents();
2348 window->assertNoEvents();
2349}
2350
2351/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002352 * Two windows: a window on the left and a window on the right.
2353 * Mouse is hovered from the right window into the left window.
2354 * Next, we tap on the left window, where the cursor was last seen.
2355 * The second tap is done onto the right window.
2356 * The mouse and tap are from two different devices.
2357 * We technically don't need to set the downtime / eventtime for these events, but setting these
2358 * explicitly helps during debugging.
2359 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2360 * In the buggy implementation, a tap on the right window would cause a crash.
2361 */
2362TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2363 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2364 sp<FakeWindowHandle> leftWindow =
2365 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2366 leftWindow->setFrame(Rect(0, 0, 200, 200));
2367
2368 sp<FakeWindowHandle> rightWindow =
2369 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2370 rightWindow->setFrame(Rect(200, 0, 400, 200));
2371
2372 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2373 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2374 // stale.
2375 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2376 const int32_t mouseDeviceId = 6;
2377 const int32_t touchDeviceId = 4;
2378 // Move the cursor from right
2379 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2380 injectMotionEvent(mDispatcher,
2381 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2382 AINPUT_SOURCE_MOUSE)
2383 .deviceId(mouseDeviceId)
2384 .downTime(baseTime + 10)
2385 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002386 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002387 .x(300)
2388 .y(100))
2389 .build()));
2390 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2391
2392 // .. to the left window
2393 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2394 injectMotionEvent(mDispatcher,
2395 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2396 AINPUT_SOURCE_MOUSE)
2397 .deviceId(mouseDeviceId)
2398 .downTime(baseTime + 10)
2399 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002400 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002401 .x(110)
2402 .y(100))
2403 .build()));
2404 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2405 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2406 // Now tap the left window
2407 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2408 injectMotionEvent(mDispatcher,
2409 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2410 AINPUT_SOURCE_TOUCHSCREEN)
2411 .deviceId(touchDeviceId)
2412 .downTime(baseTime + 40)
2413 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002414 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002415 .x(100)
2416 .y(100))
2417 .build()));
2418 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2419 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2420
2421 // release tap
2422 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2423 injectMotionEvent(mDispatcher,
2424 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2425 AINPUT_SOURCE_TOUCHSCREEN)
2426 .deviceId(touchDeviceId)
2427 .downTime(baseTime + 40)
2428 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002429 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002430 .x(100)
2431 .y(100))
2432 .build()));
2433 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2434
2435 // Tap the window on the right
2436 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2437 injectMotionEvent(mDispatcher,
2438 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2439 AINPUT_SOURCE_TOUCHSCREEN)
2440 .deviceId(touchDeviceId)
2441 .downTime(baseTime + 60)
2442 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002443 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002444 .x(300)
2445 .y(100))
2446 .build()));
2447 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2448
2449 // release tap
2450 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2451 injectMotionEvent(mDispatcher,
2452 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2453 AINPUT_SOURCE_TOUCHSCREEN)
2454 .deviceId(touchDeviceId)
2455 .downTime(baseTime + 60)
2456 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002457 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002458 .x(300)
2459 .y(100))
2460 .build()));
2461 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2462
2463 // No more events
2464 leftWindow->assertNoEvents();
2465 rightWindow->assertNoEvents();
2466}
2467
2468/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002469 * Start hovering in a window. While this hover is still active, make another window appear on top.
2470 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2471 * While the top window is present, the hovering is stopped.
2472 * Later, hovering gets resumed again.
2473 * Ensure that new hover gesture is handled correctly.
2474 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2475 * to the window that's currently being hovered over.
2476 */
2477TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2478 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2479 sp<FakeWindowHandle> window =
2480 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2481 window->setFrame(Rect(0, 0, 200, 200));
2482
2483 // Only a single window is present at first
2484 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2485
2486 // Start hovering in the window
2487 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2488 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2489 .build());
2490 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2491
2492 // Now, an obscuring window appears!
2493 sp<FakeWindowHandle> obscuringWindow =
2494 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2495 ADISPLAY_ID_DEFAULT,
2496 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2497 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2498 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2499 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2500 obscuringWindow->setNoInputChannel(true);
2501 obscuringWindow->setFocusable(false);
2502 obscuringWindow->setAlpha(1.0);
2503 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2504
2505 // While this new obscuring window is present, the hovering is stopped
2506 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2507 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2508 .build());
2509 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2510
2511 // Now the obscuring window goes away.
2512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2513
2514 // And a new hover gesture starts.
2515 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2516 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2517 .build());
2518 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2519}
2520
2521/**
2522 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2523 * the obscuring window.
2524 */
2525TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2526 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2527 sp<FakeWindowHandle> window =
2528 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2529 window->setFrame(Rect(0, 0, 200, 200));
2530
2531 // Only a single window is present at first
2532 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2533
2534 // Start hovering in the window
2535 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2536 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2537 .build());
2538 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2539
2540 // Now, an obscuring window appears!
2541 sp<FakeWindowHandle> obscuringWindow =
2542 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2543 ADISPLAY_ID_DEFAULT,
2544 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2545 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2546 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2547 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2548 obscuringWindow->setNoInputChannel(true);
2549 obscuringWindow->setFocusable(false);
2550 obscuringWindow->setAlpha(1.0);
2551 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2552
2553 // While this new obscuring window is present, the hovering continues. The event can't go to the
2554 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2555 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2556 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2557 .build());
2558 obscuringWindow->assertNoEvents();
2559 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2560
2561 // Now the obscuring window goes away.
2562 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2563
2564 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2565 // so it should generate a HOVER_ENTER
2566 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2567 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2568 .build());
2569 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2570
2571 // Now the MOVE should be getting dispatched normally
2572 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2573 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2574 .build());
2575 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2576}
2577
2578/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002579 * Two windows: a window on the left and a window on the right.
2580 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2581 * down. Then, on the left window, also place second touch pointer down.
2582 * This test tries to reproduce a crash.
2583 * In the buggy implementation, second pointer down on the left window would cause a crash.
2584 */
2585TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2586 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2587 sp<FakeWindowHandle> leftWindow =
2588 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2589 leftWindow->setFrame(Rect(0, 0, 200, 200));
2590
2591 sp<FakeWindowHandle> rightWindow =
2592 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2593 rightWindow->setFrame(Rect(200, 0, 400, 200));
2594
2595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2596
2597 const int32_t touchDeviceId = 4;
2598 const int32_t mouseDeviceId = 6;
2599 NotifyMotionArgs args;
2600
2601 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002602 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2603 .deviceId(mouseDeviceId)
2604 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2605 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002606 leftWindow->consumeMotionEvent(
2607 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2608
2609 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002610 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2611 .deviceId(mouseDeviceId)
2612 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2613 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2614 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002615
2616 leftWindow->consumeMotionEvent(
2617 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2618 leftWindow->consumeMotionEvent(
2619 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2620
Prabir Pradhan678438e2023-04-13 19:32:51 +00002621 mDispatcher->notifyMotion(
2622 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2623 .deviceId(mouseDeviceId)
2624 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2625 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2626 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2627 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002628 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2629
2630 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002631 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2632 .deviceId(touchDeviceId)
2633 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2634 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002635 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2636
2637 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2638
2639 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002640 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2641 .deviceId(touchDeviceId)
2642 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2643 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2644 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002645 leftWindow->consumeMotionEvent(
2646 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2647 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2648 // current implementation.
2649 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2650 rightWindow->consumeMotionEvent(
2651 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2652
2653 leftWindow->assertNoEvents();
2654 rightWindow->assertNoEvents();
2655}
2656
2657/**
2658 * On a single window, use two different devices: mouse and touch.
2659 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2660 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2661 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2662 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2663 * represent a new gesture.
2664 */
2665TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2666 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2667 sp<FakeWindowHandle> window =
2668 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2669 window->setFrame(Rect(0, 0, 400, 400));
2670
2671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2672
2673 const int32_t touchDeviceId = 4;
2674 const int32_t mouseDeviceId = 6;
2675 NotifyMotionArgs args;
2676
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002677 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002678 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2679 .deviceId(touchDeviceId)
2680 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2681 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002682 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002683 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2684 .deviceId(touchDeviceId)
2685 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2686 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2687 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002688 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002689 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2690 .deviceId(touchDeviceId)
2691 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2692 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2693 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002694 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2695 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2696 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2697
2698 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002699 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2700 .deviceId(mouseDeviceId)
2701 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2702 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2703 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002704
2705 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002706 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002707 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2708
Prabir Pradhan678438e2023-04-13 19:32:51 +00002709 mDispatcher->notifyMotion(
2710 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2711 .deviceId(mouseDeviceId)
2712 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2713 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2714 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2715 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002716 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2717
2718 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002719 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2720 .deviceId(touchDeviceId)
2721 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2722 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2723 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002724 // The pointer_down event should be ignored
2725 window->assertNoEvents();
2726}
2727
2728/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002729 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2730 * the injected event.
2731 */
2732TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2733 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2734 sp<FakeWindowHandle> window =
2735 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2736 window->setFrame(Rect(0, 0, 400, 400));
2737
2738 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2739
2740 const int32_t touchDeviceId = 4;
2741 NotifyMotionArgs args;
2742 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2743 // completion.
2744 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2745 injectMotionEvent(mDispatcher,
2746 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2747 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002748 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002749 .x(50)
2750 .y(50))
2751 .build()));
2752 window->consumeMotionEvent(
2753 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2754
2755 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2756 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002757 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2758 .deviceId(touchDeviceId)
2759 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2760 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002761
2762 window->consumeMotionEvent(
2763 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2764 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2765}
2766
2767/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002768 * This test is similar to the test above, but the sequence of injected events is different.
2769 *
2770 * Two windows: a window on the left and a window on the right.
2771 * Mouse is hovered over the left window.
2772 * Next, we tap on the left window, where the cursor was last seen.
2773 *
2774 * After that, we inject one finger down onto the right window, and then a second finger down onto
2775 * the left window.
2776 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2777 * window (first), and then another on the left window (second).
2778 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2779 * In the buggy implementation, second finger down on the left window would cause a crash.
2780 */
2781TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2782 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2783 sp<FakeWindowHandle> leftWindow =
2784 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2785 leftWindow->setFrame(Rect(0, 0, 200, 200));
2786
2787 sp<FakeWindowHandle> rightWindow =
2788 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2789 rightWindow->setFrame(Rect(200, 0, 400, 200));
2790
2791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2792
2793 const int32_t mouseDeviceId = 6;
2794 const int32_t touchDeviceId = 4;
2795 // Hover over the left window. Keep the cursor there.
2796 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2797 injectMotionEvent(mDispatcher,
2798 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2799 AINPUT_SOURCE_MOUSE)
2800 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002801 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002802 .x(50)
2803 .y(50))
2804 .build()));
2805 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2806
2807 // Tap on left window
2808 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2809 injectMotionEvent(mDispatcher,
2810 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2811 AINPUT_SOURCE_TOUCHSCREEN)
2812 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002813 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002814 .x(100)
2815 .y(100))
2816 .build()));
2817
2818 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2819 injectMotionEvent(mDispatcher,
2820 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2821 AINPUT_SOURCE_TOUCHSCREEN)
2822 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002823 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002824 .x(100)
2825 .y(100))
2826 .build()));
2827 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2828 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2829 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2830
2831 // First finger down on right window
2832 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2833 injectMotionEvent(mDispatcher,
2834 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2835 AINPUT_SOURCE_TOUCHSCREEN)
2836 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002837 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002838 .x(300)
2839 .y(100))
2840 .build()));
2841 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2842
2843 // Second finger down on the left window
2844 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2845 injectMotionEvent(mDispatcher,
2846 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2847 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002848 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002849 .x(300)
2850 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002851 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002852 .x(100)
2853 .y(100))
2854 .build()));
2855 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2856 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2857
2858 // No more events
2859 leftWindow->assertNoEvents();
2860 rightWindow->assertNoEvents();
2861}
2862
2863/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002864 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2865 * While the touch is down, new hover events from the stylus device should be ignored. After the
2866 * touch is gone, stylus hovering should start working again.
2867 */
2868TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2869 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2870 sp<FakeWindowHandle> window =
2871 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2872 window->setFrame(Rect(0, 0, 200, 200));
2873
2874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2875
2876 const int32_t stylusDeviceId = 5;
2877 const int32_t touchDeviceId = 4;
2878 // Start hovering with stylus
2879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2880 injectMotionEvent(mDispatcher,
2881 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2882 AINPUT_SOURCE_STYLUS)
2883 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002884 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002885 .x(50)
2886 .y(50))
2887 .build()));
2888 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2889
2890 // Finger down on the window
2891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2892 injectMotionEvent(mDispatcher,
2893 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2894 AINPUT_SOURCE_TOUCHSCREEN)
2895 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002896 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002897 .x(100)
2898 .y(100))
2899 .build()));
2900 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2901 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2902
2903 // Try to continue hovering with stylus. Since we are already down, injection should fail
2904 ASSERT_EQ(InputEventInjectionResult::FAILED,
2905 injectMotionEvent(mDispatcher,
2906 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2907 AINPUT_SOURCE_STYLUS)
2908 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002909 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002910 .x(50)
2911 .y(50))
2912 .build()));
2913 // No event should be sent. This event should be ignored because a pointer from another device
2914 // is already down.
2915
2916 // Lift up the finger
2917 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2918 injectMotionEvent(mDispatcher,
2919 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2920 AINPUT_SOURCE_TOUCHSCREEN)
2921 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002922 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002923 .x(100)
2924 .y(100))
2925 .build()));
2926 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2927
2928 // Now that the touch is gone, stylus hovering should start working again
2929 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2930 injectMotionEvent(mDispatcher,
2931 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2932 AINPUT_SOURCE_STYLUS)
2933 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002934 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002935 .x(50)
2936 .y(50))
2937 .build()));
2938 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2939 // No more events
2940 window->assertNoEvents();
2941}
2942
2943/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002944 * A spy window above a window with no input channel.
2945 * Start hovering with a stylus device, and then tap with it.
2946 * Ensure spy window receives the entire sequence.
2947 */
2948TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2949 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2950 sp<FakeWindowHandle> spyWindow =
2951 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2952 spyWindow->setFrame(Rect(0, 0, 200, 200));
2953 spyWindow->setTrustedOverlay(true);
2954 spyWindow->setSpy(true);
2955 sp<FakeWindowHandle> window =
2956 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2957 window->setNoInputChannel(true);
2958 window->setFrame(Rect(0, 0, 200, 200));
2959
2960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2961
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002962 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002963 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2964 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2965 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002966 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2967 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002968 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2969 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2970 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002971 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2972
2973 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002974 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2975 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2976 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002977 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2978
2979 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002980 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2981 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2982 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002983 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2984
2985 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002986 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2987 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2988 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002989 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2990 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002991 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2992 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2993 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002994 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2995
2996 // No more events
2997 spyWindow->assertNoEvents();
2998 window->assertNoEvents();
2999}
3000
3001/**
3002 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
3003 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3004 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3005 * While the mouse is down, new move events from the touch device should be ignored.
3006 */
3007TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3008 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3009 sp<FakeWindowHandle> spyWindow =
3010 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3011 spyWindow->setFrame(Rect(0, 0, 200, 200));
3012 spyWindow->setTrustedOverlay(true);
3013 spyWindow->setSpy(true);
3014 sp<FakeWindowHandle> window =
3015 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3016 window->setFrame(Rect(0, 0, 200, 200));
3017
3018 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3019
3020 const int32_t mouseDeviceId = 7;
3021 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003022
3023 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003024 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3025 .deviceId(mouseDeviceId)
3026 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3027 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003028 spyWindow->consumeMotionEvent(
3029 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3030 window->consumeMotionEvent(
3031 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3032
3033 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003034 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3035 .deviceId(touchDeviceId)
3036 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3037 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003038 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3039 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3040 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3041 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3042
Prabir Pradhan678438e2023-04-13 19:32:51 +00003043 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3044 .deviceId(touchDeviceId)
3045 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3046 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003047 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3048 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3049
3050 // Pilfer the stream
3051 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3052 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3053
Prabir Pradhan678438e2023-04-13 19:32:51 +00003054 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3055 .deviceId(touchDeviceId)
3056 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3057 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003058 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3059
3060 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003061 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3062 .deviceId(mouseDeviceId)
3063 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3064 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3065 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003066
3067 spyWindow->consumeMotionEvent(
3068 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3069 spyWindow->consumeMotionEvent(
3070 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3071 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3072
Prabir Pradhan678438e2023-04-13 19:32:51 +00003073 mDispatcher->notifyMotion(
3074 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3075 .deviceId(mouseDeviceId)
3076 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3077 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3078 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3079 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003080 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3081 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3082
3083 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003084 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3085 .deviceId(mouseDeviceId)
3086 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3087 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3088 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003089 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3090 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3091
3092 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003093 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3094 .deviceId(touchDeviceId)
3095 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3096 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003097
3098 // No more events
3099 spyWindow->assertNoEvents();
3100 window->assertNoEvents();
3101}
3102
3103/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003104 * On the display, have a single window, and also an area where there's no window.
3105 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
3106 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
3107 */
3108TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3109 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3110 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003111 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003112
3113 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003114
3115 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00003116 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003117
3118 mDispatcher->waitForIdle();
3119 window->assertNoEvents();
3120
3121 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003122 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003123 mDispatcher->waitForIdle();
3124 window->consumeMotionDown();
3125}
3126
3127/**
3128 * Same test as above, but instead of touching the empty space, the first touch goes to
3129 * non-touchable window.
3130 */
3131TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3132 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3133 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003134 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003135 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3136 window1->setTouchable(false);
3137 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003138 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003139 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3140
3141 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3142
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003143 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003144 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003145
3146 mDispatcher->waitForIdle();
3147 window1->assertNoEvents();
3148 window2->assertNoEvents();
3149
3150 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003151 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003152 mDispatcher->waitForIdle();
3153 window2->consumeMotionDown();
3154}
3155
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003156/**
3157 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3158 * to the event time of the first ACTION_DOWN sent to the particular window.
3159 */
3160TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3161 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3162 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003163 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003164 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3165 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003166 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003167 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3168
3169 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3170
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003171 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003172 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003173
3174 mDispatcher->waitForIdle();
3175 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003176 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003177 window2->assertNoEvents();
3178 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
3179 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
3180 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
3181
3182 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003183 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003184 mDispatcher->waitForIdle();
3185 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003186 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003187 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
3188 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
3189 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
3190 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
3191
3192 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003193 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003194 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003195 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003196
3197 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003198 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003199 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003200 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003201
3202 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3203 window1->consumeMotionMove();
3204 window1->assertNoEvents();
3205
3206 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003207 mDispatcher->notifyMotion(
3208 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003209 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003210 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003211
Prabir Pradhan678438e2023-04-13 19:32:51 +00003212 mDispatcher->notifyMotion(
3213 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003214 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003215 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003216}
3217
Garfield Tandf26e862020-07-01 20:18:19 -07003218TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003219 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003220 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003221 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003222 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003223 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003224 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003225 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003226
3227 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3228
3229 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3230
3231 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003232 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003233 injectMotionEvent(mDispatcher,
3234 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3235 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003236 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003237 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003238 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003239
3240 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003241 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003242 injectMotionEvent(mDispatcher,
3243 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3244 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003245 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003246 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003247 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3248 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003249
3250 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003251 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003252 injectMotionEvent(mDispatcher,
3253 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3254 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003255 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003256 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003257 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3258 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003259
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003260 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003261 injectMotionEvent(mDispatcher,
3262 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3263 AINPUT_SOURCE_MOUSE)
3264 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3265 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003266 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003267 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003268 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003269
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003270 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003271 injectMotionEvent(mDispatcher,
3272 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3273 AINPUT_SOURCE_MOUSE)
3274 .buttonState(0)
3275 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003276 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003277 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003278 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003279
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003281 injectMotionEvent(mDispatcher,
3282 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3283 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003284 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003285 .build()));
3286 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3287
3288 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003289 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003290 injectMotionEvent(mDispatcher,
3291 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3292 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003293 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003294 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003295 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003296
3297 // No more events
3298 windowLeft->assertNoEvents();
3299 windowRight->assertNoEvents();
3300}
3301
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003302/**
3303 * Put two fingers down (and don't release them) and click the mouse button.
3304 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3305 * currently active gesture should be canceled, and the new one should proceed.
3306 */
3307TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3308 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3309 sp<FakeWindowHandle> window =
3310 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3311 window->setFrame(Rect(0, 0, 600, 800));
3312
3313 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3314
3315 const int32_t touchDeviceId = 4;
3316 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003317
3318 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003319 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3320 .deviceId(touchDeviceId)
3321 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3322 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003323
Prabir Pradhan678438e2023-04-13 19:32:51 +00003324 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3325 .deviceId(touchDeviceId)
3326 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3327 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3328 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003329 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3330 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3331
3332 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003333 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3334 .deviceId(mouseDeviceId)
3335 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3336 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3337 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003338 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3339 WithPointerCount(2u)));
3340 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3341
Prabir Pradhan678438e2023-04-13 19:32:51 +00003342 mDispatcher->notifyMotion(
3343 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3344 .deviceId(mouseDeviceId)
3345 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3346 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3347 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3348 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003349 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3350
3351 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3352 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003353 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3354 .deviceId(touchDeviceId)
3355 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3356 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3357 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003358 window->assertNoEvents();
3359}
3360
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003361TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3362 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3363
3364 sp<FakeWindowHandle> spyWindow =
3365 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3366 spyWindow->setFrame(Rect(0, 0, 600, 800));
3367 spyWindow->setTrustedOverlay(true);
3368 spyWindow->setSpy(true);
3369 sp<FakeWindowHandle> window =
3370 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3371 window->setFrame(Rect(0, 0, 600, 800));
3372
3373 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3375
3376 // Send mouse cursor to the window
3377 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3378 injectMotionEvent(mDispatcher,
3379 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3380 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003381 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003382 .x(100)
3383 .y(100))
3384 .build()));
3385
3386 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3387 WithSource(AINPUT_SOURCE_MOUSE)));
3388 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3389 WithSource(AINPUT_SOURCE_MOUSE)));
3390
3391 window->assertNoEvents();
3392 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003393}
3394
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003395TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3396 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3397
3398 sp<FakeWindowHandle> spyWindow =
3399 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3400 spyWindow->setFrame(Rect(0, 0, 600, 800));
3401 spyWindow->setTrustedOverlay(true);
3402 spyWindow->setSpy(true);
3403 sp<FakeWindowHandle> window =
3404 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3405 window->setFrame(Rect(0, 0, 600, 800));
3406
3407 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3408 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3409
3410 // Send mouse cursor to the window
3411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3412 injectMotionEvent(mDispatcher,
3413 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3414 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003415 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003416 .x(100)
3417 .y(100))
3418 .build()));
3419
3420 // Move mouse cursor
3421 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3422 injectMotionEvent(mDispatcher,
3423 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3424 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003425 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003426 .x(110)
3427 .y(110))
3428 .build()));
3429
3430 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3431 WithSource(AINPUT_SOURCE_MOUSE)));
3432 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3433 WithSource(AINPUT_SOURCE_MOUSE)));
3434 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3435 WithSource(AINPUT_SOURCE_MOUSE)));
3436 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3437 WithSource(AINPUT_SOURCE_MOUSE)));
3438 // Touch down on the window
3439 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3440 injectMotionEvent(mDispatcher,
3441 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3442 AINPUT_SOURCE_TOUCHSCREEN)
3443 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003444 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003445 .x(200)
3446 .y(200))
3447 .build()));
3448 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3449 WithSource(AINPUT_SOURCE_MOUSE)));
3450 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3451 WithSource(AINPUT_SOURCE_MOUSE)));
3452 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3453 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3454 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3455 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3456
3457 // pilfer the motion, retaining the gesture on the spy window.
3458 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3459 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3460 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3461
3462 // Touch UP on the window
3463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3464 injectMotionEvent(mDispatcher,
3465 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3466 AINPUT_SOURCE_TOUCHSCREEN)
3467 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003468 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003469 .x(200)
3470 .y(200))
3471 .build()));
3472 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3473 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3474
3475 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3476 // to send a new gesture. It should again go to both windows (spy and the window below), just
3477 // like the first gesture did, before pilfering. The window configuration has not changed.
3478
3479 // One more tap - DOWN
3480 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3481 injectMotionEvent(mDispatcher,
3482 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3483 AINPUT_SOURCE_TOUCHSCREEN)
3484 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003485 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003486 .x(250)
3487 .y(250))
3488 .build()));
3489 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3490 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3491 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3492 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3493
3494 // Touch UP on the window
3495 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3496 injectMotionEvent(mDispatcher,
3497 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3498 AINPUT_SOURCE_TOUCHSCREEN)
3499 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003500 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003501 .x(250)
3502 .y(250))
3503 .build()));
3504 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3505 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3506 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3507 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3508
3509 window->assertNoEvents();
3510 spyWindow->assertNoEvents();
3511}
3512
Garfield Tandf26e862020-07-01 20:18:19 -07003513// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3514// directly in this test.
3515TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003516 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003517 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003518 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003519 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003520
3521 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3522
3523 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3524
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003525 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003526 injectMotionEvent(mDispatcher,
3527 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3528 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003529 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003530 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003531 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003532 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003534 injectMotionEvent(mDispatcher,
3535 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3536 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003537 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003538 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003539 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3540 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003541
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003542 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003543 injectMotionEvent(mDispatcher,
3544 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3545 AINPUT_SOURCE_MOUSE)
3546 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3547 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003548 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003549 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003550 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003551
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003553 injectMotionEvent(mDispatcher,
3554 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3555 AINPUT_SOURCE_MOUSE)
3556 .buttonState(0)
3557 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003558 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003559 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003560 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003561
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003562 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003563 injectMotionEvent(mDispatcher,
3564 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3565 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003566 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003567 .build()));
3568 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3569
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003570 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3571 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3572 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003573 injectMotionEvent(mDispatcher,
3574 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3575 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003576 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003577 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003578 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003579}
3580
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003581/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003582 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3583 * is generated.
3584 */
3585TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3586 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3587 sp<FakeWindowHandle> window =
3588 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3589 window->setFrame(Rect(0, 0, 1200, 800));
3590
3591 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3592
3593 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3594
3595 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3596 injectMotionEvent(mDispatcher,
3597 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3598 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003599 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003600 .x(300)
3601 .y(400))
3602 .build()));
3603 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3604
3605 // Remove the window, but keep the channel.
3606 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3607 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3608}
3609
3610/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003611 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3612 */
3613TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3614 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3615 sp<FakeWindowHandle> window =
3616 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3617 window->setFrame(Rect(0, 0, 100, 100));
3618
3619 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3620
3621 const int32_t mouseDeviceId = 7;
3622 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003623
3624 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003625 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3626 .deviceId(mouseDeviceId)
3627 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3628 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003629 window->consumeMotionEvent(
3630 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3631
3632 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003633 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3634 .deviceId(touchDeviceId)
3635 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3636 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003637
3638 window->consumeMotionEvent(
3639 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3640 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3641}
3642
3643/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003644 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003645 * The tap causes a HOVER_EXIT event to be generated because the current event
3646 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003647 */
3648TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3649 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3650 sp<FakeWindowHandle> window =
3651 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3652 window->setFrame(Rect(0, 0, 100, 100));
3653
3654 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3655
3656 // Inject a hover_move from mouse.
3657 NotifyMotionArgs motionArgs =
3658 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3659 ADISPLAY_ID_DEFAULT, {{50, 50}});
3660 motionArgs.xCursorPosition = 50;
3661 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003662 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003663 ASSERT_NO_FATAL_FAILURE(
3664 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3665 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003666
3667 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003668 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3669 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3670 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003671 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003672 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3673 WithSource(AINPUT_SOURCE_MOUSE))));
3674
3675 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003676 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3677 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3678
Prabir Pradhan678438e2023-04-13 19:32:51 +00003679 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3680 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003681 ASSERT_NO_FATAL_FAILURE(
3682 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3683 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3684}
3685
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003686TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3687 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3688 sp<FakeWindowHandle> windowDefaultDisplay =
3689 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3690 ADISPLAY_ID_DEFAULT);
3691 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3692 sp<FakeWindowHandle> windowSecondDisplay =
3693 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3694 SECOND_DISPLAY_ID);
3695 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3696
3697 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3698 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3699
3700 // Set cursor position in window in default display and check that hover enter and move
3701 // events are generated.
3702 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3703 injectMotionEvent(mDispatcher,
3704 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3705 AINPUT_SOURCE_MOUSE)
3706 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003707 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003708 .x(300)
3709 .y(600))
3710 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003711 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003712
3713 // Remove all windows in secondary display and check that no event happens on window in
3714 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003715 mDispatcher->setInputWindows(
3716 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003717 windowDefaultDisplay->assertNoEvents();
3718
3719 // Move cursor position in window in default display and check that only hover move
3720 // event is generated and not hover enter event.
3721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3722 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3723 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3724 injectMotionEvent(mDispatcher,
3725 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3726 AINPUT_SOURCE_MOUSE)
3727 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003728 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003729 .x(400)
3730 .y(700))
3731 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003732 windowDefaultDisplay->consumeMotionEvent(
3733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3734 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003735 windowDefaultDisplay->assertNoEvents();
3736}
3737
Garfield Tan00f511d2019-06-12 16:55:40 -07003738TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003739 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003740
3741 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003742 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003743 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003744 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003745 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003746 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003747
3748 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3749
Arthur Hung72d8dc32020-03-28 00:48:39 +00003750 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003751
3752 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3753 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003754 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003755 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003756 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003757 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003758 windowRight->assertNoEvents();
3759}
3760
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003761TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003762 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003763 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3764 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003765 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003766
Arthur Hung72d8dc32020-03-28 00:48:39 +00003767 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003768 setFocusedWindow(window);
3769
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003770 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003771
Prabir Pradhan678438e2023-04-13 19:32:51 +00003772 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003773
3774 // Window should receive key down event.
3775 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3776
3777 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3778 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003779 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003780 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003781 AKEY_EVENT_FLAG_CANCELED);
3782}
3783
3784TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003785 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003786 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3787 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003788
Arthur Hung72d8dc32020-03-28 00:48:39 +00003789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003790
Prabir Pradhan678438e2023-04-13 19:32:51 +00003791 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3792 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003793
3794 // Window should receive motion down event.
3795 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3796
3797 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3798 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003799 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003800 window->consumeMotionEvent(
3801 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003802}
3803
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003804TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3805 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3806 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3807 "Fake Window", ADISPLAY_ID_DEFAULT);
3808
3809 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3810
3811 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3812 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3813 .build());
3814
3815 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3816
3817 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3818 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3819 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3820
3821 // After the device has been reset, a new hovering stream can be sent to the window
3822 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3823 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3824 .build());
3825 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3826}
3827
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003828TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3829 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003830 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3831 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003832 window->setFocusable(true);
3833
3834 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3835 setFocusedWindow(window);
3836
3837 window->consumeFocusEvent(true);
3838
Prabir Pradhan678438e2023-04-13 19:32:51 +00003839 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003840 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3841 const nsecs_t injectTime = keyArgs.eventTime;
3842 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003843 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003844 // The dispatching time should be always greater than or equal to intercept key timeout.
3845 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3846 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3847 std::chrono::nanoseconds(interceptKeyTimeout).count());
3848}
3849
3850TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3851 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003852 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3853 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003854 window->setFocusable(true);
3855
3856 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3857 setFocusedWindow(window);
3858
3859 window->consumeFocusEvent(true);
3860
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003861 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003862 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3863 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003864
3865 // Window should receive key event immediately when same key up.
3866 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3867 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3868}
3869
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003870/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003871 * Two windows. First is a regular window. Second does not overlap with the first, and has
3872 * WATCH_OUTSIDE_TOUCH.
3873 * Both windows are owned by the same UID.
3874 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3875 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3876 */
3877TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3878 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3879 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3880 "First Window", ADISPLAY_ID_DEFAULT);
3881 window->setFrame(Rect{0, 0, 100, 100});
3882
3883 sp<FakeWindowHandle> outsideWindow =
3884 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3885 ADISPLAY_ID_DEFAULT);
3886 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3887 outsideWindow->setWatchOutsideTouch(true);
3888 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3889 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3890
3891 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003892 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3893 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3894 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003895 window->consumeMotionDown();
3896 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3897 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3898 outsideWindow->consumeMotionEvent(
3899 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3900}
3901
3902/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003903 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3904 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3905 * ACTION_OUTSIDE event is sent per gesture.
3906 */
3907TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3908 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3909 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003910 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3911 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003912 window->setWatchOutsideTouch(true);
3913 window->setFrame(Rect{0, 0, 100, 100});
3914 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003915 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3916 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003917 secondWindow->setFrame(Rect{100, 100, 200, 200});
3918 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003919 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3920 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003921 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3922 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3923
3924 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003925 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3926 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3927 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003928 window->assertNoEvents();
3929 secondWindow->assertNoEvents();
3930
3931 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3932 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003933 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3934 ADISPLAY_ID_DEFAULT,
3935 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003936 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3937 window->consumeMotionEvent(
3938 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003939 secondWindow->consumeMotionDown();
3940 thirdWindow->assertNoEvents();
3941
3942 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3943 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003944 mDispatcher->notifyMotion(
3945 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3946 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003947 window->assertNoEvents();
3948 secondWindow->consumeMotionMove();
3949 thirdWindow->consumeMotionDown();
3950}
3951
Prabir Pradhan814fe082022-07-22 20:22:18 +00003952TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3953 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003954 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3955 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003956 window->setFocusable(true);
3957
Patrick Williamsd828f302023-04-28 17:52:08 -05003958 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003959 setFocusedWindow(window);
3960
3961 window->consumeFocusEvent(true);
3962
Prabir Pradhan678438e2023-04-13 19:32:51 +00003963 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3964 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3965 mDispatcher->notifyKey(keyDown);
3966 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003967
3968 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3969 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3970
3971 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003972 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003973
3974 window->consumeFocusEvent(false);
3975
Prabir Pradhan678438e2023-04-13 19:32:51 +00003976 mDispatcher->notifyKey(keyDown);
3977 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003978 window->assertNoEvents();
3979}
3980
Arthur Hung96483742022-11-15 03:30:48 +00003981TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3982 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3983 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3984 "Fake Window", ADISPLAY_ID_DEFAULT);
3985 // Ensure window is non-split and have some transform.
3986 window->setPreventSplitting(true);
3987 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003988 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003989
3990 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3991 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3992 {50, 50}))
3993 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3994 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3995
3996 const MotionEvent secondFingerDownEvent =
3997 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3998 .displayId(ADISPLAY_ID_DEFAULT)
3999 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004000 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
4001 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00004002 .build();
4003 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4004 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
4005 InputEventInjectionSync::WAIT_FOR_RESULT))
4006 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4007
4008 const MotionEvent* event = window->consumeMotion();
4009 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4010 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4011 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4012 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4013 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
4014}
4015
Harry Cuttsb166c002023-05-09 13:06:05 +00004016TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4017 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4018 sp<FakeWindowHandle> window =
4019 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4020 window->setFrame(Rect(0, 0, 400, 400));
4021 sp<FakeWindowHandle> trustedOverlay =
4022 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4023 ADISPLAY_ID_DEFAULT);
4024 trustedOverlay->setSpy(true);
4025 trustedOverlay->setTrustedOverlay(true);
4026
4027 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
4028
4029 // Start a three-finger touchpad swipe
4030 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4031 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4032 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4033 .build());
4034 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4035 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4036 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4037 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4038 .build());
4039 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4040 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4041 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4042 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4043 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4044 .build());
4045
4046 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4047 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4048 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4049
4050 // Move the swipe a bit
4051 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4052 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4053 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4054 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4055 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4056 .build());
4057
4058 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4059
4060 // End the swipe
4061 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4062 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4063 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4064 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4065 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4066 .build());
4067 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4068 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4069 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4070 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4071 .build());
4072 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4073 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4074 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4075 .build());
4076
4077 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4078 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4079 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4080
4081 window->assertNoEvents();
4082}
4083
4084TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4085 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4086 sp<FakeWindowHandle> window =
4087 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4088 window->setFrame(Rect(0, 0, 400, 400));
4089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4090
4091 // Start a three-finger touchpad swipe
4092 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4093 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4094 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4095 .build());
4096 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4097 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4098 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4099 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4100 .build());
4101 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4102 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4103 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4104 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4105 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4106 .build());
4107
4108 // Move the swipe a bit
4109 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4110 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4111 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4112 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4113 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4114 .build());
4115
4116 // End the swipe
4117 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4118 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4119 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4120 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4121 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4122 .build());
4123 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4124 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4125 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4126 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4127 .build());
4128 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4129 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4130 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4131 .build());
4132
4133 window->assertNoEvents();
4134}
4135
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004136/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004137 * Ensure the correct coordinate spaces are used by InputDispatcher.
4138 *
4139 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4140 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4141 * space.
4142 */
4143class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4144public:
4145 void SetUp() override {
4146 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004147 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004148 }
4149
4150 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4151 gui::DisplayInfo info;
4152 info.displayId = displayId;
4153 info.transform = transform;
4154 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004155 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004156 }
4157
4158 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4159 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004160 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004161 }
4162
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004163 void removeAllWindowsAndDisplays() {
4164 mDisplayInfos.clear();
4165 mWindowInfos.clear();
4166 }
4167
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004168 // Set up a test scenario where the display has a scaled projection and there are two windows
4169 // on the display.
4170 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4171 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4172 // respectively.
4173 ui::Transform displayTransform;
4174 displayTransform.set(2, 0, 0, 4);
4175 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4176
4177 std::shared_ptr<FakeApplicationHandle> application =
4178 std::make_shared<FakeApplicationHandle>();
4179
4180 // Add two windows to the display. Their frames are represented in the display space.
4181 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004182 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4183 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004184 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4185 addWindow(firstWindow);
4186
4187 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004188 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4189 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004190 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4191 addWindow(secondWindow);
4192 return {std::move(firstWindow), std::move(secondWindow)};
4193 }
4194
4195private:
4196 std::vector<gui::DisplayInfo> mDisplayInfos;
4197 std::vector<gui::WindowInfo> mWindowInfos;
4198};
4199
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004200TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004201 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4202 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004203 // selected so that if the hit test was performed with the point and the bounds being in
4204 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004205 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4206 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4207 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004208
4209 firstWindow->consumeMotionDown();
4210 secondWindow->assertNoEvents();
4211}
4212
4213// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4214// the event should be treated as being in the logical display space.
4215TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4216 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4217 // Send down to the first window. The point is represented in the logical display space. The
4218 // point is selected so that if the hit test was done in logical display space, then it would
4219 // end up in the incorrect window.
4220 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4221 PointF{75 * 2, 55 * 4});
4222
4223 firstWindow->consumeMotionDown();
4224 secondWindow->assertNoEvents();
4225}
4226
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004227// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4228// event should be treated as being in the logical display space.
4229TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4230 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4231
4232 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4233 ui::Transform injectedEventTransform;
4234 injectedEventTransform.set(matrix);
4235 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4236 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4237
4238 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4239 .displayId(ADISPLAY_ID_DEFAULT)
4240 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004241 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004242 .x(untransformedPoint.x)
4243 .y(untransformedPoint.y))
4244 .build();
4245 event.transform(matrix);
4246
4247 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4248 InputEventInjectionSync::WAIT_FOR_RESULT);
4249
4250 firstWindow->consumeMotionDown();
4251 secondWindow->assertNoEvents();
4252}
4253
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004254TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4255 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4256
4257 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004258 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4259 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4260 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004261
4262 firstWindow->assertNoEvents();
4263 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004264 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004265 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4266
4267 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4268 EXPECT_EQ(300, event->getRawX(0));
4269 EXPECT_EQ(880, event->getRawY(0));
4270
4271 // Ensure that the x and y values are in the window's coordinate space.
4272 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4273 // the logical display space. This will be the origin of the window space.
4274 EXPECT_EQ(100, event->getX(0));
4275 EXPECT_EQ(80, event->getY(0));
4276}
4277
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004278/** Ensure consistent behavior of InputDispatcher in all orientations. */
4279class InputDispatcherDisplayOrientationFixture
4280 : public InputDispatcherDisplayProjectionTest,
4281 public ::testing::WithParamInterface<ui::Rotation> {};
4282
4283// This test verifies the touchable region of a window for all rotations of the display by tapping
4284// in different locations on the display, specifically points close to the four corners of a
4285// window.
4286TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4287 constexpr static int32_t displayWidth = 400;
4288 constexpr static int32_t displayHeight = 800;
4289
4290 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4291
4292 const auto rotation = GetParam();
4293
4294 // Set up the display with the specified rotation.
4295 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4296 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4297 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4298 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4299 logicalDisplayWidth, logicalDisplayHeight);
4300 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4301
4302 // Create a window with its bounds determined in the logical display.
4303 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4304 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4305 sp<FakeWindowHandle> window =
4306 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4307 window->setFrame(frameInDisplay, displayTransform);
4308 addWindow(window);
4309
4310 // The following points in logical display space should be inside the window.
4311 static const std::array<vec2, 4> insidePoints{
4312 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4313 for (const auto pointInsideWindow : insidePoints) {
4314 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4315 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004316 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4317 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4318 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004319 window->consumeMotionDown();
4320
Prabir Pradhan678438e2023-04-13 19:32:51 +00004321 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4322 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4323 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004324 window->consumeMotionUp();
4325 }
4326
4327 // The following points in logical display space should be outside the window.
4328 static const std::array<vec2, 5> outsidePoints{
4329 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4330 for (const auto pointOutsideWindow : outsidePoints) {
4331 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4332 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004333 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4334 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4335 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004336
Prabir Pradhan678438e2023-04-13 19:32:51 +00004337 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4338 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4339 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004340 }
4341 window->assertNoEvents();
4342}
4343
4344// Run the precision tests for all rotations.
4345INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4346 InputDispatcherDisplayOrientationFixture,
4347 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4348 ui::ROTATION_270),
4349 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4350 return ftl::enum_string(testParamInfo.param);
4351 });
4352
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004353using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4354 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004355
4356class TransferTouchFixture : public InputDispatcherTest,
4357 public ::testing::WithParamInterface<TransferFunction> {};
4358
4359TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004360 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004361
4362 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004363 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004364 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4365 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004366 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004367 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004368 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4369 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004370 sp<FakeWindowHandle> wallpaper =
4371 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4372 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004373 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004374 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004375
4376 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004377 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4378 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004379
Svet Ganov5d3bc372020-01-26 23:11:07 -08004380 // Only the first window should get the down event
4381 firstWindow->consumeMotionDown();
4382 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004383 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004384
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004385 // Transfer touch to the second window
4386 TransferFunction f = GetParam();
4387 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4388 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004389 // The first window gets cancel and the second gets down
4390 firstWindow->consumeMotionCancel();
4391 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004392 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004393
4394 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004395 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4396 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004397 // The first window gets no events and the second gets up
4398 firstWindow->assertNoEvents();
4399 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004400 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004401}
4402
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004403/**
4404 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4405 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4406 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4407 * natural to the user.
4408 * In this test, we are sending a pointer to both spy window and first window. We then try to
4409 * transfer touch to the second window. The dispatcher should identify the first window as the
4410 * one that should lose the gesture, and therefore the action should be to move the gesture from
4411 * the first window to the second.
4412 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4413 * the other API, as well.
4414 */
4415TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4416 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4417
4418 // Create a couple of windows + a spy window
4419 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004420 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004421 spyWindow->setTrustedOverlay(true);
4422 spyWindow->setSpy(true);
4423 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004424 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004425 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004426 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004427
4428 // Add the windows to the dispatcher
4429 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4430
4431 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004432 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4433 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004434 // Only the first window and spy should get the down event
4435 spyWindow->consumeMotionDown();
4436 firstWindow->consumeMotionDown();
4437
4438 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4439 // if f === 'transferTouch'.
4440 TransferFunction f = GetParam();
4441 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4442 ASSERT_TRUE(success);
4443 // The first window gets cancel and the second gets down
4444 firstWindow->consumeMotionCancel();
4445 secondWindow->consumeMotionDown();
4446
4447 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004448 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4449 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004450 // The first window gets no events and the second+spy get up
4451 firstWindow->assertNoEvents();
4452 spyWindow->consumeMotionUp();
4453 secondWindow->consumeMotionUp();
4454}
4455
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004456TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004457 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004458
4459 PointF touchPoint = {10, 10};
4460
4461 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004462 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004463 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4464 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004465 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004466 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004467 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4468 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004469 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004470
4471 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004472 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004473
4474 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004475 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4476 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4477 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004478 // Only the first window should get the down event
4479 firstWindow->consumeMotionDown();
4480 secondWindow->assertNoEvents();
4481
4482 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004483 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4484 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004485 // Only the first window should get the pointer down event
4486 firstWindow->consumeMotionPointerDown(1);
4487 secondWindow->assertNoEvents();
4488
4489 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004490 TransferFunction f = GetParam();
4491 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4492 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004493 // The first window gets cancel and the second gets down and pointer down
4494 firstWindow->consumeMotionCancel();
4495 secondWindow->consumeMotionDown();
4496 secondWindow->consumeMotionPointerDown(1);
4497
4498 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004499 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4500 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004501 // The first window gets nothing and the second gets pointer up
4502 firstWindow->assertNoEvents();
4503 secondWindow->consumeMotionPointerUp(1);
4504
4505 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004506 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4507 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004508 // The first window gets nothing and the second gets up
4509 firstWindow->assertNoEvents();
4510 secondWindow->consumeMotionUp();
4511}
4512
Arthur Hungc539dbb2022-12-08 07:45:36 +00004513TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4514 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4515
4516 // Create a couple of windows
4517 sp<FakeWindowHandle> firstWindow =
4518 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4519 ADISPLAY_ID_DEFAULT);
4520 firstWindow->setDupTouchToWallpaper(true);
4521 sp<FakeWindowHandle> secondWindow =
4522 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4523 ADISPLAY_ID_DEFAULT);
4524 secondWindow->setDupTouchToWallpaper(true);
4525
4526 sp<FakeWindowHandle> wallpaper1 =
4527 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4528 wallpaper1->setIsWallpaper(true);
4529
4530 sp<FakeWindowHandle> wallpaper2 =
4531 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4532 wallpaper2->setIsWallpaper(true);
4533 // Add the windows to the dispatcher
4534 mDispatcher->setInputWindows(
4535 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4536
4537 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004538 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4539 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004540
4541 // Only the first window should get the down event
4542 firstWindow->consumeMotionDown();
4543 secondWindow->assertNoEvents();
4544 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4545 wallpaper2->assertNoEvents();
4546
4547 // Transfer touch focus to the second window
4548 TransferFunction f = GetParam();
4549 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4550 ASSERT_TRUE(success);
4551
4552 // The first window gets cancel and the second gets down
4553 firstWindow->consumeMotionCancel();
4554 secondWindow->consumeMotionDown();
4555 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4556 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4557
4558 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004559 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4560 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004561 // The first window gets no events and the second gets up
4562 firstWindow->assertNoEvents();
4563 secondWindow->consumeMotionUp();
4564 wallpaper1->assertNoEvents();
4565 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4566}
4567
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004568// For the cases of single pointer touch and two pointers non-split touch, the api's
4569// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4570// for the case where there are multiple pointers split across several windows.
4571INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4572 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004573 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4574 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004575 return dispatcher->transferTouch(destChannelToken,
4576 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004577 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004578 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4579 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004580 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004581 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004582 }));
4583
Svet Ganov5d3bc372020-01-26 23:11:07 -08004584TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004585 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004586
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004587 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004588 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4589 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004590 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004591
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004592 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004593 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4594 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004595 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004596
4597 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004598 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004599
4600 PointF pointInFirst = {300, 200};
4601 PointF pointInSecond = {300, 600};
4602
4603 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004604 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4605 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4606 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004607 // Only the first window should get the down event
4608 firstWindow->consumeMotionDown();
4609 secondWindow->assertNoEvents();
4610
4611 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004612 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4613 ADISPLAY_ID_DEFAULT,
4614 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004615 // The first window gets a move and the second a down
4616 firstWindow->consumeMotionMove();
4617 secondWindow->consumeMotionDown();
4618
4619 // Transfer touch focus to the second window
4620 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4621 // The first window gets cancel and the new gets pointer down (it already saw down)
4622 firstWindow->consumeMotionCancel();
4623 secondWindow->consumeMotionPointerDown(1);
4624
4625 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004626 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4627 ADISPLAY_ID_DEFAULT,
4628 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004629 // The first window gets nothing and the second gets pointer up
4630 firstWindow->assertNoEvents();
4631 secondWindow->consumeMotionPointerUp(1);
4632
4633 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004634 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4635 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004636 // The first window gets nothing and the second gets up
4637 firstWindow->assertNoEvents();
4638 secondWindow->consumeMotionUp();
4639}
4640
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004641// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4642// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4643// touch is not supported, so the touch should continue on those windows and the transferred-to
4644// window should get nothing.
4645TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4646 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4647
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004648 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004649 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4650 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004651 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004652
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004653 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004654 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4655 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004656 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004657
4658 // Add the windows to the dispatcher
4659 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4660
4661 PointF pointInFirst = {300, 200};
4662 PointF pointInSecond = {300, 600};
4663
4664 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004665 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4666 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4667 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004668 // Only the first window should get the down event
4669 firstWindow->consumeMotionDown();
4670 secondWindow->assertNoEvents();
4671
4672 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004673 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4674 ADISPLAY_ID_DEFAULT,
4675 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004676 // The first window gets a move and the second a down
4677 firstWindow->consumeMotionMove();
4678 secondWindow->consumeMotionDown();
4679
4680 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004681 const bool transferred =
4682 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004683 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4684 ASSERT_FALSE(transferred);
4685 firstWindow->assertNoEvents();
4686 secondWindow->assertNoEvents();
4687
4688 // The rest of the dispatch should proceed as normal
4689 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004690 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4691 ADISPLAY_ID_DEFAULT,
4692 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004693 // The first window gets MOVE and the second gets pointer up
4694 firstWindow->consumeMotionMove();
4695 secondWindow->consumeMotionUp();
4696
4697 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004698 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4699 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004700 // The first window gets nothing and the second gets up
4701 firstWindow->consumeMotionUp();
4702 secondWindow->assertNoEvents();
4703}
4704
Arthur Hungabbb9d82021-09-01 14:52:30 +00004705// This case will create two windows and one mirrored window on the default display and mirror
4706// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4707// the windows info of second display before default display.
4708TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4709 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4710 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004711 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004712 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004713 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004714 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004715 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004716
4717 sp<FakeWindowHandle> mirrorWindowInPrimary =
4718 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4719 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004720
4721 sp<FakeWindowHandle> firstWindowInSecondary =
4722 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4723 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004724
4725 sp<FakeWindowHandle> secondWindowInSecondary =
4726 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4727 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004728
4729 // Update window info, let it find window handle of second display first.
4730 mDispatcher->setInputWindows(
4731 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4732 {ADISPLAY_ID_DEFAULT,
4733 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4734
4735 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4736 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4737 {50, 50}))
4738 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4739
4740 // Window should receive motion event.
4741 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4742
4743 // Transfer touch focus
4744 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4745 secondWindowInPrimary->getToken()));
4746 // The first window gets cancel.
4747 firstWindowInPrimary->consumeMotionCancel();
4748 secondWindowInPrimary->consumeMotionDown();
4749
4750 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4751 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4752 ADISPLAY_ID_DEFAULT, {150, 50}))
4753 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4754 firstWindowInPrimary->assertNoEvents();
4755 secondWindowInPrimary->consumeMotionMove();
4756
4757 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4758 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4759 {150, 50}))
4760 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4761 firstWindowInPrimary->assertNoEvents();
4762 secondWindowInPrimary->consumeMotionUp();
4763}
4764
4765// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4766// 'transferTouch' api.
4767TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4768 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4769 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004770 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004771 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004772 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004773 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004774 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004775
4776 sp<FakeWindowHandle> mirrorWindowInPrimary =
4777 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4778 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004779
4780 sp<FakeWindowHandle> firstWindowInSecondary =
4781 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4782 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004783
4784 sp<FakeWindowHandle> secondWindowInSecondary =
4785 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4786 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004787
4788 // Update window info, let it find window handle of second display first.
4789 mDispatcher->setInputWindows(
4790 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4791 {ADISPLAY_ID_DEFAULT,
4792 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4793
4794 // Touch on second display.
4795 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4796 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4797 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4798
4799 // Window should receive motion event.
4800 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4801
4802 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004803 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004804
4805 // The first window gets cancel.
4806 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4807 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4808
4809 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4810 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4811 SECOND_DISPLAY_ID, {150, 50}))
4812 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4813 firstWindowInPrimary->assertNoEvents();
4814 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4815
4816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4817 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4818 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4819 firstWindowInPrimary->assertNoEvents();
4820 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4821}
4822
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004823TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004824 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004825 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4826 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004827
Vishnu Nair47074b82020-08-14 11:54:47 -07004828 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004830 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004831
4832 window->consumeFocusEvent(true);
4833
Prabir Pradhan678438e2023-04-13 19:32:51 +00004834 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004835
4836 // Window should receive key down event.
4837 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004838
4839 // Should have poked user activity
4840 mFakePolicy->assertUserActivityPoked();
4841}
4842
4843TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4844 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4845 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4846 "Fake Window", ADISPLAY_ID_DEFAULT);
4847
4848 window->setDisableUserActivity(true);
4849 window->setFocusable(true);
4850 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4851 setFocusedWindow(window);
4852
4853 window->consumeFocusEvent(true);
4854
4855 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4856
4857 // Window should receive key down event.
4858 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4859
4860 // Should have poked user activity
4861 mFakePolicy->assertUserActivityNotPoked();
4862}
4863
4864TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4865 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4866 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4867 "Fake Window", ADISPLAY_ID_DEFAULT);
4868
4869 window->setFocusable(true);
4870 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4871 setFocusedWindow(window);
4872
4873 window->consumeFocusEvent(true);
4874
4875 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4876 mDispatcher->waitForIdle();
4877
4878 // System key is not passed down
4879 window->assertNoEvents();
4880
4881 // Should have poked user activity
4882 mFakePolicy->assertUserActivityPoked();
4883}
4884
4885TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4886 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4887 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4888 "Fake Window", ADISPLAY_ID_DEFAULT);
4889
4890 window->setFocusable(true);
4891 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4892 setFocusedWindow(window);
4893
4894 window->consumeFocusEvent(true);
4895
4896 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4897 mDispatcher->waitForIdle();
4898
4899 // System key is not passed down
4900 window->assertNoEvents();
4901
4902 // Should have poked user activity
4903 mFakePolicy->assertUserActivityPoked();
4904}
4905
4906TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4907 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4908 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4909 "Fake Window", ADISPLAY_ID_DEFAULT);
4910
4911 window->setDisableUserActivity(true);
4912 window->setFocusable(true);
4913 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4914 setFocusedWindow(window);
4915
4916 window->consumeFocusEvent(true);
4917
4918 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4919 mDispatcher->waitForIdle();
4920
4921 // System key is not passed down
4922 window->assertNoEvents();
4923
4924 // Should have poked user activity
4925 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004926}
4927
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004928TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4929 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4930 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4931 "Fake Window", ADISPLAY_ID_DEFAULT);
4932
4933 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4934
4935 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4936 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4937 ADISPLAY_ID_DEFAULT, {100, 100}))
4938 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4939
4940 window->consumeMotionEvent(
4941 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4942
4943 // Should have poked user activity
4944 mFakePolicy->assertUserActivityPoked();
4945}
4946
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004947TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004948 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004949 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4950 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004951
Arthur Hung72d8dc32020-03-28 00:48:39 +00004952 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004953
Prabir Pradhan678438e2023-04-13 19:32:51 +00004954 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004955 mDispatcher->waitForIdle();
4956
4957 window->assertNoEvents();
4958}
4959
4960// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4961TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004962 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004963 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4964 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004965
Arthur Hung72d8dc32020-03-28 00:48:39 +00004966 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004967
4968 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004969 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004970 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004971 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4972 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004973
4974 // Window should receive only the motion event
4975 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4976 window->assertNoEvents(); // Key event or focus event will not be received
4977}
4978
arthurhungea3f4fc2020-12-21 23:18:53 +08004979TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4980 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4981
arthurhungea3f4fc2020-12-21 23:18:53 +08004982 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004983 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4984 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004985 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004986
arthurhungea3f4fc2020-12-21 23:18:53 +08004987 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004988 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4989 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004990 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004991
4992 // Add the windows to the dispatcher
4993 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4994
4995 PointF pointInFirst = {300, 200};
4996 PointF pointInSecond = {300, 600};
4997
4998 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004999 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5000 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5001 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005002 // Only the first window should get the down event
5003 firstWindow->consumeMotionDown();
5004 secondWindow->assertNoEvents();
5005
5006 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005007 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5008 ADISPLAY_ID_DEFAULT,
5009 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005010 // The first window gets a move and the second a down
5011 firstWindow->consumeMotionMove();
5012 secondWindow->consumeMotionDown();
5013
5014 // Send pointer cancel to the second window
5015 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005016 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08005017 {pointInFirst, pointInSecond});
5018 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00005019 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08005020 // The first window gets move and the second gets cancel.
5021 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5022 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5023
5024 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005025 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5026 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08005027 // The first window gets up and the second gets nothing.
5028 firstWindow->consumeMotionUp();
5029 secondWindow->assertNoEvents();
5030}
5031
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005032TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
5033 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5034
5035 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005036 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5038 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
5039 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
5040 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
5041
Harry Cutts33476232023-01-30 19:57:29 +00005042 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005043 window->assertNoEvents();
5044 mDispatcher->waitForIdle();
5045}
5046
chaviwd1c23182019-12-20 18:44:56 -08005047class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00005048public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005049 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005050 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07005051 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005052 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07005053 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00005054 }
5055
chaviwd1c23182019-12-20 18:44:56 -08005056 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
5057
5058 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005059 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
5060 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08005061 }
5062
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005063 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
5064
5065 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
5066
chaviwd1c23182019-12-20 18:44:56 -08005067 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005068 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08005069 expectedDisplayId, expectedFlags);
5070 }
5071
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005072 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005073 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005074 expectedDisplayId, expectedFlags);
5075 }
5076
chaviwd1c23182019-12-20 18:44:56 -08005077 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005078 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08005079 expectedDisplayId, expectedFlags);
5080 }
5081
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005082 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005083 mInputReceiver->consumeMotionEvent(
5084 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
5085 WithDisplayId(expectedDisplayId),
5086 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005087 }
5088
Arthur Hungfbfa5722021-11-16 02:45:54 +00005089 void consumeMotionPointerDown(int32_t pointerIdx) {
5090 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
5091 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005092 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005093 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00005094 }
5095
Evan Rosky84f07f02021-04-16 10:42:42 -07005096 MotionEvent* consumeMotion() {
5097 InputEvent* event = mInputReceiver->consume();
5098 if (!event) {
5099 ADD_FAILURE() << "No event was produced";
5100 return nullptr;
5101 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005102 if (event->getType() != InputEventType::MOTION) {
5103 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07005104 return nullptr;
5105 }
5106 return static_cast<MotionEvent*>(event);
5107 }
5108
chaviwd1c23182019-12-20 18:44:56 -08005109 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
5110
5111private:
5112 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00005113};
5114
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005115using InputDispatcherMonitorTest = InputDispatcherTest;
5116
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005117/**
5118 * Two entities that receive touch: A window, and a global monitor.
5119 * The touch goes to the window, and then the window disappears.
5120 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
5121 * for the monitor, as well.
5122 * 1. foregroundWindow
5123 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
5124 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005125TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005126 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5127 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005128 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005129
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005130 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005131
5132 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5133 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5134 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5135 {100, 200}))
5136 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5137
5138 // Both the foreground window and the global monitor should receive the touch down
5139 window->consumeMotionDown();
5140 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5141
5142 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5143 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5144 ADISPLAY_ID_DEFAULT, {110, 200}))
5145 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5146
5147 window->consumeMotionMove();
5148 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5149
5150 // Now the foreground window goes away
5151 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5152 window->consumeMotionCancel();
5153 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5154
5155 // If more events come in, there will be no more foreground window to send them to. This will
5156 // cause a cancel for the monitor, as well.
5157 ASSERT_EQ(InputEventInjectionResult::FAILED,
5158 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5159 ADISPLAY_ID_DEFAULT, {120, 200}))
5160 << "Injection should fail because the window was removed";
5161 window->assertNoEvents();
5162 // Global monitor now gets the cancel
5163 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5164}
5165
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005166TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005167 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005168 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5169 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005170 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005171
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005172 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005173
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005174 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005175 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005176 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005177 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005178 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005179}
5180
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005181TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5182 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005183
Chris Yea209fde2020-07-22 13:54:51 -07005184 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005185 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5186 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005187 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005188
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005190 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005191 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005192 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005193 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005194
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005195 // Pilfer pointers from the monitor.
5196 // This should not do anything and the window should continue to receive events.
5197 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005198
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005199 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005200 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5201 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005202 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005203
5204 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5205 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005206}
5207
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005208TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005209 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005210 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5211 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005212 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5213 window->setWindowOffset(20, 40);
5214 window->setWindowTransform(0, 1, -1, 0);
5215
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005216 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005217
5218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5219 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5220 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5221 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5222 MotionEvent* event = monitor.consumeMotion();
5223 // Even though window has transform, gesture monitor must not.
5224 ASSERT_EQ(ui::Transform(), event->getTransform());
5225}
5226
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005227TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005228 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005229 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005230
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005231 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005232 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005233 << "Injection should fail if there is a monitor, but no touchable window";
5234 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005235}
5236
chaviw81e2bb92019-12-18 15:03:51 -08005237TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005238 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005239 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5240 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005241
Arthur Hung72d8dc32020-03-28 00:48:39 +00005242 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005243
5244 NotifyMotionArgs motionArgs =
5245 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5246 ADISPLAY_ID_DEFAULT);
5247
Prabir Pradhan678438e2023-04-13 19:32:51 +00005248 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005249 // Window should receive motion down event.
5250 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5251
5252 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005253 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005254 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5255 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5256 motionArgs.pointerCoords[0].getX() - 10);
5257
Prabir Pradhan678438e2023-04-13 19:32:51 +00005258 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005259 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005260 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005261}
5262
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005263/**
5264 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5265 * the device default right away. In the test scenario, we check both the default value,
5266 * and the action of enabling / disabling.
5267 */
5268TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005269 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005270 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5271 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005272 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005273
5274 // Set focused application.
5275 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005276 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005277
5278 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005279 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005280 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005281 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005282
5283 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005284 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005285 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005286 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005287
5288 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005289 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005290 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005291 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005292 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005294 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005295 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005296
5297 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005298 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005299 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005300 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005301
5302 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005303 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005304 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005305 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005306 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005307 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005308 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005309 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005310
5311 window->assertNoEvents();
5312}
5313
Gang Wange9087892020-01-07 12:17:14 -05005314TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005315 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005316 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5317 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005318
5319 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005320 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005321
Arthur Hung72d8dc32020-03-28 00:48:39 +00005322 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005323 setFocusedWindow(window);
5324
Harry Cutts33476232023-01-30 19:57:29 +00005325 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005326
Prabir Pradhan678438e2023-04-13 19:32:51 +00005327 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5328 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005329
5330 InputEvent* event = window->consume();
5331 ASSERT_NE(event, nullptr);
5332
5333 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5334 ASSERT_NE(verified, nullptr);
5335 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5336
5337 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5338 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5339 ASSERT_EQ(keyArgs.source, verified->source);
5340 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5341
5342 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5343
5344 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005345 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005346 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005347 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5348 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5349 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5350 ASSERT_EQ(0, verifiedKey.repeatCount);
5351}
5352
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005353TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005354 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005355 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5356 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005357
5358 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5359
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005360 ui::Transform transform;
5361 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5362
5363 gui::DisplayInfo displayInfo;
5364 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5365 displayInfo.transform = transform;
5366
Patrick Williamsd828f302023-04-28 17:52:08 -05005367 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005368
Prabir Pradhan678438e2023-04-13 19:32:51 +00005369 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005370 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5371 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005372 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005373
5374 InputEvent* event = window->consume();
5375 ASSERT_NE(event, nullptr);
5376
5377 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5378 ASSERT_NE(verified, nullptr);
5379 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5380
5381 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5382 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5383 EXPECT_EQ(motionArgs.source, verified->source);
5384 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5385
5386 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5387
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005388 const vec2 rawXY =
5389 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5390 motionArgs.pointerCoords[0].getXYValue());
5391 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5392 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005393 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005394 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005395 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005396 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5397 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5398}
5399
chaviw09c8d2d2020-08-24 15:48:26 -07005400/**
5401 * Ensure that separate calls to sign the same data are generating the same key.
5402 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5403 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5404 * tests.
5405 */
5406TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5407 KeyEvent event = getTestKeyEvent();
5408 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5409
5410 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5411 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5412 ASSERT_EQ(hmac1, hmac2);
5413}
5414
5415/**
5416 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5417 */
5418TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5419 KeyEvent event = getTestKeyEvent();
5420 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5421 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5422
5423 verifiedEvent.deviceId += 1;
5424 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5425
5426 verifiedEvent.source += 1;
5427 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5428
5429 verifiedEvent.eventTimeNanos += 1;
5430 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5431
5432 verifiedEvent.displayId += 1;
5433 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5434
5435 verifiedEvent.action += 1;
5436 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5437
5438 verifiedEvent.downTimeNanos += 1;
5439 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5440
5441 verifiedEvent.flags += 1;
5442 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5443
5444 verifiedEvent.keyCode += 1;
5445 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5446
5447 verifiedEvent.scanCode += 1;
5448 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5449
5450 verifiedEvent.metaState += 1;
5451 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5452
5453 verifiedEvent.repeatCount += 1;
5454 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5455}
5456
Vishnu Nair958da932020-08-21 17:12:37 -07005457TEST_F(InputDispatcherTest, SetFocusedWindow) {
5458 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5459 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005460 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005461 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005462 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005463 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5464
5465 // Top window is also focusable but is not granted focus.
5466 windowTop->setFocusable(true);
5467 windowSecond->setFocusable(true);
5468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5469 setFocusedWindow(windowSecond);
5470
5471 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005472 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5473 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005474
5475 // Focused window should receive event.
5476 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5477 windowTop->assertNoEvents();
5478}
5479
5480TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5481 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5482 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005483 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005484 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5485
5486 window->setFocusable(true);
5487 // Release channel for window is no longer valid.
5488 window->releaseChannel();
5489 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5490 setFocusedWindow(window);
5491
5492 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005493 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5494 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005495
5496 // window channel is invalid, so it should not receive any input event.
5497 window->assertNoEvents();
5498}
5499
5500TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5501 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5502 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005503 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005504 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005505 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5506
Vishnu Nair958da932020-08-21 17:12:37 -07005507 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5508 setFocusedWindow(window);
5509
5510 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005511 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5512 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005513
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005514 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005515 window->assertNoEvents();
5516}
5517
5518TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5519 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5520 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005521 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005522 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005523 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005524 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5525
5526 windowTop->setFocusable(true);
5527 windowSecond->setFocusable(true);
5528 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5529 setFocusedWindow(windowTop);
5530 windowTop->consumeFocusEvent(true);
5531
Chavi Weingarten847e8512023-03-29 00:26:09 +00005532 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5533 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005534 windowSecond->consumeFocusEvent(true);
5535 windowTop->consumeFocusEvent(false);
5536
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005537 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5538 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005539
5540 // Focused window should receive event.
5541 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5542}
5543
Chavi Weingarten847e8512023-03-29 00:26:09 +00005544TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005545 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5546 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005547 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005548 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005549 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005550 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5551
5552 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005553 windowSecond->setFocusable(false);
5554 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005556 setFocusedWindow(windowTop);
5557 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005558
Chavi Weingarten847e8512023-03-29 00:26:09 +00005559 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5560 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005561
5562 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005563 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005564 windowSecond->assertNoEvents();
5565}
5566
5567TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5568 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5569 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005570 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005571 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005572 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5573 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005574 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5575
5576 window->setFocusable(true);
5577 previousFocusedWindow->setFocusable(true);
5578 window->setVisible(false);
5579 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5580 setFocusedWindow(previousFocusedWindow);
5581 previousFocusedWindow->consumeFocusEvent(true);
5582
5583 // Requesting focus on invisible window takes focus from currently focused window.
5584 setFocusedWindow(window);
5585 previousFocusedWindow->consumeFocusEvent(false);
5586
5587 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005589 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5590 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005591
5592 // Window does not get focus event or key down.
5593 window->assertNoEvents();
5594
5595 // Window becomes visible.
5596 window->setVisible(true);
5597 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5598
5599 // Window receives focus event.
5600 window->consumeFocusEvent(true);
5601 // Focused window receives key down.
5602 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5603}
5604
Vishnu Nair599f1412021-06-21 10:39:58 -07005605TEST_F(InputDispatcherTest, DisplayRemoved) {
5606 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5607 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005608 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005609 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5610
5611 // window is granted focus.
5612 window->setFocusable(true);
5613 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5614 setFocusedWindow(window);
5615 window->consumeFocusEvent(true);
5616
5617 // When a display is removed window loses focus.
5618 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5619 window->consumeFocusEvent(false);
5620}
5621
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005622/**
5623 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5624 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5625 * of the 'slipperyEnterWindow'.
5626 *
5627 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5628 * a way so that the touched location is no longer covered by the top window.
5629 *
5630 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5631 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5632 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5633 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5634 * with ACTION_DOWN).
5635 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5636 * window moved itself away from the touched location and had Flag::SLIPPERY.
5637 *
5638 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5639 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5640 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5641 *
5642 * In this test, we ensure that the event received by the bottom window has
5643 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5644 */
5645TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005646 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5647 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005648
5649 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5650 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5651
5652 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005653 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005654 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005655 // Make sure this one overlaps the bottom window
5656 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5657 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5658 // one. Windows with the same owner are not considered to be occluding each other.
5659 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5660
5661 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005662 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005663 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5664
5665 mDispatcher->setInputWindows(
5666 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5667
5668 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005669 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5670 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5671 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005672 slipperyExitWindow->consumeMotionDown();
5673 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5674 mDispatcher->setInputWindows(
5675 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5676
Prabir Pradhan678438e2023-04-13 19:32:51 +00005677 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5678 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5679 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005680
5681 slipperyExitWindow->consumeMotionCancel();
5682
5683 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5684 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5685}
5686
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005687/**
5688 * Two windows, one on the left and another on the right. The left window is slippery. The right
5689 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5690 * touch moves from the left window into the right window, the gesture should continue to go to the
5691 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5692 * reproduces a crash.
5693 */
5694TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5695 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5696
5697 sp<FakeWindowHandle> leftSlipperyWindow =
5698 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5699 leftSlipperyWindow->setSlippery(true);
5700 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5701
5702 sp<FakeWindowHandle> rightDropTouchesWindow =
5703 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5704 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5705 rightDropTouchesWindow->setDropInput(true);
5706
5707 mDispatcher->setInputWindows(
5708 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5709
5710 // Start touch in the left window
5711 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5712 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5713 .build());
5714 leftSlipperyWindow->consumeMotionDown();
5715
5716 // And move it into the right window
5717 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5718 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5719 .build());
5720
5721 // Since the right window isn't eligible to receive input, touch does not slip.
5722 // The left window continues to receive the gesture.
5723 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5724 rightDropTouchesWindow->assertNoEvents();
5725}
5726
Garfield Tan1c7bc862020-01-28 13:24:04 -08005727class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5728protected:
5729 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5730 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5731
Chris Yea209fde2020-07-22 13:54:51 -07005732 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005733 sp<FakeWindowHandle> mWindow;
5734
5735 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005736 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005737 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005738 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005739 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005740 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5741 ASSERT_EQ(OK, mDispatcher->start());
5742
5743 setUpWindow();
5744 }
5745
5746 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005747 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005748 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005749
Vishnu Nair47074b82020-08-14 11:54:47 -07005750 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005751 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005752 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005753 mWindow->consumeFocusEvent(true);
5754 }
5755
Chris Ye2ad95392020-09-01 13:44:44 -07005756 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005757 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, 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; // Otherwise 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.
5763 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5764 }
5765
5766 void expectKeyRepeatOnce(int32_t repeatCount) {
5767 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5768 InputEvent* repeatEvent = mWindow->consume();
5769 ASSERT_NE(nullptr, repeatEvent);
5770
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005771 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005772
5773 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5774 uint32_t eventAction = repeatKeyEvent->getAction();
5775 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5776 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5777 }
5778
Chris Ye2ad95392020-09-01 13:44:44 -07005779 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005780 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005781 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005782 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005783 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005784
5785 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005786 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005787 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005788 }
5789};
5790
5791TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005792 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005793 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5794 expectKeyRepeatOnce(repeatCount);
5795 }
5796}
5797
5798TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005799 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005800 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5801 expectKeyRepeatOnce(repeatCount);
5802 }
Harry Cutts33476232023-01-30 19:57:29 +00005803 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005804 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005805 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5806 expectKeyRepeatOnce(repeatCount);
5807 }
5808}
5809
5810TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005811 sendAndConsumeKeyDown(/*deviceId=*/1);
5812 expectKeyRepeatOnce(/*repeatCount=*/1);
5813 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005814 mWindow->assertNoEvents();
5815}
5816
5817TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005818 sendAndConsumeKeyDown(/*deviceId=*/1);
5819 expectKeyRepeatOnce(/*repeatCount=*/1);
5820 sendAndConsumeKeyDown(/*deviceId=*/2);
5821 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005822 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005823 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005824 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005825 expectKeyRepeatOnce(/*repeatCount=*/2);
5826 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005827 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005828 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005829 mWindow->assertNoEvents();
5830}
5831
5832TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005833 sendAndConsumeKeyDown(/*deviceId=*/1);
5834 expectKeyRepeatOnce(/*repeatCount=*/1);
5835 sendAndConsumeKeyDown(/*deviceId=*/2);
5836 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005837 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005838 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005839 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005840 mWindow->assertNoEvents();
5841}
5842
liushenxiang42232912021-05-21 20:24:09 +08005843TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5844 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005845 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005846 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005847 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5848 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5849 mWindow->assertNoEvents();
5850}
5851
Garfield Tan1c7bc862020-01-28 13:24:04 -08005852TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005853 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005854 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005855 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5856 InputEvent* repeatEvent = mWindow->consume();
5857 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5858 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5859 IdGenerator::getSource(repeatEvent->getId()));
5860 }
5861}
5862
5863TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005864 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005865 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005866
5867 std::unordered_set<int32_t> idSet;
5868 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5869 InputEvent* repeatEvent = mWindow->consume();
5870 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5871 int32_t id = repeatEvent->getId();
5872 EXPECT_EQ(idSet.end(), idSet.find(id));
5873 idSet.insert(id);
5874 }
5875}
5876
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005877/* Test InputDispatcher for MultiDisplay */
5878class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5879public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005880 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005881 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005882
Chris Yea209fde2020-07-22 13:54:51 -07005883 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005884 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005885 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005886
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005887 // Set focus window for primary display, but focused display would be second one.
5888 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005889 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005890 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005891 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005892 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005893
Chris Yea209fde2020-07-22 13:54:51 -07005894 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005895 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005896 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005897 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005898 // Set focus display to second one.
5899 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5900 // Set focus window for second display.
5901 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005902 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005903 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005904 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005905 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005906 }
5907
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005908 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005909 InputDispatcherTest::TearDown();
5910
Chris Yea209fde2020-07-22 13:54:51 -07005911 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005912 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005913 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005914 windowInSecondary.clear();
5915 }
5916
5917protected:
Chris Yea209fde2020-07-22 13:54:51 -07005918 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005919 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005920 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005921 sp<FakeWindowHandle> windowInSecondary;
5922};
5923
5924TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5925 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005926 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5927 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5928 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005929 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005930 windowInSecondary->assertNoEvents();
5931
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005932 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5934 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5935 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005936 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005937 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005938}
5939
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005940TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005941 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005942 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5943 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005944 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005945 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005946 windowInSecondary->assertNoEvents();
5947
5948 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005949 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005950 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005951 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005952 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005953
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005954 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005955 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005956
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005957 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005958 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005959 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005960
5961 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005962 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005963 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005964 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005965 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005966 windowInSecondary->assertNoEvents();
5967}
5968
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005969// Test per-display input monitors for motion event.
5970TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005971 FakeMonitorReceiver monitorInPrimary =
5972 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5973 FakeMonitorReceiver monitorInSecondary =
5974 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005975
5976 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005977 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5978 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5979 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005980 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005981 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005982 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005983 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005984
5985 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005986 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5987 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5988 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005989 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005990 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005991 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005992 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005993
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005994 // Lift up the touch from the second display
5995 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5996 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5997 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5998 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5999 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
6000
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006001 // Test inject a non-pointer motion event.
6002 // If specific a display, it will dispatch to the focused window of particular display,
6003 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006004 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6005 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
6006 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006007 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006008 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006009 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006010 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006011}
6012
6013// Test per-display input monitors for key event.
6014TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006015 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08006016 FakeMonitorReceiver monitorInPrimary =
6017 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6018 FakeMonitorReceiver monitorInSecondary =
6019 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006020
6021 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6023 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006024 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006025 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006026 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006027 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006028}
6029
Vishnu Nair958da932020-08-21 17:12:37 -07006030TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6031 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006032 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006033 secondWindowInPrimary->setFocusable(true);
6034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
6035 setFocusedWindow(secondWindowInPrimary);
6036 windowInPrimary->consumeFocusEvent(false);
6037 secondWindowInPrimary->consumeFocusEvent(true);
6038
6039 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6041 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006042 windowInPrimary->assertNoEvents();
6043 windowInSecondary->assertNoEvents();
6044 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6045}
6046
Arthur Hungdfd528e2021-12-08 13:23:04 +00006047TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6048 FakeMonitorReceiver monitorInPrimary =
6049 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6050 FakeMonitorReceiver monitorInSecondary =
6051 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6052
6053 // Test touch down on primary display.
6054 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6055 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6056 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6057 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6058 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6059
6060 // Test touch down on second display.
6061 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6062 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6063 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6064 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6065 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6066
6067 // Trigger cancel touch.
6068 mDispatcher->cancelCurrentTouch();
6069 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6070 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6071 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6072 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6073
6074 // Test inject a move motion event, no window/monitor should receive the event.
6075 ASSERT_EQ(InputEventInjectionResult::FAILED,
6076 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6077 ADISPLAY_ID_DEFAULT, {110, 200}))
6078 << "Inject motion event should return InputEventInjectionResult::FAILED";
6079 windowInPrimary->assertNoEvents();
6080 monitorInPrimary.assertNoEvents();
6081
6082 ASSERT_EQ(InputEventInjectionResult::FAILED,
6083 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6084 SECOND_DISPLAY_ID, {110, 200}))
6085 << "Inject motion event should return InputEventInjectionResult::FAILED";
6086 windowInSecondary->assertNoEvents();
6087 monitorInSecondary.assertNoEvents();
6088}
6089
Jackal Guof9696682018-10-05 12:23:23 +08006090class InputFilterTest : public InputDispatcherTest {
6091protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006092 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6093 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006094 NotifyMotionArgs motionArgs;
6095
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006096 motionArgs =
6097 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006098 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006099 motionArgs =
6100 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006101 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006102 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006103 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006104 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6105 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006106 } else {
6107 mFakePolicy->assertFilterInputEventWasNotCalled();
6108 }
6109 }
6110
6111 void testNotifyKey(bool expectToBeFiltered) {
6112 NotifyKeyArgs keyArgs;
6113
6114 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006115 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006116 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006117 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006118 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006119
6120 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006121 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006122 } else {
6123 mFakePolicy->assertFilterInputEventWasNotCalled();
6124 }
6125 }
6126};
6127
6128// Test InputFilter for MotionEvent
6129TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6130 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6131 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6132 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6133
6134 // Enable InputFilter
6135 mDispatcher->setInputFilterEnabled(true);
6136 // Test touch on both primary and second display, and check if both events are filtered.
6137 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6138 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6139
6140 // Disable InputFilter
6141 mDispatcher->setInputFilterEnabled(false);
6142 // Test touch on both primary and second display, and check if both events aren't filtered.
6143 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6144 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6145}
6146
6147// Test InputFilter for KeyEvent
6148TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6149 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6150 testNotifyKey(/*expectToBeFiltered*/ false);
6151
6152 // Enable InputFilter
6153 mDispatcher->setInputFilterEnabled(true);
6154 // Send a key event, and check if it is filtered.
6155 testNotifyKey(/*expectToBeFiltered*/ true);
6156
6157 // Disable InputFilter
6158 mDispatcher->setInputFilterEnabled(false);
6159 // Send a key event, and check if it isn't filtered.
6160 testNotifyKey(/*expectToBeFiltered*/ false);
6161}
6162
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006163// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6164// logical display coordinate space.
6165TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6166 ui::Transform firstDisplayTransform;
6167 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6168 ui::Transform secondDisplayTransform;
6169 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6170
6171 std::vector<gui::DisplayInfo> displayInfos(2);
6172 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6173 displayInfos[0].transform = firstDisplayTransform;
6174 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6175 displayInfos[1].transform = secondDisplayTransform;
6176
Patrick Williamsd828f302023-04-28 17:52:08 -05006177 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006178
6179 // Enable InputFilter
6180 mDispatcher->setInputFilterEnabled(true);
6181
6182 // Ensure the correct transforms are used for the displays.
6183 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6184 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6185}
6186
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006187class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6188protected:
6189 virtual void SetUp() override {
6190 InputDispatcherTest::SetUp();
6191
6192 /**
6193 * We don't need to enable input filter to test the injected event policy, but we enabled it
6194 * here to make the tests more realistic, since this policy only matters when inputfilter is
6195 * on.
6196 */
6197 mDispatcher->setInputFilterEnabled(true);
6198
6199 std::shared_ptr<InputApplicationHandle> application =
6200 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006201 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6202 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006203
6204 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6205 mWindow->setFocusable(true);
6206 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6207 setFocusedWindow(mWindow);
6208 mWindow->consumeFocusEvent(true);
6209 }
6210
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006211 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6212 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006213 KeyEvent event;
6214
6215 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6216 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6217 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006218 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006219 const int32_t additionalPolicyFlags =
6220 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006222 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006223 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6224 policyFlags | additionalPolicyFlags));
6225
6226 InputEvent* received = mWindow->consume();
6227 ASSERT_NE(nullptr, received);
6228 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006229 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006230 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6231 ASSERT_EQ(flags, keyEvent.getFlags());
6232 }
6233
6234 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6235 int32_t flags) {
6236 MotionEvent event;
6237 PointerProperties pointerProperties[1];
6238 PointerCoords pointerCoords[1];
6239 pointerProperties[0].clear();
6240 pointerProperties[0].id = 0;
6241 pointerCoords[0].clear();
6242 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6243 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6244
6245 ui::Transform identityTransform;
6246 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6247 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6248 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6249 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6250 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006251 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006252 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006253 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6254
6255 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6256 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006257 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006258 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6259 policyFlags | additionalPolicyFlags));
6260
6261 InputEvent* received = mWindow->consume();
6262 ASSERT_NE(nullptr, received);
6263 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006264 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006265 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6266 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006267 }
6268
6269private:
6270 sp<FakeWindowHandle> mWindow;
6271};
6272
6273TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006274 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6275 // filter. Without it, the event will no different from a regularly injected event, and the
6276 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006277 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6278 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006279}
6280
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006281TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006282 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006283 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006284 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6285}
6286
6287TEST_F(InputFilterInjectionPolicyTest,
6288 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6289 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006290 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006291 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006292}
6293
6294TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006295 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6296 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006297}
6298
chaviwfd6d3512019-03-25 13:23:49 -07006299class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006300 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006301 InputDispatcherTest::SetUp();
6302
Chris Yea209fde2020-07-22 13:54:51 -07006303 std::shared_ptr<FakeApplicationHandle> application =
6304 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006305 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006306 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006307 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006308
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006309 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006310 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006311 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006312
6313 // Set focused application.
6314 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006315 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006316
6317 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006318 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006319 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006320 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006321 }
6322
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006323 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006324 InputDispatcherTest::TearDown();
6325
6326 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006327 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006328 }
6329
6330protected:
6331 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006332 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006333 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006334};
6335
6336// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6337// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6338// the onPointerDownOutsideFocus callback.
6339TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006340 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006341 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6342 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006343 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006344 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006345
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006346 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006347 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6348}
6349
6350// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6351// DOWN on the window that doesn't have focus. Ensure no window received the
6352// onPointerDownOutsideFocus callback.
6353TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006354 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006355 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006356 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006357 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006358
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006359 ASSERT_TRUE(mDispatcher->waitForIdle());
6360 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006361}
6362
6363// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6364// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6365TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6367 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006368 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006369 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006370
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006371 ASSERT_TRUE(mDispatcher->waitForIdle());
6372 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006373}
6374
6375// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6376// DOWN on the window that already has focus. Ensure no window received the
6377// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006378TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006379 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006380 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006381 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006382 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006383 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006384
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006385 ASSERT_TRUE(mDispatcher->waitForIdle());
6386 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006387}
6388
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006389// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6390// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6391TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6392 const MotionEvent event =
6393 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6394 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006395 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006396 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6397 .build();
6398 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6399 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6400 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6401
6402 ASSERT_TRUE(mDispatcher->waitForIdle());
6403 mFakePolicy->assertOnPointerDownWasNotCalled();
6404 // Ensure that the unfocused window did not receive any FOCUS events.
6405 mUnfocusedWindow->assertNoEvents();
6406}
6407
chaviwaf87b3e2019-10-01 16:59:28 -07006408// These tests ensures we can send touch events to a single client when there are multiple input
6409// windows that point to the same client token.
6410class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6411 virtual void SetUp() override {
6412 InputDispatcherTest::SetUp();
6413
Chris Yea209fde2020-07-22 13:54:51 -07006414 std::shared_ptr<FakeApplicationHandle> application =
6415 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006416 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6417 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006418 mWindow1->setFrame(Rect(0, 0, 100, 100));
6419
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006420 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6421 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006422 mWindow2->setFrame(Rect(100, 100, 200, 200));
6423
Arthur Hung72d8dc32020-03-28 00:48:39 +00006424 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006425 }
6426
6427protected:
6428 sp<FakeWindowHandle> mWindow1;
6429 sp<FakeWindowHandle> mWindow2;
6430
6431 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006432 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006433 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6434 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006435 }
6436
6437 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6438 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006439 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006440 InputEvent* event = window->consume();
6441
6442 ASSERT_NE(nullptr, event) << name.c_str()
6443 << ": consumer should have returned non-NULL event.";
6444
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006445 ASSERT_EQ(InputEventType::MOTION, event->getType())
6446 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006447
6448 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006449 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006450 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006451
6452 for (size_t i = 0; i < points.size(); i++) {
6453 float expectedX = points[i].x;
6454 float expectedY = points[i].y;
6455
6456 EXPECT_EQ(expectedX, motionEvent.getX(i))
6457 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6458 << ", got " << motionEvent.getX(i);
6459 EXPECT_EQ(expectedY, motionEvent.getY(i))
6460 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6461 << ", got " << motionEvent.getY(i);
6462 }
6463 }
chaviw9eaa22c2020-07-01 16:21:27 -07006464
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006465 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006466 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006467 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6468 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006469
6470 // Always consume from window1 since it's the window that has the InputReceiver
6471 consumeMotionEvent(mWindow1, action, expectedPoints);
6472 }
chaviwaf87b3e2019-10-01 16:59:28 -07006473};
6474
6475TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6476 // Touch Window 1
6477 PointF touchedPoint = {10, 10};
6478 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006479 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006480
6481 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006482 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006483
6484 // Touch Window 2
6485 touchedPoint = {150, 150};
6486 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006487 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006488}
6489
chaviw9eaa22c2020-07-01 16:21:27 -07006490TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6491 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006492 mWindow2->setWindowScale(0.5f, 0.5f);
6493
6494 // Touch Window 1
6495 PointF touchedPoint = {10, 10};
6496 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006497 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006498 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006499 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006500
6501 // Touch Window 2
6502 touchedPoint = {150, 150};
6503 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006504 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6505 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006506
chaviw9eaa22c2020-07-01 16:21:27 -07006507 // Update the transform so rotation is set
6508 mWindow2->setWindowTransform(0, -1, 1, 0);
6509 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6510 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006511}
6512
chaviw9eaa22c2020-07-01 16:21:27 -07006513TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
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]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006524 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006525
chaviw9eaa22c2020-07-01 16:21:27 -07006526 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006527 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006528 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006529
chaviw9eaa22c2020-07-01 16:21:27 -07006530 // Update the transform so rotation is set for Window 2
6531 mWindow2->setWindowTransform(0, -1, 1, 0);
6532 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006533 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006534}
6535
chaviw9eaa22c2020-07-01 16:21:27 -07006536TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006537 mWindow2->setWindowScale(0.5f, 0.5f);
6538
6539 // Touch Window 1
6540 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6541 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006542 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006543
6544 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006545 touchedPoints.push_back(PointF{150, 150});
6546 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006547
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006548 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006549
6550 // Move both windows
6551 touchedPoints = {{20, 20}, {175, 175}};
6552 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6553 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6554
chaviw9eaa22c2020-07-01 16:21:27 -07006555 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006556
chaviw9eaa22c2020-07-01 16:21:27 -07006557 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006558 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006559 expectedPoints.pop_back();
6560
6561 // Touch Window 2
6562 mWindow2->setWindowTransform(0, -1, 1, 0);
6563 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006564 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006565
6566 // Move both windows
6567 touchedPoints = {{20, 20}, {175, 175}};
6568 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6569 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6570
6571 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006572}
6573
6574TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6575 mWindow1->setWindowScale(0.5f, 0.5f);
6576
6577 // Touch Window 1
6578 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6579 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006580 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006581
6582 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006583 touchedPoints.push_back(PointF{150, 150});
6584 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006585
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006586 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006587
6588 // Move both windows
6589 touchedPoints = {{20, 20}, {175, 175}};
6590 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6591 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6592
chaviw9eaa22c2020-07-01 16:21:27 -07006593 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006594}
6595
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006596/**
6597 * When one of the windows is slippery, the touch should not slip into the other window with the
6598 * same input channel.
6599 */
6600TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6601 mWindow1->setSlippery(true);
6602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6603
6604 // Touch down in window 1
6605 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6606 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6607 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6608
6609 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6610 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6611 // getting generated.
6612 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6613 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6614
6615 consumeMotionEvent(mWindow1, ACTION_MOVE, {{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