blob: fecd0d131594891563ae836e59e06a6a1963060d [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) {
Garfield Tandf26e862020-07-01 20:18:19 -07001732 MotionEvent event = MotionEventBuilder(action, source)
1733 .displayId(displayId)
1734 .eventTime(eventTime)
1735 .rawXCursorPosition(cursorPosition.x)
1736 .rawYCursorPosition(cursorPosition.y)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001737 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Garfield Tandf26e862020-07-01 20:18:19 -07001738 .x(position.x)
1739 .y(position.y))
1740 .build();
Arthur Hungb92218b2018-08-14 12:00:21 +08001741
1742 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001743 return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid,
1744 policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001745}
1746
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001747static InputEventInjectionResult injectMotionDown(
1748 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId,
1749 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001750 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location);
Garfield Tan00f511d2019-06-12 16:55:40 -07001751}
1752
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001753static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001754 int32_t source, int32_t displayId,
1755 const PointF& location = {100, 200}) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001756 return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location);
Michael Wright3a240c42019-12-10 20:53:41 +00001757}
1758
Jackal Guof9696682018-10-05 12:23:23 +08001759static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) {
1760 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1761 // Define a valid key event.
Harry Cutts33476232023-01-30 19:57:29 +00001762 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001763 displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A,
1764 KEY_A, AMETA_NONE, currentTime);
Jackal Guof9696682018-10-05 12:23:23 +08001765
1766 return args;
1767}
1768
Josep del Riob3981622023-04-18 15:49:45 +00001769static NotifyKeyArgs generateSystemShortcutArgs(int32_t action,
1770 int32_t displayId = ADISPLAY_ID_NONE) {
1771 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1772 // Define a valid key event.
1773 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1774 displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON,
1775 currentTime);
1776
1777 return args;
1778}
1779
1780static NotifyKeyArgs generateAssistantKeyArgs(int32_t action,
1781 int32_t displayId = ADISPLAY_ID_NONE) {
1782 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1783 // Define a valid key event.
1784 NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
1785 displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT,
1786 AMETA_NONE, currentTime);
1787
1788 return args;
1789}
1790
Prabir Pradhan678438e2023-04-13 19:32:51 +00001791[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
1792 int32_t displayId,
1793 const std::vector<PointF>& points) {
chaviwd1c23182019-12-20 18:44:56 -08001794 size_t pointerCount = points.size();
chaviwaf87b3e2019-10-01 16:59:28 -07001795 if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) {
1796 EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer";
1797 }
1798
chaviwd1c23182019-12-20 18:44:56 -08001799 PointerProperties pointerProperties[pointerCount];
1800 PointerCoords pointerCoords[pointerCount];
Jackal Guof9696682018-10-05 12:23:23 +08001801
chaviwd1c23182019-12-20 18:44:56 -08001802 for (size_t i = 0; i < pointerCount; i++) {
1803 pointerProperties[i].clear();
1804 pointerProperties[i].id = i;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001805 pointerProperties[i].toolType = ToolType::FINGER;
Jackal Guof9696682018-10-05 12:23:23 +08001806
chaviwd1c23182019-12-20 18:44:56 -08001807 pointerCoords[i].clear();
1808 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x);
1809 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y);
1810 }
Jackal Guof9696682018-10-05 12:23:23 +08001811
1812 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1813 // Define a valid motion event.
Harry Cutts33476232023-01-30 19:57:29 +00001814 NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId,
Garfield Tan00f511d2019-06-12 16:55:40 -07001815 POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0,
1816 AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviwd1c23182019-12-20 18:44:56 -08001817 AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties,
1818 pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0,
Garfield Tan00f511d2019-06-12 16:55:40 -07001819 AMOTION_EVENT_INVALID_CURSOR_POSITION,
1820 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
Jackal Guof9696682018-10-05 12:23:23 +08001821
1822 return args;
1823}
1824
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08001825static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) {
1826 return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points);
1827}
1828
chaviwd1c23182019-12-20 18:44:56 -08001829static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) {
1830 return generateMotionArgs(action, source, displayId, {PointF{100, 200}});
1831}
1832
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001833static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs(
1834 const PointerCaptureRequest& request) {
Harry Cutts33476232023-01-30 19:57:29 +00001835 return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request);
Prabir Pradhan99987712020-11-10 18:43:05 -08001836}
1837
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001838/**
1839 * When a window unexpectedly disposes of its input channel, policy should be notified about the
1840 * broken channel.
1841 */
1842TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) {
1843 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1844 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001845 sp<FakeWindowHandle>::make(application, mDispatcher,
1846 "Window that breaks its input channel", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001847
1848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1849
1850 // Window closes its channel, but the window remains.
1851 window->destroyReceiver();
1852 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token);
1853}
1854
Arthur Hungb92218b2018-08-14 12:00:21 +08001855TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001856 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001857 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1858 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001859
Arthur Hung72d8dc32020-03-28 00:48:39 +00001860 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001861 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1862 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1863 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001864
1865 // Window should receive motion event.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001866 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001867}
1868
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001869TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) {
1870 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001871 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1872 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001873
1874 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1875 // Inject a MotionEvent to an unknown display.
1876 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1877 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE))
1878 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1879
1880 // Window should receive motion event.
1881 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1882}
1883
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001884/**
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001885 * Calling setInputWindows once should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001886 * This test serves as a sanity check for the next test, where setInputWindows is
1887 * called twice.
1888 */
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001889TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) {
Chris Yea209fde2020-07-22 13:54:51 -07001890 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001891 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1892 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001893 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001894
1895 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001896 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001897 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1898 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001899 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001900
1901 // Window should receive motion event.
1902 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1903}
1904
1905/**
1906 * Calling setInputWindows twice, with the same info, should not cause any issues.
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001907 */
1908TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001909 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001910 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
1911 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001912 window->setFrame(Rect(0, 0, 100, 100));
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001913
1914 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
1915 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001917 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1918 {50, 50}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001919 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07001920
1921 // Window should receive motion event.
1922 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
1923}
1924
Arthur Hungb92218b2018-08-14 12:00:21 +08001925// The foreground window should receive the first touch down event.
1926TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07001927 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001928 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001929 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001930 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001931 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001932
Arthur Hung72d8dc32020-03-28 00:48:39 +00001933 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001934 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1935 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
1936 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08001937
1938 // Top window should receive the touch down event. Second window should not receive anything.
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001939 windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08001940 windowSecond->assertNoEvents();
1941}
1942
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001943/**
1944 * Two windows: A top window, and a wallpaper behind the window.
1945 * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window
1946 * gets ACTION_CANCEL.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001947 * 1. foregroundWindow <-- dup touch to wallpaper
1948 * 2. wallpaperWindow <-- is wallpaper
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001949 */
1950TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) {
1951 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1952 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001953 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001954 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001955 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001956 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001957 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001958
1959 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
1960 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1961 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
1962 {100, 200}))
1963 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1964
1965 // Both foreground window and its wallpaper should receive the touch down
1966 foregroundWindow->consumeMotionDown();
1967 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1968
1969 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
1970 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
1971 ADISPLAY_ID_DEFAULT, {110, 200}))
1972 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
1973
1974 foregroundWindow->consumeMotionMove();
1975 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1976
1977 // Now the foreground window goes away, but the wallpaper stays
1978 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
1979 foregroundWindow->consumeMotionCancel();
1980 // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
1981 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
1982}
1983
1984/**
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001985 * Two fingers down on the window, and lift off the first finger.
1986 * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event
1987 * contains a single pointer.
1988 */
1989TEST_F(InputDispatcherTest, CancelAfterPointer0Up) {
1990 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
1991 sp<FakeWindowHandle> window =
1992 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
1993
1994 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001995 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00001996 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
1997 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
1998 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08001999 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002000 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2001 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2002 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
2003 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002004 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002005 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2006 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2007 .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100))
2008 .build());
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002009 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2010 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2011 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2012
2013 // Remove the window. The gesture should be canceled
2014 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
2015 const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}};
2016 window->consumeMotionEvent(
2017 AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers)));
2018}
2019
2020/**
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002021 * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above,
2022 * with the following differences:
2023 * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to
2024 * clean up the connection.
2025 * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful.
2026 * Ensure that there's no crash in the dispatcher.
2027 */
2028TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) {
2029 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2030 sp<FakeWindowHandle> foregroundWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002031 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002032 foregroundWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002033 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002034 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002035 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002036
2037 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
2038 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2039 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2040 {100, 200}))
2041 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2042
2043 // Both foreground window and its wallpaper should receive the touch down
2044 foregroundWindow->consumeMotionDown();
2045 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2046
2047 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2048 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2049 ADISPLAY_ID_DEFAULT, {110, 200}))
2050 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2051
2052 foregroundWindow->consumeMotionMove();
2053 wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2054
2055 // Wallpaper closes its channel, but the window remains.
2056 wallpaperWindow->destroyReceiver();
2057 mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token);
2058
2059 // Now the foreground window goes away, but the wallpaper stays, even though its channel
2060 // is no longer valid.
2061 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}});
2062 foregroundWindow->consumeMotionCancel();
2063}
2064
Arthur Hungc539dbb2022-12-08 07:45:36 +00002065class ShouldSplitTouchFixture : public InputDispatcherTest,
2066 public ::testing::WithParamInterface<bool> {};
2067INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture,
2068 ::testing::Values(true, false));
Siarhei Vishniakou2b030972021-11-18 10:01:27 -08002069/**
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002070 * A single window that receives touch (on top), and a wallpaper window underneath it.
2071 * The top window gets a multitouch gesture.
2072 * Ensure that wallpaper gets the same gesture.
2073 */
Arthur Hungc539dbb2022-12-08 07:45:36 +00002074TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002075 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002076 sp<FakeWindowHandle> foregroundWindow =
2077 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
2078 foregroundWindow->setDupTouchToWallpaper(true);
2079 foregroundWindow->setPreventSplitting(GetParam());
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002080
2081 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002082 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002083 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002084
Arthur Hungc539dbb2022-12-08 07:45:36 +00002085 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}});
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002086
2087 // Touch down on top window
2088 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2089 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2090 {100, 100}))
2091 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2092
2093 // Both top window and its wallpaper should receive the touch down
Arthur Hungc539dbb2022-12-08 07:45:36 +00002094 foregroundWindow->consumeMotionDown();
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002095 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2096
2097 // Second finger down on the top window
2098 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002099 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002100 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002101 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2102 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002103 .build();
2104 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2105 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2106 InputEventInjectionSync::WAIT_FOR_RESULT))
2107 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2108
Harry Cutts33476232023-01-30 19:57:29 +00002109 foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
2110 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002111 expectedWallpaperFlags);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002112
2113 const MotionEvent secondFingerUpEvent =
2114 MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2115 .displayId(ADISPLAY_ID_DEFAULT)
2116 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002117 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2118 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002119 .build();
2120 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2121 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
2122 InputEventInjectionSync::WAIT_FOR_RESULT))
2123 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2124 foregroundWindow->consumeMotionPointerUp(0);
2125 wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2126
2127 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002128 injectMotionEvent(mDispatcher,
2129 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2130 AINPUT_SOURCE_TOUCHSCREEN)
2131 .displayId(ADISPLAY_ID_DEFAULT)
2132 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
2133 .pointer(PointerBuilder(/* id */ 1,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002134 ToolType::FINGER)
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08002135 .x(100)
2136 .y(100))
2137 .build(),
2138 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT))
Arthur Hungc539dbb2022-12-08 07:45:36 +00002139 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2140 foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
2141 wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002142}
2143
2144/**
2145 * Two windows: a window on the left and window on the right.
2146 * A third window, wallpaper, is behind both windows, and spans both top windows.
2147 * The first touch down goes to the left window. A second pointer touches down on the right window.
2148 * The touch is split, so both left and right windows should receive ACTION_DOWN.
2149 * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by
2150 * ACTION_POINTER_DOWN(1).
2151 */
2152TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) {
2153 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2154 sp<FakeWindowHandle> leftWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002155 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002156 leftWindow->setFrame(Rect(0, 0, 200, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002157 leftWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002158
2159 sp<FakeWindowHandle> rightWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002160 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002161 rightWindow->setFrame(Rect(200, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002162 rightWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002163
2164 sp<FakeWindowHandle> wallpaperWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002165 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002166 wallpaperWindow->setFrame(Rect(0, 0, 400, 200));
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002167 wallpaperWindow->setIsWallpaper(true);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002168
2169 mDispatcher->setInputWindows(
2170 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
2171
2172 // Touch down on left window
2173 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2174 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
2175 {100, 100}))
2176 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2177
2178 // Both foreground window and its wallpaper should receive the touch down
2179 leftWindow->consumeMotionDown();
2180 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2181
2182 // Second finger down on the right window
2183 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002184 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002185 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002186 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2187 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002188 .build();
2189 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2190 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
2191 InputEventInjectionSync::WAIT_FOR_RESULT))
2192 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2193
2194 leftWindow->consumeMotionMove();
2195 // Since the touch is split, right window gets ACTION_DOWN
2196 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00002197 wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002198 expectedWallpaperFlags);
2199
2200 // Now, leftWindow, which received the first finger, disappears.
2201 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}});
2202 leftWindow->consumeMotionCancel();
2203 // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too.
2204 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2205
2206 // The pointer that's still down on the right window moves, and goes to the right window only.
2207 // As far as the dispatcher's concerned though, both pointers are still present.
2208 const MotionEvent secondFingerMoveEvent =
2209 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
2210 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100))
2212 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110))
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002213 .build();
2214 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2215 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
2216 InputEventInjectionSync::WAIT_FOR_RESULT));
2217 rightWindow->consumeMotionMove();
2218
2219 leftWindow->assertNoEvents();
2220 rightWindow->assertNoEvents();
2221 wallpaperWindow->assertNoEvents();
2222}
2223
Arthur Hungc539dbb2022-12-08 07:45:36 +00002224/**
2225 * Two windows: a window on the left with dup touch to wallpaper and window on the right without it.
2226 * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL
2227 * The right window should receive ACTION_DOWN.
2228 */
2229TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) {
Arthur Hung74c248d2022-11-23 07:09:59 +00002230 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Arthur Hungc539dbb2022-12-08 07:45:36 +00002231 sp<FakeWindowHandle> leftWindow =
2232 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2233 leftWindow->setFrame(Rect(0, 0, 200, 200));
2234 leftWindow->setDupTouchToWallpaper(true);
2235 leftWindow->setSlippery(true);
2236
2237 sp<FakeWindowHandle> rightWindow =
2238 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2239 rightWindow->setFrame(Rect(200, 0, 400, 200));
Arthur Hung74c248d2022-11-23 07:09:59 +00002240
2241 sp<FakeWindowHandle> wallpaperWindow =
2242 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
2243 wallpaperWindow->setIsWallpaper(true);
Arthur Hung74c248d2022-11-23 07:09:59 +00002244
Arthur Hungc539dbb2022-12-08 07:45:36 +00002245 mDispatcher->setInputWindows(
2246 {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}});
Arthur Hung74c248d2022-11-23 07:09:59 +00002247
Arthur Hungc539dbb2022-12-08 07:45:36 +00002248 // Touch down on left window
Arthur Hung74c248d2022-11-23 07:09:59 +00002249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2250 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002251 {100, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002252 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungc539dbb2022-12-08 07:45:36 +00002253
2254 // Both foreground window and its wallpaper should receive the touch down
2255 leftWindow->consumeMotionDown();
Arthur Hung74c248d2022-11-23 07:09:59 +00002256 wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
2257
Arthur Hungc539dbb2022-12-08 07:45:36 +00002258 // Move to right window, the left window should receive cancel.
Arthur Hung74c248d2022-11-23 07:09:59 +00002259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Arthur Hungc539dbb2022-12-08 07:45:36 +00002260 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
2261 ADISPLAY_ID_DEFAULT, {201, 100}))
Arthur Hung74c248d2022-11-23 07:09:59 +00002262 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
2263
Arthur Hungc539dbb2022-12-08 07:45:36 +00002264 leftWindow->consumeMotionCancel();
2265 rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
2266 wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Arthur Hung74c248d2022-11-23 07:09:59 +00002267}
2268
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08002269/**
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002270 * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not
2271 * interactive, it might stop sending this flag.
2272 * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure
2273 * to have a consistent input stream.
2274 *
2275 * Test procedure:
2276 * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL.
2277 * DOWN (new gesture).
2278 *
2279 * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent
2280 * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app.
2281 *
2282 * We technically just need a single window here, but we are using two windows (spy on top and a
2283 * regular window below) to emulate the actual situation where it happens on the device.
2284 */
2285TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) {
2286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2287 sp<FakeWindowHandle> spyWindow =
2288 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2289 spyWindow->setFrame(Rect(0, 0, 200, 200));
2290 spyWindow->setTrustedOverlay(true);
2291 spyWindow->setSpy(true);
2292
2293 sp<FakeWindowHandle> window =
2294 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2295 window->setFrame(Rect(0, 0, 200, 200));
2296
2297 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2298 const int32_t touchDeviceId = 4;
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002299
2300 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002301 mDispatcher->notifyMotion(
2302 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2303 .deviceId(touchDeviceId)
2304 .policyFlags(DEFAULT_POLICY_FLAGS)
2305 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2306 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002307
Prabir Pradhan678438e2023-04-13 19:32:51 +00002308 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2309 .deviceId(touchDeviceId)
2310 .policyFlags(DEFAULT_POLICY_FLAGS)
2311 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2312 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2313 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002314 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2315 spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2316 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2317 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2318
2319 // Cancel the current gesture. Send the cancel without the default policy flags.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002320 mDispatcher->notifyMotion(
2321 MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN)
2322 .deviceId(touchDeviceId)
2323 .policyFlags(0)
2324 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2325 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
2326 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002327 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2328 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL));
2329
2330 // We don't need to reset the device to reproduce the issue, but the reset event typically
2331 // follows, so we keep it here to model the actual listener behaviour more closely.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002332 mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId});
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002333
2334 // Start new gesture
Prabir Pradhan678438e2023-04-13 19:32:51 +00002335 mDispatcher->notifyMotion(
2336 MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2337 .deviceId(touchDeviceId)
2338 .policyFlags(DEFAULT_POLICY_FLAGS)
2339 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
2340 .build());
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08002341 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2342 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2343
2344 // No more events
2345 spyWindow->assertNoEvents();
2346 window->assertNoEvents();
2347}
2348
2349/**
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002350 * Two windows: a window on the left and a window on the right.
2351 * Mouse is hovered from the right window into the left window.
2352 * Next, we tap on the left window, where the cursor was last seen.
2353 * The second tap is done onto the right window.
2354 * The mouse and tap are from two different devices.
2355 * We technically don't need to set the downtime / eventtime for these events, but setting these
2356 * explicitly helps during debugging.
2357 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2358 * In the buggy implementation, a tap on the right window would cause a crash.
2359 */
2360TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) {
2361 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2362 sp<FakeWindowHandle> leftWindow =
2363 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2364 leftWindow->setFrame(Rect(0, 0, 200, 200));
2365
2366 sp<FakeWindowHandle> rightWindow =
2367 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2368 rightWindow->setFrame(Rect(200, 0, 400, 200));
2369
2370 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2371 // All times need to start at the current time, otherwise the dispatcher will drop the events as
2372 // stale.
2373 const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC);
2374 const int32_t mouseDeviceId = 6;
2375 const int32_t touchDeviceId = 4;
2376 // Move the cursor from right
2377 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2378 injectMotionEvent(mDispatcher,
2379 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2380 AINPUT_SOURCE_MOUSE)
2381 .deviceId(mouseDeviceId)
2382 .downTime(baseTime + 10)
2383 .eventTime(baseTime + 20)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002384 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002385 .x(300)
2386 .y(100))
2387 .build()));
2388 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2389
2390 // .. to the left window
2391 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2392 injectMotionEvent(mDispatcher,
2393 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2394 AINPUT_SOURCE_MOUSE)
2395 .deviceId(mouseDeviceId)
2396 .downTime(baseTime + 10)
2397 .eventTime(baseTime + 30)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002398 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002399 .x(110)
2400 .y(100))
2401 .build()));
2402 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2403 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2404 // Now tap the left window
2405 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2406 injectMotionEvent(mDispatcher,
2407 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2408 AINPUT_SOURCE_TOUCHSCREEN)
2409 .deviceId(touchDeviceId)
2410 .downTime(baseTime + 40)
2411 .eventTime(baseTime + 40)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002412 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002413 .x(100)
2414 .y(100))
2415 .build()));
2416 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2417 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2418
2419 // release tap
2420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2421 injectMotionEvent(mDispatcher,
2422 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2423 AINPUT_SOURCE_TOUCHSCREEN)
2424 .deviceId(touchDeviceId)
2425 .downTime(baseTime + 40)
2426 .eventTime(baseTime + 50)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002427 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002428 .x(100)
2429 .y(100))
2430 .build()));
2431 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2432
2433 // Tap the window on the right
2434 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2435 injectMotionEvent(mDispatcher,
2436 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2437 AINPUT_SOURCE_TOUCHSCREEN)
2438 .deviceId(touchDeviceId)
2439 .downTime(baseTime + 60)
2440 .eventTime(baseTime + 60)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002441 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002442 .x(300)
2443 .y(100))
2444 .build()));
2445 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2446
2447 // release tap
2448 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2449 injectMotionEvent(mDispatcher,
2450 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2451 AINPUT_SOURCE_TOUCHSCREEN)
2452 .deviceId(touchDeviceId)
2453 .downTime(baseTime + 60)
2454 .eventTime(baseTime + 70)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002455 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002456 .x(300)
2457 .y(100))
2458 .build()));
2459 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2460
2461 // No more events
2462 leftWindow->assertNoEvents();
2463 rightWindow->assertNoEvents();
2464}
2465
2466/**
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002467 * Start hovering in a window. While this hover is still active, make another window appear on top.
2468 * The top, obstructing window has no input channel, so it's not supposed to receive input.
2469 * While the top window is present, the hovering is stopped.
2470 * Later, hovering gets resumed again.
2471 * Ensure that new hover gesture is handled correctly.
2472 * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly
2473 * to the window that's currently being hovered over.
2474 */
2475TEST_F(InputDispatcherTest, HoverWhileWindowAppears) {
2476 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2477 sp<FakeWindowHandle> window =
2478 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2479 window->setFrame(Rect(0, 0, 200, 200));
2480
2481 // Only a single window is present at first
2482 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2483
2484 // Start hovering in the window
2485 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2486 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2487 .build());
2488 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2489
2490 // Now, an obscuring window appears!
2491 sp<FakeWindowHandle> obscuringWindow =
2492 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2493 ADISPLAY_ID_DEFAULT,
2494 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2495 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2496 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2497 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2498 obscuringWindow->setNoInputChannel(true);
2499 obscuringWindow->setFocusable(false);
2500 obscuringWindow->setAlpha(1.0);
2501 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2502
2503 // While this new obscuring window is present, the hovering is stopped
2504 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2505 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2506 .build());
2507 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2508
2509 // Now the obscuring window goes away.
2510 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2511
2512 // And a new hover gesture starts.
2513 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2514 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2515 .build());
2516 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2517}
2518
2519/**
2520 * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to
2521 * the obscuring window.
2522 */
2523TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) {
2524 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2525 sp<FakeWindowHandle> window =
2526 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2527 window->setFrame(Rect(0, 0, 200, 200));
2528
2529 // Only a single window is present at first
2530 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2531
2532 // Start hovering in the window
2533 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2534 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2535 .build());
2536 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2537
2538 // Now, an obscuring window appears!
2539 sp<FakeWindowHandle> obscuringWindow =
2540 sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window",
2541 ADISPLAY_ID_DEFAULT,
2542 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
2543 obscuringWindow->setFrame(Rect(0, 0, 200, 200));
2544 obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED);
2545 obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
2546 obscuringWindow->setNoInputChannel(true);
2547 obscuringWindow->setFocusable(false);
2548 obscuringWindow->setAlpha(1.0);
2549 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
2550
2551 // While this new obscuring window is present, the hovering continues. The event can't go to the
2552 // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window.
2553 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2554 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2555 .build());
2556 obscuringWindow->assertNoEvents();
2557 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2558
2559 // Now the obscuring window goes away.
2560 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2561
2562 // Hovering continues in the same position. The hovering pointer re-enters the bottom window,
2563 // so it should generate a HOVER_ENTER
2564 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2565 .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100))
2566 .build());
2567 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2568
2569 // Now the MOVE should be getting dispatched normally
2570 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS)
2571 .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110))
2572 .build());
2573 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE));
2574}
2575
2576/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002577 * Two windows: a window on the left and a window on the right.
2578 * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains
2579 * down. Then, on the left window, also place second touch pointer down.
2580 * This test tries to reproduce a crash.
2581 * In the buggy implementation, second pointer down on the left window would cause a crash.
2582 */
2583TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) {
2584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2585 sp<FakeWindowHandle> leftWindow =
2586 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2587 leftWindow->setFrame(Rect(0, 0, 200, 200));
2588
2589 sp<FakeWindowHandle> rightWindow =
2590 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2591 rightWindow->setFrame(Rect(200, 0, 400, 200));
2592
2593 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2594
2595 const int32_t touchDeviceId = 4;
2596 const int32_t mouseDeviceId = 6;
2597 NotifyMotionArgs args;
2598
2599 // Start hovering over the left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002600 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
2601 .deviceId(mouseDeviceId)
2602 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2603 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002604 leftWindow->consumeMotionEvent(
2605 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
2606
2607 // Mouse down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002608 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2609 .deviceId(mouseDeviceId)
2610 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2611 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2612 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002613
2614 leftWindow->consumeMotionEvent(
2615 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
2616 leftWindow->consumeMotionEvent(
2617 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2618
Prabir Pradhan678438e2023-04-13 19:32:51 +00002619 mDispatcher->notifyMotion(
2620 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2621 .deviceId(mouseDeviceId)
2622 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2623 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2624 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
2625 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002626 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2627
2628 // First touch pointer down on right window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002629 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2630 .deviceId(touchDeviceId)
2631 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2632 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002633 leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
2634
2635 rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2636
2637 // Second touch pointer down on left window
Prabir Pradhan678438e2023-04-13 19:32:51 +00002638 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2639 .deviceId(touchDeviceId)
2640 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2641 .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100))
2642 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002643 leftWindow->consumeMotionEvent(
2644 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2645 // This MOVE event is not necessary (doesn't carry any new information), but it's there in the
2646 // current implementation.
2647 const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}};
2648 rightWindow->consumeMotionEvent(
2649 AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers)));
2650
2651 leftWindow->assertNoEvents();
2652 rightWindow->assertNoEvents();
2653}
2654
2655/**
2656 * On a single window, use two different devices: mouse and touch.
2657 * Touch happens first, with two pointers going down, and then the first pointer leaving.
2658 * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL.
2659 * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored,
2660 * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not
2661 * represent a new gesture.
2662 */
2663TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) {
2664 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2665 sp<FakeWindowHandle> window =
2666 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2667 window->setFrame(Rect(0, 0, 400, 400));
2668
2669 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2670
2671 const int32_t touchDeviceId = 4;
2672 const int32_t mouseDeviceId = 6;
2673 NotifyMotionArgs args;
2674
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08002675 // First touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002676 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2677 .deviceId(touchDeviceId)
2678 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2679 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002680 // Second touch pointer down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002681 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2682 .deviceId(touchDeviceId)
2683 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2684 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2685 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002686 // First touch pointer lifts. The second one remains down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002687 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN)
2688 .deviceId(touchDeviceId)
2689 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2690 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2691 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002692 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2693 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
2694 window->consumeMotionEvent(WithMotionAction(POINTER_0_UP));
2695
2696 // Mouse down. The touch should be canceled
Prabir Pradhan678438e2023-04-13 19:32:51 +00002697 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2698 .deviceId(mouseDeviceId)
2699 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2700 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2701 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002702
2703 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -08002704 WithPointerCount(1u)));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002705 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
2706
Prabir Pradhan678438e2023-04-13 19:32:51 +00002707 mDispatcher->notifyMotion(
2708 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
2709 .deviceId(mouseDeviceId)
2710 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
2711 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
2712 .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100))
2713 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002714 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
2715
2716 // Second touch pointer down.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002717 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2718 .deviceId(touchDeviceId)
2719 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2720 .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100))
2721 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002722 // The pointer_down event should be ignored
2723 window->assertNoEvents();
2724}
2725
2726/**
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002727 * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels
2728 * the injected event.
2729 */
2730TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) {
2731 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2732 sp<FakeWindowHandle> window =
2733 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2734 window->setFrame(Rect(0, 0, 400, 400));
2735
2736 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2737
2738 const int32_t touchDeviceId = 4;
2739 NotifyMotionArgs args;
2740 // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after
2741 // completion.
2742 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2743 injectMotionEvent(mDispatcher,
2744 MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
2745 .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002746 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002747 .x(50)
2748 .y(50))
2749 .build()));
2750 window->consumeMotionEvent(
2751 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2752
2753 // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer
2754 // should be canceled and the new gesture should take over.
Prabir Pradhan678438e2023-04-13 19:32:51 +00002755 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2756 .deviceId(touchDeviceId)
2757 .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100))
2758 .build());
Siarhei Vishniakou56e79092023-02-21 19:13:16 -08002759
2760 window->consumeMotionEvent(
2761 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID)));
2762 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
2763}
2764
2765/**
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002766 * This test is similar to the test above, but the sequence of injected events is different.
2767 *
2768 * Two windows: a window on the left and a window on the right.
2769 * Mouse is hovered over the left window.
2770 * Next, we tap on the left window, where the cursor was last seen.
2771 *
2772 * After that, we inject one finger down onto the right window, and then a second finger down onto
2773 * the left window.
2774 * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right
2775 * window (first), and then another on the left window (second).
2776 * This test reproduces a crash where there is a mismatch between the downTime and eventTime.
2777 * In the buggy implementation, second finger down on the left window would cause a crash.
2778 */
2779TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) {
2780 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2781 sp<FakeWindowHandle> leftWindow =
2782 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
2783 leftWindow->setFrame(Rect(0, 0, 200, 200));
2784
2785 sp<FakeWindowHandle> rightWindow =
2786 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
2787 rightWindow->setFrame(Rect(200, 0, 400, 200));
2788
2789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}});
2790
2791 const int32_t mouseDeviceId = 6;
2792 const int32_t touchDeviceId = 4;
2793 // Hover over the left window. Keep the cursor there.
2794 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2795 injectMotionEvent(mDispatcher,
2796 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2797 AINPUT_SOURCE_MOUSE)
2798 .deviceId(mouseDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002799 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002800 .x(50)
2801 .y(50))
2802 .build()));
2803 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2804
2805 // Tap on left window
2806 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2807 injectMotionEvent(mDispatcher,
2808 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2809 AINPUT_SOURCE_TOUCHSCREEN)
2810 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002811 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002812 .x(100)
2813 .y(100))
2814 .build()));
2815
2816 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2817 injectMotionEvent(mDispatcher,
2818 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2819 AINPUT_SOURCE_TOUCHSCREEN)
2820 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002821 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002822 .x(100)
2823 .y(100))
2824 .build()));
2825 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2826 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2827 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2828
2829 // First finger down on right window
2830 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2831 injectMotionEvent(mDispatcher,
2832 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2833 AINPUT_SOURCE_TOUCHSCREEN)
2834 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002835 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002836 .x(300)
2837 .y(100))
2838 .build()));
2839 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2840
2841 // Second finger down on the left window
2842 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2843 injectMotionEvent(mDispatcher,
2844 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
2845 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002846 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002847 .x(300)
2848 .y(100))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002849 .pointer(PointerBuilder(1, ToolType::FINGER)
Siarhei Vishniakou6464e462023-02-06 18:57:59 -08002850 .x(100)
2851 .y(100))
2852 .build()));
2853 leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2854 rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE));
2855
2856 // No more events
2857 leftWindow->assertNoEvents();
2858 rightWindow->assertNoEvents();
2859}
2860
2861/**
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002862 * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs.
2863 * While the touch is down, new hover events from the stylus device should be ignored. After the
2864 * touch is gone, stylus hovering should start working again.
2865 */
2866TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) {
2867 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2868 sp<FakeWindowHandle> window =
2869 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2870 window->setFrame(Rect(0, 0, 200, 200));
2871
2872 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
2873
2874 const int32_t stylusDeviceId = 5;
2875 const int32_t touchDeviceId = 4;
2876 // Start hovering with stylus
2877 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2878 injectMotionEvent(mDispatcher,
2879 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
2880 AINPUT_SOURCE_STYLUS)
2881 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002882 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002883 .x(50)
2884 .y(50))
2885 .build()));
2886 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2887
2888 // Finger down on the window
2889 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2890 injectMotionEvent(mDispatcher,
2891 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
2892 AINPUT_SOURCE_TOUCHSCREEN)
2893 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002894 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002895 .x(100)
2896 .y(100))
2897 .build()));
2898 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
2899 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN));
2900
2901 // Try to continue hovering with stylus. Since we are already down, injection should fail
2902 ASSERT_EQ(InputEventInjectionResult::FAILED,
2903 injectMotionEvent(mDispatcher,
2904 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2905 AINPUT_SOURCE_STYLUS)
2906 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002907 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002908 .x(50)
2909 .y(50))
2910 .build()));
2911 // No event should be sent. This event should be ignored because a pointer from another device
2912 // is already down.
2913
2914 // Lift up the finger
2915 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2916 injectMotionEvent(mDispatcher,
2917 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
2918 AINPUT_SOURCE_TOUCHSCREEN)
2919 .deviceId(touchDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002920 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002921 .x(100)
2922 .y(100))
2923 .build()));
2924 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP));
2925
2926 // Now that the touch is gone, stylus hovering should start working again
2927 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
2928 injectMotionEvent(mDispatcher,
2929 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
2930 AINPUT_SOURCE_STYLUS)
2931 .deviceId(stylusDeviceId)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002932 .pointer(PointerBuilder(0, ToolType::STYLUS)
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08002933 .x(50)
2934 .y(50))
2935 .build()));
2936 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
2937 // No more events
2938 window->assertNoEvents();
2939}
2940
2941/**
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002942 * A spy window above a window with no input channel.
2943 * Start hovering with a stylus device, and then tap with it.
2944 * Ensure spy window receives the entire sequence.
2945 */
2946TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) {
2947 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
2948 sp<FakeWindowHandle> spyWindow =
2949 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
2950 spyWindow->setFrame(Rect(0, 0, 200, 200));
2951 spyWindow->setTrustedOverlay(true);
2952 spyWindow->setSpy(true);
2953 sp<FakeWindowHandle> window =
2954 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
2955 window->setNoInputChannel(true);
2956 window->setFrame(Rect(0, 0, 200, 200));
2957
2958 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
2959
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002960 // Start hovering with stylus
Prabir Pradhan678438e2023-04-13 19:32:51 +00002961 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2962 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2963 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002964 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2965 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002966 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2967 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2968 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002969 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2970
2971 // Stylus touches down
Prabir Pradhan678438e2023-04-13 19:32:51 +00002972 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS)
2973 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2974 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002975 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
2976
2977 // Stylus goes up
Prabir Pradhan678438e2023-04-13 19:32:51 +00002978 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS)
2979 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2980 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002981 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP));
2982
2983 // Again hover
Prabir Pradhan678438e2023-04-13 19:32:51 +00002984 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
2985 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2986 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002987 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
2988 // Stop hovering
Prabir Pradhan678438e2023-04-13 19:32:51 +00002989 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS)
2990 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
2991 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002992 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
2993
2994 // No more events
2995 spyWindow->assertNoEvents();
2996 window->assertNoEvents();
2997}
2998
2999/**
3000 * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream.
3001 * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse
3002 * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active.
3003 * While the mouse is down, new move events from the touch device should be ignored.
3004 */
3005TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) {
3006 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3007 sp<FakeWindowHandle> spyWindow =
3008 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3009 spyWindow->setFrame(Rect(0, 0, 200, 200));
3010 spyWindow->setTrustedOverlay(true);
3011 spyWindow->setSpy(true);
3012 sp<FakeWindowHandle> window =
3013 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3014 window->setFrame(Rect(0, 0, 200, 200));
3015
3016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3017
3018 const int32_t mouseDeviceId = 7;
3019 const int32_t touchDeviceId = 4;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003020
3021 // Hover a bit with mouse first
Prabir Pradhan678438e2023-04-13 19:32:51 +00003022 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3023 .deviceId(mouseDeviceId)
3024 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3025 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003026 spyWindow->consumeMotionEvent(
3027 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3028 window->consumeMotionEvent(
3029 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3030
3031 // Start touching
Prabir Pradhan678438e2023-04-13 19:32:51 +00003032 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3033 .deviceId(touchDeviceId)
3034 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3035 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003036 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3037 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3038 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3039 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3040
Prabir Pradhan678438e2023-04-13 19:32:51 +00003041 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3042 .deviceId(touchDeviceId)
3043 .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55))
3044 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003045 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3046 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3047
3048 // Pilfer the stream
3049 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3050 window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL));
3051
Prabir Pradhan678438e2023-04-13 19:32:51 +00003052 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3053 .deviceId(touchDeviceId)
3054 .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60))
3055 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003056 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3057
3058 // Mouse down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003059 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3060 .deviceId(mouseDeviceId)
3061 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3062 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3063 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003064
3065 spyWindow->consumeMotionEvent(
3066 AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId)));
3067 spyWindow->consumeMotionEvent(
3068 AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3069 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3070
Prabir Pradhan678438e2023-04-13 19:32:51 +00003071 mDispatcher->notifyMotion(
3072 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3073 .deviceId(mouseDeviceId)
3074 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3075 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3076 .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100))
3077 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003078 spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3079 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3080
3081 // Mouse move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003082 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
3083 .deviceId(mouseDeviceId)
3084 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3085 .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110))
3086 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003087 spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3088 window->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
3089
3090 // Touch move!
Prabir Pradhan678438e2023-04-13 19:32:51 +00003091 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3092 .deviceId(touchDeviceId)
3093 .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65))
3094 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003095
3096 // No more events
3097 spyWindow->assertNoEvents();
3098 window->assertNoEvents();
3099}
3100
3101/**
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003102 * On the display, have a single window, and also an area where there's no window.
3103 * First pointer touches the "no window" area of the screen. Second pointer touches the window.
3104 * Make sure that the window receives the second pointer, and first pointer is simply ignored.
3105 */
3106TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) {
3107 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3108 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003109 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003110
3111 mDispatcher->setInputWindows({{DISPLAY_ID, {window}}});
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003112
3113 // Touch down on the empty space
Prabir Pradhan678438e2023-04-13 19:32:51 +00003114 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003115
3116 mDispatcher->waitForIdle();
3117 window->assertNoEvents();
3118
3119 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003120 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003121 mDispatcher->waitForIdle();
3122 window->consumeMotionDown();
3123}
3124
3125/**
3126 * Same test as above, but instead of touching the empty space, the first touch goes to
3127 * non-touchable window.
3128 */
3129TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) {
3130 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3131 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003132 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003133 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3134 window1->setTouchable(false);
3135 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003136 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003137 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3138
3139 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3140
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003141 // Touch down on the non-touchable window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003142 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003143
3144 mDispatcher->waitForIdle();
3145 window1->assertNoEvents();
3146 window2->assertNoEvents();
3147
3148 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003149 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08003150 mDispatcher->waitForIdle();
3151 window2->consumeMotionDown();
3152}
3153
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003154/**
3155 * When splitting touch events the downTime should be adjusted such that the downTime corresponds
3156 * to the event time of the first ACTION_DOWN sent to the particular window.
3157 */
3158TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
3159 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3160 sp<FakeWindowHandle> window1 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003161 sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003162 window1->setTouchableRegion(Region{{0, 0, 100, 100}});
3163 sp<FakeWindowHandle> window2 =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003164 sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003165 window2->setTouchableRegion(Region{{100, 0, 200, 100}});
3166
3167 mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}});
3168
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003169 // Touch down on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003170 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003171
3172 mDispatcher->waitForIdle();
3173 InputEvent* inputEvent1 = window1->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003174 ASSERT_NE(inputEvent1, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003175 window2->assertNoEvents();
3176 MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
3177 nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
3178 ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime());
3179
3180 // Now touch down on the window with another pointer
Prabir Pradhan678438e2023-04-13 19:32:51 +00003181 mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003182 mDispatcher->waitForIdle();
3183 InputEvent* inputEvent2 = window2->consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003184 ASSERT_NE(inputEvent2, nullptr);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003185 MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
3186 nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
3187 ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
3188 ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime());
3189
3190 // Now move the pointer on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003191 mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003192 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003193 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003194
3195 // Now add new touch down on the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003196 mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003197 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003198 window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003199
3200 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
3201 window1->consumeMotionMove();
3202 window1->assertNoEvents();
3203
3204 // Now move the pointer on the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003205 mDispatcher->notifyMotion(
3206 generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003207 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003208 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003209
Prabir Pradhan678438e2023-04-13 19:32:51 +00003210 mDispatcher->notifyMotion(
3211 generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003212 mDispatcher->waitForIdle();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08003213 window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003214}
3215
Garfield Tandf26e862020-07-01 20:18:19 -07003216TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003217 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003218 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003219 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003220 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003221 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003222 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003223 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003224
3225 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3226
3227 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
3228
3229 // Start cursor position in right window so that we can move the cursor to left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003230 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003231 injectMotionEvent(mDispatcher,
3232 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3233 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003234 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003235 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003236 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003237
3238 // Move cursor into left window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003239 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003240 injectMotionEvent(mDispatcher,
3241 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3242 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003243 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003244 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003245 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3246 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003247
3248 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003249 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003250 injectMotionEvent(mDispatcher,
3251 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3252 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003253 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003254 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003255 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3256 windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003257
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003258 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003259 injectMotionEvent(mDispatcher,
3260 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3261 AINPUT_SOURCE_MOUSE)
3262 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3263 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003264 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003265 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003266 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003267
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003268 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003269 injectMotionEvent(mDispatcher,
3270 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3271 AINPUT_SOURCE_MOUSE)
3272 .buttonState(0)
3273 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003274 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003275 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003276 windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003277
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003278 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003279 injectMotionEvent(mDispatcher,
3280 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3281 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003282 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003283 .build()));
3284 windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3285
3286 // Move mouse cursor back to right window
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003287 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003288 injectMotionEvent(mDispatcher,
3289 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3290 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003291 .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003292 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003293 windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003294
3295 // No more events
3296 windowLeft->assertNoEvents();
3297 windowRight->assertNoEvents();
3298}
3299
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003300/**
3301 * Put two fingers down (and don't release them) and click the mouse button.
3302 * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the
3303 * currently active gesture should be canceled, and the new one should proceed.
3304 */
3305TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) {
3306 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3307 sp<FakeWindowHandle> window =
3308 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3309 window->setFrame(Rect(0, 0, 600, 800));
3310
3311 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3312
3313 const int32_t touchDeviceId = 4;
3314 const int32_t mouseDeviceId = 6;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003315
3316 // Two pointers down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003317 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3318 .deviceId(touchDeviceId)
3319 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3320 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003321
Prabir Pradhan678438e2023-04-13 19:32:51 +00003322 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3323 .deviceId(touchDeviceId)
3324 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
3325 .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120))
3326 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003327 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
3328 window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
3329
3330 // Inject a series of mouse events for a mouse click
Prabir Pradhan678438e2023-04-13 19:32:51 +00003331 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3332 .deviceId(mouseDeviceId)
3333 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3334 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3335 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003336 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId),
3337 WithPointerCount(2u)));
3338 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId)));
3339
Prabir Pradhan678438e2023-04-13 19:32:51 +00003340 mDispatcher->notifyMotion(
3341 MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE)
3342 .deviceId(mouseDeviceId)
3343 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3344 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
3345 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
3346 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003347 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
3348
3349 // Try to send more touch events while the mouse is down. Since it's a continuation of an
3350 // already canceled gesture, it should be ignored.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003351 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
3352 .deviceId(touchDeviceId)
3353 .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101))
3354 .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121))
3355 .build());
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003356 window->assertNoEvents();
3357}
3358
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003359TEST_F(InputDispatcherTest, HoverWithSpyWindows) {
3360 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3361
3362 sp<FakeWindowHandle> spyWindow =
3363 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3364 spyWindow->setFrame(Rect(0, 0, 600, 800));
3365 spyWindow->setTrustedOverlay(true);
3366 spyWindow->setSpy(true);
3367 sp<FakeWindowHandle> window =
3368 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3369 window->setFrame(Rect(0, 0, 600, 800));
3370
3371 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3372 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3373
3374 // Send mouse cursor to the window
3375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3376 injectMotionEvent(mDispatcher,
3377 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3378 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003379 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003380 .x(100)
3381 .y(100))
3382 .build()));
3383
3384 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3385 WithSource(AINPUT_SOURCE_MOUSE)));
3386 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3387 WithSource(AINPUT_SOURCE_MOUSE)));
3388
3389 window->assertNoEvents();
3390 spyWindow->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003391}
3392
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003393TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) {
3394 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3395
3396 sp<FakeWindowHandle> spyWindow =
3397 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
3398 spyWindow->setFrame(Rect(0, 0, 600, 800));
3399 spyWindow->setTrustedOverlay(true);
3400 spyWindow->setSpy(true);
3401 sp<FakeWindowHandle> window =
3402 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3403 window->setFrame(Rect(0, 0, 600, 800));
3404
3405 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3406 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}});
3407
3408 // Send mouse cursor to the window
3409 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3410 injectMotionEvent(mDispatcher,
3411 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3412 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003413 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003414 .x(100)
3415 .y(100))
3416 .build()));
3417
3418 // Move mouse cursor
3419 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3420 injectMotionEvent(mDispatcher,
3421 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3422 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003423 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003424 .x(110)
3425 .y(110))
3426 .build()));
3427
3428 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3429 WithSource(AINPUT_SOURCE_MOUSE)));
3430 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3431 WithSource(AINPUT_SOURCE_MOUSE)));
3432 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3433 WithSource(AINPUT_SOURCE_MOUSE)));
3434 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3435 WithSource(AINPUT_SOURCE_MOUSE)));
3436 // Touch down on the window
3437 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3438 injectMotionEvent(mDispatcher,
3439 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3440 AINPUT_SOURCE_TOUCHSCREEN)
3441 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003442 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003443 .x(200)
3444 .y(200))
3445 .build()));
3446 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3447 WithSource(AINPUT_SOURCE_MOUSE)));
3448 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3449 WithSource(AINPUT_SOURCE_MOUSE)));
3450 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3451 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3452 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3453 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3454
3455 // pilfer the motion, retaining the gesture on the spy window.
3456 EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken()));
3457 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
3458 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3459
3460 // Touch UP on the window
3461 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3462 injectMotionEvent(mDispatcher,
3463 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3464 AINPUT_SOURCE_TOUCHSCREEN)
3465 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003466 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003467 .x(200)
3468 .y(200))
3469 .build()));
3470 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3471 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3472
3473 // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going
3474 // to send a new gesture. It should again go to both windows (spy and the window below), just
3475 // like the first gesture did, before pilfering. The window configuration has not changed.
3476
3477 // One more tap - DOWN
3478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3479 injectMotionEvent(mDispatcher,
3480 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
3481 AINPUT_SOURCE_TOUCHSCREEN)
3482 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003483 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003484 .x(250)
3485 .y(250))
3486 .build()));
3487 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3488 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3489 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3490 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3491
3492 // Touch UP on the window
3493 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3494 injectMotionEvent(mDispatcher,
3495 MotionEventBuilder(AMOTION_EVENT_ACTION_UP,
3496 AINPUT_SOURCE_TOUCHSCREEN)
3497 .deviceId(SECOND_DEVICE_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003498 .pointer(PointerBuilder(0, ToolType::FINGER)
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08003499 .x(250)
3500 .y(250))
3501 .build()));
3502 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3503 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3504 spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3505 WithSource(AINPUT_SOURCE_TOUCHSCREEN)));
3506
3507 window->assertNoEvents();
3508 spyWindow->assertNoEvents();
3509}
3510
Garfield Tandf26e862020-07-01 20:18:19 -07003511// This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected
3512// directly in this test.
3513TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) {
Chris Yea209fde2020-07-22 13:54:51 -07003514 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tandf26e862020-07-01 20:18:19 -07003515 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003516 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Garfield Tandf26e862020-07-01 20:18:19 -07003517 window->setFrame(Rect(0, 0, 1200, 800));
Garfield Tandf26e862020-07-01 20:18:19 -07003518
3519 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3520
3521 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3522
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003523 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003524 injectMotionEvent(mDispatcher,
3525 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3526 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003527 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003528 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003529 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Garfield Tandf26e862020-07-01 20:18:19 -07003530 // Inject a series of mouse events for a mouse click
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003531 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003532 injectMotionEvent(mDispatcher,
3533 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
3534 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003535 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003536 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003537 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3538 window->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
Garfield Tandf26e862020-07-01 20:18:19 -07003539
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003540 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003541 injectMotionEvent(mDispatcher,
3542 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS,
3543 AINPUT_SOURCE_MOUSE)
3544 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
3545 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003546 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003547 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003548 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS));
Garfield Tandf26e862020-07-01 20:18:19 -07003549
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003551 injectMotionEvent(mDispatcher,
3552 MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE,
3553 AINPUT_SOURCE_MOUSE)
3554 .buttonState(0)
3555 .actionButton(AMOTION_EVENT_BUTTON_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003556 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003557 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003558 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE));
Garfield Tandf26e862020-07-01 20:18:19 -07003559
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003560 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tandf26e862020-07-01 20:18:19 -07003561 injectMotionEvent(mDispatcher,
3562 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
3563 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003564 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003565 .build()));
3566 window->consumeMotionUp(ADISPLAY_ID_DEFAULT);
3567
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07003568 // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the
3569 // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail.
3570 ASSERT_EQ(InputEventInjectionResult::FAILED,
Garfield Tandf26e862020-07-01 20:18:19 -07003571 injectMotionEvent(mDispatcher,
3572 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT,
3573 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003574 .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400))
Garfield Tandf26e862020-07-01 20:18:19 -07003575 .build()));
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08003576 window->assertNoEvents();
Garfield Tandf26e862020-07-01 20:18:19 -07003577}
3578
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003579/**
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003580 * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event
3581 * is generated.
3582 */
3583TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) {
3584 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3585 sp<FakeWindowHandle> window =
3586 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3587 window->setFrame(Rect(0, 0, 1200, 800));
3588
3589 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3590
3591 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3592
3593 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3594 injectMotionEvent(mDispatcher,
3595 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER,
3596 AINPUT_SOURCE_MOUSE)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003597 .pointer(PointerBuilder(0, ToolType::MOUSE)
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003598 .x(300)
3599 .y(400))
3600 .build()));
3601 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
3602
3603 // Remove the window, but keep the channel.
3604 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
3605 window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT));
3606}
3607
3608/**
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003609 * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT.
3610 */
3611TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) {
3612 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3613 sp<FakeWindowHandle> window =
3614 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3615 window->setFrame(Rect(0, 0, 100, 100));
3616
3617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3618
3619 const int32_t mouseDeviceId = 7;
3620 const int32_t touchDeviceId = 4;
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003621
3622 // Start hovering with the mouse
Prabir Pradhan678438e2023-04-13 19:32:51 +00003623 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE)
3624 .deviceId(mouseDeviceId)
3625 .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10))
3626 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003627 window->consumeMotionEvent(
3628 AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId)));
3629
3630 // Touch goes down
Prabir Pradhan678438e2023-04-13 19:32:51 +00003631 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3632 .deviceId(touchDeviceId)
3633 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
3634 .build());
Siarhei Vishniakou4e1ffa52023-02-21 11:50:34 -08003635
3636 window->consumeMotionEvent(
3637 AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId)));
3638 window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId)));
3639}
3640
3641/**
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003642 * Inject a mouse hover event followed by a tap from touchscreen.
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003643 * The tap causes a HOVER_EXIT event to be generated because the current event
3644 * stream's source has been switched.
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003645 */
3646TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) {
3647 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3648 sp<FakeWindowHandle> window =
3649 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
3650 window->setFrame(Rect(0, 0, 100, 100));
3651
3652 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3653
3654 // Inject a hover_move from mouse.
3655 NotifyMotionArgs motionArgs =
3656 generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE,
3657 ADISPLAY_ID_DEFAULT, {{50, 50}});
3658 motionArgs.xCursorPosition = 50;
3659 motionArgs.yCursorPosition = 50;
Prabir Pradhan678438e2023-04-13 19:32:51 +00003660 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003661 ASSERT_NO_FATAL_FAILURE(
3662 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3663 WithSource(AINPUT_SOURCE_MOUSE))));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003664
3665 // Tap on the window
Prabir Pradhan678438e2023-04-13 19:32:51 +00003666 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3667 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3668 {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003669 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00003670 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3671 WithSource(AINPUT_SOURCE_MOUSE))));
3672
3673 ASSERT_NO_FATAL_FAILURE(
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003674 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3675 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3676
Prabir Pradhan678438e2023-04-13 19:32:51 +00003677 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
3678 ADISPLAY_ID_DEFAULT, {{10, 10}}));
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08003679 ASSERT_NO_FATAL_FAILURE(
3680 window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3681 WithSource(AINPUT_SOURCE_TOUCHSCREEN))));
3682}
3683
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003684TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) {
3685 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3686 sp<FakeWindowHandle> windowDefaultDisplay =
3687 sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay",
3688 ADISPLAY_ID_DEFAULT);
3689 windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800));
3690 sp<FakeWindowHandle> windowSecondDisplay =
3691 sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay",
3692 SECOND_DISPLAY_ID);
3693 windowSecondDisplay->setFrame(Rect(0, 0, 600, 800));
3694
3695 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3696 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3697
3698 // Set cursor position in window in default display and check that hover enter and move
3699 // events are generated.
3700 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3701 injectMotionEvent(mDispatcher,
3702 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3703 AINPUT_SOURCE_MOUSE)
3704 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003705 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003706 .x(300)
3707 .y(600))
3708 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003709 windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003710
3711 // Remove all windows in secondary display and check that no event happens on window in
3712 // primary display.
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003713 mDispatcher->setInputWindows(
3714 {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}});
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003715 windowDefaultDisplay->assertNoEvents();
3716
3717 // Move cursor position in window in default display and check that only hover move
3718 // event is generated and not hover enter event.
3719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}},
3720 {SECOND_DISPLAY_ID, {windowSecondDisplay}}});
3721 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3722 injectMotionEvent(mDispatcher,
3723 MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE,
3724 AINPUT_SOURCE_MOUSE)
3725 .displayId(ADISPLAY_ID_DEFAULT)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003726 .pointer(PointerBuilder(0, ToolType::MOUSE)
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003727 .x(400)
3728 .y(700))
3729 .build()));
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08003730 windowDefaultDisplay->consumeMotionEvent(
3731 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3732 WithSource(AINPUT_SOURCE_MOUSE)));
Tommy Nordgrendae9dfc2022-10-13 11:25:57 +02003733 windowDefaultDisplay->assertNoEvents();
3734}
3735
Garfield Tan00f511d2019-06-12 16:55:40 -07003736TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) {
Chris Yea209fde2020-07-22 13:54:51 -07003737 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Garfield Tan00f511d2019-06-12 16:55:40 -07003738
3739 sp<FakeWindowHandle> windowLeft =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003740 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003741 windowLeft->setFrame(Rect(0, 0, 600, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003742 sp<FakeWindowHandle> windowRight =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003743 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003744 windowRight->setFrame(Rect(600, 0, 1200, 800));
Garfield Tan00f511d2019-06-12 16:55:40 -07003745
3746 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
3747
Arthur Hung72d8dc32020-03-28 00:48:39 +00003748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}});
Garfield Tan00f511d2019-06-12 16:55:40 -07003749
3750 // Inject an event with coordinate in the area of right window, with mouse cursor in the area of
3751 // left window. This event should be dispatched to the left window.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08003752 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Garfield Tan00f511d2019-06-12 16:55:40 -07003753 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07003754 ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400}));
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08003755 windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Garfield Tan00f511d2019-06-12 16:55:40 -07003756 windowRight->assertNoEvents();
3757}
3758
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003759TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003760 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003761 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3762 "Fake Window", ADISPLAY_ID_DEFAULT);
Vishnu Nair47074b82020-08-14 11:54:47 -07003763 window->setFocusable(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003764
Arthur Hung72d8dc32020-03-28 00:48:39 +00003765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07003766 setFocusedWindow(window);
3767
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003768 window->consumeFocusEvent(true);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003769
Prabir Pradhan678438e2023-04-13 19:32:51 +00003770 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003771
3772 // Window should receive key down event.
3773 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3774
3775 // When device reset happens, that key stream should be terminated with FLAG_CANCELED
3776 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003777 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003778 window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003779 AKEY_EVENT_FLAG_CANCELED);
3780}
3781
3782TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) {
Chris Yea209fde2020-07-22 13:54:51 -07003783 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003784 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3785 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003786
Arthur Hung72d8dc32020-03-28 00:48:39 +00003787 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003788
Prabir Pradhan678438e2023-04-13 19:32:51 +00003789 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3790 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003791
3792 // Window should receive motion down event.
3793 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3794
3795 // When device reset happens, that motion stream should be terminated with ACTION_CANCEL
3796 // on the app side.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003797 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08003798 window->consumeMotionEvent(
3799 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08003800}
3801
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07003802TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) {
3803 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3804 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3805 "Fake Window", ADISPLAY_ID_DEFAULT);
3806
3807 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3808
3809 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3810 .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10))
3811 .build());
3812
3813 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3814
3815 // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT
3816 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
3817 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT));
3818
3819 // After the device has been reset, a new hovering stream can be sent to the window
3820 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
3821 .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15))
3822 .build());
3823 window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER));
3824}
3825
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003826TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
3827 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003828 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3829 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003830 window->setFocusable(true);
3831
3832 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3833 setFocusedWindow(window);
3834
3835 window->consumeFocusEvent(true);
3836
Prabir Pradhan678438e2023-04-13 19:32:51 +00003837 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003838 const std::chrono::milliseconds interceptKeyTimeout = 50ms;
3839 const nsecs_t injectTime = keyArgs.eventTime;
3840 mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003841 mDispatcher->notifyKey(keyArgs);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003842 // The dispatching time should be always greater than or equal to intercept key timeout.
3843 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3844 ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >=
3845 std::chrono::nanoseconds(interceptKeyTimeout).count());
3846}
3847
3848TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
3849 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003850 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3851 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003852 window->setFocusable(true);
3853
3854 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
3855 setFocusedWindow(window);
3856
3857 window->consumeFocusEvent(true);
3858
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003859 mFakePolicy->setInterceptKeyTimeout(150ms);
Prabir Pradhan678438e2023-04-13 19:32:51 +00003860 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
3861 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08003862
3863 // Window should receive key event immediately when same key up.
3864 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3865 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3866}
3867
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07003868/**
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003869 * Two windows. First is a regular window. Second does not overlap with the first, and has
3870 * WATCH_OUTSIDE_TOUCH.
3871 * Both windows are owned by the same UID.
3872 * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero
3873 * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID.
3874 */
3875TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) {
3876 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3877 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3878 "First Window", ADISPLAY_ID_DEFAULT);
3879 window->setFrame(Rect{0, 0, 100, 100});
3880
3881 sp<FakeWindowHandle> outsideWindow =
3882 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3883 ADISPLAY_ID_DEFAULT);
3884 outsideWindow->setFrame(Rect{100, 100, 200, 200});
3885 outsideWindow->setWatchOutsideTouch(true);
3886 // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped
3887 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}});
3888
3889 // Tap on first window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003890 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3891 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3892 {PointF{50, 50}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003893 window->consumeMotionDown();
3894 // The coordinates of the tap in 'outsideWindow' are relative to its top left corner.
3895 // Therefore, we should offset them by (100, 100) relative to the screen's top left corner.
3896 outsideWindow->consumeMotionEvent(
3897 AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50)));
3898}
3899
3900/**
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003901 * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when
3902 * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one
3903 * ACTION_OUTSIDE event is sent per gesture.
3904 */
3905TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) {
3906 // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH.
3907 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003908 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3909 "First Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003910 window->setWatchOutsideTouch(true);
3911 window->setFrame(Rect{0, 0, 100, 100});
3912 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003913 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
3914 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003915 secondWindow->setFrame(Rect{100, 100, 200, 200});
3916 sp<FakeWindowHandle> thirdWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003917 sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window",
3918 ADISPLAY_ID_DEFAULT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003919 thirdWindow->setFrame(Rect{200, 200, 300, 300});
3920 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}});
3921
3922 // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003923 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
3924 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3925 {PointF{-10, -10}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003926 window->assertNoEvents();
3927 secondWindow->assertNoEvents();
3928
3929 // The second pointer lands inside `secondWindow`, which should receive a DOWN event.
3930 // Now, `window` should get ACTION_OUTSIDE.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003931 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
3932 ADISPLAY_ID_DEFAULT,
3933 {PointF{-10, -10}, PointF{105, 105}}));
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -08003934 const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}};
3935 window->consumeMotionEvent(
3936 AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers)));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003937 secondWindow->consumeMotionDown();
3938 thirdWindow->assertNoEvents();
3939
3940 // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is
3941 // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture.
Prabir Pradhan678438e2023-04-13 19:32:51 +00003942 mDispatcher->notifyMotion(
3943 generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3944 {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}));
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00003945 window->assertNoEvents();
3946 secondWindow->consumeMotionMove();
3947 thirdWindow->consumeMotionDown();
3948}
3949
Prabir Pradhan814fe082022-07-22 20:22:18 +00003950TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) {
3951 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003952 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3953 "Fake Window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003954 window->setFocusable(true);
3955
Patrick Williamsd828f302023-04-28 17:52:08 -05003956 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003957 setFocusedWindow(window);
3958
3959 window->consumeFocusEvent(true);
3960
Prabir Pradhan678438e2023-04-13 19:32:51 +00003961 const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
3962 const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
3963 mDispatcher->notifyKey(keyDown);
3964 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003965
3966 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
3967 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
3968
3969 // All windows are removed from the display. Ensure that we can no longer dispatch to it.
Patrick Williamsd828f302023-04-28 17:52:08 -05003970 mDispatcher->onWindowInfosChanged({{}, {}, 0, 0});
Prabir Pradhan814fe082022-07-22 20:22:18 +00003971
3972 window->consumeFocusEvent(false);
3973
Prabir Pradhan678438e2023-04-13 19:32:51 +00003974 mDispatcher->notifyKey(keyDown);
3975 mDispatcher->notifyKey(keyUp);
Prabir Pradhan814fe082022-07-22 20:22:18 +00003976 window->assertNoEvents();
3977}
3978
Arthur Hung96483742022-11-15 03:30:48 +00003979TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) {
3980 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
3981 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
3982 "Fake Window", ADISPLAY_ID_DEFAULT);
3983 // Ensure window is non-split and have some transform.
3984 window->setPreventSplitting(true);
3985 window->setWindowOffset(20, 40);
Patrick Williamsd828f302023-04-28 17:52:08 -05003986 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Arthur Hung96483742022-11-15 03:30:48 +00003987
3988 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
3989 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
3990 {50, 50}))
3991 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
3992 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
3993
3994 const MotionEvent secondFingerDownEvent =
3995 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
3996 .displayId(ADISPLAY_ID_DEFAULT)
3997 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003998 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
3999 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50))
Arthur Hung96483742022-11-15 03:30:48 +00004000 .build();
4001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4002 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
4003 InputEventInjectionSync::WAIT_FOR_RESULT))
4004 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4005
4006 const MotionEvent* event = window->consumeMotion();
4007 EXPECT_EQ(POINTER_1_DOWN, event->getAction());
4008 EXPECT_EQ(70, event->getX(0)); // 50 + 20
4009 EXPECT_EQ(90, event->getY(0)); // 50 + 40
4010 EXPECT_EQ(-10, event->getX(1)); // -30 + 20
4011 EXPECT_EQ(-10, event->getY(1)); // -50 + 40
4012}
4013
Harry Cuttsb166c002023-05-09 13:06:05 +00004014TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) {
4015 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4016 sp<FakeWindowHandle> window =
4017 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4018 window->setFrame(Rect(0, 0, 400, 400));
4019 sp<FakeWindowHandle> trustedOverlay =
4020 sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay",
4021 ADISPLAY_ID_DEFAULT);
4022 trustedOverlay->setSpy(true);
4023 trustedOverlay->setTrustedOverlay(true);
4024
4025 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}});
4026
4027 // Start a three-finger touchpad swipe
4028 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4029 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4030 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4031 .build());
4032 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4033 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4034 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4035 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4036 .build());
4037 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4038 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4039 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4040 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4041 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4042 .build());
4043
4044 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN));
4045 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN));
4046 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN));
4047
4048 // Move the swipe a bit
4049 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4050 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4051 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4052 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4053 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4054 .build());
4055
4056 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
4057
4058 // End the swipe
4059 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4060 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4061 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4062 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4063 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4064 .build());
4065 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4066 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4067 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4068 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4069 .build());
4070 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4071 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4072 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4073 .build());
4074
4075 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP));
4076 trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP));
4077 trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP));
4078
4079 window->assertNoEvents();
4080}
4081
4082TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) {
4083 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4084 sp<FakeWindowHandle> window =
4085 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4086 window->setFrame(Rect(0, 0, 400, 400));
4087 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4088
4089 // Start a three-finger touchpad swipe
4090 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE)
4091 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4092 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4093 .build());
4094 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE)
4095 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4096 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4097 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4098 .build());
4099 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE)
4100 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100))
4101 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100))
4102 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100))
4103 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4104 .build());
4105
4106 // Move the swipe a bit
4107 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE)
4108 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4109 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4110 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4111 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4112 .build());
4113
4114 // End the swipe
4115 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE)
4116 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4117 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4118 .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105))
4119 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4120 .build());
4121 mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE)
4122 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4123 .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105))
4124 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4125 .build());
4126 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE)
4127 .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105))
4128 .classification(MotionClassification::MULTI_FINGER_SWIPE)
4129 .build());
4130
4131 window->assertNoEvents();
4132}
4133
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +00004134/**
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004135 * Ensure the correct coordinate spaces are used by InputDispatcher.
4136 *
4137 * InputDispatcher works in the display space, so its coordinate system is relative to the display
4138 * panel. Windows get events in the window space, and get raw coordinates in the logical display
4139 * space.
4140 */
4141class InputDispatcherDisplayProjectionTest : public InputDispatcherTest {
4142public:
4143 void SetUp() override {
4144 InputDispatcherTest::SetUp();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004145 removeAllWindowsAndDisplays();
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004146 }
4147
4148 void addDisplayInfo(int displayId, const ui::Transform& transform) {
4149 gui::DisplayInfo info;
4150 info.displayId = displayId;
4151 info.transform = transform;
4152 mDisplayInfos.push_back(std::move(info));
Patrick Williamsd828f302023-04-28 17:52:08 -05004153 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004154 }
4155
4156 void addWindow(const sp<WindowInfoHandle>& windowHandle) {
4157 mWindowInfos.push_back(*windowHandle->getInfo());
Patrick Williamsd828f302023-04-28 17:52:08 -05004158 mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0});
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004159 }
4160
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004161 void removeAllWindowsAndDisplays() {
4162 mDisplayInfos.clear();
4163 mWindowInfos.clear();
4164 }
4165
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004166 // Set up a test scenario where the display has a scaled projection and there are two windows
4167 // on the display.
4168 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() {
4169 // The display has a projection that has a scale factor of 2 and 4 in the x and y directions
4170 // respectively.
4171 ui::Transform displayTransform;
4172 displayTransform.set(2, 0, 0, 4);
4173 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4174
4175 std::shared_ptr<FakeApplicationHandle> application =
4176 std::make_shared<FakeApplicationHandle>();
4177
4178 // Add two windows to the display. Their frames are represented in the display space.
4179 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004180 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4181 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004182 firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform);
4183 addWindow(firstWindow);
4184
4185 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004186 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4187 ADISPLAY_ID_DEFAULT);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004188 secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform);
4189 addWindow(secondWindow);
4190 return {std::move(firstWindow), std::move(secondWindow)};
4191 }
4192
4193private:
4194 std::vector<gui::DisplayInfo> mDisplayInfos;
4195 std::vector<gui::WindowInfo> mWindowInfos;
4196};
4197
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004198TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) {
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004199 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4200 // Send down to the first window. The point is represented in the display space. The point is
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004201 // selected so that if the hit test was performed with the point and the bounds being in
4202 // different coordinate spaces, the event would end up in the incorrect window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004203 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4204 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4205 {PointF{75, 55}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004206
4207 firstWindow->consumeMotionDown();
4208 secondWindow->assertNoEvents();
4209}
4210
4211// Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API,
4212// the event should be treated as being in the logical display space.
4213TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) {
4214 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4215 // Send down to the first window. The point is represented in the logical display space. The
4216 // point is selected so that if the hit test was done in logical display space, then it would
4217 // end up in the incorrect window.
4218 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4219 PointF{75 * 2, 55 * 4});
4220
4221 firstWindow->consumeMotionDown();
4222 secondWindow->assertNoEvents();
4223}
4224
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004225// Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed
4226// event should be treated as being in the logical display space.
4227TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) {
4228 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4229
4230 const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0};
4231 ui::Transform injectedEventTransform;
4232 injectedEventTransform.set(matrix);
4233 const vec2 expectedPoint{75, 55}; // The injected point in the logical display space.
4234 const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint);
4235
4236 MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
4237 .displayId(ADISPLAY_ID_DEFAULT)
4238 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004239 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004240 .x(untransformedPoint.x)
4241 .y(untransformedPoint.y))
4242 .build();
4243 event.transform(matrix);
4244
4245 injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT,
4246 InputEventInjectionSync::WAIT_FOR_RESULT);
4247
4248 firstWindow->consumeMotionDown();
4249 secondWindow->assertNoEvents();
4250}
4251
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004252TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) {
4253 auto [firstWindow, secondWindow] = setupScaledDisplayScenario();
4254
4255 // Send down to the second window.
Prabir Pradhan678438e2023-04-13 19:32:51 +00004256 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4257 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4258 {PointF{150, 220}}));
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004259
4260 firstWindow->assertNoEvents();
4261 const MotionEvent* event = secondWindow->consumeMotion();
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07004262 ASSERT_NE(nullptr, event);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07004263 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction());
4264
4265 // Ensure that the events from the "getRaw" API are in logical display coordinates.
4266 EXPECT_EQ(300, event->getRawX(0));
4267 EXPECT_EQ(880, event->getRawY(0));
4268
4269 // Ensure that the x and y values are in the window's coordinate space.
4270 // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in
4271 // the logical display space. This will be the origin of the window space.
4272 EXPECT_EQ(100, event->getX(0));
4273 EXPECT_EQ(80, event->getY(0));
4274}
4275
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004276/** Ensure consistent behavior of InputDispatcher in all orientations. */
4277class InputDispatcherDisplayOrientationFixture
4278 : public InputDispatcherDisplayProjectionTest,
4279 public ::testing::WithParamInterface<ui::Rotation> {};
4280
4281// This test verifies the touchable region of a window for all rotations of the display by tapping
4282// in different locations on the display, specifically points close to the four corners of a
4283// window.
4284TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) {
4285 constexpr static int32_t displayWidth = 400;
4286 constexpr static int32_t displayHeight = 800;
4287
4288 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4289
4290 const auto rotation = GetParam();
4291
4292 // Set up the display with the specified rotation.
4293 const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270;
4294 const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth;
4295 const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight;
4296 const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation),
4297 logicalDisplayWidth, logicalDisplayHeight);
4298 addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform);
4299
4300 // Create a window with its bounds determined in the logical display.
4301 const Rect frameInLogicalDisplay(100, 100, 200, 300);
4302 const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay);
4303 sp<FakeWindowHandle> window =
4304 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
4305 window->setFrame(frameInDisplay, displayTransform);
4306 addWindow(window);
4307
4308 // The following points in logical display space should be inside the window.
4309 static const std::array<vec2, 4> insidePoints{
4310 {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}};
4311 for (const auto pointInsideWindow : insidePoints) {
4312 const vec2 p = displayTransform.inverse().transform(pointInsideWindow);
4313 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004314 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4315 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4316 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004317 window->consumeMotionDown();
4318
Prabir Pradhan678438e2023-04-13 19:32:51 +00004319 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4320 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4321 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004322 window->consumeMotionUp();
4323 }
4324
4325 // The following points in logical display space should be outside the window.
4326 static const std::array<vec2, 5> outsidePoints{
4327 {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}};
4328 for (const auto pointOutsideWindow : outsidePoints) {
4329 const vec2 p = displayTransform.inverse().transform(pointOutsideWindow);
4330 const PointF pointInDisplaySpace{p.x, p.y};
Prabir Pradhan678438e2023-04-13 19:32:51 +00004331 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4332 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4333 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004334
Prabir Pradhan678438e2023-04-13 19:32:51 +00004335 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP,
4336 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4337 {pointInDisplaySpace}));
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00004338 }
4339 window->assertNoEvents();
4340}
4341
4342// Run the precision tests for all rotations.
4343INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests,
4344 InputDispatcherDisplayOrientationFixture,
4345 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
4346 ui::ROTATION_270),
4347 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
4348 return ftl::enum_string(testParamInfo.param);
4349 });
4350
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004351using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher,
4352 sp<IBinder>, sp<IBinder>)>;
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004353
4354class TransferTouchFixture : public InputDispatcherTest,
4355 public ::testing::WithParamInterface<TransferFunction> {};
4356
4357TEST_P(TransferTouchFixture, TransferTouch_OnePointer) {
Chris Yea209fde2020-07-22 13:54:51 -07004358 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004359
4360 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004361 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004362 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4363 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004364 firstWindow->setDupTouchToWallpaper(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004365 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004366 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4367 ADISPLAY_ID_DEFAULT);
Arthur Hungc539dbb2022-12-08 07:45:36 +00004368 sp<FakeWindowHandle> wallpaper =
4369 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT);
4370 wallpaper->setIsWallpaper(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004371 // Add the windows to the dispatcher
Arthur Hungc539dbb2022-12-08 07:45:36 +00004372 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004373
4374 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004375 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4376 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004377
Svet Ganov5d3bc372020-01-26 23:11:07 -08004378 // Only the first window should get the down event
4379 firstWindow->consumeMotionDown();
4380 secondWindow->assertNoEvents();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004381 wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004382
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004383 // Transfer touch to the second window
4384 TransferFunction f = GetParam();
4385 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4386 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004387 // The first window gets cancel and the second gets down
4388 firstWindow->consumeMotionCancel();
4389 secondWindow->consumeMotionDown();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004390 wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004391
4392 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004393 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4394 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004395 // The first window gets no events and the second gets up
4396 firstWindow->assertNoEvents();
4397 secondWindow->consumeMotionUp();
Arthur Hungc539dbb2022-12-08 07:45:36 +00004398 wallpaper->assertNoEvents();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004399}
4400
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004401/**
4402 * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch
4403 * from. When we have spy windows, there are several windows to choose from: either spy, or the
4404 * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most
4405 * natural to the user.
4406 * In this test, we are sending a pointer to both spy window and first window. We then try to
4407 * transfer touch to the second window. The dispatcher should identify the first window as the
4408 * one that should lose the gesture, and therefore the action should be to move the gesture from
4409 * the first window to the second.
4410 * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test
4411 * the other API, as well.
4412 */
4413TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) {
4414 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4415
4416 // Create a couple of windows + a spy window
4417 sp<FakeWindowHandle> spyWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004418 sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004419 spyWindow->setTrustedOverlay(true);
4420 spyWindow->setSpy(true);
4421 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004422 sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004423 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004424 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004425
4426 // Add the windows to the dispatcher
4427 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}});
4428
4429 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004430 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4431 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004432 // Only the first window and spy should get the down event
4433 spyWindow->consumeMotionDown();
4434 firstWindow->consumeMotionDown();
4435
4436 // Transfer touch to the second window. Non-spy window should be preferred over the spy window
4437 // if f === 'transferTouch'.
4438 TransferFunction f = GetParam();
4439 const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4440 ASSERT_TRUE(success);
4441 // The first window gets cancel and the second gets down
4442 firstWindow->consumeMotionCancel();
4443 secondWindow->consumeMotionDown();
4444
4445 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004446 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4447 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004448 // The first window gets no events and the second+spy get up
4449 firstWindow->assertNoEvents();
4450 spyWindow->consumeMotionUp();
4451 secondWindow->consumeMotionUp();
4452}
4453
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004454TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004455 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004456
4457 PointF touchPoint = {10, 10};
4458
4459 // Create a couple of windows
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004460 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004461 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4462 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004463 firstWindow->setPreventSplitting(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004464 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004465 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4466 ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08004467 secondWindow->setPreventSplitting(true);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004468
4469 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004470 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004471
4472 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004473 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4474 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4475 {touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004476 // Only the first window should get the down event
4477 firstWindow->consumeMotionDown();
4478 secondWindow->assertNoEvents();
4479
4480 // Send pointer down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004481 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4482 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004483 // Only the first window should get the pointer down event
4484 firstWindow->consumeMotionPointerDown(1);
4485 secondWindow->assertNoEvents();
4486
4487 // Transfer touch focus to the second window
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004488 TransferFunction f = GetParam();
4489 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4490 ASSERT_TRUE(success);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004491 // The first window gets cancel and the second gets down and pointer down
4492 firstWindow->consumeMotionCancel();
4493 secondWindow->consumeMotionDown();
4494 secondWindow->consumeMotionPointerDown(1);
4495
4496 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004497 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4498 ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004499 // The first window gets nothing and the second gets pointer up
4500 firstWindow->assertNoEvents();
4501 secondWindow->consumeMotionPointerUp(1);
4502
4503 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004504 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4505 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004506 // The first window gets nothing and the second gets up
4507 firstWindow->assertNoEvents();
4508 secondWindow->consumeMotionUp();
4509}
4510
Arthur Hungc539dbb2022-12-08 07:45:36 +00004511TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) {
4512 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4513
4514 // Create a couple of windows
4515 sp<FakeWindowHandle> firstWindow =
4516 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4517 ADISPLAY_ID_DEFAULT);
4518 firstWindow->setDupTouchToWallpaper(true);
4519 sp<FakeWindowHandle> secondWindow =
4520 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4521 ADISPLAY_ID_DEFAULT);
4522 secondWindow->setDupTouchToWallpaper(true);
4523
4524 sp<FakeWindowHandle> wallpaper1 =
4525 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT);
4526 wallpaper1->setIsWallpaper(true);
4527
4528 sp<FakeWindowHandle> wallpaper2 =
4529 sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT);
4530 wallpaper2->setIsWallpaper(true);
4531 // Add the windows to the dispatcher
4532 mDispatcher->setInputWindows(
4533 {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}});
4534
4535 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004536 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4537 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004538
4539 // Only the first window should get the down event
4540 firstWindow->consumeMotionDown();
4541 secondWindow->assertNoEvents();
4542 wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4543 wallpaper2->assertNoEvents();
4544
4545 // Transfer touch focus to the second window
4546 TransferFunction f = GetParam();
4547 bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken());
4548 ASSERT_TRUE(success);
4549
4550 // The first window gets cancel and the second gets down
4551 firstWindow->consumeMotionCancel();
4552 secondWindow->consumeMotionDown();
4553 wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4554 wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4555
4556 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004557 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4558 ADISPLAY_ID_DEFAULT));
Arthur Hungc539dbb2022-12-08 07:45:36 +00004559 // The first window gets no events and the second gets up
4560 firstWindow->assertNoEvents();
4561 secondWindow->consumeMotionUp();
4562 wallpaper1->assertNoEvents();
4563 wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags);
4564}
4565
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004566// For the cases of single pointer touch and two pointers non-split touch, the api's
4567// 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ
4568// for the case where there are multiple pointers split across several windows.
4569INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture,
4570 ::testing::Values(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004571 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4572 sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004573 return dispatcher->transferTouch(destChannelToken,
4574 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004575 },
Siarhei Vishniakou18050092021-09-01 13:32:49 -07004576 [&](const std::unique_ptr<InputDispatcher>& dispatcher,
4577 sp<IBinder> from, sp<IBinder> to) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004578 return dispatcher->transferTouchFocus(from, to,
Harry Cutts33476232023-01-30 19:57:29 +00004579 /*isDragAndDrop=*/false);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004580 }));
4581
Svet Ganov5d3bc372020-01-26 23:11:07 -08004582TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) {
Chris Yea209fde2020-07-22 13:54:51 -07004583 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Svet Ganov5d3bc372020-01-26 23:11:07 -08004584
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004585 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004586 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4587 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004588 firstWindow->setFrame(Rect(0, 0, 600, 400));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004589
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10004590 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004591 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4592 ADISPLAY_ID_DEFAULT);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004593 secondWindow->setFrame(Rect(0, 400, 600, 800));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004594
4595 // Add the windows to the dispatcher
Arthur Hung72d8dc32020-03-28 00:48:39 +00004596 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
Svet Ganov5d3bc372020-01-26 23:11:07 -08004597
4598 PointF pointInFirst = {300, 200};
4599 PointF pointInSecond = {300, 600};
4600
4601 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004602 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4603 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4604 {pointInFirst}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004605 // Only the first window should get the down event
4606 firstWindow->consumeMotionDown();
4607 secondWindow->assertNoEvents();
4608
4609 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004610 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4611 ADISPLAY_ID_DEFAULT,
4612 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004613 // The first window gets a move and the second a down
4614 firstWindow->consumeMotionMove();
4615 secondWindow->consumeMotionDown();
4616
4617 // Transfer touch focus to the second window
4618 mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken());
4619 // The first window gets cancel and the new gets pointer down (it already saw down)
4620 firstWindow->consumeMotionCancel();
4621 secondWindow->consumeMotionPointerDown(1);
4622
4623 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004624 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4625 ADISPLAY_ID_DEFAULT,
4626 {pointInFirst, pointInSecond}));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004627 // The first window gets nothing and the second gets pointer up
4628 firstWindow->assertNoEvents();
4629 secondWindow->consumeMotionPointerUp(1);
4630
4631 // Send up event to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004632 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4633 ADISPLAY_ID_DEFAULT));
Svet Ganov5d3bc372020-01-26 23:11:07 -08004634 // The first window gets nothing and the second gets up
4635 firstWindow->assertNoEvents();
4636 secondWindow->consumeMotionUp();
4637}
4638
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004639// Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api.
4640// Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving
4641// touch is not supported, so the touch should continue on those windows and the transferred-to
4642// window should get nothing.
4643TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) {
4644 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4645
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004646 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004647 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4648 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004649 firstWindow->setFrame(Rect(0, 0, 600, 400));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004650
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004651 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004652 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4653 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004654 secondWindow->setFrame(Rect(0, 400, 600, 800));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004655
4656 // Add the windows to the dispatcher
4657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4658
4659 PointF pointInFirst = {300, 200};
4660 PointF pointInSecond = {300, 600};
4661
4662 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004663 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4664 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4665 {pointInFirst}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004666 // Only the first window should get the down event
4667 firstWindow->consumeMotionDown();
4668 secondWindow->assertNoEvents();
4669
4670 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004671 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4672 ADISPLAY_ID_DEFAULT,
4673 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004674 // The first window gets a move and the second a down
4675 firstWindow->consumeMotionMove();
4676 secondWindow->consumeMotionDown();
4677
4678 // Transfer touch focus to the second window
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004679 const bool transferred =
4680 mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004681 // The 'transferTouch' call should not succeed, because there are 2 touched windows
4682 ASSERT_FALSE(transferred);
4683 firstWindow->assertNoEvents();
4684 secondWindow->assertNoEvents();
4685
4686 // The rest of the dispatch should proceed as normal
4687 // Send pointer up to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004688 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN,
4689 ADISPLAY_ID_DEFAULT,
4690 {pointInFirst, pointInSecond}));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004691 // The first window gets MOVE and the second gets pointer up
4692 firstWindow->consumeMotionMove();
4693 secondWindow->consumeMotionUp();
4694
4695 // Send up event to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004696 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
4697 ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00004698 // The first window gets nothing and the second gets up
4699 firstWindow->consumeMotionUp();
4700 secondWindow->assertNoEvents();
4701}
4702
Arthur Hungabbb9d82021-09-01 14:52:30 +00004703// This case will create two windows and one mirrored window on the default display and mirror
4704// two windows on the second display. It will test if 'transferTouchFocus' works fine if we put
4705// the windows info of second display before default display.
4706TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) {
4707 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4708 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004709 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004710 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004711 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004712 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004713 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004714
4715 sp<FakeWindowHandle> mirrorWindowInPrimary =
4716 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4717 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004718
4719 sp<FakeWindowHandle> firstWindowInSecondary =
4720 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4721 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004722
4723 sp<FakeWindowHandle> secondWindowInSecondary =
4724 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4725 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004726
4727 // Update window info, let it find window handle of second display first.
4728 mDispatcher->setInputWindows(
4729 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4730 {ADISPLAY_ID_DEFAULT,
4731 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4732
4733 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4734 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4735 {50, 50}))
4736 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4737
4738 // Window should receive motion event.
4739 firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4740
4741 // Transfer touch focus
4742 ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(),
4743 secondWindowInPrimary->getToken()));
4744 // The first window gets cancel.
4745 firstWindowInPrimary->consumeMotionCancel();
4746 secondWindowInPrimary->consumeMotionDown();
4747
4748 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4749 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4750 ADISPLAY_ID_DEFAULT, {150, 50}))
4751 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4752 firstWindowInPrimary->assertNoEvents();
4753 secondWindowInPrimary->consumeMotionMove();
4754
4755 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4756 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4757 {150, 50}))
4758 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4759 firstWindowInPrimary->assertNoEvents();
4760 secondWindowInPrimary->consumeMotionUp();
4761}
4762
4763// Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use
4764// 'transferTouch' api.
4765TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) {
4766 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4767 sp<FakeWindowHandle> firstWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004768 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004769 firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004770 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004771 sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00004772 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004773
4774 sp<FakeWindowHandle> mirrorWindowInPrimary =
4775 firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT);
4776 mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004777
4778 sp<FakeWindowHandle> firstWindowInSecondary =
4779 firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4780 firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004781
4782 sp<FakeWindowHandle> secondWindowInSecondary =
4783 secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID);
4784 secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004785
4786 // Update window info, let it find window handle of second display first.
4787 mDispatcher->setInputWindows(
4788 {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}},
4789 {ADISPLAY_ID_DEFAULT,
4790 {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}});
4791
4792 // Touch on second display.
4793 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4794 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50}))
4795 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4796
4797 // Window should receive motion event.
4798 firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4799
4800 // Transfer touch focus
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07004801 ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID));
Arthur Hungabbb9d82021-09-01 14:52:30 +00004802
4803 // The first window gets cancel.
4804 firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID);
4805 secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID);
4806
4807 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4808 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
4809 SECOND_DISPLAY_ID, {150, 50}))
4810 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4811 firstWindowInPrimary->assertNoEvents();
4812 secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID);
4813
4814 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4815 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50}))
4816 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4817 firstWindowInPrimary->assertNoEvents();
4818 secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID);
4819}
4820
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004821TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004822 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004823 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4824 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004825
Vishnu Nair47074b82020-08-14 11:54:47 -07004826 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00004827 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07004828 setFocusedWindow(window);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004829
4830 window->consumeFocusEvent(true);
4831
Prabir Pradhan678438e2023-04-13 19:32:51 +00004832 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004833
4834 // Window should receive key down event.
4835 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Josep del Riob3981622023-04-18 15:49:45 +00004836
4837 // Should have poked user activity
4838 mFakePolicy->assertUserActivityPoked();
4839}
4840
4841TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) {
4842 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4843 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4844 "Fake Window", ADISPLAY_ID_DEFAULT);
4845
4846 window->setDisableUserActivity(true);
4847 window->setFocusable(true);
4848 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4849 setFocusedWindow(window);
4850
4851 window->consumeFocusEvent(true);
4852
4853 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4854
4855 // Window should receive key down event.
4856 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
4857
4858 // Should have poked user activity
4859 mFakePolicy->assertUserActivityNotPoked();
4860}
4861
4862TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
4863 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4864 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4865 "Fake Window", ADISPLAY_ID_DEFAULT);
4866
4867 window->setFocusable(true);
4868 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4869 setFocusedWindow(window);
4870
4871 window->consumeFocusEvent(true);
4872
4873 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4874 mDispatcher->waitForIdle();
4875
4876 // System key is not passed down
4877 window->assertNoEvents();
4878
4879 // Should have poked user activity
4880 mFakePolicy->assertUserActivityPoked();
4881}
4882
4883TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
4884 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4885 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4886 "Fake Window", ADISPLAY_ID_DEFAULT);
4887
4888 window->setFocusable(true);
4889 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4890 setFocusedWindow(window);
4891
4892 window->consumeFocusEvent(true);
4893
4894 mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4895 mDispatcher->waitForIdle();
4896
4897 // System key is not passed down
4898 window->assertNoEvents();
4899
4900 // Should have poked user activity
4901 mFakePolicy->assertUserActivityPoked();
4902}
4903
4904TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
4905 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4906 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4907 "Fake Window", ADISPLAY_ID_DEFAULT);
4908
4909 window->setDisableUserActivity(true);
4910 window->setFocusable(true);
4911 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4912 setFocusedWindow(window);
4913
4914 window->consumeFocusEvent(true);
4915
4916 mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
4917 mDispatcher->waitForIdle();
4918
4919 // System key is not passed down
4920 window->assertNoEvents();
4921
4922 // Should have poked user activity
4923 mFakePolicy->assertUserActivityPoked();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004924}
4925
4926TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004927 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004928 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4929 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004930
Arthur Hung72d8dc32020-03-28 00:48:39 +00004931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004932
Prabir Pradhan678438e2023-04-13 19:32:51 +00004933 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004934 mDispatcher->waitForIdle();
4935
4936 window->assertNoEvents();
4937}
4938
4939// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4940TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004941 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004942 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4943 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004944
Arthur Hung72d8dc32020-03-28 00:48:39 +00004945 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004946
4947 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004948 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004949 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004950 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4951 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004952
4953 // Window should receive only the motion event
4954 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4955 window->assertNoEvents(); // Key event or focus event will not be received
4956}
4957
arthurhungea3f4fc2020-12-21 23:18:53 +08004958TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4959 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4960
arthurhungea3f4fc2020-12-21 23:18:53 +08004961 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004962 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4963 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004964 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004965
arthurhungea3f4fc2020-12-21 23:18:53 +08004966 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004967 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4968 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004969 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004970
4971 // Add the windows to the dispatcher
4972 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4973
4974 PointF pointInFirst = {300, 200};
4975 PointF pointInSecond = {300, 600};
4976
4977 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004978 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4979 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4980 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004981 // Only the first window should get the down event
4982 firstWindow->consumeMotionDown();
4983 secondWindow->assertNoEvents();
4984
4985 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004986 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4987 ADISPLAY_ID_DEFAULT,
4988 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08004989 // The first window gets a move and the second a down
4990 firstWindow->consumeMotionMove();
4991 secondWindow->consumeMotionDown();
4992
4993 // Send pointer cancel to the second window
4994 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08004995 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08004996 {pointInFirst, pointInSecond});
4997 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00004998 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08004999 // The first window gets move and the second gets cancel.
5000 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5001 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5002
5003 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005004 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5005 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08005006 // The first window gets up and the second gets nothing.
5007 firstWindow->consumeMotionUp();
5008 secondWindow->assertNoEvents();
5009}
5010
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005011TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
5012 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5013
5014 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005015 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5017 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
5018 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
5019 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
5020
Harry Cutts33476232023-01-30 19:57:29 +00005021 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005022 window->assertNoEvents();
5023 mDispatcher->waitForIdle();
5024}
5025
chaviwd1c23182019-12-20 18:44:56 -08005026class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00005027public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005028 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005029 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07005030 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005031 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07005032 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00005033 }
5034
chaviwd1c23182019-12-20 18:44:56 -08005035 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
5036
5037 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005038 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
5039 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08005040 }
5041
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005042 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
5043
5044 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
5045
chaviwd1c23182019-12-20 18:44:56 -08005046 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005047 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08005048 expectedDisplayId, expectedFlags);
5049 }
5050
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005051 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005052 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005053 expectedDisplayId, expectedFlags);
5054 }
5055
chaviwd1c23182019-12-20 18:44:56 -08005056 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005057 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08005058 expectedDisplayId, expectedFlags);
5059 }
5060
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005061 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005062 mInputReceiver->consumeMotionEvent(
5063 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
5064 WithDisplayId(expectedDisplayId),
5065 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005066 }
5067
Arthur Hungfbfa5722021-11-16 02:45:54 +00005068 void consumeMotionPointerDown(int32_t pointerIdx) {
5069 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
5070 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005071 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005072 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00005073 }
5074
Evan Rosky84f07f02021-04-16 10:42:42 -07005075 MotionEvent* consumeMotion() {
5076 InputEvent* event = mInputReceiver->consume();
5077 if (!event) {
5078 ADD_FAILURE() << "No event was produced";
5079 return nullptr;
5080 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005081 if (event->getType() != InputEventType::MOTION) {
5082 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07005083 return nullptr;
5084 }
5085 return static_cast<MotionEvent*>(event);
5086 }
5087
chaviwd1c23182019-12-20 18:44:56 -08005088 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
5089
5090private:
5091 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00005092};
5093
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005094using InputDispatcherMonitorTest = InputDispatcherTest;
5095
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005096/**
5097 * Two entities that receive touch: A window, and a global monitor.
5098 * The touch goes to the window, and then the window disappears.
5099 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
5100 * for the monitor, as well.
5101 * 1. foregroundWindow
5102 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
5103 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005104TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005105 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5106 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005107 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005108
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005109 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005110
5111 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5112 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5113 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5114 {100, 200}))
5115 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5116
5117 // Both the foreground window and the global monitor should receive the touch down
5118 window->consumeMotionDown();
5119 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5120
5121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5122 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5123 ADISPLAY_ID_DEFAULT, {110, 200}))
5124 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5125
5126 window->consumeMotionMove();
5127 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5128
5129 // Now the foreground window goes away
5130 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5131 window->consumeMotionCancel();
5132 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5133
5134 // If more events come in, there will be no more foreground window to send them to. This will
5135 // cause a cancel for the monitor, as well.
5136 ASSERT_EQ(InputEventInjectionResult::FAILED,
5137 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5138 ADISPLAY_ID_DEFAULT, {120, 200}))
5139 << "Injection should fail because the window was removed";
5140 window->assertNoEvents();
5141 // Global monitor now gets the cancel
5142 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5143}
5144
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005145TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005146 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005147 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5148 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005149 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005150
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005151 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005152
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005153 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005154 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005155 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005156 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005157 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005158}
5159
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005160TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5161 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005162
Chris Yea209fde2020-07-22 13:54:51 -07005163 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005164 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5165 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005166 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005167
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005169 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005170 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005171 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005172 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005173
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005174 // Pilfer pointers from the monitor.
5175 // This should not do anything and the window should continue to receive events.
5176 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005177
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005178 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005179 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5180 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005181 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005182
5183 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5184 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005185}
5186
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005187TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005188 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005189 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5190 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005191 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5192 window->setWindowOffset(20, 40);
5193 window->setWindowTransform(0, 1, -1, 0);
5194
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005195 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005196
5197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5198 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5199 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5200 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5201 MotionEvent* event = monitor.consumeMotion();
5202 // Even though window has transform, gesture monitor must not.
5203 ASSERT_EQ(ui::Transform(), event->getTransform());
5204}
5205
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005206TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005207 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005208 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005209
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005210 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005211 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005212 << "Injection should fail if there is a monitor, but no touchable window";
5213 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005214}
5215
chaviw81e2bb92019-12-18 15:03:51 -08005216TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005217 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005218 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5219 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005220
Arthur Hung72d8dc32020-03-28 00:48:39 +00005221 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005222
5223 NotifyMotionArgs motionArgs =
5224 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5225 ADISPLAY_ID_DEFAULT);
5226
Prabir Pradhan678438e2023-04-13 19:32:51 +00005227 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005228 // Window should receive motion down event.
5229 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5230
5231 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005232 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005233 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5234 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5235 motionArgs.pointerCoords[0].getX() - 10);
5236
Prabir Pradhan678438e2023-04-13 19:32:51 +00005237 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005238 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005239 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005240}
5241
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005242/**
5243 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5244 * the device default right away. In the test scenario, we check both the default value,
5245 * and the action of enabling / disabling.
5246 */
5247TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005248 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005249 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5250 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005251 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005252
5253 // Set focused application.
5254 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005255 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005256
5257 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005258 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005259 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005260 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005261
5262 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005263 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005264 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005265 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005266
5267 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005268 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005269 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005270 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005271 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005272 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005273 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005274 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005275
5276 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005277 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005278 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005279 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005280
5281 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005282 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005283 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005284 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005285 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005286 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005287 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005288 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005289
5290 window->assertNoEvents();
5291}
5292
Gang Wange9087892020-01-07 12:17:14 -05005293TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005294 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005295 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5296 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005297
5298 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005299 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005300
Arthur Hung72d8dc32020-03-28 00:48:39 +00005301 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005302 setFocusedWindow(window);
5303
Harry Cutts33476232023-01-30 19:57:29 +00005304 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005305
Prabir Pradhan678438e2023-04-13 19:32:51 +00005306 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5307 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005308
5309 InputEvent* event = window->consume();
5310 ASSERT_NE(event, nullptr);
5311
5312 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5313 ASSERT_NE(verified, nullptr);
5314 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5315
5316 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5317 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5318 ASSERT_EQ(keyArgs.source, verified->source);
5319 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5320
5321 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5322
5323 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005324 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005325 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005326 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5327 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5328 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5329 ASSERT_EQ(0, verifiedKey.repeatCount);
5330}
5331
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005332TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005333 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005334 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5335 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005336
5337 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5338
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005339 ui::Transform transform;
5340 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5341
5342 gui::DisplayInfo displayInfo;
5343 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5344 displayInfo.transform = transform;
5345
Patrick Williamsd828f302023-04-28 17:52:08 -05005346 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005347
Prabir Pradhan678438e2023-04-13 19:32:51 +00005348 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005349 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5350 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005351 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005352
5353 InputEvent* event = window->consume();
5354 ASSERT_NE(event, nullptr);
5355
5356 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5357 ASSERT_NE(verified, nullptr);
5358 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5359
5360 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5361 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5362 EXPECT_EQ(motionArgs.source, verified->source);
5363 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5364
5365 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5366
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005367 const vec2 rawXY =
5368 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5369 motionArgs.pointerCoords[0].getXYValue());
5370 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5371 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005372 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005373 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005374 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005375 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5376 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5377}
5378
chaviw09c8d2d2020-08-24 15:48:26 -07005379/**
5380 * Ensure that separate calls to sign the same data are generating the same key.
5381 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5382 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5383 * tests.
5384 */
5385TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5386 KeyEvent event = getTestKeyEvent();
5387 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5388
5389 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5390 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5391 ASSERT_EQ(hmac1, hmac2);
5392}
5393
5394/**
5395 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5396 */
5397TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5398 KeyEvent event = getTestKeyEvent();
5399 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5400 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5401
5402 verifiedEvent.deviceId += 1;
5403 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5404
5405 verifiedEvent.source += 1;
5406 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5407
5408 verifiedEvent.eventTimeNanos += 1;
5409 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5410
5411 verifiedEvent.displayId += 1;
5412 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5413
5414 verifiedEvent.action += 1;
5415 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5416
5417 verifiedEvent.downTimeNanos += 1;
5418 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5419
5420 verifiedEvent.flags += 1;
5421 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5422
5423 verifiedEvent.keyCode += 1;
5424 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5425
5426 verifiedEvent.scanCode += 1;
5427 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5428
5429 verifiedEvent.metaState += 1;
5430 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5431
5432 verifiedEvent.repeatCount += 1;
5433 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5434}
5435
Vishnu Nair958da932020-08-21 17:12:37 -07005436TEST_F(InputDispatcherTest, SetFocusedWindow) {
5437 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5438 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005439 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005440 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005441 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005442 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5443
5444 // Top window is also focusable but is not granted focus.
5445 windowTop->setFocusable(true);
5446 windowSecond->setFocusable(true);
5447 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5448 setFocusedWindow(windowSecond);
5449
5450 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005451 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5452 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005453
5454 // Focused window should receive event.
5455 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5456 windowTop->assertNoEvents();
5457}
5458
5459TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5460 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5461 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005462 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005463 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5464
5465 window->setFocusable(true);
5466 // Release channel for window is no longer valid.
5467 window->releaseChannel();
5468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5469 setFocusedWindow(window);
5470
5471 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005472 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5473 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005474
5475 // window channel is invalid, so it should not receive any input event.
5476 window->assertNoEvents();
5477}
5478
5479TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5480 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5481 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005482 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005483 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005484 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5485
Vishnu Nair958da932020-08-21 17:12:37 -07005486 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5487 setFocusedWindow(window);
5488
5489 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005490 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5491 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005492
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005493 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005494 window->assertNoEvents();
5495}
5496
5497TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5498 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5499 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005500 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005501 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005502 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005503 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5504
5505 windowTop->setFocusable(true);
5506 windowSecond->setFocusable(true);
5507 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5508 setFocusedWindow(windowTop);
5509 windowTop->consumeFocusEvent(true);
5510
Chavi Weingarten847e8512023-03-29 00:26:09 +00005511 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5512 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005513 windowSecond->consumeFocusEvent(true);
5514 windowTop->consumeFocusEvent(false);
5515
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005516 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5517 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005518
5519 // Focused window should receive event.
5520 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5521}
5522
Chavi Weingarten847e8512023-03-29 00:26:09 +00005523TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005524 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5525 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005526 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005527 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005528 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005529 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5530
5531 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005532 windowSecond->setFocusable(false);
5533 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005534 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005535 setFocusedWindow(windowTop);
5536 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005537
Chavi Weingarten847e8512023-03-29 00:26:09 +00005538 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5539 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005540
5541 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005542 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005543 windowSecond->assertNoEvents();
5544}
5545
5546TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5547 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5548 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005549 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005550 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005551 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5552 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005553 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5554
5555 window->setFocusable(true);
5556 previousFocusedWindow->setFocusable(true);
5557 window->setVisible(false);
5558 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5559 setFocusedWindow(previousFocusedWindow);
5560 previousFocusedWindow->consumeFocusEvent(true);
5561
5562 // Requesting focus on invisible window takes focus from currently focused window.
5563 setFocusedWindow(window);
5564 previousFocusedWindow->consumeFocusEvent(false);
5565
5566 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005567 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005568 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5569 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005570
5571 // Window does not get focus event or key down.
5572 window->assertNoEvents();
5573
5574 // Window becomes visible.
5575 window->setVisible(true);
5576 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5577
5578 // Window receives focus event.
5579 window->consumeFocusEvent(true);
5580 // Focused window receives key down.
5581 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5582}
5583
Vishnu Nair599f1412021-06-21 10:39:58 -07005584TEST_F(InputDispatcherTest, DisplayRemoved) {
5585 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5586 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005587 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005588 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5589
5590 // window is granted focus.
5591 window->setFocusable(true);
5592 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5593 setFocusedWindow(window);
5594 window->consumeFocusEvent(true);
5595
5596 // When a display is removed window loses focus.
5597 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5598 window->consumeFocusEvent(false);
5599}
5600
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005601/**
5602 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5603 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5604 * of the 'slipperyEnterWindow'.
5605 *
5606 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5607 * a way so that the touched location is no longer covered by the top window.
5608 *
5609 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5610 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5611 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5612 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5613 * with ACTION_DOWN).
5614 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5615 * window moved itself away from the touched location and had Flag::SLIPPERY.
5616 *
5617 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5618 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5619 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5620 *
5621 * In this test, we ensure that the event received by the bottom window has
5622 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5623 */
5624TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005625 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5626 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005627
5628 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5629 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5630
5631 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005632 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005633 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005634 // Make sure this one overlaps the bottom window
5635 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5636 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5637 // one. Windows with the same owner are not considered to be occluding each other.
5638 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5639
5640 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005641 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005642 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5643
5644 mDispatcher->setInputWindows(
5645 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5646
5647 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005648 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5649 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5650 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005651 slipperyExitWindow->consumeMotionDown();
5652 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5653 mDispatcher->setInputWindows(
5654 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5655
Prabir Pradhan678438e2023-04-13 19:32:51 +00005656 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5657 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5658 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005659
5660 slipperyExitWindow->consumeMotionCancel();
5661
5662 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5663 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5664}
5665
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005666/**
5667 * Two windows, one on the left and another on the right. The left window is slippery. The right
5668 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5669 * touch moves from the left window into the right window, the gesture should continue to go to the
5670 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5671 * reproduces a crash.
5672 */
5673TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5674 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5675
5676 sp<FakeWindowHandle> leftSlipperyWindow =
5677 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5678 leftSlipperyWindow->setSlippery(true);
5679 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5680
5681 sp<FakeWindowHandle> rightDropTouchesWindow =
5682 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5683 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5684 rightDropTouchesWindow->setDropInput(true);
5685
5686 mDispatcher->setInputWindows(
5687 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5688
5689 // Start touch in the left window
5690 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5691 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5692 .build());
5693 leftSlipperyWindow->consumeMotionDown();
5694
5695 // And move it into the right window
5696 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5697 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5698 .build());
5699
5700 // Since the right window isn't eligible to receive input, touch does not slip.
5701 // The left window continues to receive the gesture.
5702 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5703 rightDropTouchesWindow->assertNoEvents();
5704}
5705
Garfield Tan1c7bc862020-01-28 13:24:04 -08005706class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5707protected:
5708 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5709 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5710
Chris Yea209fde2020-07-22 13:54:51 -07005711 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005712 sp<FakeWindowHandle> mWindow;
5713
5714 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005715 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005716 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005717 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005718 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005719 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5720 ASSERT_EQ(OK, mDispatcher->start());
5721
5722 setUpWindow();
5723 }
5724
5725 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005726 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005727 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005728
Vishnu Nair47074b82020-08-14 11:54:47 -07005729 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005730 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005731 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005732 mWindow->consumeFocusEvent(true);
5733 }
5734
Chris Ye2ad95392020-09-01 13:44:44 -07005735 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005736 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005737 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005738 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005739 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005740
5741 // Window should receive key down event.
5742 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5743 }
5744
5745 void expectKeyRepeatOnce(int32_t repeatCount) {
5746 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5747 InputEvent* repeatEvent = mWindow->consume();
5748 ASSERT_NE(nullptr, repeatEvent);
5749
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005750 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005751
5752 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5753 uint32_t eventAction = repeatKeyEvent->getAction();
5754 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5755 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5756 }
5757
Chris Ye2ad95392020-09-01 13:44:44 -07005758 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005759 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005760 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005761 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005762 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005763
5764 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005765 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005766 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005767 }
5768};
5769
5770TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005771 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005772 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5773 expectKeyRepeatOnce(repeatCount);
5774 }
5775}
5776
5777TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005778 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005779 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5780 expectKeyRepeatOnce(repeatCount);
5781 }
Harry Cutts33476232023-01-30 19:57:29 +00005782 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005783 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005784 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5785 expectKeyRepeatOnce(repeatCount);
5786 }
5787}
5788
5789TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005790 sendAndConsumeKeyDown(/*deviceId=*/1);
5791 expectKeyRepeatOnce(/*repeatCount=*/1);
5792 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005793 mWindow->assertNoEvents();
5794}
5795
5796TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005797 sendAndConsumeKeyDown(/*deviceId=*/1);
5798 expectKeyRepeatOnce(/*repeatCount=*/1);
5799 sendAndConsumeKeyDown(/*deviceId=*/2);
5800 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005801 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005802 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005803 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005804 expectKeyRepeatOnce(/*repeatCount=*/2);
5805 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005806 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005807 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005808 mWindow->assertNoEvents();
5809}
5810
5811TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005812 sendAndConsumeKeyDown(/*deviceId=*/1);
5813 expectKeyRepeatOnce(/*repeatCount=*/1);
5814 sendAndConsumeKeyDown(/*deviceId=*/2);
5815 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005816 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005817 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005818 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005819 mWindow->assertNoEvents();
5820}
5821
liushenxiang42232912021-05-21 20:24:09 +08005822TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5823 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005824 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005825 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005826 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5827 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5828 mWindow->assertNoEvents();
5829}
5830
Garfield Tan1c7bc862020-01-28 13:24:04 -08005831TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005832 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005833 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005834 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5835 InputEvent* repeatEvent = mWindow->consume();
5836 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5837 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5838 IdGenerator::getSource(repeatEvent->getId()));
5839 }
5840}
5841
5842TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005843 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005844 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005845
5846 std::unordered_set<int32_t> idSet;
5847 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5848 InputEvent* repeatEvent = mWindow->consume();
5849 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5850 int32_t id = repeatEvent->getId();
5851 EXPECT_EQ(idSet.end(), idSet.find(id));
5852 idSet.insert(id);
5853 }
5854}
5855
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005856/* Test InputDispatcher for MultiDisplay */
5857class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5858public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005859 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005860 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005861
Chris Yea209fde2020-07-22 13:54:51 -07005862 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005863 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005864 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005865
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005866 // Set focus window for primary display, but focused display would be second one.
5867 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005868 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005870 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005871 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005872
Chris Yea209fde2020-07-22 13:54:51 -07005873 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005874 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005875 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005876 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005877 // Set focus display to second one.
5878 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5879 // Set focus window for second display.
5880 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005881 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005882 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005883 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005884 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005885 }
5886
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005887 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005888 InputDispatcherTest::TearDown();
5889
Chris Yea209fde2020-07-22 13:54:51 -07005890 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005891 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005892 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005893 windowInSecondary.clear();
5894 }
5895
5896protected:
Chris Yea209fde2020-07-22 13:54:51 -07005897 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005898 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005899 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005900 sp<FakeWindowHandle> windowInSecondary;
5901};
5902
5903TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5904 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005905 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5906 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5907 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005908 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005909 windowInSecondary->assertNoEvents();
5910
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005911 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005912 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5913 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5914 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005915 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005916 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005917}
5918
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005919TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005920 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005921 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5922 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005923 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005924 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005925 windowInSecondary->assertNoEvents();
5926
5927 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005928 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005929 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005930 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005931 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005932
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005933 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005934 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005935
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005936 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005937 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005938 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005939
5940 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005941 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005942 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005943 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005944 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005945 windowInSecondary->assertNoEvents();
5946}
5947
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005948// Test per-display input monitors for motion event.
5949TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005950 FakeMonitorReceiver monitorInPrimary =
5951 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5952 FakeMonitorReceiver monitorInSecondary =
5953 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005954
5955 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005956 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5957 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5958 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005959 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005960 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005961 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005962 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005963
5964 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005965 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5966 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5967 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005968 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005969 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005970 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005971 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005972
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005973 // Lift up the touch from the second display
5974 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5975 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5976 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5977 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5978 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5979
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005980 // Test inject a non-pointer motion event.
5981 // If specific a display, it will dispatch to the focused window of particular display,
5982 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5984 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5985 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005986 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005987 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005988 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005989 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005990}
5991
5992// Test per-display input monitors for key event.
5993TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005994 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005995 FakeMonitorReceiver monitorInPrimary =
5996 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5997 FakeMonitorReceiver monitorInSecondary =
5998 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005999
6000 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006001 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6002 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006003 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006004 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006005 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006006 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006007}
6008
Vishnu Nair958da932020-08-21 17:12:37 -07006009TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6010 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006011 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006012 secondWindowInPrimary->setFocusable(true);
6013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
6014 setFocusedWindow(secondWindowInPrimary);
6015 windowInPrimary->consumeFocusEvent(false);
6016 secondWindowInPrimary->consumeFocusEvent(true);
6017
6018 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006019 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6020 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006021 windowInPrimary->assertNoEvents();
6022 windowInSecondary->assertNoEvents();
6023 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6024}
6025
Arthur Hungdfd528e2021-12-08 13:23:04 +00006026TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6027 FakeMonitorReceiver monitorInPrimary =
6028 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6029 FakeMonitorReceiver monitorInSecondary =
6030 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6031
6032 // Test touch down on primary display.
6033 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6034 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6035 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6036 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6037 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6038
6039 // Test touch down on second display.
6040 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6041 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6042 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6043 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6044 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6045
6046 // Trigger cancel touch.
6047 mDispatcher->cancelCurrentTouch();
6048 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6049 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6050 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6051 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6052
6053 // Test inject a move motion event, no window/monitor should receive the event.
6054 ASSERT_EQ(InputEventInjectionResult::FAILED,
6055 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6056 ADISPLAY_ID_DEFAULT, {110, 200}))
6057 << "Inject motion event should return InputEventInjectionResult::FAILED";
6058 windowInPrimary->assertNoEvents();
6059 monitorInPrimary.assertNoEvents();
6060
6061 ASSERT_EQ(InputEventInjectionResult::FAILED,
6062 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6063 SECOND_DISPLAY_ID, {110, 200}))
6064 << "Inject motion event should return InputEventInjectionResult::FAILED";
6065 windowInSecondary->assertNoEvents();
6066 monitorInSecondary.assertNoEvents();
6067}
6068
Jackal Guof9696682018-10-05 12:23:23 +08006069class InputFilterTest : public InputDispatcherTest {
6070protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006071 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6072 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006073 NotifyMotionArgs motionArgs;
6074
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006075 motionArgs =
6076 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006077 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006078 motionArgs =
6079 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006080 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006081 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006082 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006083 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6084 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006085 } else {
6086 mFakePolicy->assertFilterInputEventWasNotCalled();
6087 }
6088 }
6089
6090 void testNotifyKey(bool expectToBeFiltered) {
6091 NotifyKeyArgs keyArgs;
6092
6093 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006094 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006095 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006096 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006097 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006098
6099 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006100 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006101 } else {
6102 mFakePolicy->assertFilterInputEventWasNotCalled();
6103 }
6104 }
6105};
6106
6107// Test InputFilter for MotionEvent
6108TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6109 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6110 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6111 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6112
6113 // Enable InputFilter
6114 mDispatcher->setInputFilterEnabled(true);
6115 // Test touch on both primary and second display, and check if both events are filtered.
6116 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6117 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6118
6119 // Disable InputFilter
6120 mDispatcher->setInputFilterEnabled(false);
6121 // Test touch on both primary and second display, and check if both events aren't filtered.
6122 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6123 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6124}
6125
6126// Test InputFilter for KeyEvent
6127TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6128 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6129 testNotifyKey(/*expectToBeFiltered*/ false);
6130
6131 // Enable InputFilter
6132 mDispatcher->setInputFilterEnabled(true);
6133 // Send a key event, and check if it is filtered.
6134 testNotifyKey(/*expectToBeFiltered*/ true);
6135
6136 // Disable InputFilter
6137 mDispatcher->setInputFilterEnabled(false);
6138 // Send a key event, and check if it isn't filtered.
6139 testNotifyKey(/*expectToBeFiltered*/ false);
6140}
6141
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006142// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6143// logical display coordinate space.
6144TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6145 ui::Transform firstDisplayTransform;
6146 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6147 ui::Transform secondDisplayTransform;
6148 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6149
6150 std::vector<gui::DisplayInfo> displayInfos(2);
6151 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6152 displayInfos[0].transform = firstDisplayTransform;
6153 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6154 displayInfos[1].transform = secondDisplayTransform;
6155
Patrick Williamsd828f302023-04-28 17:52:08 -05006156 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006157
6158 // Enable InputFilter
6159 mDispatcher->setInputFilterEnabled(true);
6160
6161 // Ensure the correct transforms are used for the displays.
6162 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6163 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6164}
6165
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006166class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6167protected:
6168 virtual void SetUp() override {
6169 InputDispatcherTest::SetUp();
6170
6171 /**
6172 * We don't need to enable input filter to test the injected event policy, but we enabled it
6173 * here to make the tests more realistic, since this policy only matters when inputfilter is
6174 * on.
6175 */
6176 mDispatcher->setInputFilterEnabled(true);
6177
6178 std::shared_ptr<InputApplicationHandle> application =
6179 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006180 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6181 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006182
6183 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6184 mWindow->setFocusable(true);
6185 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6186 setFocusedWindow(mWindow);
6187 mWindow->consumeFocusEvent(true);
6188 }
6189
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006190 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6191 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006192 KeyEvent event;
6193
6194 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6195 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6196 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006197 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006198 const int32_t additionalPolicyFlags =
6199 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6200 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006201 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006202 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6203 policyFlags | additionalPolicyFlags));
6204
6205 InputEvent* received = mWindow->consume();
6206 ASSERT_NE(nullptr, received);
6207 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006208 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006209 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6210 ASSERT_EQ(flags, keyEvent.getFlags());
6211 }
6212
6213 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6214 int32_t flags) {
6215 MotionEvent event;
6216 PointerProperties pointerProperties[1];
6217 PointerCoords pointerCoords[1];
6218 pointerProperties[0].clear();
6219 pointerProperties[0].id = 0;
6220 pointerCoords[0].clear();
6221 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6222 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6223
6224 ui::Transform identityTransform;
6225 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6226 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6227 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6228 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6229 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006230 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006231 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006232 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6233
6234 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6235 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006236 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006237 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6238 policyFlags | additionalPolicyFlags));
6239
6240 InputEvent* received = mWindow->consume();
6241 ASSERT_NE(nullptr, received);
6242 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006243 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006244 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6245 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006246 }
6247
6248private:
6249 sp<FakeWindowHandle> mWindow;
6250};
6251
6252TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006253 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6254 // filter. Without it, the event will no different from a regularly injected event, and the
6255 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006256 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6257 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006258}
6259
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006260TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006261 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006262 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006263 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6264}
6265
6266TEST_F(InputFilterInjectionPolicyTest,
6267 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6268 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006269 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006270 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006271}
6272
6273TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006274 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6275 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006276}
6277
chaviwfd6d3512019-03-25 13:23:49 -07006278class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006279 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006280 InputDispatcherTest::SetUp();
6281
Chris Yea209fde2020-07-22 13:54:51 -07006282 std::shared_ptr<FakeApplicationHandle> application =
6283 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006284 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006285 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006286 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006287
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006288 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006289 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006290 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006291
6292 // Set focused application.
6293 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006294 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006295
6296 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006297 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006298 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006299 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006300 }
6301
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006302 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006303 InputDispatcherTest::TearDown();
6304
6305 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006306 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006307 }
6308
6309protected:
6310 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006311 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006312 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006313};
6314
6315// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6316// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6317// the onPointerDownOutsideFocus callback.
6318TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006319 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006320 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6321 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006322 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006323 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006324
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006325 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006326 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6327}
6328
6329// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6330// DOWN on the window that doesn't have focus. Ensure no window received the
6331// onPointerDownOutsideFocus callback.
6332TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006334 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006335 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006336 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006337
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006338 ASSERT_TRUE(mDispatcher->waitForIdle());
6339 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006340}
6341
6342// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6343// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6344TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006345 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6346 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006347 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006348 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006349
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006350 ASSERT_TRUE(mDispatcher->waitForIdle());
6351 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006352}
6353
6354// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6355// DOWN on the window that already has focus. Ensure no window received the
6356// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006357TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006359 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006360 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006361 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006362 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006363
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006364 ASSERT_TRUE(mDispatcher->waitForIdle());
6365 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006366}
6367
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006368// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6369// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6370TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6371 const MotionEvent event =
6372 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6373 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006374 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006375 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6376 .build();
6377 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6378 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6379 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6380
6381 ASSERT_TRUE(mDispatcher->waitForIdle());
6382 mFakePolicy->assertOnPointerDownWasNotCalled();
6383 // Ensure that the unfocused window did not receive any FOCUS events.
6384 mUnfocusedWindow->assertNoEvents();
6385}
6386
chaviwaf87b3e2019-10-01 16:59:28 -07006387// These tests ensures we can send touch events to a single client when there are multiple input
6388// windows that point to the same client token.
6389class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6390 virtual void SetUp() override {
6391 InputDispatcherTest::SetUp();
6392
Chris Yea209fde2020-07-22 13:54:51 -07006393 std::shared_ptr<FakeApplicationHandle> application =
6394 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006395 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6396 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006397 mWindow1->setFrame(Rect(0, 0, 100, 100));
6398
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006399 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6400 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006401 mWindow2->setFrame(Rect(100, 100, 200, 200));
6402
Arthur Hung72d8dc32020-03-28 00:48:39 +00006403 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006404 }
6405
6406protected:
6407 sp<FakeWindowHandle> mWindow1;
6408 sp<FakeWindowHandle> mWindow2;
6409
6410 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006411 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006412 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6413 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006414 }
6415
6416 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6417 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006418 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006419 InputEvent* event = window->consume();
6420
6421 ASSERT_NE(nullptr, event) << name.c_str()
6422 << ": consumer should have returned non-NULL event.";
6423
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006424 ASSERT_EQ(InputEventType::MOTION, event->getType())
6425 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006426
6427 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006428 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006429 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006430
6431 for (size_t i = 0; i < points.size(); i++) {
6432 float expectedX = points[i].x;
6433 float expectedY = points[i].y;
6434
6435 EXPECT_EQ(expectedX, motionEvent.getX(i))
6436 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6437 << ", got " << motionEvent.getX(i);
6438 EXPECT_EQ(expectedY, motionEvent.getY(i))
6439 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6440 << ", got " << motionEvent.getY(i);
6441 }
6442 }
chaviw9eaa22c2020-07-01 16:21:27 -07006443
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006444 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006445 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006446 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6447 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006448
6449 // Always consume from window1 since it's the window that has the InputReceiver
6450 consumeMotionEvent(mWindow1, action, expectedPoints);
6451 }
chaviwaf87b3e2019-10-01 16:59:28 -07006452};
6453
6454TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6455 // Touch Window 1
6456 PointF touchedPoint = {10, 10};
6457 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006458 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006459
6460 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006461 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006462
6463 // Touch Window 2
6464 touchedPoint = {150, 150};
6465 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006466 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006467}
6468
chaviw9eaa22c2020-07-01 16:21:27 -07006469TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6470 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006471 mWindow2->setWindowScale(0.5f, 0.5f);
6472
6473 // Touch Window 1
6474 PointF touchedPoint = {10, 10};
6475 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006476 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006477 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006478 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006479
6480 // Touch Window 2
6481 touchedPoint = {150, 150};
6482 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006483 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6484 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006485
chaviw9eaa22c2020-07-01 16:21:27 -07006486 // Update the transform so rotation is set
6487 mWindow2->setWindowTransform(0, -1, 1, 0);
6488 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6489 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006490}
6491
chaviw9eaa22c2020-07-01 16:21:27 -07006492TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006493 mWindow2->setWindowScale(0.5f, 0.5f);
6494
6495 // Touch Window 1
6496 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6497 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006498 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006499
6500 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006501 touchedPoints.push_back(PointF{150, 150});
6502 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006503 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006504
chaviw9eaa22c2020-07-01 16:21:27 -07006505 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006506 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006507 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006508
chaviw9eaa22c2020-07-01 16:21:27 -07006509 // Update the transform so rotation is set for Window 2
6510 mWindow2->setWindowTransform(0, -1, 1, 0);
6511 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006512 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006513}
6514
chaviw9eaa22c2020-07-01 16:21:27 -07006515TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006516 mWindow2->setWindowScale(0.5f, 0.5f);
6517
6518 // Touch Window 1
6519 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6520 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006521 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006522
6523 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006524 touchedPoints.push_back(PointF{150, 150});
6525 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006526
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006527 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006528
6529 // Move both windows
6530 touchedPoints = {{20, 20}, {175, 175}};
6531 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6532 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6533
chaviw9eaa22c2020-07-01 16:21:27 -07006534 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006535
chaviw9eaa22c2020-07-01 16:21:27 -07006536 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006537 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006538 expectedPoints.pop_back();
6539
6540 // Touch Window 2
6541 mWindow2->setWindowTransform(0, -1, 1, 0);
6542 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006543 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006544
6545 // Move both windows
6546 touchedPoints = {{20, 20}, {175, 175}};
6547 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6548 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6549
6550 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006551}
6552
6553TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6554 mWindow1->setWindowScale(0.5f, 0.5f);
6555
6556 // Touch Window 1
6557 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6558 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006559 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006560
6561 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006562 touchedPoints.push_back(PointF{150, 150});
6563 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006564
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006565 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006566
6567 // Move both windows
6568 touchedPoints = {{20, 20}, {175, 175}};
6569 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6570 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6571
chaviw9eaa22c2020-07-01 16:21:27 -07006572 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006573}
6574
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006575/**
6576 * When one of the windows is slippery, the touch should not slip into the other window with the
6577 * same input channel.
6578 */
6579TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6580 mWindow1->setSlippery(true);
6581 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6582
6583 // Touch down in window 1
6584 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6585 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6586 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6587
6588 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6589 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6590 // getting generated.
6591 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6592 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6593
6594 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6595}
6596
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006597class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6598 virtual void SetUp() override {
6599 InputDispatcherTest::SetUp();
6600
Chris Yea209fde2020-07-22 13:54:51 -07006601 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006602 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006603 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6604 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006605 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006606 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006607 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006608
6609 // Set focused application.
6610 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6611
6612 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006613 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006614 mWindow->consumeFocusEvent(true);
6615 }
6616
6617 virtual void TearDown() override {
6618 InputDispatcherTest::TearDown();
6619 mWindow.clear();
6620 }
6621
6622protected:
Chris Yea209fde2020-07-22 13:54:51 -07006623 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006624 sp<FakeWindowHandle> mWindow;
6625 static constexpr PointF WINDOW_LOCATION = {20, 20};
6626
6627 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006628 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006629 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6630 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006631 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006632 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6633 WINDOW_LOCATION));
6634 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006635
6636 sp<FakeWindowHandle> addSpyWindow() {
6637 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006638 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006639 spy->setTrustedOverlay(true);
6640 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006641 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006642 spy->setDispatchingTimeout(30ms);
6643 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6644 return spy;
6645 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006646};
6647
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006648// Send a tap and respond, which should not cause an ANR.
6649TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6650 tapOnWindow();
6651 mWindow->consumeMotionDown();
6652 mWindow->consumeMotionUp();
6653 ASSERT_TRUE(mDispatcher->waitForIdle());
6654 mFakePolicy->assertNotifyAnrWasNotCalled();
6655}
6656
6657// Send a regular key and respond, which should not cause an ANR.
6658TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006659 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006660 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6661 ASSERT_TRUE(mDispatcher->waitForIdle());
6662 mFakePolicy->assertNotifyAnrWasNotCalled();
6663}
6664
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006665TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6666 mWindow->setFocusable(false);
6667 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6668 mWindow->consumeFocusEvent(false);
6669
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006670 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006671 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6672 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6673 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006674 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006675 // Key will not go to window because we have no focused window.
6676 // The 'no focused window' ANR timer should start instead.
6677
6678 // Now, the focused application goes away.
6679 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6680 // The key should get dropped and there should be no ANR.
6681
6682 ASSERT_TRUE(mDispatcher->waitForIdle());
6683 mFakePolicy->assertNotifyAnrWasNotCalled();
6684}
6685
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006686// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006687// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6688// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006689TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006690 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006691 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6692 WINDOW_LOCATION));
6693
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006694 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6695 ASSERT_TRUE(sequenceNum);
6696 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006697 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006698
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006699 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006700 mWindow->consumeMotionEvent(
6701 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006702 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006703 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006704}
6705
6706// Send a key to the app and have the app not respond right away.
6707TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6708 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006709 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006710 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6711 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006712 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006713 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006714 ASSERT_TRUE(mDispatcher->waitForIdle());
6715}
6716
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006717// We have a focused application, but no focused window
6718TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006719 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6721 mWindow->consumeFocusEvent(false);
6722
6723 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006724 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006725 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6726 WINDOW_LOCATION));
6727 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6728 mDispatcher->waitForIdle();
6729 mFakePolicy->assertNotifyAnrWasNotCalled();
6730
6731 // Once a focused event arrives, we get an ANR for this application
6732 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6733 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006734 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006735 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6736 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006737 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006738 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006739 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006740 ASSERT_TRUE(mDispatcher->waitForIdle());
6741}
6742
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006743/**
6744 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6745 * there will not be an ANR.
6746 */
6747TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6748 mWindow->setFocusable(false);
6749 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6750 mWindow->consumeFocusEvent(false);
6751
6752 KeyEvent event;
6753 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6754 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6755
6756 // Define a valid key down event that is stale (too old).
6757 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6758 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006759 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006760
6761 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6762
6763 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006764 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006765 InputEventInjectionSync::WAIT_FOR_RESULT,
6766 INJECT_EVENT_TIMEOUT, policyFlags);
6767 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6768 << "Injection should fail because the event is stale";
6769
6770 ASSERT_TRUE(mDispatcher->waitForIdle());
6771 mFakePolicy->assertNotifyAnrWasNotCalled();
6772 mWindow->assertNoEvents();
6773}
6774
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006775// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006776// Make sure that we don't notify policy twice about the same ANR.
6777TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006778 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006779 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6780 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006781
6782 // Once a focused event arrives, we get an ANR for this application
6783 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6784 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006785 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006786 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6787 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006788 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006789 const std::chrono::duration appTimeout =
6790 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6791 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006792
Vishnu Naire4df8752022-09-08 09:17:55 -07006793 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006794 // ANR should not be raised again. It is up to policy to do that if it desires.
6795 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006796
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006797 // If we now get a focused window, the ANR should stop, but the policy handles that via
6798 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006799 ASSERT_TRUE(mDispatcher->waitForIdle());
6800}
6801
6802// We have a focused application, but no focused window
6803TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006804 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006805 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6806 mWindow->consumeFocusEvent(false);
6807
6808 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006809 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006810 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006811 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6812 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006813
Vishnu Naire4df8752022-09-08 09:17:55 -07006814 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6815 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006816
6817 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006818 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006819 ASSERT_TRUE(mDispatcher->waitForIdle());
6820 mWindow->assertNoEvents();
6821}
6822
6823/**
6824 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6825 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6826 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6827 * the ANR mechanism should still work.
6828 *
6829 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6830 * DOWN event, while not responding on the second one.
6831 */
6832TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6833 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6834 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6835 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6836 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6837 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006838 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006839
6840 // Now send ACTION_UP, with identical timestamp
6841 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6842 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6843 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6844 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006845 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006846
6847 // We have now sent down and up. Let's consume first event and then ANR on the second.
6848 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6849 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006850 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006851}
6852
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006853// A spy window can receive an ANR
6854TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6855 sp<FakeWindowHandle> spy = addSpyWindow();
6856
6857 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6858 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6859 WINDOW_LOCATION));
6860 mWindow->consumeMotionDown();
6861
6862 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6863 ASSERT_TRUE(sequenceNum);
6864 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006865 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006866
6867 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006868 spy->consumeMotionEvent(
6869 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006870 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006871 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006872}
6873
6874// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006875// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006876TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6877 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006878
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006879 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6880 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006881 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006882 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006883
6884 // Stuck on the ACTION_UP
6885 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006886 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006887
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006888 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006889 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006890 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6891 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006892
6893 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6894 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006895 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006896 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006897 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006898}
6899
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006900// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006901// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006902TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6903 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006904
6905 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006906 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6907 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006908
6909 mWindow->consumeMotionDown();
6910 // Stuck on the ACTION_UP
6911 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006912 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006913
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006914 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006915 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006916 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6917 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006918
6919 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6920 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006921 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006922 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006923 spy->assertNoEvents();
6924}
6925
6926TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6927 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6928
6929 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6930
6931 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6932 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6933 WINDOW_LOCATION));
6934
6935 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6936 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6937 ASSERT_TRUE(consumeSeq);
6938
Prabir Pradhanedd96402022-02-15 01:46:16 -08006939 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006940
6941 monitor.finishEvent(*consumeSeq);
6942 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6943
6944 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006945 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006946}
6947
6948// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6949// process events, you don't get an anr. When the window later becomes unresponsive again, you
6950// get an ANR again.
6951// 1. tap -> block on ACTION_UP -> receive ANR
6952// 2. consume all pending events (= queue becomes healthy again)
6953// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6954TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6955 tapOnWindow();
6956
6957 mWindow->consumeMotionDown();
6958 // Block on ACTION_UP
6959 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006960 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006961 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6962 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006963 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006964 mWindow->assertNoEvents();
6965
6966 tapOnWindow();
6967 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006968 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006969 mWindow->consumeMotionUp();
6970
6971 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006972 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006973 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006974 mWindow->assertNoEvents();
6975}
6976
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006977// If a connection remains unresponsive for a while, make sure policy is only notified once about
6978// it.
6979TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006981 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6982 WINDOW_LOCATION));
6983
6984 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006985 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006986 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006987 // 'notifyConnectionUnresponsive' should only be called once per connection
6988 mFakePolicy->assertNotifyAnrWasNotCalled();
6989 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006990 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006991 mWindow->consumeMotionEvent(
6992 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006993 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006994 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006995 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006996 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006997}
6998
6999/**
7000 * If a window is processing a motion event, and then a key event comes in, the key event should
7001 * not to to the focused window until the motion is processed.
7002 *
7003 * Warning!!!
7004 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7005 * and the injection timeout that we specify when injecting the key.
7006 * We must have the injection timeout (10ms) be smaller than
7007 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7008 *
7009 * If that value changes, this test should also change.
7010 */
7011TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7012 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7013 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7014
7015 tapOnWindow();
7016 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7017 ASSERT_TRUE(downSequenceNum);
7018 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7019 ASSERT_TRUE(upSequenceNum);
7020 // Don't finish the events yet, and send a key
7021 // Injection will "succeed" because we will eventually give up and send the key to the focused
7022 // window even if motions are still being processed. But because the injection timeout is short,
7023 // we will receive INJECTION_TIMED_OUT as the result.
7024
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007025 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007026 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007027 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7028 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007029 // Key will not be sent to the window, yet, because the window is still processing events
7030 // and the key remains pending, waiting for the touch events to be processed
7031 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7032 ASSERT_FALSE(keySequenceNum);
7033
7034 std::this_thread::sleep_for(500ms);
7035 // if we wait long enough though, dispatcher will give up, and still send the key
7036 // to the focused window, even though we have not yet finished the motion event
7037 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7038 mWindow->finishEvent(*downSequenceNum);
7039 mWindow->finishEvent(*upSequenceNum);
7040}
7041
7042/**
7043 * If a window is processing a motion event, and then a key event comes in, the key event should
7044 * not go to the focused window until the motion is processed.
7045 * If then a new motion comes in, then the pending key event should be going to the currently
7046 * focused window right away.
7047 */
7048TEST_F(InputDispatcherSingleWindowAnr,
7049 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7050 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7051 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7052
7053 tapOnWindow();
7054 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7055 ASSERT_TRUE(downSequenceNum);
7056 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7057 ASSERT_TRUE(upSequenceNum);
7058 // Don't finish the events yet, and send a key
7059 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007061 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7062 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007063 // At this point, key is still pending, and should not be sent to the application yet.
7064 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7065 ASSERT_FALSE(keySequenceNum);
7066
7067 // Now tap down again. It should cause the pending key to go to the focused window right away.
7068 tapOnWindow();
7069 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7070 // the other events yet. We can finish events in any order.
7071 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7072 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7073 mWindow->consumeMotionDown();
7074 mWindow->consumeMotionUp();
7075 mWindow->assertNoEvents();
7076}
7077
7078class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7079 virtual void SetUp() override {
7080 InputDispatcherTest::SetUp();
7081
Chris Yea209fde2020-07-22 13:54:51 -07007082 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007083 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007084 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7085 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007086 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007087 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007088 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007089
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007090 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7091 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007092 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007093 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007094
7095 // Set focused application.
7096 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007097 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007098
7099 // Expect one focus window exist in display.
7100 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007101 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007102 mFocusedWindow->consumeFocusEvent(true);
7103 }
7104
7105 virtual void TearDown() override {
7106 InputDispatcherTest::TearDown();
7107
7108 mUnfocusedWindow.clear();
7109 mFocusedWindow.clear();
7110 }
7111
7112protected:
Chris Yea209fde2020-07-22 13:54:51 -07007113 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007114 sp<FakeWindowHandle> mUnfocusedWindow;
7115 sp<FakeWindowHandle> mFocusedWindow;
7116 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7117 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7118 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7119
7120 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7121
7122 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7123
7124private:
7125 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007126 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007127 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7128 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007129 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007130 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7131 location));
7132 }
7133};
7134
7135// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7136// should be ANR'd first.
7137TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007138 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007139 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7140 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007141 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007142 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007143 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007144 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007145 // We consumed all events, so no ANR
7146 ASSERT_TRUE(mDispatcher->waitForIdle());
7147 mFakePolicy->assertNotifyAnrWasNotCalled();
7148
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007149 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007150 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7151 FOCUSED_WINDOW_LOCATION));
7152 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7153 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007154
7155 const std::chrono::duration timeout =
7156 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007157 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007158 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7159 // sequence to make it consistent
7160 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007161 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007162 mFocusedWindow->consumeMotionDown();
7163 // This cancel is generated because the connection was unresponsive
7164 mFocusedWindow->consumeMotionCancel();
7165 mFocusedWindow->assertNoEvents();
7166 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007167 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007168 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7169 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007170 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007171}
7172
7173// If we have 2 windows with identical timeouts that are both unresponsive,
7174// it doesn't matter which order they should have ANR.
7175// But we should receive ANR for both.
7176TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7177 // Set the timeout for unfocused window to match the focused window
7178 mUnfocusedWindow->setDispatchingTimeout(10ms);
7179 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7180
7181 tapOnFocusedWindow();
7182 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007183 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7184 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7185 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007186
7187 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007188 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7189 mFocusedWindow->getToken() == anrConnectionToken2);
7190 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7191 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007192
7193 ASSERT_TRUE(mDispatcher->waitForIdle());
7194 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007195
7196 mFocusedWindow->consumeMotionDown();
7197 mFocusedWindow->consumeMotionUp();
7198 mUnfocusedWindow->consumeMotionOutside();
7199
Prabir Pradhanedd96402022-02-15 01:46:16 -08007200 sp<IBinder> responsiveToken1, responsiveToken2;
7201 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7202 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007203
7204 // Both applications should be marked as responsive, in any order
7205 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7206 mFocusedWindow->getToken() == responsiveToken2);
7207 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7208 mUnfocusedWindow->getToken() == responsiveToken2);
7209 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007210}
7211
7212// If a window is already not responding, the second tap on the same window should be ignored.
7213// We should also log an error to account for the dropped event (not tested here).
7214// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7215TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7216 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007217 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007218 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007219 // Receive the events, but don't respond
7220 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7221 ASSERT_TRUE(downEventSequenceNum);
7222 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7223 ASSERT_TRUE(upEventSequenceNum);
7224 const std::chrono::duration timeout =
7225 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007226 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007227
7228 // Tap once again
7229 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007230 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007231 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7232 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007233 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007234 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7235 FOCUSED_WINDOW_LOCATION));
7236 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7237 // valid touch target
7238 mUnfocusedWindow->assertNoEvents();
7239
7240 // Consume the first tap
7241 mFocusedWindow->finishEvent(*downEventSequenceNum);
7242 mFocusedWindow->finishEvent(*upEventSequenceNum);
7243 ASSERT_TRUE(mDispatcher->waitForIdle());
7244 // The second tap did not go to the focused window
7245 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007246 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007247 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7248 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007249 mFakePolicy->assertNotifyAnrWasNotCalled();
7250}
7251
7252// If you tap outside of all windows, there will not be ANR
7253TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007254 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007255 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7256 LOCATION_OUTSIDE_ALL_WINDOWS));
7257 ASSERT_TRUE(mDispatcher->waitForIdle());
7258 mFakePolicy->assertNotifyAnrWasNotCalled();
7259}
7260
7261// Since the focused window is paused, tapping on it should not produce any events
7262TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7263 mFocusedWindow->setPaused(true);
7264 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7265
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007266 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007267 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7268 FOCUSED_WINDOW_LOCATION));
7269
7270 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7271 ASSERT_TRUE(mDispatcher->waitForIdle());
7272 // Should not ANR because the window is paused, and touches shouldn't go to it
7273 mFakePolicy->assertNotifyAnrWasNotCalled();
7274
7275 mFocusedWindow->assertNoEvents();
7276 mUnfocusedWindow->assertNoEvents();
7277}
7278
7279/**
7280 * If a window is processing a motion event, and then a key event comes in, the key event should
7281 * not to to the focused window until the motion is processed.
7282 * If a different window becomes focused at this time, the key should go to that window instead.
7283 *
7284 * Warning!!!
7285 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7286 * and the injection timeout that we specify when injecting the key.
7287 * We must have the injection timeout (10ms) be smaller than
7288 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7289 *
7290 * If that value changes, this test should also change.
7291 */
7292TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7293 // Set a long ANR timeout to prevent it from triggering
7294 mFocusedWindow->setDispatchingTimeout(2s);
7295 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7296
7297 tapOnUnfocusedWindow();
7298 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7299 ASSERT_TRUE(downSequenceNum);
7300 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7301 ASSERT_TRUE(upSequenceNum);
7302 // Don't finish the events yet, and send a key
7303 // Injection will succeed because we will eventually give up and send the key to the focused
7304 // window even if motions are still being processed.
7305
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007306 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007307 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7308 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007309 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007310 // Key will not be sent to the window, yet, because the window is still processing events
7311 // and the key remains pending, waiting for the touch events to be processed
7312 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7313 ASSERT_FALSE(keySequenceNum);
7314
7315 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007316 mFocusedWindow->setFocusable(false);
7317 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007318 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007319 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007320
7321 // Focus events should precede the key events
7322 mUnfocusedWindow->consumeFocusEvent(true);
7323 mFocusedWindow->consumeFocusEvent(false);
7324
7325 // Finish the tap events, which should unblock dispatcher
7326 mUnfocusedWindow->finishEvent(*downSequenceNum);
7327 mUnfocusedWindow->finishEvent(*upSequenceNum);
7328
7329 // Now that all queues are cleared and no backlog in the connections, the key event
7330 // can finally go to the newly focused "mUnfocusedWindow".
7331 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7332 mFocusedWindow->assertNoEvents();
7333 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007334 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007335}
7336
7337// When the touch stream is split across 2 windows, and one of them does not respond,
7338// then ANR should be raised and the touch should be canceled for the unresponsive window.
7339// The other window should not be affected by that.
7340TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7341 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007342 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7343 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7344 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007345 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007346 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007347
7348 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007349 mDispatcher->notifyMotion(
7350 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7351 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007352
7353 const std::chrono::duration timeout =
7354 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007355 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007356
7357 mUnfocusedWindow->consumeMotionDown();
7358 mFocusedWindow->consumeMotionDown();
7359 // Focused window may or may not receive ACTION_MOVE
7360 // But it should definitely receive ACTION_CANCEL due to the ANR
7361 InputEvent* event;
7362 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7363 ASSERT_TRUE(moveOrCancelSequenceNum);
7364 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7365 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007366 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007367 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7368 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7369 mFocusedWindow->consumeMotionCancel();
7370 } else {
7371 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7372 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007373 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007374 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7375 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007376
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007377 mUnfocusedWindow->assertNoEvents();
7378 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007379 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007380}
7381
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007382/**
7383 * If we have no focused window, and a key comes in, we start the ANR timer.
7384 * The focused application should add a focused window before the timer runs out to prevent ANR.
7385 *
7386 * If the user touches another application during this time, the key should be dropped.
7387 * Next, if a new focused window comes in, without toggling the focused application,
7388 * then no ANR should occur.
7389 *
7390 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7391 * but in some cases the policy may not update the focused application.
7392 */
7393TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7394 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7395 std::make_shared<FakeApplicationHandle>();
7396 focusedApplication->setDispatchingTimeout(60ms);
7397 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7398 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7399 mFocusedWindow->setFocusable(false);
7400
7401 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7402 mFocusedWindow->consumeFocusEvent(false);
7403
7404 // Send a key. The ANR timer should start because there is no focused window.
7405 // 'focusedApplication' will get blamed if this timer completes.
7406 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007407 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007408 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7409 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7410 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007412
7413 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7414 // then the injected touches won't cause the focused event to get dropped.
7415 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7416 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7417 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7418 // For this test, it means that the key would get delivered to the window once it becomes
7419 // focused.
7420 std::this_thread::sleep_for(10ms);
7421
7422 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007423 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7424 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7425 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007426
7427 // We do not consume the motion right away, because that would require dispatcher to first
7428 // process (== drop) the key event, and by that time, ANR will be raised.
7429 // Set the focused window first.
7430 mFocusedWindow->setFocusable(true);
7431 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7432 setFocusedWindow(mFocusedWindow);
7433 mFocusedWindow->consumeFocusEvent(true);
7434 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7435 // to another application. This could be a bug / behaviour in the policy.
7436
7437 mUnfocusedWindow->consumeMotionDown();
7438
7439 ASSERT_TRUE(mDispatcher->waitForIdle());
7440 // Should not ANR because we actually have a focused window. It was just added too slowly.
7441 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7442}
7443
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007444// These tests ensure we cannot send touch events to a window that's positioned behind a window
7445// that has feature NO_INPUT_CHANNEL.
7446// Layout:
7447// Top (closest to user)
7448// mNoInputWindow (above all windows)
7449// mBottomWindow
7450// Bottom (furthest from user)
7451class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7452 virtual void SetUp() override {
7453 InputDispatcherTest::SetUp();
7454
7455 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007456 mNoInputWindow =
7457 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7458 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007459 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007460 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007461 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7462 // It's perfectly valid for this window to not have an associated input channel
7463
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007464 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7465 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007466 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7467
7468 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7469 }
7470
7471protected:
7472 std::shared_ptr<FakeApplicationHandle> mApplication;
7473 sp<FakeWindowHandle> mNoInputWindow;
7474 sp<FakeWindowHandle> mBottomWindow;
7475};
7476
7477TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7478 PointF touchedPoint = {10, 10};
7479
Prabir Pradhan678438e2023-04-13 19:32:51 +00007480 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7481 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7482 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007483
7484 mNoInputWindow->assertNoEvents();
7485 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7486 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7487 // and therefore should prevent mBottomWindow from receiving touches
7488 mBottomWindow->assertNoEvents();
7489}
7490
7491/**
7492 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7493 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7494 */
7495TEST_F(InputDispatcherMultiWindowOcclusionTests,
7496 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007497 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7498 "Window with input channel and NO_INPUT_CHANNEL",
7499 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007500
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007501 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007502 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7503 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7504
7505 PointF touchedPoint = {10, 10};
7506
Prabir Pradhan678438e2023-04-13 19:32:51 +00007507 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7508 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7509 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007510
7511 mNoInputWindow->assertNoEvents();
7512 mBottomWindow->assertNoEvents();
7513}
7514
Vishnu Nair958da932020-08-21 17:12:37 -07007515class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7516protected:
7517 std::shared_ptr<FakeApplicationHandle> mApp;
7518 sp<FakeWindowHandle> mWindow;
7519 sp<FakeWindowHandle> mMirror;
7520
7521 virtual void SetUp() override {
7522 InputDispatcherTest::SetUp();
7523 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007524 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7525 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7526 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007527 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7528 mWindow->setFocusable(true);
7529 mMirror->setFocusable(true);
7530 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7531 }
7532};
7533
7534TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7535 // Request focus on a mirrored window
7536 setFocusedWindow(mMirror);
7537
7538 // window gets focused
7539 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007540 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7541 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007542 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7543}
7544
7545// A focused & mirrored window remains focused only if the window and its mirror are both
7546// focusable.
7547TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7548 setFocusedWindow(mMirror);
7549
7550 // window gets focused
7551 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007552 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7553 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007554 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007555 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7556 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007557 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7558
7559 mMirror->setFocusable(false);
7560 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7561
7562 // window loses focus since one of the windows associated with the token in not focusable
7563 mWindow->consumeFocusEvent(false);
7564
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007565 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7566 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007567 mWindow->assertNoEvents();
7568}
7569
7570// A focused & mirrored window remains focused until the window and its mirror both become
7571// invisible.
7572TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7573 setFocusedWindow(mMirror);
7574
7575 // window gets focused
7576 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007577 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7578 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007579 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007580 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7581 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007582 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7583
7584 mMirror->setVisible(false);
7585 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7586
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007587 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7588 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007589 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7591 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007592 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7593
7594 mWindow->setVisible(false);
7595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7596
7597 // window loses focus only after all windows associated with the token become invisible.
7598 mWindow->consumeFocusEvent(false);
7599
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007600 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7601 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007602 mWindow->assertNoEvents();
7603}
7604
7605// A focused & mirrored window remains focused until both windows are removed.
7606TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7607 setFocusedWindow(mMirror);
7608
7609 // window gets focused
7610 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007611 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7612 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007613 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007614 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7615 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007616 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7617
7618 // single window is removed but the window token remains focused
7619 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7620
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7622 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007623 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007624 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7625 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007626 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7627
7628 // Both windows are removed
7629 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7630 mWindow->consumeFocusEvent(false);
7631
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007632 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7633 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007634 mWindow->assertNoEvents();
7635}
7636
7637// Focus request can be pending until one window becomes visible.
7638TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7639 // Request focus on an invisible mirror.
7640 mWindow->setVisible(false);
7641 mMirror->setVisible(false);
7642 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7643 setFocusedWindow(mMirror);
7644
7645 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007646 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007647 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7648 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007649
7650 mMirror->setVisible(true);
7651 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7652
7653 // window gets focused
7654 mWindow->consumeFocusEvent(true);
7655 // window gets the pending key event
7656 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7657}
Prabir Pradhan99987712020-11-10 18:43:05 -08007658
7659class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7660protected:
7661 std::shared_ptr<FakeApplicationHandle> mApp;
7662 sp<FakeWindowHandle> mWindow;
7663 sp<FakeWindowHandle> mSecondWindow;
7664
7665 void SetUp() override {
7666 InputDispatcherTest::SetUp();
7667 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007668 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007669 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007670 mSecondWindow =
7671 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007672 mSecondWindow->setFocusable(true);
7673
7674 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7675 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7676
7677 setFocusedWindow(mWindow);
7678 mWindow->consumeFocusEvent(true);
7679 }
7680
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007681 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007682 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007683 }
7684
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007685 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7686 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007687 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007688 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7689 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007690 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007691 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007692 }
7693};
7694
7695TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7696 // Ensure that capture cannot be obtained for unfocused windows.
7697 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7698 mFakePolicy->assertSetPointerCaptureNotCalled();
7699 mSecondWindow->assertNoEvents();
7700
7701 // Ensure that capture can be enabled from the focus window.
7702 requestAndVerifyPointerCapture(mWindow, true);
7703
7704 // Ensure that capture cannot be disabled from a window that does not have capture.
7705 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7706 mFakePolicy->assertSetPointerCaptureNotCalled();
7707
7708 // Ensure that capture can be disabled from the window with capture.
7709 requestAndVerifyPointerCapture(mWindow, false);
7710}
7711
7712TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007713 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007714
7715 setFocusedWindow(mSecondWindow);
7716
7717 // Ensure that the capture disabled event was sent first.
7718 mWindow->consumeCaptureEvent(false);
7719 mWindow->consumeFocusEvent(false);
7720 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007721 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007722
7723 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007724 notifyPointerCaptureChanged({});
7725 notifyPointerCaptureChanged(request);
7726 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007727 mWindow->assertNoEvents();
7728 mSecondWindow->assertNoEvents();
7729 mFakePolicy->assertSetPointerCaptureNotCalled();
7730}
7731
7732TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007733 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007734
7735 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007736 notifyPointerCaptureChanged({});
7737 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007738
7739 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007740 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007741 mWindow->consumeCaptureEvent(false);
7742 mWindow->assertNoEvents();
7743}
7744
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007745TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7746 requestAndVerifyPointerCapture(mWindow, true);
7747
7748 // The first window loses focus.
7749 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007750 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007751 mWindow->consumeCaptureEvent(false);
7752
7753 // Request Pointer Capture from the second window before the notification from InputReader
7754 // arrives.
7755 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007756 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007757
7758 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007759 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007760
7761 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007762 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007763
7764 mSecondWindow->consumeFocusEvent(true);
7765 mSecondWindow->consumeCaptureEvent(true);
7766}
7767
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007768TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7769 // App repeatedly enables and disables capture.
7770 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7771 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7772 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7773 mFakePolicy->assertSetPointerCaptureCalled(false);
7774 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7775 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7776
7777 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7778 // first request is now stale, this should do nothing.
7779 notifyPointerCaptureChanged(firstRequest);
7780 mWindow->assertNoEvents();
7781
7782 // InputReader notifies that the second request was enabled.
7783 notifyPointerCaptureChanged(secondRequest);
7784 mWindow->consumeCaptureEvent(true);
7785}
7786
Prabir Pradhan7092e262022-05-03 16:51:09 +00007787TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7788 requestAndVerifyPointerCapture(mWindow, true);
7789
7790 // App toggles pointer capture off and on.
7791 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7792 mFakePolicy->assertSetPointerCaptureCalled(false);
7793
7794 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7795 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7796
7797 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7798 // preceding "disable" request.
7799 notifyPointerCaptureChanged(enableRequest);
7800
7801 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7802 // any notifications.
7803 mWindow->assertNoEvents();
7804}
7805
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007806class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7807protected:
7808 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007809
7810 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7811 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7812
7813 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7814 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7815
7816 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7817 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7818 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7819 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7820 MAXIMUM_OBSCURING_OPACITY);
7821
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007822 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007823 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007824 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007825
7826 sp<FakeWindowHandle> mTouchWindow;
7827
7828 virtual void SetUp() override {
7829 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007830 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007831 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7832 }
7833
7834 virtual void TearDown() override {
7835 InputDispatcherTest::TearDown();
7836 mTouchWindow.clear();
7837 }
7838
chaviw3277faf2021-05-19 16:45:23 -05007839 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7840 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007841 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007842 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007843 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007844 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007845 return window;
7846 }
7847
7848 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7849 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7850 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007851 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007852 // Generate an arbitrary PID based on the UID
7853 window->setOwnerInfo(1777 + (uid % 10000), uid);
7854 return window;
7855 }
7856
7857 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007858 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7859 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7860 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007861 }
7862};
7863
7864TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007865 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007866 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007867 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007868
7869 touch();
7870
7871 mTouchWindow->assertNoEvents();
7872}
7873
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007874TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007875 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7876 const sp<FakeWindowHandle>& w =
7877 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7878 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7879
7880 touch();
7881
7882 mTouchWindow->assertNoEvents();
7883}
7884
7885TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007886 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7887 const sp<FakeWindowHandle>& w =
7888 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7889 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7890
7891 touch();
7892
7893 w->assertNoEvents();
7894}
7895
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007896TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007897 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7898 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007899
7900 touch();
7901
7902 mTouchWindow->consumeAnyMotionDown();
7903}
7904
7905TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007906 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007907 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007908 w->setFrame(Rect(0, 0, 50, 50));
7909 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007910
7911 touch({PointF{100, 100}});
7912
7913 mTouchWindow->consumeAnyMotionDown();
7914}
7915
7916TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007917 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007918 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007919 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7920
7921 touch();
7922
7923 mTouchWindow->consumeAnyMotionDown();
7924}
7925
7926TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7927 const sp<FakeWindowHandle>& w =
7928 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7929 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007930
7931 touch();
7932
7933 mTouchWindow->consumeAnyMotionDown();
7934}
7935
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007936TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7937 const sp<FakeWindowHandle>& w =
7938 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7940
7941 touch();
7942
7943 w->assertNoEvents();
7944}
7945
7946/**
7947 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7948 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7949 * window, the occluding window will still receive ACTION_OUTSIDE event.
7950 */
7951TEST_F(InputDispatcherUntrustedTouchesTest,
7952 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7953 const sp<FakeWindowHandle>& w =
7954 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007955 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007956 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7957
7958 touch();
7959
7960 w->consumeMotionOutside();
7961}
7962
7963TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7964 const sp<FakeWindowHandle>& w =
7965 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007966 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007967 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7968
7969 touch();
7970
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007971 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007972}
7973
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007974TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007975 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007976 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7977 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007978 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7979
7980 touch();
7981
7982 mTouchWindow->consumeAnyMotionDown();
7983}
7984
7985TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7986 const sp<FakeWindowHandle>& w =
7987 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7988 MAXIMUM_OBSCURING_OPACITY);
7989 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007990
7991 touch();
7992
7993 mTouchWindow->consumeAnyMotionDown();
7994}
7995
7996TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007997 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007998 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7999 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008000 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8001
8002 touch();
8003
8004 mTouchWindow->assertNoEvents();
8005}
8006
8007TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8008 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8009 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008010 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8011 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008012 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008013 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8014 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008015 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8016
8017 touch();
8018
8019 mTouchWindow->assertNoEvents();
8020}
8021
8022TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8023 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8024 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008025 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8026 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008027 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008028 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8029 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008030 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8031
8032 touch();
8033
8034 mTouchWindow->consumeAnyMotionDown();
8035}
8036
8037TEST_F(InputDispatcherUntrustedTouchesTest,
8038 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8039 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008040 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8041 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008042 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008043 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8044 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008045 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8046
8047 touch();
8048
8049 mTouchWindow->consumeAnyMotionDown();
8050}
8051
8052TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8053 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008054 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8055 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008056 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008057 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8058 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008059 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008060
8061 touch();
8062
8063 mTouchWindow->assertNoEvents();
8064}
8065
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008066TEST_F(InputDispatcherUntrustedTouchesTest,
8067 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8068 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008069 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8070 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008071 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008072 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8073 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008074 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8075
8076 touch();
8077
8078 mTouchWindow->assertNoEvents();
8079}
8080
8081TEST_F(InputDispatcherUntrustedTouchesTest,
8082 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8083 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008084 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8085 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008086 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008087 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8088 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008089 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8090
8091 touch();
8092
8093 mTouchWindow->consumeAnyMotionDown();
8094}
8095
8096TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8097 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008098 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8099 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008100 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8101
8102 touch();
8103
8104 mTouchWindow->consumeAnyMotionDown();
8105}
8106
8107TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8108 const sp<FakeWindowHandle>& w =
8109 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8110 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8111
8112 touch();
8113
8114 mTouchWindow->consumeAnyMotionDown();
8115}
8116
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008117TEST_F(InputDispatcherUntrustedTouchesTest,
8118 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8119 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8120 const sp<FakeWindowHandle>& w =
8121 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8122 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8123
8124 touch();
8125
8126 mTouchWindow->assertNoEvents();
8127}
8128
8129TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8130 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8131 const sp<FakeWindowHandle>& w =
8132 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8133 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8134
8135 touch();
8136
8137 mTouchWindow->consumeAnyMotionDown();
8138}
8139
8140TEST_F(InputDispatcherUntrustedTouchesTest,
8141 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8142 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8143 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008144 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8145 OPACITY_ABOVE_THRESHOLD);
8146 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8147
8148 touch();
8149
8150 mTouchWindow->consumeAnyMotionDown();
8151}
8152
8153TEST_F(InputDispatcherUntrustedTouchesTest,
8154 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8155 const sp<FakeWindowHandle>& w1 =
8156 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8157 OPACITY_BELOW_THRESHOLD);
8158 const sp<FakeWindowHandle>& w2 =
8159 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8160 OPACITY_BELOW_THRESHOLD);
8161 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8162
8163 touch();
8164
8165 mTouchWindow->assertNoEvents();
8166}
8167
8168/**
8169 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8170 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8171 * (which alone would result in allowing touches) does not affect the blocking behavior.
8172 */
8173TEST_F(InputDispatcherUntrustedTouchesTest,
8174 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8175 const sp<FakeWindowHandle>& wB =
8176 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8177 OPACITY_BELOW_THRESHOLD);
8178 const sp<FakeWindowHandle>& wC =
8179 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8180 OPACITY_BELOW_THRESHOLD);
8181 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8182
8183 touch();
8184
8185 mTouchWindow->assertNoEvents();
8186}
8187
8188/**
8189 * This test is testing that a window from a different UID but with same application token doesn't
8190 * block the touch. Apps can share the application token for close UI collaboration for example.
8191 */
8192TEST_F(InputDispatcherUntrustedTouchesTest,
8193 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8194 const sp<FakeWindowHandle>& w =
8195 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8196 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008197 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8198
8199 touch();
8200
8201 mTouchWindow->consumeAnyMotionDown();
8202}
8203
arthurhungb89ccb02020-12-30 16:19:01 +08008204class InputDispatcherDragTests : public InputDispatcherTest {
8205protected:
8206 std::shared_ptr<FakeApplicationHandle> mApp;
8207 sp<FakeWindowHandle> mWindow;
8208 sp<FakeWindowHandle> mSecondWindow;
8209 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008210 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008211 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8212 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008213
8214 void SetUp() override {
8215 InputDispatcherTest::SetUp();
8216 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008217 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008218 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008219
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008220 mSecondWindow =
8221 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008222 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008223
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008224 mSpyWindow =
8225 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008226 mSpyWindow->setSpy(true);
8227 mSpyWindow->setTrustedOverlay(true);
8228 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8229
arthurhungb89ccb02020-12-30 16:19:01 +08008230 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008231 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008232 }
8233
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008234 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8235 switch (fromSource) {
8236 case AINPUT_SOURCE_TOUCHSCREEN:
8237 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8238 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8239 ADISPLAY_ID_DEFAULT, {50, 50}))
8240 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8241 break;
8242 case AINPUT_SOURCE_STYLUS:
8243 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8244 injectMotionEvent(
8245 mDispatcher,
8246 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8247 AINPUT_SOURCE_STYLUS)
8248 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008249 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008250 .x(50)
8251 .y(50))
8252 .build()));
8253 break;
8254 case AINPUT_SOURCE_MOUSE:
8255 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8256 injectMotionEvent(
8257 mDispatcher,
8258 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8259 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8260 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008261 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008262 .x(50)
8263 .y(50))
8264 .build()));
8265 break;
8266 default:
8267 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8268 }
arthurhungb89ccb02020-12-30 16:19:01 +08008269
8270 // Window should receive motion event.
8271 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008272 // Spy window should also receive motion event
8273 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008274 }
8275
8276 // Start performing drag, we will create a drag window and transfer touch to it.
8277 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8278 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008279 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008280 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008281 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008282 }
arthurhungb89ccb02020-12-30 16:19:01 +08008283
8284 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008285 mDragWindow =
8286 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008287 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008288 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008289 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008290
8291 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008292 bool transferred =
8293 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008294 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008295 if (transferred) {
8296 mWindow->consumeMotionCancel();
8297 mDragWindow->consumeMotionDown();
8298 }
8299 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008300 }
8301};
8302
8303TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008304 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008305
8306 // Move on window.
8307 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8308 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8309 ADISPLAY_ID_DEFAULT, {50, 50}))
8310 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8311 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8312 mWindow->consumeDragEvent(false, 50, 50);
8313 mSecondWindow->assertNoEvents();
8314
8315 // Move to another window.
8316 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8317 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8318 ADISPLAY_ID_DEFAULT, {150, 50}))
8319 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8320 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8321 mWindow->consumeDragEvent(true, 150, 50);
8322 mSecondWindow->consumeDragEvent(false, 50, 50);
8323
8324 // Move back to original window.
8325 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8326 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8327 ADISPLAY_ID_DEFAULT, {50, 50}))
8328 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8329 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8330 mWindow->consumeDragEvent(false, 50, 50);
8331 mSecondWindow->consumeDragEvent(true, -50, 50);
8332
8333 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8334 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8335 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8336 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8337 mWindow->assertNoEvents();
8338 mSecondWindow->assertNoEvents();
8339}
8340
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008341TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008342 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008343
8344 // No cancel event after drag start
8345 mSpyWindow->assertNoEvents();
8346
8347 const MotionEvent secondFingerDownEvent =
8348 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8349 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008350 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8351 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008352 .build();
8353 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8354 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8355 InputEventInjectionSync::WAIT_FOR_RESULT))
8356 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8357
8358 // Receives cancel for first pointer after next pointer down
8359 mSpyWindow->consumeMotionCancel();
8360 mSpyWindow->consumeMotionDown();
8361
8362 mSpyWindow->assertNoEvents();
8363}
8364
arthurhungf452d0b2021-01-06 00:19:52 +08008365TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008366 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008367
8368 // Move on window.
8369 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8370 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8371 ADISPLAY_ID_DEFAULT, {50, 50}))
8372 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8373 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8374 mWindow->consumeDragEvent(false, 50, 50);
8375 mSecondWindow->assertNoEvents();
8376
8377 // Move to another window.
8378 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8379 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8380 ADISPLAY_ID_DEFAULT, {150, 50}))
8381 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8382 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8383 mWindow->consumeDragEvent(true, 150, 50);
8384 mSecondWindow->consumeDragEvent(false, 50, 50);
8385
8386 // drop to another window.
8387 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8388 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8389 {150, 50}))
8390 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8391 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8392 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8393 mWindow->assertNoEvents();
8394 mSecondWindow->assertNoEvents();
8395}
8396
arthurhung6d4bed92021-03-17 11:59:33 +08008397TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008398 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008399
8400 // Move on window and keep button pressed.
8401 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8402 injectMotionEvent(mDispatcher,
8403 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8404 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008405 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008406 .build()))
8407 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8408 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8409 mWindow->consumeDragEvent(false, 50, 50);
8410 mSecondWindow->assertNoEvents();
8411
8412 // Move to another window and release button, expect to drop item.
8413 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8414 injectMotionEvent(mDispatcher,
8415 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8416 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008417 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008418 .build()))
8419 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8420 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8421 mWindow->assertNoEvents();
8422 mSecondWindow->assertNoEvents();
8423 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8424
8425 // nothing to the window.
8426 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8427 injectMotionEvent(mDispatcher,
8428 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8429 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008430 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008431 .build()))
8432 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8433 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8434 mWindow->assertNoEvents();
8435 mSecondWindow->assertNoEvents();
8436}
8437
Arthur Hung54745652022-04-20 07:17:41 +00008438TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008439 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008440
8441 // Set second window invisible.
8442 mSecondWindow->setVisible(false);
8443 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8444
8445 // Move on window.
8446 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8447 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8448 ADISPLAY_ID_DEFAULT, {50, 50}))
8449 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8450 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8451 mWindow->consumeDragEvent(false, 50, 50);
8452 mSecondWindow->assertNoEvents();
8453
8454 // Move to another window.
8455 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8456 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8457 ADISPLAY_ID_DEFAULT, {150, 50}))
8458 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8459 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8460 mWindow->consumeDragEvent(true, 150, 50);
8461 mSecondWindow->assertNoEvents();
8462
8463 // drop to another window.
8464 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8465 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8466 {150, 50}))
8467 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8468 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8469 mFakePolicy->assertDropTargetEquals(nullptr);
8470 mWindow->assertNoEvents();
8471 mSecondWindow->assertNoEvents();
8472}
8473
Arthur Hung54745652022-04-20 07:17:41 +00008474TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008475 // Ensure window could track pointerIds if it didn't support split touch.
8476 mWindow->setPreventSplitting(true);
8477
Arthur Hung54745652022-04-20 07:17:41 +00008478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8479 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8480 {50, 50}))
8481 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8482 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8483
8484 const MotionEvent secondFingerDownEvent =
8485 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8486 .displayId(ADISPLAY_ID_DEFAULT)
8487 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008488 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8489 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008490 .build();
8491 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8492 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8493 InputEventInjectionSync::WAIT_FOR_RESULT))
8494 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008495 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008496
8497 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008498 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008499}
8500
8501TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8502 // First down on second window.
8503 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8504 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8505 {150, 50}))
8506 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8507
8508 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8509
8510 // Second down on first window.
8511 const MotionEvent secondFingerDownEvent =
8512 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8513 .displayId(ADISPLAY_ID_DEFAULT)
8514 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008515 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8516 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008517 .build();
8518 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8519 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8520 InputEventInjectionSync::WAIT_FOR_RESULT))
8521 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8522 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8523
8524 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008525 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008526
8527 // Move on window.
8528 const MotionEvent secondFingerMoveEvent =
8529 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8530 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008531 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8532 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008533 .build();
8534 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8535 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8536 InputEventInjectionSync::WAIT_FOR_RESULT));
8537 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8538 mWindow->consumeDragEvent(false, 50, 50);
8539 mSecondWindow->consumeMotionMove();
8540
8541 // Release the drag pointer should perform drop.
8542 const MotionEvent secondFingerUpEvent =
8543 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8544 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008545 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8546 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008547 .build();
8548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8549 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8550 InputEventInjectionSync::WAIT_FOR_RESULT));
8551 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8552 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8553 mWindow->assertNoEvents();
8554 mSecondWindow->consumeMotionMove();
8555}
8556
Arthur Hung3915c1f2022-05-31 07:17:17 +00008557TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008558 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008559
8560 // Update window of second display.
8561 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008562 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008563 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8564
8565 // Let second display has a touch state.
8566 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8567 injectMotionEvent(mDispatcher,
8568 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8569 AINPUT_SOURCE_TOUCHSCREEN)
8570 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008571 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008572 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008573 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008574 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008575 // Update window again.
8576 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8577
8578 // Move on window.
8579 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8580 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8581 ADISPLAY_ID_DEFAULT, {50, 50}))
8582 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8583 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8584 mWindow->consumeDragEvent(false, 50, 50);
8585 mSecondWindow->assertNoEvents();
8586
8587 // Move to another window.
8588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8589 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8590 ADISPLAY_ID_DEFAULT, {150, 50}))
8591 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8592 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8593 mWindow->consumeDragEvent(true, 150, 50);
8594 mSecondWindow->consumeDragEvent(false, 50, 50);
8595
8596 // drop to another window.
8597 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8598 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8599 {150, 50}))
8600 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8601 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8602 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8603 mWindow->assertNoEvents();
8604 mSecondWindow->assertNoEvents();
8605}
8606
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008607TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8608 startDrag(true, AINPUT_SOURCE_MOUSE);
8609 // Move on window.
8610 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8611 injectMotionEvent(mDispatcher,
8612 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8613 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8614 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008615 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008616 .x(50)
8617 .y(50))
8618 .build()))
8619 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8620 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8621 mWindow->consumeDragEvent(false, 50, 50);
8622 mSecondWindow->assertNoEvents();
8623
8624 // Move to another window.
8625 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8626 injectMotionEvent(mDispatcher,
8627 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8628 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8629 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008630 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008631 .x(150)
8632 .y(50))
8633 .build()))
8634 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8635 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8636 mWindow->consumeDragEvent(true, 150, 50);
8637 mSecondWindow->consumeDragEvent(false, 50, 50);
8638
8639 // drop to another window.
8640 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8641 injectMotionEvent(mDispatcher,
8642 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8643 .buttonState(0)
8644 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008645 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008646 .x(150)
8647 .y(50))
8648 .build()))
8649 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8650 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8651 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8652 mWindow->assertNoEvents();
8653 mSecondWindow->assertNoEvents();
8654}
8655
Vishnu Nair062a8672021-09-03 16:07:44 -07008656class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8657
8658TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8659 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008660 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8661 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008662 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008663 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8664 window->setFocusable(true);
8665 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8666 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008667 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008668
8669 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008670 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008671 window->assertNoEvents();
8672
Prabir Pradhan678438e2023-04-13 19:32:51 +00008673 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8674 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008675 window->assertNoEvents();
8676
8677 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008678 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008679 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8680
Prabir Pradhan678438e2023-04-13 19:32:51 +00008681 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008682 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8683
Prabir Pradhan678438e2023-04-13 19:32:51 +00008684 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8685 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008686 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8687 window->assertNoEvents();
8688}
8689
8690TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8691 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8692 std::make_shared<FakeApplicationHandle>();
8693 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008694 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8695 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008696 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8697 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008698 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008699 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008700 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8701 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008702 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008703 window->setOwnerInfo(222, 222);
8704 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8705 window->setFocusable(true);
8706 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8707 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008708 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008709
8710 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008711 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008712 window->assertNoEvents();
8713
Prabir Pradhan678438e2023-04-13 19:32:51 +00008714 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8715 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008716 window->assertNoEvents();
8717
8718 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008719 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8721
Prabir Pradhan678438e2023-04-13 19:32:51 +00008722 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008723 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8724
Prabir Pradhan678438e2023-04-13 19:32:51 +00008725 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8726 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008727 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8728 window->assertNoEvents();
8729}
8730
8731TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8732 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8733 std::make_shared<FakeApplicationHandle>();
8734 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008735 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8736 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008737 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8738 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008739 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008740 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008741 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8742 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008743 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008744 window->setOwnerInfo(222, 222);
8745 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8746 window->setFocusable(true);
8747 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8748 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008749 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008750
8751 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008752 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008753 window->assertNoEvents();
8754
Prabir Pradhan678438e2023-04-13 19:32:51 +00008755 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8756 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008757 window->assertNoEvents();
8758
8759 // When the window is no longer obscured because it went on top, it should get input
8760 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8761
Prabir Pradhan678438e2023-04-13 19:32:51 +00008762 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008763 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8764
Prabir Pradhan678438e2023-04-13 19:32:51 +00008765 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8766 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008767 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8768 window->assertNoEvents();
8769}
8770
Antonio Kantekf16f2832021-09-28 04:39:20 +00008771class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8772protected:
8773 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008774 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008775 sp<FakeWindowHandle> mWindow;
8776 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008777 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008778
8779 void SetUp() override {
8780 InputDispatcherTest::SetUp();
8781
8782 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008783 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008784 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008785 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008786 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008787 mSecondWindow =
8788 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008789 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008790 mThirdWindow =
8791 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8792 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8793 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008794
8795 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008796 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8797 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8798 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008799 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008800
Antonio Kantek15beb512022-06-13 22:35:41 +00008801 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008802 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008803 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008804 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8805 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008806 mThirdWindow->assertNoEvents();
8807 }
8808
8809 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8810 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008811 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008812 SECOND_DISPLAY_ID)) {
8813 mWindow->assertNoEvents();
8814 mSecondWindow->assertNoEvents();
8815 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008816 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008817 }
8818
Antonio Kantek15beb512022-06-13 22:35:41 +00008819 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8820 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008821 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8822 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008823 mWindow->consumeTouchModeEvent(inTouchMode);
8824 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008825 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008826 }
8827};
8828
Antonio Kantek26defcf2022-02-08 01:12:27 +00008829TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008830 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008831 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8832 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008833 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008834}
8835
Antonio Kantek26defcf2022-02-08 01:12:27 +00008836TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8837 const WindowInfo& windowInfo = *mWindow->getInfo();
8838 int32_t ownerPid = windowInfo.ownerPid;
8839 int32_t ownerUid = windowInfo.ownerUid;
8840 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8841 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008842 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008843 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008844 mWindow->assertNoEvents();
8845 mSecondWindow->assertNoEvents();
8846}
8847
8848TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8849 const WindowInfo& windowInfo = *mWindow->getInfo();
8850 int32_t ownerPid = windowInfo.ownerPid;
8851 int32_t ownerUid = windowInfo.ownerUid;
8852 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008853 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008854 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008855}
8856
Antonio Kantekf16f2832021-09-28 04:39:20 +00008857TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008858 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008859 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8860 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008861 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008862 mWindow->assertNoEvents();
8863 mSecondWindow->assertNoEvents();
8864}
8865
Antonio Kantek15beb512022-06-13 22:35:41 +00008866TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8867 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8868 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8869 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008870 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008871 mWindow->assertNoEvents();
8872 mSecondWindow->assertNoEvents();
8873 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8874}
8875
Antonio Kantek48710e42022-03-24 14:19:30 -07008876TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8877 // Interact with the window first.
8878 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8879 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8880 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8881
8882 // Then remove focus.
8883 mWindow->setFocusable(false);
8884 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8885
8886 // Assert that caller can switch touch mode by owning one of the last interacted window.
8887 const WindowInfo& windowInfo = *mWindow->getInfo();
8888 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8889 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008890 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008891}
8892
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008893class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8894public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008895 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008896 std::shared_ptr<FakeApplicationHandle> application =
8897 std::make_shared<FakeApplicationHandle>();
8898 std::string name = "Fake Spy ";
8899 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008900 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8901 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008902 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008903 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008904 return spy;
8905 }
8906
8907 sp<FakeWindowHandle> createForeground() {
8908 std::shared_ptr<FakeApplicationHandle> application =
8909 std::make_shared<FakeApplicationHandle>();
8910 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008911 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8912 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008913 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008914 return window;
8915 }
8916
8917private:
8918 int mSpyCount{0};
8919};
8920
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008921using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008922/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008923 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8924 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008925TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8926 ScopedSilentDeath _silentDeath;
8927
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008928 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008929 spy->setTrustedOverlay(false);
8930 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8931 ".* not a trusted overlay");
8932}
8933
8934/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008935 * Input injection into a display with a spy window but no foreground windows should succeed.
8936 */
8937TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008938 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008939 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8940
8941 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8942 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8943 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8944 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8945}
8946
8947/**
8948 * Verify the order in which different input windows receive events. The touched foreground window
8949 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8950 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8951 * receive events before ones belows it.
8952 *
8953 * Here, we set up a scenario with four windows in the following Z order from the top:
8954 * spy1, spy2, window, spy3.
8955 * We then inject an event and verify that the foreground "window" receives it first, followed by
8956 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8957 * window.
8958 */
8959TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8960 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008961 auto spy1 = createSpy();
8962 auto spy2 = createSpy();
8963 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008964 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8965 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8966 const size_t numChannels = channels.size();
8967
Michael Wright8e9a8562022-02-09 13:44:29 +00008968 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008969 if (!epollFd.ok()) {
8970 FAIL() << "Failed to create epoll fd";
8971 }
8972
8973 for (size_t i = 0; i < numChannels; i++) {
8974 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8975 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8976 FAIL() << "Failed to add fd to epoll";
8977 }
8978 }
8979
8980 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8981 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8982 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8983
8984 std::vector<size_t> eventOrder;
8985 std::vector<struct epoll_event> events(numChannels);
8986 for (;;) {
8987 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8988 (100ms).count());
8989 if (nFds < 0) {
8990 FAIL() << "Failed to call epoll_wait";
8991 }
8992 if (nFds == 0) {
8993 break; // epoll_wait timed out
8994 }
8995 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008996 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008997 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008998 channels[i]->consumeMotionDown();
8999 }
9000 }
9001
9002 // Verify the order in which the events were received.
9003 EXPECT_EQ(3u, eventOrder.size());
9004 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9005 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9006 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9007}
9008
9009/**
9010 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9011 */
9012TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9013 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009014 auto spy = createSpy();
9015 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009016 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9017
9018 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9019 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9020 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9021 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9022 spy->assertNoEvents();
9023}
9024
9025/**
9026 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9027 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9028 * to the window.
9029 */
9030TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9031 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009032 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009033 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9035
9036 // Inject an event outside the spy window's touchable region.
9037 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9038 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9039 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9040 window->consumeMotionDown();
9041 spy->assertNoEvents();
9042 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9043 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9044 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9045 window->consumeMotionUp();
9046 spy->assertNoEvents();
9047
9048 // Inject an event inside the spy window's touchable region.
9049 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9050 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9051 {5, 10}))
9052 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9053 window->consumeMotionDown();
9054 spy->consumeMotionDown();
9055}
9056
9057/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009058 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009059 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009060 */
9061TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9062 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009063 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009064 auto spy = createSpy();
9065 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009066 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009067 spy->setFrame(Rect{0, 0, 20, 20});
9068 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9069
9070 // Inject an event outside the spy window's frame and touchable region.
9071 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009072 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9073 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009074 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9075 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009076 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009077}
9078
9079/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009080 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9081 * pointers that are down within its bounds.
9082 */
9083TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9084 auto windowLeft = createForeground();
9085 windowLeft->setFrame({0, 0, 100, 200});
9086 auto windowRight = createForeground();
9087 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009088 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009089 spy->setFrame({0, 0, 200, 200});
9090 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9091
9092 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9093 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9094 {50, 50}))
9095 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9096 windowLeft->consumeMotionDown();
9097 spy->consumeMotionDown();
9098
9099 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009100 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009101 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009102 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9103 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009104 .build();
9105 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9106 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9107 InputEventInjectionSync::WAIT_FOR_RESULT))
9108 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9109 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009110 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009111}
9112
9113/**
9114 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9115 * the spy should receive the second pointer with ACTION_DOWN.
9116 */
9117TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9118 auto window = createForeground();
9119 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009120 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009121 spyRight->setFrame({100, 0, 200, 200});
9122 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9123
9124 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9125 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9126 {50, 50}))
9127 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9128 window->consumeMotionDown();
9129 spyRight->assertNoEvents();
9130
9131 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009132 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009133 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009134 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9135 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009136 .build();
9137 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9138 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9139 InputEventInjectionSync::WAIT_FOR_RESULT))
9140 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009141 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009142 spyRight->consumeMotionDown();
9143}
9144
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009145/**
9146 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9147 * windows should be allowed to control split touch.
9148 */
9149TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009150 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009151 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009152 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009153 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009154
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009155 auto window = createForeground();
9156 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009157
9158 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9159
9160 // First finger down, no window touched.
9161 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9162 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9163 {100, 200}))
9164 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9165 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9166 window->assertNoEvents();
9167
9168 // Second finger down on window, the window should receive touch down.
9169 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009170 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009171 .displayId(ADISPLAY_ID_DEFAULT)
9172 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009173 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9174 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009175 .build();
9176 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9177 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9178 InputEventInjectionSync::WAIT_FOR_RESULT))
9179 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9180
9181 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009182 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009183}
9184
9185/**
9186 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9187 * do not receive key events.
9188 */
9189TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009190 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009191 spy->setFocusable(false);
9192
9193 auto window = createForeground();
9194 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9195 setFocusedWindow(window);
9196 window->consumeFocusEvent(true);
9197
9198 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9199 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9200 window->consumeKeyDown(ADISPLAY_ID_NONE);
9201
9202 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9203 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9204 window->consumeKeyUp(ADISPLAY_ID_NONE);
9205
9206 spy->assertNoEvents();
9207}
9208
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009209using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9210
9211/**
9212 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9213 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9214 */
9215TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9216 auto window = createForeground();
9217 auto spy1 = createSpy();
9218 auto spy2 = createSpy();
9219 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9220
9221 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9222 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9223 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9224 window->consumeMotionDown();
9225 spy1->consumeMotionDown();
9226 spy2->consumeMotionDown();
9227
9228 // Pilfer pointers from the second spy window.
9229 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9230 spy2->assertNoEvents();
9231 spy1->consumeMotionCancel();
9232 window->consumeMotionCancel();
9233
9234 // The rest of the gesture should only be sent to the second spy window.
9235 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9236 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9237 ADISPLAY_ID_DEFAULT))
9238 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9239 spy2->consumeMotionMove();
9240 spy1->assertNoEvents();
9241 window->assertNoEvents();
9242}
9243
9244/**
9245 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9246 * in the middle of the gesture.
9247 */
9248TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9249 auto window = createForeground();
9250 auto spy = createSpy();
9251 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9252
9253 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9254 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9255 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9256 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9257 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9258
9259 window->releaseChannel();
9260
9261 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9262
9263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9264 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9265 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9266 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9267}
9268
9269/**
9270 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9271 * the spy, but not to any other windows.
9272 */
9273TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9274 auto spy = createSpy();
9275 auto window = createForeground();
9276
9277 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9278
9279 // First finger down on the window and the spy.
9280 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9281 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9282 {100, 200}))
9283 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9284 spy->consumeMotionDown();
9285 window->consumeMotionDown();
9286
9287 // Spy window pilfers the pointers.
9288 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9289 window->consumeMotionCancel();
9290
9291 // Second finger down on the window and spy, but the window should not receive the pointer down.
9292 const MotionEvent secondFingerDownEvent =
9293 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9294 .displayId(ADISPLAY_ID_DEFAULT)
9295 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009296 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9297 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009298 .build();
9299 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9300 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9301 InputEventInjectionSync::WAIT_FOR_RESULT))
9302 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9303
Harry Cutts33476232023-01-30 19:57:29 +00009304 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009305
9306 // Third finger goes down outside all windows, so injection should fail.
9307 const MotionEvent thirdFingerDownEvent =
9308 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9309 .displayId(ADISPLAY_ID_DEFAULT)
9310 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009311 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9312 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9313 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009314 .build();
9315 ASSERT_EQ(InputEventInjectionResult::FAILED,
9316 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9317 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009318 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009319
9320 spy->assertNoEvents();
9321 window->assertNoEvents();
9322}
9323
9324/**
9325 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9326 */
9327TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9328 auto spy = createSpy();
9329 spy->setFrame(Rect(0, 0, 100, 100));
9330 auto window = createForeground();
9331 window->setFrame(Rect(0, 0, 200, 200));
9332
9333 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9334
9335 // First finger down on the window only
9336 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9337 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9338 {150, 150}))
9339 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9340 window->consumeMotionDown();
9341
9342 // Second finger down on the spy and window
9343 const MotionEvent secondFingerDownEvent =
9344 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9345 .displayId(ADISPLAY_ID_DEFAULT)
9346 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009347 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9348 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009349 .build();
9350 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9351 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9352 InputEventInjectionSync::WAIT_FOR_RESULT))
9353 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9354 spy->consumeMotionDown();
9355 window->consumeMotionPointerDown(1);
9356
9357 // Third finger down on the spy and window
9358 const MotionEvent thirdFingerDownEvent =
9359 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9360 .displayId(ADISPLAY_ID_DEFAULT)
9361 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009362 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9363 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9364 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009365 .build();
9366 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9367 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9368 InputEventInjectionSync::WAIT_FOR_RESULT))
9369 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9370 spy->consumeMotionPointerDown(1);
9371 window->consumeMotionPointerDown(2);
9372
9373 // Spy window pilfers the pointers.
9374 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9375 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9376 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9377
9378 spy->assertNoEvents();
9379 window->assertNoEvents();
9380}
9381
9382/**
9383 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9384 * other windows should be canceled. If this results in the cancellation of all pointers for some
9385 * window, then that window should receive ACTION_CANCEL.
9386 */
9387TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9388 auto spy = createSpy();
9389 spy->setFrame(Rect(0, 0, 100, 100));
9390 auto window = createForeground();
9391 window->setFrame(Rect(0, 0, 200, 200));
9392
9393 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9394
9395 // First finger down on both spy and window
9396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9397 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9398 {10, 10}))
9399 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9400 window->consumeMotionDown();
9401 spy->consumeMotionDown();
9402
9403 // Second finger down on the spy and window
9404 const MotionEvent secondFingerDownEvent =
9405 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9406 .displayId(ADISPLAY_ID_DEFAULT)
9407 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009408 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9409 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009410 .build();
9411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9412 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9413 InputEventInjectionSync::WAIT_FOR_RESULT))
9414 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9415 spy->consumeMotionPointerDown(1);
9416 window->consumeMotionPointerDown(1);
9417
9418 // Spy window pilfers the pointers.
9419 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9420 window->consumeMotionCancel();
9421
9422 spy->assertNoEvents();
9423 window->assertNoEvents();
9424}
9425
9426/**
9427 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9428 * be sent to other windows
9429 */
9430TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9431 auto spy = createSpy();
9432 spy->setFrame(Rect(0, 0, 100, 100));
9433 auto window = createForeground();
9434 window->setFrame(Rect(0, 0, 200, 200));
9435
9436 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9437
9438 // First finger down on both window and spy
9439 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9440 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9441 {10, 10}))
9442 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9443 window->consumeMotionDown();
9444 spy->consumeMotionDown();
9445
9446 // Spy window pilfers the pointers.
9447 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9448 window->consumeMotionCancel();
9449
9450 // Second finger down on the window only
9451 const MotionEvent secondFingerDownEvent =
9452 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9453 .displayId(ADISPLAY_ID_DEFAULT)
9454 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009455 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9456 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009457 .build();
9458 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9459 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9460 InputEventInjectionSync::WAIT_FOR_RESULT))
9461 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9462 window->consumeMotionDown();
9463 window->assertNoEvents();
9464
9465 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9466 spy->consumeMotionMove();
9467 spy->assertNoEvents();
9468}
9469
Prabir Pradhand65552b2021-10-07 11:23:50 -07009470class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9471public:
9472 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9473 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9474 std::make_shared<FakeApplicationHandle>();
9475 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009476 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9477 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009478 overlay->setFocusable(false);
9479 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009480 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009481 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009482 overlay->setTrustedOverlay(true);
9483
9484 std::shared_ptr<FakeApplicationHandle> application =
9485 std::make_shared<FakeApplicationHandle>();
9486 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009487 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9488 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009489 window->setFocusable(true);
9490 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009491
9492 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9493 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9494 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009495 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009496 return {std::move(overlay), std::move(window)};
9497 }
9498
9499 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009500 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009501 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009502 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009503 }
9504
9505 void sendStylusEvent(int32_t action) {
9506 NotifyMotionArgs motionArgs =
9507 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9508 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009509 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009510 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009511 }
9512};
9513
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009514using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9515
9516TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9517 ScopedSilentDeath _silentDeath;
9518
Prabir Pradhand65552b2021-10-07 11:23:50 -07009519 auto [overlay, window] = setupStylusOverlayScenario();
9520 overlay->setTrustedOverlay(false);
9521 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9522 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9523 ".* not a trusted overlay");
9524}
9525
9526TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9527 auto [overlay, window] = setupStylusOverlayScenario();
9528 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9529
9530 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9531 overlay->consumeMotionDown();
9532 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9533 overlay->consumeMotionUp();
9534
9535 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9536 window->consumeMotionDown();
9537 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9538 window->consumeMotionUp();
9539
9540 overlay->assertNoEvents();
9541 window->assertNoEvents();
9542}
9543
9544TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9545 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009546 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009547 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9548
9549 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9550 overlay->consumeMotionDown();
9551 window->consumeMotionDown();
9552 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9553 overlay->consumeMotionUp();
9554 window->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
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009565/**
9566 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9567 * The scenario is as follows:
9568 * - The stylus interceptor overlay is configured as a spy window.
9569 * - The stylus interceptor spy receives the start of a new stylus gesture.
9570 * - It pilfers pointers and then configures itself to no longer be a spy.
9571 * - The stylus interceptor continues to receive the rest of the gesture.
9572 */
9573TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9574 auto [overlay, window] = setupStylusOverlayScenario();
9575 overlay->setSpy(true);
9576 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9577
9578 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9579 overlay->consumeMotionDown();
9580 window->consumeMotionDown();
9581
9582 // The interceptor pilfers the pointers.
9583 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9584 window->consumeMotionCancel();
9585
9586 // The interceptor configures itself so that it is no longer a spy.
9587 overlay->setSpy(false);
9588 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9589
9590 // It continues to receive the rest of the stylus gesture.
9591 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9592 overlay->consumeMotionMove();
9593 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9594 overlay->consumeMotionUp();
9595
9596 window->assertNoEvents();
9597}
9598
Prabir Pradhan5735a322022-04-11 17:23:34 +00009599struct User {
9600 int32_t mPid;
9601 int32_t mUid;
9602 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9603 std::unique_ptr<InputDispatcher>& mDispatcher;
9604
9605 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9606 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9607
9608 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9609 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9610 ADISPLAY_ID_DEFAULT, {100, 200},
9611 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9612 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9613 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9614 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9615 }
9616
9617 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009618 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009619 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009620 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009621 mPolicyFlags);
9622 }
9623
9624 sp<FakeWindowHandle> createWindow() const {
9625 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9626 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009627 sp<FakeWindowHandle> window =
9628 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9629 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009630 window->setOwnerInfo(mPid, mUid);
9631 return window;
9632 }
9633};
9634
9635using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9636
9637TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9638 auto owner = User(mDispatcher, 10, 11);
9639 auto window = owner.createWindow();
9640 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9641
9642 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9643 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9644 window->consumeMotionDown();
9645
9646 setFocusedWindow(window);
9647 window->consumeFocusEvent(true);
9648
9649 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9650 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9651 window->consumeKeyDown(ADISPLAY_ID_NONE);
9652}
9653
9654TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9655 auto owner = User(mDispatcher, 10, 11);
9656 auto window = owner.createWindow();
9657 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9658
9659 auto rando = User(mDispatcher, 20, 21);
9660 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9661 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9662
9663 setFocusedWindow(window);
9664 window->consumeFocusEvent(true);
9665
9666 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9667 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9668 window->assertNoEvents();
9669}
9670
9671TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9672 auto owner = User(mDispatcher, 10, 11);
9673 auto window = owner.createWindow();
9674 auto spy = owner.createWindow();
9675 spy->setSpy(true);
9676 spy->setTrustedOverlay(true);
9677 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9678
9679 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9680 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9681 spy->consumeMotionDown();
9682 window->consumeMotionDown();
9683}
9684
9685TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9686 auto owner = User(mDispatcher, 10, 11);
9687 auto window = owner.createWindow();
9688
9689 auto rando = User(mDispatcher, 20, 21);
9690 auto randosSpy = rando.createWindow();
9691 randosSpy->setSpy(true);
9692 randosSpy->setTrustedOverlay(true);
9693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9694
9695 // The event is targeted at owner's window, so injection should succeed, but the spy should
9696 // not receive the event.
9697 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9698 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9699 randosSpy->assertNoEvents();
9700 window->consumeMotionDown();
9701}
9702
9703TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9704 auto owner = User(mDispatcher, 10, 11);
9705 auto window = owner.createWindow();
9706
9707 auto rando = User(mDispatcher, 20, 21);
9708 auto randosSpy = rando.createWindow();
9709 randosSpy->setSpy(true);
9710 randosSpy->setTrustedOverlay(true);
9711 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9712
9713 // A user that has injection permission can inject into any window.
9714 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9715 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9716 ADISPLAY_ID_DEFAULT));
9717 randosSpy->consumeMotionDown();
9718 window->consumeMotionDown();
9719
9720 setFocusedWindow(randosSpy);
9721 randosSpy->consumeFocusEvent(true);
9722
9723 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9724 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9725 window->assertNoEvents();
9726}
9727
9728TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9729 auto owner = User(mDispatcher, 10, 11);
9730 auto window = owner.createWindow();
9731
9732 auto rando = User(mDispatcher, 20, 21);
9733 auto randosWindow = rando.createWindow();
9734 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9735 randosWindow->setWatchOutsideTouch(true);
9736 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9737
9738 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9739 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9740 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9741 window->consumeMotionDown();
9742 randosWindow->consumeMotionOutside();
9743}
9744
Garfield Tane84e6f92019-08-29 17:28:41 -07009745} // namespace android::inputdispatcher