blob: d4103fd19c663bf339ddc488f29a54c1c2e8fa16 [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
Garfield Tan1c7bc862020-01-28 13:24:04 -08005666class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5667protected:
5668 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5669 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5670
Chris Yea209fde2020-07-22 13:54:51 -07005671 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005672 sp<FakeWindowHandle> mWindow;
5673
5674 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005675 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005676 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005677 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005678 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005679 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5680 ASSERT_EQ(OK, mDispatcher->start());
5681
5682 setUpWindow();
5683 }
5684
5685 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005686 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005687 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005688
Vishnu Nair47074b82020-08-14 11:54:47 -07005689 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005690 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005691 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005692 mWindow->consumeFocusEvent(true);
5693 }
5694
Chris Ye2ad95392020-09-01 13:44:44 -07005695 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005696 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005697 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005698 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005699 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005700
5701 // Window should receive key down event.
5702 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5703 }
5704
5705 void expectKeyRepeatOnce(int32_t repeatCount) {
5706 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5707 InputEvent* repeatEvent = mWindow->consume();
5708 ASSERT_NE(nullptr, repeatEvent);
5709
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005710 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005711
5712 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5713 uint32_t eventAction = repeatKeyEvent->getAction();
5714 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5715 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5716 }
5717
Chris Ye2ad95392020-09-01 13:44:44 -07005718 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005719 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005720 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005721 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005722 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005723
5724 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005725 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005726 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005727 }
5728};
5729
5730TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005731 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005732 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5733 expectKeyRepeatOnce(repeatCount);
5734 }
5735}
5736
5737TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005738 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005739 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5740 expectKeyRepeatOnce(repeatCount);
5741 }
Harry Cutts33476232023-01-30 19:57:29 +00005742 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005743 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005744 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5745 expectKeyRepeatOnce(repeatCount);
5746 }
5747}
5748
5749TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005750 sendAndConsumeKeyDown(/*deviceId=*/1);
5751 expectKeyRepeatOnce(/*repeatCount=*/1);
5752 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005753 mWindow->assertNoEvents();
5754}
5755
5756TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005757 sendAndConsumeKeyDown(/*deviceId=*/1);
5758 expectKeyRepeatOnce(/*repeatCount=*/1);
5759 sendAndConsumeKeyDown(/*deviceId=*/2);
5760 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005761 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005762 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005763 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005764 expectKeyRepeatOnce(/*repeatCount=*/2);
5765 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005766 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005767 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005768 mWindow->assertNoEvents();
5769}
5770
5771TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005772 sendAndConsumeKeyDown(/*deviceId=*/1);
5773 expectKeyRepeatOnce(/*repeatCount=*/1);
5774 sendAndConsumeKeyDown(/*deviceId=*/2);
5775 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005776 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005777 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005778 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005779 mWindow->assertNoEvents();
5780}
5781
liushenxiang42232912021-05-21 20:24:09 +08005782TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5783 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005784 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005785 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005786 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5787 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5788 mWindow->assertNoEvents();
5789}
5790
Garfield Tan1c7bc862020-01-28 13:24:04 -08005791TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005792 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005793 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005794 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5795 InputEvent* repeatEvent = mWindow->consume();
5796 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5797 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5798 IdGenerator::getSource(repeatEvent->getId()));
5799 }
5800}
5801
5802TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005803 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005804 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005805
5806 std::unordered_set<int32_t> idSet;
5807 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5808 InputEvent* repeatEvent = mWindow->consume();
5809 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5810 int32_t id = repeatEvent->getId();
5811 EXPECT_EQ(idSet.end(), idSet.find(id));
5812 idSet.insert(id);
5813 }
5814}
5815
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005816/* Test InputDispatcher for MultiDisplay */
5817class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5818public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005819 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005820 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005821
Chris Yea209fde2020-07-22 13:54:51 -07005822 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005823 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005824 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005825
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005826 // Set focus window for primary display, but focused display would be second one.
5827 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005828 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005829 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005830 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005831 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005832
Chris Yea209fde2020-07-22 13:54:51 -07005833 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005834 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005835 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005836 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005837 // Set focus display to second one.
5838 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5839 // Set focus window for second display.
5840 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005841 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005842 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005843 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005844 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005845 }
5846
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005847 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005848 InputDispatcherTest::TearDown();
5849
Chris Yea209fde2020-07-22 13:54:51 -07005850 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005851 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005852 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005853 windowInSecondary.clear();
5854 }
5855
5856protected:
Chris Yea209fde2020-07-22 13:54:51 -07005857 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005858 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005859 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005860 sp<FakeWindowHandle> windowInSecondary;
5861};
5862
5863TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5864 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005865 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5866 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5867 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005868 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005869 windowInSecondary->assertNoEvents();
5870
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005871 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005872 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5873 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5874 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005875 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005876 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005877}
5878
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005879TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005880 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005881 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5882 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005883 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005884 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005885 windowInSecondary->assertNoEvents();
5886
5887 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005888 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005889 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005890 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005891 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005892
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005893 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005894 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005895
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005896 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005897 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005898 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005899
5900 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005901 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005902 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005903 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005904 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005905 windowInSecondary->assertNoEvents();
5906}
5907
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005908// Test per-display input monitors for motion event.
5909TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005910 FakeMonitorReceiver monitorInPrimary =
5911 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5912 FakeMonitorReceiver monitorInSecondary =
5913 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005914
5915 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005916 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5917 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5918 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005919 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005920 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005921 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005922 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005923
5924 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005925 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5926 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5927 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005928 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005929 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005930 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005931 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005932
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005933 // Lift up the touch from the second display
5934 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5935 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5936 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5937 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5938 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5939
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005940 // Test inject a non-pointer motion event.
5941 // If specific a display, it will dispatch to the focused window of particular display,
5942 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005943 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5944 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
5945 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005946 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005947 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005948 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005949 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005950}
5951
5952// Test per-display input monitors for key event.
5953TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005954 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08005955 FakeMonitorReceiver monitorInPrimary =
5956 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5957 FakeMonitorReceiver monitorInSecondary =
5958 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005959
5960 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005961 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5962 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005963 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005964 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005965 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08005966 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005967}
5968
Vishnu Nair958da932020-08-21 17:12:37 -07005969TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
5970 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005971 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005972 secondWindowInPrimary->setFocusable(true);
5973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
5974 setFocusedWindow(secondWindowInPrimary);
5975 windowInPrimary->consumeFocusEvent(false);
5976 secondWindowInPrimary->consumeFocusEvent(true);
5977
5978 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005979 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
5980 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005981 windowInPrimary->assertNoEvents();
5982 windowInSecondary->assertNoEvents();
5983 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5984}
5985
Arthur Hungdfd528e2021-12-08 13:23:04 +00005986TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
5987 FakeMonitorReceiver monitorInPrimary =
5988 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5989 FakeMonitorReceiver monitorInSecondary =
5990 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
5991
5992 // Test touch down on primary display.
5993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5994 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5995 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5996 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5997 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5998
5999 // Test touch down on second display.
6000 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6001 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6002 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6003 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6004 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6005
6006 // Trigger cancel touch.
6007 mDispatcher->cancelCurrentTouch();
6008 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6009 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6010 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6011 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6012
6013 // Test inject a move motion event, no window/monitor should receive the event.
6014 ASSERT_EQ(InputEventInjectionResult::FAILED,
6015 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6016 ADISPLAY_ID_DEFAULT, {110, 200}))
6017 << "Inject motion event should return InputEventInjectionResult::FAILED";
6018 windowInPrimary->assertNoEvents();
6019 monitorInPrimary.assertNoEvents();
6020
6021 ASSERT_EQ(InputEventInjectionResult::FAILED,
6022 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6023 SECOND_DISPLAY_ID, {110, 200}))
6024 << "Inject motion event should return InputEventInjectionResult::FAILED";
6025 windowInSecondary->assertNoEvents();
6026 monitorInSecondary.assertNoEvents();
6027}
6028
Jackal Guof9696682018-10-05 12:23:23 +08006029class InputFilterTest : public InputDispatcherTest {
6030protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006031 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6032 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006033 NotifyMotionArgs motionArgs;
6034
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006035 motionArgs =
6036 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006037 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006038 motionArgs =
6039 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006040 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006041 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006042 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006043 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6044 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006045 } else {
6046 mFakePolicy->assertFilterInputEventWasNotCalled();
6047 }
6048 }
6049
6050 void testNotifyKey(bool expectToBeFiltered) {
6051 NotifyKeyArgs keyArgs;
6052
6053 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006054 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006055 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006056 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006057 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006058
6059 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006060 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006061 } else {
6062 mFakePolicy->assertFilterInputEventWasNotCalled();
6063 }
6064 }
6065};
6066
6067// Test InputFilter for MotionEvent
6068TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6069 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6070 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6071 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6072
6073 // Enable InputFilter
6074 mDispatcher->setInputFilterEnabled(true);
6075 // Test touch on both primary and second display, and check if both events are filtered.
6076 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6077 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6078
6079 // Disable InputFilter
6080 mDispatcher->setInputFilterEnabled(false);
6081 // Test touch on both primary and second display, and check if both events aren't filtered.
6082 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6083 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6084}
6085
6086// Test InputFilter for KeyEvent
6087TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6088 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6089 testNotifyKey(/*expectToBeFiltered*/ false);
6090
6091 // Enable InputFilter
6092 mDispatcher->setInputFilterEnabled(true);
6093 // Send a key event, and check if it is filtered.
6094 testNotifyKey(/*expectToBeFiltered*/ true);
6095
6096 // Disable InputFilter
6097 mDispatcher->setInputFilterEnabled(false);
6098 // Send a key event, and check if it isn't filtered.
6099 testNotifyKey(/*expectToBeFiltered*/ false);
6100}
6101
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006102// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6103// logical display coordinate space.
6104TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6105 ui::Transform firstDisplayTransform;
6106 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6107 ui::Transform secondDisplayTransform;
6108 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6109
6110 std::vector<gui::DisplayInfo> displayInfos(2);
6111 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6112 displayInfos[0].transform = firstDisplayTransform;
6113 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6114 displayInfos[1].transform = secondDisplayTransform;
6115
Patrick Williamsd828f302023-04-28 17:52:08 -05006116 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006117
6118 // Enable InputFilter
6119 mDispatcher->setInputFilterEnabled(true);
6120
6121 // Ensure the correct transforms are used for the displays.
6122 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6123 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6124}
6125
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006126class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6127protected:
6128 virtual void SetUp() override {
6129 InputDispatcherTest::SetUp();
6130
6131 /**
6132 * We don't need to enable input filter to test the injected event policy, but we enabled it
6133 * here to make the tests more realistic, since this policy only matters when inputfilter is
6134 * on.
6135 */
6136 mDispatcher->setInputFilterEnabled(true);
6137
6138 std::shared_ptr<InputApplicationHandle> application =
6139 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006140 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6141 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006142
6143 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6144 mWindow->setFocusable(true);
6145 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6146 setFocusedWindow(mWindow);
6147 mWindow->consumeFocusEvent(true);
6148 }
6149
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006150 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6151 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006152 KeyEvent event;
6153
6154 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6155 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6156 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006157 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006158 const int32_t additionalPolicyFlags =
6159 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6160 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006161 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006162 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6163 policyFlags | additionalPolicyFlags));
6164
6165 InputEvent* received = mWindow->consume();
6166 ASSERT_NE(nullptr, received);
6167 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006168 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006169 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6170 ASSERT_EQ(flags, keyEvent.getFlags());
6171 }
6172
6173 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6174 int32_t flags) {
6175 MotionEvent event;
6176 PointerProperties pointerProperties[1];
6177 PointerCoords pointerCoords[1];
6178 pointerProperties[0].clear();
6179 pointerProperties[0].id = 0;
6180 pointerCoords[0].clear();
6181 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6182 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6183
6184 ui::Transform identityTransform;
6185 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6186 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6187 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6188 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6189 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006190 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006191 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006192 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6193
6194 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006196 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006197 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6198 policyFlags | additionalPolicyFlags));
6199
6200 InputEvent* received = mWindow->consume();
6201 ASSERT_NE(nullptr, received);
6202 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006203 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006204 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6205 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006206 }
6207
6208private:
6209 sp<FakeWindowHandle> mWindow;
6210};
6211
6212TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006213 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6214 // filter. Without it, the event will no different from a regularly injected event, and the
6215 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006216 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6217 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006218}
6219
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006220TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006221 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006222 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006223 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6224}
6225
6226TEST_F(InputFilterInjectionPolicyTest,
6227 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6228 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006229 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006230 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006231}
6232
6233TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006234 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6235 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006236}
6237
chaviwfd6d3512019-03-25 13:23:49 -07006238class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006239 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006240 InputDispatcherTest::SetUp();
6241
Chris Yea209fde2020-07-22 13:54:51 -07006242 std::shared_ptr<FakeApplicationHandle> application =
6243 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006244 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006245 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006246 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006247
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006248 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006249 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006250 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006251
6252 // Set focused application.
6253 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006254 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006255
6256 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006257 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006258 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006259 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006260 }
6261
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006262 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006263 InputDispatcherTest::TearDown();
6264
6265 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006266 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006267 }
6268
6269protected:
6270 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006271 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006272 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006273};
6274
6275// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6276// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6277// the onPointerDownOutsideFocus callback.
6278TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006279 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006280 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6281 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006282 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006283 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006284
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006285 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006286 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6287}
6288
6289// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6290// DOWN on the window that doesn't have focus. Ensure no window received the
6291// onPointerDownOutsideFocus callback.
6292TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006293 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006294 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006295 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006296 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006297
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006298 ASSERT_TRUE(mDispatcher->waitForIdle());
6299 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006300}
6301
6302// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6303// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6304TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6306 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006307 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006308 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006309
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006310 ASSERT_TRUE(mDispatcher->waitForIdle());
6311 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006312}
6313
6314// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6315// DOWN on the window that already has focus. Ensure no window received the
6316// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006317TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006318 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006319 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006320 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006321 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006322 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006323
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006324 ASSERT_TRUE(mDispatcher->waitForIdle());
6325 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006326}
6327
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006328// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6329// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6330TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6331 const MotionEvent event =
6332 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6333 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006334 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006335 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6336 .build();
6337 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6338 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6339 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6340
6341 ASSERT_TRUE(mDispatcher->waitForIdle());
6342 mFakePolicy->assertOnPointerDownWasNotCalled();
6343 // Ensure that the unfocused window did not receive any FOCUS events.
6344 mUnfocusedWindow->assertNoEvents();
6345}
6346
chaviwaf87b3e2019-10-01 16:59:28 -07006347// These tests ensures we can send touch events to a single client when there are multiple input
6348// windows that point to the same client token.
6349class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6350 virtual void SetUp() override {
6351 InputDispatcherTest::SetUp();
6352
Chris Yea209fde2020-07-22 13:54:51 -07006353 std::shared_ptr<FakeApplicationHandle> application =
6354 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006355 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6356 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006357 mWindow1->setFrame(Rect(0, 0, 100, 100));
6358
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006359 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6360 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006361 mWindow2->setFrame(Rect(100, 100, 200, 200));
6362
Arthur Hung72d8dc32020-03-28 00:48:39 +00006363 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006364 }
6365
6366protected:
6367 sp<FakeWindowHandle> mWindow1;
6368 sp<FakeWindowHandle> mWindow2;
6369
6370 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006371 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006372 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6373 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006374 }
6375
6376 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6377 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006378 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006379 InputEvent* event = window->consume();
6380
6381 ASSERT_NE(nullptr, event) << name.c_str()
6382 << ": consumer should have returned non-NULL event.";
6383
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006384 ASSERT_EQ(InputEventType::MOTION, event->getType())
6385 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006386
6387 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006388 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006389 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006390
6391 for (size_t i = 0; i < points.size(); i++) {
6392 float expectedX = points[i].x;
6393 float expectedY = points[i].y;
6394
6395 EXPECT_EQ(expectedX, motionEvent.getX(i))
6396 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6397 << ", got " << motionEvent.getX(i);
6398 EXPECT_EQ(expectedY, motionEvent.getY(i))
6399 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6400 << ", got " << motionEvent.getY(i);
6401 }
6402 }
chaviw9eaa22c2020-07-01 16:21:27 -07006403
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006404 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006405 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006406 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6407 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006408
6409 // Always consume from window1 since it's the window that has the InputReceiver
6410 consumeMotionEvent(mWindow1, action, expectedPoints);
6411 }
chaviwaf87b3e2019-10-01 16:59:28 -07006412};
6413
6414TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6415 // Touch Window 1
6416 PointF touchedPoint = {10, 10};
6417 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006418 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006419
6420 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006421 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006422
6423 // Touch Window 2
6424 touchedPoint = {150, 150};
6425 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006426 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006427}
6428
chaviw9eaa22c2020-07-01 16:21:27 -07006429TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6430 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006431 mWindow2->setWindowScale(0.5f, 0.5f);
6432
6433 // Touch Window 1
6434 PointF touchedPoint = {10, 10};
6435 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006436 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006437 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006438 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006439
6440 // Touch Window 2
6441 touchedPoint = {150, 150};
6442 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006443 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6444 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006445
chaviw9eaa22c2020-07-01 16:21:27 -07006446 // Update the transform so rotation is set
6447 mWindow2->setWindowTransform(0, -1, 1, 0);
6448 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6449 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006450}
6451
chaviw9eaa22c2020-07-01 16:21:27 -07006452TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006453 mWindow2->setWindowScale(0.5f, 0.5f);
6454
6455 // Touch Window 1
6456 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6457 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006458 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006459
6460 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006461 touchedPoints.push_back(PointF{150, 150});
6462 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006463 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006464
chaviw9eaa22c2020-07-01 16:21:27 -07006465 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006466 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006467 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006468
chaviw9eaa22c2020-07-01 16:21:27 -07006469 // Update the transform so rotation is set for Window 2
6470 mWindow2->setWindowTransform(0, -1, 1, 0);
6471 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006472 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006473}
6474
chaviw9eaa22c2020-07-01 16:21:27 -07006475TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006476 mWindow2->setWindowScale(0.5f, 0.5f);
6477
6478 // Touch Window 1
6479 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6480 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006481 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006482
6483 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006484 touchedPoints.push_back(PointF{150, 150});
6485 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006486
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006487 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006488
6489 // Move both windows
6490 touchedPoints = {{20, 20}, {175, 175}};
6491 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6492 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6493
chaviw9eaa22c2020-07-01 16:21:27 -07006494 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006495
chaviw9eaa22c2020-07-01 16:21:27 -07006496 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006497 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006498 expectedPoints.pop_back();
6499
6500 // Touch Window 2
6501 mWindow2->setWindowTransform(0, -1, 1, 0);
6502 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006503 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006504
6505 // Move both windows
6506 touchedPoints = {{20, 20}, {175, 175}};
6507 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6508 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6509
6510 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006511}
6512
6513TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6514 mWindow1->setWindowScale(0.5f, 0.5f);
6515
6516 // Touch Window 1
6517 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6518 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006519 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006520
6521 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006522 touchedPoints.push_back(PointF{150, 150});
6523 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006524
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006525 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006526
6527 // Move both windows
6528 touchedPoints = {{20, 20}, {175, 175}};
6529 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6530 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6531
chaviw9eaa22c2020-07-01 16:21:27 -07006532 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006533}
6534
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006535/**
6536 * When one of the windows is slippery, the touch should not slip into the other window with the
6537 * same input channel.
6538 */
6539TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6540 mWindow1->setSlippery(true);
6541 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6542
6543 // Touch down in window 1
6544 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6545 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6546 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6547
6548 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6549 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6550 // getting generated.
6551 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6552 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6553
6554 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6555}
6556
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006557class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6558 virtual void SetUp() override {
6559 InputDispatcherTest::SetUp();
6560
Chris Yea209fde2020-07-22 13:54:51 -07006561 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006562 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006563 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6564 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006565 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006566 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006567 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006568
6569 // Set focused application.
6570 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6571
6572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006573 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006574 mWindow->consumeFocusEvent(true);
6575 }
6576
6577 virtual void TearDown() override {
6578 InputDispatcherTest::TearDown();
6579 mWindow.clear();
6580 }
6581
6582protected:
Chris Yea209fde2020-07-22 13:54:51 -07006583 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006584 sp<FakeWindowHandle> mWindow;
6585 static constexpr PointF WINDOW_LOCATION = {20, 20};
6586
6587 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006588 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006589 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6590 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006591 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006592 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6593 WINDOW_LOCATION));
6594 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006595
6596 sp<FakeWindowHandle> addSpyWindow() {
6597 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006598 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006599 spy->setTrustedOverlay(true);
6600 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006601 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006602 spy->setDispatchingTimeout(30ms);
6603 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6604 return spy;
6605 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006606};
6607
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006608// Send a tap and respond, which should not cause an ANR.
6609TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6610 tapOnWindow();
6611 mWindow->consumeMotionDown();
6612 mWindow->consumeMotionUp();
6613 ASSERT_TRUE(mDispatcher->waitForIdle());
6614 mFakePolicy->assertNotifyAnrWasNotCalled();
6615}
6616
6617// Send a regular key and respond, which should not cause an ANR.
6618TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006619 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006620 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6621 ASSERT_TRUE(mDispatcher->waitForIdle());
6622 mFakePolicy->assertNotifyAnrWasNotCalled();
6623}
6624
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006625TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6626 mWindow->setFocusable(false);
6627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6628 mWindow->consumeFocusEvent(false);
6629
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006630 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006631 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6632 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6633 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006634 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006635 // Key will not go to window because we have no focused window.
6636 // The 'no focused window' ANR timer should start instead.
6637
6638 // Now, the focused application goes away.
6639 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6640 // The key should get dropped and there should be no ANR.
6641
6642 ASSERT_TRUE(mDispatcher->waitForIdle());
6643 mFakePolicy->assertNotifyAnrWasNotCalled();
6644}
6645
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006646// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006647// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6648// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006649TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006650 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006651 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6652 WINDOW_LOCATION));
6653
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006654 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6655 ASSERT_TRUE(sequenceNum);
6656 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006657 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006658
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006659 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006660 mWindow->consumeMotionEvent(
6661 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006662 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006663 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006664}
6665
6666// Send a key to the app and have the app not respond right away.
6667TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6668 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006669 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006670 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6671 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006672 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006673 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006674 ASSERT_TRUE(mDispatcher->waitForIdle());
6675}
6676
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006677// We have a focused application, but no focused window
6678TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006679 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006680 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6681 mWindow->consumeFocusEvent(false);
6682
6683 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006684 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006685 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6686 WINDOW_LOCATION));
6687 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6688 mDispatcher->waitForIdle();
6689 mFakePolicy->assertNotifyAnrWasNotCalled();
6690
6691 // Once a focused event arrives, we get an ANR for this application
6692 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6693 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006694 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006695 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6696 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006697 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006698 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006699 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006700 ASSERT_TRUE(mDispatcher->waitForIdle());
6701}
6702
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006703/**
6704 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6705 * there will not be an ANR.
6706 */
6707TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6708 mWindow->setFocusable(false);
6709 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6710 mWindow->consumeFocusEvent(false);
6711
6712 KeyEvent event;
6713 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6714 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6715
6716 // Define a valid key down event that is stale (too old).
6717 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6718 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006719 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006720
6721 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6722
6723 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006724 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006725 InputEventInjectionSync::WAIT_FOR_RESULT,
6726 INJECT_EVENT_TIMEOUT, policyFlags);
6727 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6728 << "Injection should fail because the event is stale";
6729
6730 ASSERT_TRUE(mDispatcher->waitForIdle());
6731 mFakePolicy->assertNotifyAnrWasNotCalled();
6732 mWindow->assertNoEvents();
6733}
6734
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006735// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006736// Make sure that we don't notify policy twice about the same ANR.
6737TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006738 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006739 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6740 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006741
6742 // Once a focused event arrives, we get an ANR for this application
6743 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6744 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006745 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006746 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6747 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006748 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006749 const std::chrono::duration appTimeout =
6750 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6751 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006752
Vishnu Naire4df8752022-09-08 09:17:55 -07006753 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006754 // ANR should not be raised again. It is up to policy to do that if it desires.
6755 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006756
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006757 // If we now get a focused window, the ANR should stop, but the policy handles that via
6758 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006759 ASSERT_TRUE(mDispatcher->waitForIdle());
6760}
6761
6762// We have a focused application, but no focused window
6763TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006764 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006765 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6766 mWindow->consumeFocusEvent(false);
6767
6768 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006769 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006770 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006771 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6772 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006773
Vishnu Naire4df8752022-09-08 09:17:55 -07006774 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6775 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006776
6777 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006778 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006779 ASSERT_TRUE(mDispatcher->waitForIdle());
6780 mWindow->assertNoEvents();
6781}
6782
6783/**
6784 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6785 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6786 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6787 * the ANR mechanism should still work.
6788 *
6789 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6790 * DOWN event, while not responding on the second one.
6791 */
6792TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6793 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6794 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6795 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6796 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6797 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006798 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006799
6800 // Now send ACTION_UP, with identical timestamp
6801 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6802 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6803 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6804 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006805 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006806
6807 // We have now sent down and up. Let's consume first event and then ANR on the second.
6808 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6809 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006810 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006811}
6812
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006813// A spy window can receive an ANR
6814TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6815 sp<FakeWindowHandle> spy = addSpyWindow();
6816
6817 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6818 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6819 WINDOW_LOCATION));
6820 mWindow->consumeMotionDown();
6821
6822 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6823 ASSERT_TRUE(sequenceNum);
6824 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006825 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006826
6827 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006828 spy->consumeMotionEvent(
6829 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006830 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006831 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006832}
6833
6834// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006835// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006836TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6837 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006838
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006839 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6840 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006841 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006842 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006843
6844 // Stuck on the ACTION_UP
6845 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006846 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006847
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006848 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006849 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006850 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6851 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006852
6853 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6854 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006855 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006856 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006857 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006858}
6859
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006860// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006861// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006862TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6863 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006864
6865 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006866 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6867 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006868
6869 mWindow->consumeMotionDown();
6870 // Stuck on the ACTION_UP
6871 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006872 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006873
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006874 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006875 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006876 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6877 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006878
6879 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6880 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006881 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006882 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006883 spy->assertNoEvents();
6884}
6885
6886TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6887 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6888
6889 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6890
6891 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6892 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6893 WINDOW_LOCATION));
6894
6895 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6896 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6897 ASSERT_TRUE(consumeSeq);
6898
Prabir Pradhanedd96402022-02-15 01:46:16 -08006899 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006900
6901 monitor.finishEvent(*consumeSeq);
6902 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6903
6904 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006905 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006906}
6907
6908// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6909// process events, you don't get an anr. When the window later becomes unresponsive again, you
6910// get an ANR again.
6911// 1. tap -> block on ACTION_UP -> receive ANR
6912// 2. consume all pending events (= queue becomes healthy again)
6913// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6914TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6915 tapOnWindow();
6916
6917 mWindow->consumeMotionDown();
6918 // Block on ACTION_UP
6919 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006920 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006921 mWindow->consumeMotionUp(); // Now the connection should be healthy again
6922 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006923 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006924 mWindow->assertNoEvents();
6925
6926 tapOnWindow();
6927 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006928 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006929 mWindow->consumeMotionUp();
6930
6931 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006932 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006933 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006934 mWindow->assertNoEvents();
6935}
6936
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006937// If a connection remains unresponsive for a while, make sure policy is only notified once about
6938// it.
6939TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006941 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6942 WINDOW_LOCATION));
6943
6944 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006945 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006946 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006947 // 'notifyConnectionUnresponsive' should only be called once per connection
6948 mFakePolicy->assertNotifyAnrWasNotCalled();
6949 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006950 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006951 mWindow->consumeMotionEvent(
6952 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006953 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006954 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006955 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006956 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006957}
6958
6959/**
6960 * If a window is processing a motion event, and then a key event comes in, the key event should
6961 * not to to the focused window until the motion is processed.
6962 *
6963 * Warning!!!
6964 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
6965 * and the injection timeout that we specify when injecting the key.
6966 * We must have the injection timeout (10ms) be smaller than
6967 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
6968 *
6969 * If that value changes, this test should also change.
6970 */
6971TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
6972 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
6973 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6974
6975 tapOnWindow();
6976 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
6977 ASSERT_TRUE(downSequenceNum);
6978 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
6979 ASSERT_TRUE(upSequenceNum);
6980 // Don't finish the events yet, and send a key
6981 // Injection will "succeed" because we will eventually give up and send the key to the focused
6982 // window even if motions are still being processed. But because the injection timeout is short,
6983 // we will receive INJECTION_TIMED_OUT as the result.
6984
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006985 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006986 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006987 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6988 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006989 // Key will not be sent to the window, yet, because the window is still processing events
6990 // and the key remains pending, waiting for the touch events to be processed
6991 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
6992 ASSERT_FALSE(keySequenceNum);
6993
6994 std::this_thread::sleep_for(500ms);
6995 // if we wait long enough though, dispatcher will give up, and still send the key
6996 // to the focused window, even though we have not yet finished the motion event
6997 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6998 mWindow->finishEvent(*downSequenceNum);
6999 mWindow->finishEvent(*upSequenceNum);
7000}
7001
7002/**
7003 * If a window is processing a motion event, and then a key event comes in, the key event should
7004 * not go to the focused window until the motion is processed.
7005 * If then a new motion comes in, then the pending key event should be going to the currently
7006 * focused window right away.
7007 */
7008TEST_F(InputDispatcherSingleWindowAnr,
7009 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7010 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7011 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7012
7013 tapOnWindow();
7014 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7015 ASSERT_TRUE(downSequenceNum);
7016 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7017 ASSERT_TRUE(upSequenceNum);
7018 // Don't finish the events yet, and send a key
7019 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007020 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007021 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7022 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007023 // At this point, key is still pending, and should not be sent to the application yet.
7024 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7025 ASSERT_FALSE(keySequenceNum);
7026
7027 // Now tap down again. It should cause the pending key to go to the focused window right away.
7028 tapOnWindow();
7029 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7030 // the other events yet. We can finish events in any order.
7031 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7032 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7033 mWindow->consumeMotionDown();
7034 mWindow->consumeMotionUp();
7035 mWindow->assertNoEvents();
7036}
7037
7038class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7039 virtual void SetUp() override {
7040 InputDispatcherTest::SetUp();
7041
Chris Yea209fde2020-07-22 13:54:51 -07007042 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007043 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007044 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7045 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007046 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007047 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007048 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007049
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007050 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7051 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007052 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007053 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007054
7055 // Set focused application.
7056 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007057 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007058
7059 // Expect one focus window exist in display.
7060 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007061 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007062 mFocusedWindow->consumeFocusEvent(true);
7063 }
7064
7065 virtual void TearDown() override {
7066 InputDispatcherTest::TearDown();
7067
7068 mUnfocusedWindow.clear();
7069 mFocusedWindow.clear();
7070 }
7071
7072protected:
Chris Yea209fde2020-07-22 13:54:51 -07007073 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007074 sp<FakeWindowHandle> mUnfocusedWindow;
7075 sp<FakeWindowHandle> mFocusedWindow;
7076 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7077 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7078 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7079
7080 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7081
7082 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7083
7084private:
7085 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007086 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007087 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7088 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007089 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007090 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7091 location));
7092 }
7093};
7094
7095// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7096// should be ANR'd first.
7097TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007098 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007099 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7100 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007101 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007102 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007103 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007104 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007105 // We consumed all events, so no ANR
7106 ASSERT_TRUE(mDispatcher->waitForIdle());
7107 mFakePolicy->assertNotifyAnrWasNotCalled();
7108
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007109 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007110 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7111 FOCUSED_WINDOW_LOCATION));
7112 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7113 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007114
7115 const std::chrono::duration timeout =
7116 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007117 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007118 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7119 // sequence to make it consistent
7120 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007121 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007122 mFocusedWindow->consumeMotionDown();
7123 // This cancel is generated because the connection was unresponsive
7124 mFocusedWindow->consumeMotionCancel();
7125 mFocusedWindow->assertNoEvents();
7126 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007127 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007128 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7129 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007130 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007131}
7132
7133// If we have 2 windows with identical timeouts that are both unresponsive,
7134// it doesn't matter which order they should have ANR.
7135// But we should receive ANR for both.
7136TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7137 // Set the timeout for unfocused window to match the focused window
7138 mUnfocusedWindow->setDispatchingTimeout(10ms);
7139 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7140
7141 tapOnFocusedWindow();
7142 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007143 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7144 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7145 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007146
7147 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007148 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7149 mFocusedWindow->getToken() == anrConnectionToken2);
7150 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7151 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007152
7153 ASSERT_TRUE(mDispatcher->waitForIdle());
7154 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007155
7156 mFocusedWindow->consumeMotionDown();
7157 mFocusedWindow->consumeMotionUp();
7158 mUnfocusedWindow->consumeMotionOutside();
7159
Prabir Pradhanedd96402022-02-15 01:46:16 -08007160 sp<IBinder> responsiveToken1, responsiveToken2;
7161 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7162 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007163
7164 // Both applications should be marked as responsive, in any order
7165 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7166 mFocusedWindow->getToken() == responsiveToken2);
7167 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7168 mUnfocusedWindow->getToken() == responsiveToken2);
7169 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007170}
7171
7172// If a window is already not responding, the second tap on the same window should be ignored.
7173// We should also log an error to account for the dropped event (not tested here).
7174// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7175TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7176 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007177 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007178 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007179 // Receive the events, but don't respond
7180 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7181 ASSERT_TRUE(downEventSequenceNum);
7182 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7183 ASSERT_TRUE(upEventSequenceNum);
7184 const std::chrono::duration timeout =
7185 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007186 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007187
7188 // Tap once again
7189 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007190 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007191 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7192 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007193 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007194 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7195 FOCUSED_WINDOW_LOCATION));
7196 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7197 // valid touch target
7198 mUnfocusedWindow->assertNoEvents();
7199
7200 // Consume the first tap
7201 mFocusedWindow->finishEvent(*downEventSequenceNum);
7202 mFocusedWindow->finishEvent(*upEventSequenceNum);
7203 ASSERT_TRUE(mDispatcher->waitForIdle());
7204 // The second tap did not go to the focused window
7205 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007206 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007207 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7208 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007209 mFakePolicy->assertNotifyAnrWasNotCalled();
7210}
7211
7212// If you tap outside of all windows, there will not be ANR
7213TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007214 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007215 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7216 LOCATION_OUTSIDE_ALL_WINDOWS));
7217 ASSERT_TRUE(mDispatcher->waitForIdle());
7218 mFakePolicy->assertNotifyAnrWasNotCalled();
7219}
7220
7221// Since the focused window is paused, tapping on it should not produce any events
7222TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7223 mFocusedWindow->setPaused(true);
7224 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7225
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007226 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007227 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7228 FOCUSED_WINDOW_LOCATION));
7229
7230 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7231 ASSERT_TRUE(mDispatcher->waitForIdle());
7232 // Should not ANR because the window is paused, and touches shouldn't go to it
7233 mFakePolicy->assertNotifyAnrWasNotCalled();
7234
7235 mFocusedWindow->assertNoEvents();
7236 mUnfocusedWindow->assertNoEvents();
7237}
7238
7239/**
7240 * If a window is processing a motion event, and then a key event comes in, the key event should
7241 * not to to the focused window until the motion is processed.
7242 * If a different window becomes focused at this time, the key should go to that window instead.
7243 *
7244 * Warning!!!
7245 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7246 * and the injection timeout that we specify when injecting the key.
7247 * We must have the injection timeout (10ms) be smaller than
7248 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7249 *
7250 * If that value changes, this test should also change.
7251 */
7252TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7253 // Set a long ANR timeout to prevent it from triggering
7254 mFocusedWindow->setDispatchingTimeout(2s);
7255 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7256
7257 tapOnUnfocusedWindow();
7258 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7259 ASSERT_TRUE(downSequenceNum);
7260 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7261 ASSERT_TRUE(upSequenceNum);
7262 // Don't finish the events yet, and send a key
7263 // Injection will succeed because we will eventually give up and send the key to the focused
7264 // window even if motions are still being processed.
7265
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007266 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007267 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7268 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007269 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007270 // Key will not be sent to the window, yet, because the window is still processing events
7271 // and the key remains pending, waiting for the touch events to be processed
7272 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7273 ASSERT_FALSE(keySequenceNum);
7274
7275 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007276 mFocusedWindow->setFocusable(false);
7277 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007278 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007279 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007280
7281 // Focus events should precede the key events
7282 mUnfocusedWindow->consumeFocusEvent(true);
7283 mFocusedWindow->consumeFocusEvent(false);
7284
7285 // Finish the tap events, which should unblock dispatcher
7286 mUnfocusedWindow->finishEvent(*downSequenceNum);
7287 mUnfocusedWindow->finishEvent(*upSequenceNum);
7288
7289 // Now that all queues are cleared and no backlog in the connections, the key event
7290 // can finally go to the newly focused "mUnfocusedWindow".
7291 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7292 mFocusedWindow->assertNoEvents();
7293 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007294 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007295}
7296
7297// When the touch stream is split across 2 windows, and one of them does not respond,
7298// then ANR should be raised and the touch should be canceled for the unresponsive window.
7299// The other window should not be affected by that.
7300TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7301 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007302 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7303 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7304 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007305 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007306 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007307
7308 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007309 mDispatcher->notifyMotion(
7310 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7311 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007312
7313 const std::chrono::duration timeout =
7314 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007315 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007316
7317 mUnfocusedWindow->consumeMotionDown();
7318 mFocusedWindow->consumeMotionDown();
7319 // Focused window may or may not receive ACTION_MOVE
7320 // But it should definitely receive ACTION_CANCEL due to the ANR
7321 InputEvent* event;
7322 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7323 ASSERT_TRUE(moveOrCancelSequenceNum);
7324 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7325 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007326 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007327 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7328 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7329 mFocusedWindow->consumeMotionCancel();
7330 } else {
7331 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7332 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007333 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007334 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7335 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007336
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007337 mUnfocusedWindow->assertNoEvents();
7338 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007339 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007340}
7341
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007342/**
7343 * If we have no focused window, and a key comes in, we start the ANR timer.
7344 * The focused application should add a focused window before the timer runs out to prevent ANR.
7345 *
7346 * If the user touches another application during this time, the key should be dropped.
7347 * Next, if a new focused window comes in, without toggling the focused application,
7348 * then no ANR should occur.
7349 *
7350 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7351 * but in some cases the policy may not update the focused application.
7352 */
7353TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7354 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7355 std::make_shared<FakeApplicationHandle>();
7356 focusedApplication->setDispatchingTimeout(60ms);
7357 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7358 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7359 mFocusedWindow->setFocusable(false);
7360
7361 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7362 mFocusedWindow->consumeFocusEvent(false);
7363
7364 // Send a key. The ANR timer should start because there is no focused window.
7365 // 'focusedApplication' will get blamed if this timer completes.
7366 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007367 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007368 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7369 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7370 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007372
7373 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7374 // then the injected touches won't cause the focused event to get dropped.
7375 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7376 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7377 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7378 // For this test, it means that the key would get delivered to the window once it becomes
7379 // focused.
7380 std::this_thread::sleep_for(10ms);
7381
7382 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007383 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7384 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7385 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007386
7387 // We do not consume the motion right away, because that would require dispatcher to first
7388 // process (== drop) the key event, and by that time, ANR will be raised.
7389 // Set the focused window first.
7390 mFocusedWindow->setFocusable(true);
7391 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7392 setFocusedWindow(mFocusedWindow);
7393 mFocusedWindow->consumeFocusEvent(true);
7394 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7395 // to another application. This could be a bug / behaviour in the policy.
7396
7397 mUnfocusedWindow->consumeMotionDown();
7398
7399 ASSERT_TRUE(mDispatcher->waitForIdle());
7400 // Should not ANR because we actually have a focused window. It was just added too slowly.
7401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7402}
7403
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007404// These tests ensure we cannot send touch events to a window that's positioned behind a window
7405// that has feature NO_INPUT_CHANNEL.
7406// Layout:
7407// Top (closest to user)
7408// mNoInputWindow (above all windows)
7409// mBottomWindow
7410// Bottom (furthest from user)
7411class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7412 virtual void SetUp() override {
7413 InputDispatcherTest::SetUp();
7414
7415 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007416 mNoInputWindow =
7417 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7418 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007419 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007420 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007421 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7422 // It's perfectly valid for this window to not have an associated input channel
7423
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007424 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7425 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007426 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7427
7428 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7429 }
7430
7431protected:
7432 std::shared_ptr<FakeApplicationHandle> mApplication;
7433 sp<FakeWindowHandle> mNoInputWindow;
7434 sp<FakeWindowHandle> mBottomWindow;
7435};
7436
7437TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7438 PointF touchedPoint = {10, 10};
7439
Prabir Pradhan678438e2023-04-13 19:32:51 +00007440 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7441 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7442 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007443
7444 mNoInputWindow->assertNoEvents();
7445 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7446 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7447 // and therefore should prevent mBottomWindow from receiving touches
7448 mBottomWindow->assertNoEvents();
7449}
7450
7451/**
7452 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7453 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7454 */
7455TEST_F(InputDispatcherMultiWindowOcclusionTests,
7456 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007457 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7458 "Window with input channel and NO_INPUT_CHANNEL",
7459 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007460
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007461 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007462 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7463 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7464
7465 PointF touchedPoint = {10, 10};
7466
Prabir Pradhan678438e2023-04-13 19:32:51 +00007467 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7468 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7469 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007470
7471 mNoInputWindow->assertNoEvents();
7472 mBottomWindow->assertNoEvents();
7473}
7474
Vishnu Nair958da932020-08-21 17:12:37 -07007475class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7476protected:
7477 std::shared_ptr<FakeApplicationHandle> mApp;
7478 sp<FakeWindowHandle> mWindow;
7479 sp<FakeWindowHandle> mMirror;
7480
7481 virtual void SetUp() override {
7482 InputDispatcherTest::SetUp();
7483 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007484 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7485 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7486 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007487 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7488 mWindow->setFocusable(true);
7489 mMirror->setFocusable(true);
7490 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7491 }
7492};
7493
7494TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7495 // Request focus on a mirrored window
7496 setFocusedWindow(mMirror);
7497
7498 // window gets focused
7499 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7501 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007502 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7503}
7504
7505// A focused & mirrored window remains focused only if the window and its mirror are both
7506// focusable.
7507TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7508 setFocusedWindow(mMirror);
7509
7510 // window gets focused
7511 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007512 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7513 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007514 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007515 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7516 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007517 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7518
7519 mMirror->setFocusable(false);
7520 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7521
7522 // window loses focus since one of the windows associated with the token in not focusable
7523 mWindow->consumeFocusEvent(false);
7524
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007525 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7526 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007527 mWindow->assertNoEvents();
7528}
7529
7530// A focused & mirrored window remains focused until the window and its mirror both become
7531// invisible.
7532TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7533 setFocusedWindow(mMirror);
7534
7535 // window gets focused
7536 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007537 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7538 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007539 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007540 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7541 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007542 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7543
7544 mMirror->setVisible(false);
7545 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7546
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007547 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7548 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007549 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007550 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7551 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007552 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7553
7554 mWindow->setVisible(false);
7555 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7556
7557 // window loses focus only after all windows associated with the token become invisible.
7558 mWindow->consumeFocusEvent(false);
7559
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007560 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7561 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007562 mWindow->assertNoEvents();
7563}
7564
7565// A focused & mirrored window remains focused until both windows are removed.
7566TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7567 setFocusedWindow(mMirror);
7568
7569 // window gets focused
7570 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007571 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7572 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007573 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007574 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7575 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007576 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7577
7578 // single window is removed but the window token remains focused
7579 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7580
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007581 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7582 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007583 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007584 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7585 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007586 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7587
7588 // Both windows are removed
7589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7590 mWindow->consumeFocusEvent(false);
7591
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007592 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7593 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007594 mWindow->assertNoEvents();
7595}
7596
7597// Focus request can be pending until one window becomes visible.
7598TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7599 // Request focus on an invisible mirror.
7600 mWindow->setVisible(false);
7601 mMirror->setVisible(false);
7602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7603 setFocusedWindow(mMirror);
7604
7605 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007606 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007607 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7608 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007609
7610 mMirror->setVisible(true);
7611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7612
7613 // window gets focused
7614 mWindow->consumeFocusEvent(true);
7615 // window gets the pending key event
7616 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7617}
Prabir Pradhan99987712020-11-10 18:43:05 -08007618
7619class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7620protected:
7621 std::shared_ptr<FakeApplicationHandle> mApp;
7622 sp<FakeWindowHandle> mWindow;
7623 sp<FakeWindowHandle> mSecondWindow;
7624
7625 void SetUp() override {
7626 InputDispatcherTest::SetUp();
7627 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007628 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007629 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007630 mSecondWindow =
7631 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007632 mSecondWindow->setFocusable(true);
7633
7634 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7635 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7636
7637 setFocusedWindow(mWindow);
7638 mWindow->consumeFocusEvent(true);
7639 }
7640
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007641 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007642 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007643 }
7644
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007645 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7646 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007647 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007648 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7649 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007650 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007651 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007652 }
7653};
7654
7655TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7656 // Ensure that capture cannot be obtained for unfocused windows.
7657 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7658 mFakePolicy->assertSetPointerCaptureNotCalled();
7659 mSecondWindow->assertNoEvents();
7660
7661 // Ensure that capture can be enabled from the focus window.
7662 requestAndVerifyPointerCapture(mWindow, true);
7663
7664 // Ensure that capture cannot be disabled from a window that does not have capture.
7665 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7666 mFakePolicy->assertSetPointerCaptureNotCalled();
7667
7668 // Ensure that capture can be disabled from the window with capture.
7669 requestAndVerifyPointerCapture(mWindow, false);
7670}
7671
7672TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007673 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007674
7675 setFocusedWindow(mSecondWindow);
7676
7677 // Ensure that the capture disabled event was sent first.
7678 mWindow->consumeCaptureEvent(false);
7679 mWindow->consumeFocusEvent(false);
7680 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007681 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007682
7683 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007684 notifyPointerCaptureChanged({});
7685 notifyPointerCaptureChanged(request);
7686 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007687 mWindow->assertNoEvents();
7688 mSecondWindow->assertNoEvents();
7689 mFakePolicy->assertSetPointerCaptureNotCalled();
7690}
7691
7692TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007693 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007694
7695 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007696 notifyPointerCaptureChanged({});
7697 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007698
7699 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007700 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007701 mWindow->consumeCaptureEvent(false);
7702 mWindow->assertNoEvents();
7703}
7704
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007705TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7706 requestAndVerifyPointerCapture(mWindow, true);
7707
7708 // The first window loses focus.
7709 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007710 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007711 mWindow->consumeCaptureEvent(false);
7712
7713 // Request Pointer Capture from the second window before the notification from InputReader
7714 // arrives.
7715 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007716 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007717
7718 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007719 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007720
7721 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007722 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007723
7724 mSecondWindow->consumeFocusEvent(true);
7725 mSecondWindow->consumeCaptureEvent(true);
7726}
7727
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007728TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7729 // App repeatedly enables and disables capture.
7730 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7731 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7732 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7733 mFakePolicy->assertSetPointerCaptureCalled(false);
7734 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7735 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7736
7737 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7738 // first request is now stale, this should do nothing.
7739 notifyPointerCaptureChanged(firstRequest);
7740 mWindow->assertNoEvents();
7741
7742 // InputReader notifies that the second request was enabled.
7743 notifyPointerCaptureChanged(secondRequest);
7744 mWindow->consumeCaptureEvent(true);
7745}
7746
Prabir Pradhan7092e262022-05-03 16:51:09 +00007747TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7748 requestAndVerifyPointerCapture(mWindow, true);
7749
7750 // App toggles pointer capture off and on.
7751 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7752 mFakePolicy->assertSetPointerCaptureCalled(false);
7753
7754 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7755 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7756
7757 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7758 // preceding "disable" request.
7759 notifyPointerCaptureChanged(enableRequest);
7760
7761 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7762 // any notifications.
7763 mWindow->assertNoEvents();
7764}
7765
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007766class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7767protected:
7768 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007769
7770 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7771 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7772
7773 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7774 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7775
7776 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7777 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7778 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7779 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7780 MAXIMUM_OBSCURING_OPACITY);
7781
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007782 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007783 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007784 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007785
7786 sp<FakeWindowHandle> mTouchWindow;
7787
7788 virtual void SetUp() override {
7789 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007790 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007791 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7792 }
7793
7794 virtual void TearDown() override {
7795 InputDispatcherTest::TearDown();
7796 mTouchWindow.clear();
7797 }
7798
chaviw3277faf2021-05-19 16:45:23 -05007799 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7800 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007801 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007802 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007803 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007804 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007805 return window;
7806 }
7807
7808 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7809 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7810 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007811 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007812 // Generate an arbitrary PID based on the UID
7813 window->setOwnerInfo(1777 + (uid % 10000), uid);
7814 return window;
7815 }
7816
7817 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007818 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7819 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7820 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007821 }
7822};
7823
7824TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007825 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007826 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007827 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007828
7829 touch();
7830
7831 mTouchWindow->assertNoEvents();
7832}
7833
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007834TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007835 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7836 const sp<FakeWindowHandle>& w =
7837 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7838 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7839
7840 touch();
7841
7842 mTouchWindow->assertNoEvents();
7843}
7844
7845TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007846 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7847 const sp<FakeWindowHandle>& w =
7848 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7849 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7850
7851 touch();
7852
7853 w->assertNoEvents();
7854}
7855
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007856TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007857 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7858 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007859
7860 touch();
7861
7862 mTouchWindow->consumeAnyMotionDown();
7863}
7864
7865TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007866 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007867 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007868 w->setFrame(Rect(0, 0, 50, 50));
7869 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007870
7871 touch({PointF{100, 100}});
7872
7873 mTouchWindow->consumeAnyMotionDown();
7874}
7875
7876TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007877 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007878 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007879 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7880
7881 touch();
7882
7883 mTouchWindow->consumeAnyMotionDown();
7884}
7885
7886TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7887 const sp<FakeWindowHandle>& w =
7888 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7889 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007890
7891 touch();
7892
7893 mTouchWindow->consumeAnyMotionDown();
7894}
7895
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007896TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7897 const sp<FakeWindowHandle>& w =
7898 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7900
7901 touch();
7902
7903 w->assertNoEvents();
7904}
7905
7906/**
7907 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7908 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7909 * window, the occluding window will still receive ACTION_OUTSIDE event.
7910 */
7911TEST_F(InputDispatcherUntrustedTouchesTest,
7912 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7913 const sp<FakeWindowHandle>& w =
7914 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007915 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007916 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7917
7918 touch();
7919
7920 w->consumeMotionOutside();
7921}
7922
7923TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
7924 const sp<FakeWindowHandle>& w =
7925 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007926 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007927 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7928
7929 touch();
7930
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08007931 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007932}
7933
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007934TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007935 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007936 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7937 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007938 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7939
7940 touch();
7941
7942 mTouchWindow->consumeAnyMotionDown();
7943}
7944
7945TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
7946 const sp<FakeWindowHandle>& w =
7947 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7948 MAXIMUM_OBSCURING_OPACITY);
7949 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007950
7951 touch();
7952
7953 mTouchWindow->consumeAnyMotionDown();
7954}
7955
7956TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007957 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007958 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
7959 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007960 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7961
7962 touch();
7963
7964 mTouchWindow->assertNoEvents();
7965}
7966
7967TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
7968 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
7969 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007970 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7971 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007972 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007973 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7974 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007975 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7976
7977 touch();
7978
7979 mTouchWindow->assertNoEvents();
7980}
7981
7982TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
7983 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
7984 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007985 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
7986 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007987 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00007988 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
7989 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007990 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
7991
7992 touch();
7993
7994 mTouchWindow->consumeAnyMotionDown();
7995}
7996
7997TEST_F(InputDispatcherUntrustedTouchesTest,
7998 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
7999 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008000 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8001 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008002 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008003 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8004 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008005 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8006
8007 touch();
8008
8009 mTouchWindow->consumeAnyMotionDown();
8010}
8011
8012TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8013 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008014 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8015 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008016 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008017 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8018 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008019 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008020
8021 touch();
8022
8023 mTouchWindow->assertNoEvents();
8024}
8025
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008026TEST_F(InputDispatcherUntrustedTouchesTest,
8027 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8028 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008029 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8030 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008031 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008032 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8033 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008034 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8035
8036 touch();
8037
8038 mTouchWindow->assertNoEvents();
8039}
8040
8041TEST_F(InputDispatcherUntrustedTouchesTest,
8042 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8043 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008044 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8045 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008046 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008047 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8048 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008049 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8050
8051 touch();
8052
8053 mTouchWindow->consumeAnyMotionDown();
8054}
8055
8056TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8057 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008058 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8059 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008060 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8061
8062 touch();
8063
8064 mTouchWindow->consumeAnyMotionDown();
8065}
8066
8067TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8068 const sp<FakeWindowHandle>& w =
8069 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8070 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8071
8072 touch();
8073
8074 mTouchWindow->consumeAnyMotionDown();
8075}
8076
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008077TEST_F(InputDispatcherUntrustedTouchesTest,
8078 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8079 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8080 const sp<FakeWindowHandle>& w =
8081 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8082 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8083
8084 touch();
8085
8086 mTouchWindow->assertNoEvents();
8087}
8088
8089TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8090 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8091 const sp<FakeWindowHandle>& w =
8092 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8093 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8094
8095 touch();
8096
8097 mTouchWindow->consumeAnyMotionDown();
8098}
8099
8100TEST_F(InputDispatcherUntrustedTouchesTest,
8101 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8102 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8103 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008104 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8105 OPACITY_ABOVE_THRESHOLD);
8106 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8107
8108 touch();
8109
8110 mTouchWindow->consumeAnyMotionDown();
8111}
8112
8113TEST_F(InputDispatcherUntrustedTouchesTest,
8114 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8115 const sp<FakeWindowHandle>& w1 =
8116 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8117 OPACITY_BELOW_THRESHOLD);
8118 const sp<FakeWindowHandle>& w2 =
8119 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8120 OPACITY_BELOW_THRESHOLD);
8121 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8122
8123 touch();
8124
8125 mTouchWindow->assertNoEvents();
8126}
8127
8128/**
8129 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8130 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8131 * (which alone would result in allowing touches) does not affect the blocking behavior.
8132 */
8133TEST_F(InputDispatcherUntrustedTouchesTest,
8134 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8135 const sp<FakeWindowHandle>& wB =
8136 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8137 OPACITY_BELOW_THRESHOLD);
8138 const sp<FakeWindowHandle>& wC =
8139 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8140 OPACITY_BELOW_THRESHOLD);
8141 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8142
8143 touch();
8144
8145 mTouchWindow->assertNoEvents();
8146}
8147
8148/**
8149 * This test is testing that a window from a different UID but with same application token doesn't
8150 * block the touch. Apps can share the application token for close UI collaboration for example.
8151 */
8152TEST_F(InputDispatcherUntrustedTouchesTest,
8153 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8154 const sp<FakeWindowHandle>& w =
8155 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8156 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008157 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8158
8159 touch();
8160
8161 mTouchWindow->consumeAnyMotionDown();
8162}
8163
arthurhungb89ccb02020-12-30 16:19:01 +08008164class InputDispatcherDragTests : public InputDispatcherTest {
8165protected:
8166 std::shared_ptr<FakeApplicationHandle> mApp;
8167 sp<FakeWindowHandle> mWindow;
8168 sp<FakeWindowHandle> mSecondWindow;
8169 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008170 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008171 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8172 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008173
8174 void SetUp() override {
8175 InputDispatcherTest::SetUp();
8176 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008177 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008178 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008179
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008180 mSecondWindow =
8181 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008182 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008183
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008184 mSpyWindow =
8185 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008186 mSpyWindow->setSpy(true);
8187 mSpyWindow->setTrustedOverlay(true);
8188 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8189
arthurhungb89ccb02020-12-30 16:19:01 +08008190 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008191 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008192 }
8193
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008194 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8195 switch (fromSource) {
8196 case AINPUT_SOURCE_TOUCHSCREEN:
8197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8198 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8199 ADISPLAY_ID_DEFAULT, {50, 50}))
8200 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8201 break;
8202 case AINPUT_SOURCE_STYLUS:
8203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8204 injectMotionEvent(
8205 mDispatcher,
8206 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8207 AINPUT_SOURCE_STYLUS)
8208 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008209 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008210 .x(50)
8211 .y(50))
8212 .build()));
8213 break;
8214 case AINPUT_SOURCE_MOUSE:
8215 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8216 injectMotionEvent(
8217 mDispatcher,
8218 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8219 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8220 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008221 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008222 .x(50)
8223 .y(50))
8224 .build()));
8225 break;
8226 default:
8227 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8228 }
arthurhungb89ccb02020-12-30 16:19:01 +08008229
8230 // Window should receive motion event.
8231 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008232 // Spy window should also receive motion event
8233 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008234 }
8235
8236 // Start performing drag, we will create a drag window and transfer touch to it.
8237 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8238 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008239 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008240 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008241 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008242 }
arthurhungb89ccb02020-12-30 16:19:01 +08008243
8244 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008245 mDragWindow =
8246 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008247 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008248 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008249 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008250
8251 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008252 bool transferred =
8253 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008254 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008255 if (transferred) {
8256 mWindow->consumeMotionCancel();
8257 mDragWindow->consumeMotionDown();
8258 }
8259 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008260 }
8261};
8262
8263TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008264 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008265
8266 // Move on window.
8267 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8268 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8269 ADISPLAY_ID_DEFAULT, {50, 50}))
8270 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8271 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8272 mWindow->consumeDragEvent(false, 50, 50);
8273 mSecondWindow->assertNoEvents();
8274
8275 // Move to another window.
8276 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8277 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8278 ADISPLAY_ID_DEFAULT, {150, 50}))
8279 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8280 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8281 mWindow->consumeDragEvent(true, 150, 50);
8282 mSecondWindow->consumeDragEvent(false, 50, 50);
8283
8284 // Move back to original window.
8285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8286 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8287 ADISPLAY_ID_DEFAULT, {50, 50}))
8288 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8289 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8290 mWindow->consumeDragEvent(false, 50, 50);
8291 mSecondWindow->consumeDragEvent(true, -50, 50);
8292
8293 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8294 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8295 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8296 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8297 mWindow->assertNoEvents();
8298 mSecondWindow->assertNoEvents();
8299}
8300
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008301TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008302 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008303
8304 // No cancel event after drag start
8305 mSpyWindow->assertNoEvents();
8306
8307 const MotionEvent secondFingerDownEvent =
8308 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8309 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008310 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8311 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008312 .build();
8313 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8314 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8315 InputEventInjectionSync::WAIT_FOR_RESULT))
8316 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8317
8318 // Receives cancel for first pointer after next pointer down
8319 mSpyWindow->consumeMotionCancel();
8320 mSpyWindow->consumeMotionDown();
8321
8322 mSpyWindow->assertNoEvents();
8323}
8324
arthurhungf452d0b2021-01-06 00:19:52 +08008325TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008326 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008327
8328 // Move on window.
8329 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8330 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8331 ADISPLAY_ID_DEFAULT, {50, 50}))
8332 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8333 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8334 mWindow->consumeDragEvent(false, 50, 50);
8335 mSecondWindow->assertNoEvents();
8336
8337 // Move to another window.
8338 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8339 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8340 ADISPLAY_ID_DEFAULT, {150, 50}))
8341 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8342 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8343 mWindow->consumeDragEvent(true, 150, 50);
8344 mSecondWindow->consumeDragEvent(false, 50, 50);
8345
8346 // drop to another window.
8347 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8348 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8349 {150, 50}))
8350 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8351 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8352 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8353 mWindow->assertNoEvents();
8354 mSecondWindow->assertNoEvents();
8355}
8356
arthurhung6d4bed92021-03-17 11:59:33 +08008357TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008358 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008359
8360 // Move on window and keep button pressed.
8361 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8362 injectMotionEvent(mDispatcher,
8363 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8364 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008365 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008366 .build()))
8367 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8368 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8369 mWindow->consumeDragEvent(false, 50, 50);
8370 mSecondWindow->assertNoEvents();
8371
8372 // Move to another window and release button, expect to drop item.
8373 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8374 injectMotionEvent(mDispatcher,
8375 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8376 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008377 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008378 .build()))
8379 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8380 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8381 mWindow->assertNoEvents();
8382 mSecondWindow->assertNoEvents();
8383 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8384
8385 // nothing to the window.
8386 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8387 injectMotionEvent(mDispatcher,
8388 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8389 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008390 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008391 .build()))
8392 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8393 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8394 mWindow->assertNoEvents();
8395 mSecondWindow->assertNoEvents();
8396}
8397
Arthur Hung54745652022-04-20 07:17:41 +00008398TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008399 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008400
8401 // Set second window invisible.
8402 mSecondWindow->setVisible(false);
8403 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8404
8405 // Move on window.
8406 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8407 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8408 ADISPLAY_ID_DEFAULT, {50, 50}))
8409 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8410 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8411 mWindow->consumeDragEvent(false, 50, 50);
8412 mSecondWindow->assertNoEvents();
8413
8414 // Move to another window.
8415 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8416 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8417 ADISPLAY_ID_DEFAULT, {150, 50}))
8418 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8419 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8420 mWindow->consumeDragEvent(true, 150, 50);
8421 mSecondWindow->assertNoEvents();
8422
8423 // drop to another window.
8424 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8425 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8426 {150, 50}))
8427 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8428 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8429 mFakePolicy->assertDropTargetEquals(nullptr);
8430 mWindow->assertNoEvents();
8431 mSecondWindow->assertNoEvents();
8432}
8433
Arthur Hung54745652022-04-20 07:17:41 +00008434TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008435 // Ensure window could track pointerIds if it didn't support split touch.
8436 mWindow->setPreventSplitting(true);
8437
Arthur Hung54745652022-04-20 07:17:41 +00008438 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8439 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8440 {50, 50}))
8441 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8442 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8443
8444 const MotionEvent secondFingerDownEvent =
8445 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8446 .displayId(ADISPLAY_ID_DEFAULT)
8447 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008448 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8449 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008450 .build();
8451 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8452 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8453 InputEventInjectionSync::WAIT_FOR_RESULT))
8454 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008455 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008456
8457 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008458 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008459}
8460
8461TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8462 // First down on second window.
8463 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8464 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8465 {150, 50}))
8466 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8467
8468 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8469
8470 // Second down on first window.
8471 const MotionEvent secondFingerDownEvent =
8472 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8473 .displayId(ADISPLAY_ID_DEFAULT)
8474 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008475 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8476 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008477 .build();
8478 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8479 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8480 InputEventInjectionSync::WAIT_FOR_RESULT))
8481 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8482 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8483
8484 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008485 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008486
8487 // Move on window.
8488 const MotionEvent secondFingerMoveEvent =
8489 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8490 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008491 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8492 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008493 .build();
8494 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8495 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8496 InputEventInjectionSync::WAIT_FOR_RESULT));
8497 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8498 mWindow->consumeDragEvent(false, 50, 50);
8499 mSecondWindow->consumeMotionMove();
8500
8501 // Release the drag pointer should perform drop.
8502 const MotionEvent secondFingerUpEvent =
8503 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8504 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008505 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8506 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008507 .build();
8508 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8509 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8510 InputEventInjectionSync::WAIT_FOR_RESULT));
8511 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8512 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8513 mWindow->assertNoEvents();
8514 mSecondWindow->consumeMotionMove();
8515}
8516
Arthur Hung3915c1f2022-05-31 07:17:17 +00008517TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008518 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008519
8520 // Update window of second display.
8521 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008522 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008523 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8524
8525 // Let second display has a touch state.
8526 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8527 injectMotionEvent(mDispatcher,
8528 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8529 AINPUT_SOURCE_TOUCHSCREEN)
8530 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008531 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008532 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008533 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008534 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008535 // Update window again.
8536 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8537
8538 // Move on window.
8539 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8540 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8541 ADISPLAY_ID_DEFAULT, {50, 50}))
8542 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8543 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8544 mWindow->consumeDragEvent(false, 50, 50);
8545 mSecondWindow->assertNoEvents();
8546
8547 // Move to another window.
8548 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8549 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8550 ADISPLAY_ID_DEFAULT, {150, 50}))
8551 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8552 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8553 mWindow->consumeDragEvent(true, 150, 50);
8554 mSecondWindow->consumeDragEvent(false, 50, 50);
8555
8556 // drop to another window.
8557 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8558 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8559 {150, 50}))
8560 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8561 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8562 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8563 mWindow->assertNoEvents();
8564 mSecondWindow->assertNoEvents();
8565}
8566
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008567TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8568 startDrag(true, AINPUT_SOURCE_MOUSE);
8569 // Move on window.
8570 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8571 injectMotionEvent(mDispatcher,
8572 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8573 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8574 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008575 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008576 .x(50)
8577 .y(50))
8578 .build()))
8579 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8580 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8581 mWindow->consumeDragEvent(false, 50, 50);
8582 mSecondWindow->assertNoEvents();
8583
8584 // Move to another window.
8585 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8586 injectMotionEvent(mDispatcher,
8587 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8588 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8589 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008590 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008591 .x(150)
8592 .y(50))
8593 .build()))
8594 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8595 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8596 mWindow->consumeDragEvent(true, 150, 50);
8597 mSecondWindow->consumeDragEvent(false, 50, 50);
8598
8599 // drop to another window.
8600 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8601 injectMotionEvent(mDispatcher,
8602 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8603 .buttonState(0)
8604 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008605 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008606 .x(150)
8607 .y(50))
8608 .build()))
8609 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8610 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8611 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8612 mWindow->assertNoEvents();
8613 mSecondWindow->assertNoEvents();
8614}
8615
Vishnu Nair062a8672021-09-03 16:07:44 -07008616class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8617
8618TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8619 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008620 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8621 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008622 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008623 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8624 window->setFocusable(true);
8625 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8626 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008627 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008628
8629 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008630 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008631 window->assertNoEvents();
8632
Prabir Pradhan678438e2023-04-13 19:32:51 +00008633 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8634 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008635 window->assertNoEvents();
8636
8637 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008638 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008639 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8640
Prabir Pradhan678438e2023-04-13 19:32:51 +00008641 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008642 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8643
Prabir Pradhan678438e2023-04-13 19:32:51 +00008644 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8645 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008646 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8647 window->assertNoEvents();
8648}
8649
8650TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8651 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8652 std::make_shared<FakeApplicationHandle>();
8653 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008654 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8655 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008656 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8657 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008658 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008659 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->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008663 window->setOwnerInfo(222, 222);
8664 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8665 window->setFocusable(true);
8666 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8667 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008668 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008669
8670 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008671 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008672 window->assertNoEvents();
8673
Prabir Pradhan678438e2023-04-13 19:32:51 +00008674 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8675 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008676 window->assertNoEvents();
8677
8678 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008679 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008680 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8681
Prabir Pradhan678438e2023-04-13 19:32:51 +00008682 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008683 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8684
Prabir Pradhan678438e2023-04-13 19:32:51 +00008685 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8686 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008687 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8688 window->assertNoEvents();
8689}
8690
8691TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8692 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8693 std::make_shared<FakeApplicationHandle>();
8694 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008695 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8696 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008697 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8698 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008699 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008700 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008701 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8702 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008703 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008704 window->setOwnerInfo(222, 222);
8705 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8706 window->setFocusable(true);
8707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8708 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008709 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008710
8711 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008712 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008713 window->assertNoEvents();
8714
Prabir Pradhan678438e2023-04-13 19:32:51 +00008715 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8716 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008717 window->assertNoEvents();
8718
8719 // When the window is no longer obscured because it went on top, it should get input
8720 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
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);
8728 window->assertNoEvents();
8729}
8730
Antonio Kantekf16f2832021-09-28 04:39:20 +00008731class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8732protected:
8733 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008734 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008735 sp<FakeWindowHandle> mWindow;
8736 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008737 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008738
8739 void SetUp() override {
8740 InputDispatcherTest::SetUp();
8741
8742 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008743 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008744 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008745 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008746 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008747 mSecondWindow =
8748 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008749 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008750 mThirdWindow =
8751 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8752 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8753 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008754
8755 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008756 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8757 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8758 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008759 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008760
Antonio Kantek15beb512022-06-13 22:35:41 +00008761 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008762 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008763 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008764 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8765 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008766 mThirdWindow->assertNoEvents();
8767 }
8768
8769 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8770 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008771 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008772 SECOND_DISPLAY_ID)) {
8773 mWindow->assertNoEvents();
8774 mSecondWindow->assertNoEvents();
8775 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008776 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008777 }
8778
Antonio Kantek15beb512022-06-13 22:35:41 +00008779 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8780 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008781 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8782 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008783 mWindow->consumeTouchModeEvent(inTouchMode);
8784 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008785 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008786 }
8787};
8788
Antonio Kantek26defcf2022-02-08 01:12:27 +00008789TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008790 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008791 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8792 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008793 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008794}
8795
Antonio Kantek26defcf2022-02-08 01:12:27 +00008796TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8797 const WindowInfo& windowInfo = *mWindow->getInfo();
8798 int32_t ownerPid = windowInfo.ownerPid;
8799 int32_t ownerUid = windowInfo.ownerUid;
8800 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8801 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008802 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008803 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008804 mWindow->assertNoEvents();
8805 mSecondWindow->assertNoEvents();
8806}
8807
8808TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8809 const WindowInfo& windowInfo = *mWindow->getInfo();
8810 int32_t ownerPid = windowInfo.ownerPid;
8811 int32_t ownerUid = windowInfo.ownerUid;
8812 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008813 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008814 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008815}
8816
Antonio Kantekf16f2832021-09-28 04:39:20 +00008817TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008818 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008819 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8820 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008821 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008822 mWindow->assertNoEvents();
8823 mSecondWindow->assertNoEvents();
8824}
8825
Antonio Kantek15beb512022-06-13 22:35:41 +00008826TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8827 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8828 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8829 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008830 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008831 mWindow->assertNoEvents();
8832 mSecondWindow->assertNoEvents();
8833 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8834}
8835
Antonio Kantek48710e42022-03-24 14:19:30 -07008836TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8837 // Interact with the window first.
8838 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8839 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8840 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8841
8842 // Then remove focus.
8843 mWindow->setFocusable(false);
8844 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8845
8846 // Assert that caller can switch touch mode by owning one of the last interacted window.
8847 const WindowInfo& windowInfo = *mWindow->getInfo();
8848 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8849 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008850 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008851}
8852
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008853class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8854public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008855 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008856 std::shared_ptr<FakeApplicationHandle> application =
8857 std::make_shared<FakeApplicationHandle>();
8858 std::string name = "Fake Spy ";
8859 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008860 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8861 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008862 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008863 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008864 return spy;
8865 }
8866
8867 sp<FakeWindowHandle> createForeground() {
8868 std::shared_ptr<FakeApplicationHandle> application =
8869 std::make_shared<FakeApplicationHandle>();
8870 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008871 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8872 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008873 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008874 return window;
8875 }
8876
8877private:
8878 int mSpyCount{0};
8879};
8880
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008881using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008882/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008883 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8884 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008885TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8886 ScopedSilentDeath _silentDeath;
8887
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008888 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008889 spy->setTrustedOverlay(false);
8890 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8891 ".* not a trusted overlay");
8892}
8893
8894/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008895 * Input injection into a display with a spy window but no foreground windows should succeed.
8896 */
8897TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008898 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008899 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8900
8901 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8902 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8903 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8904 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8905}
8906
8907/**
8908 * Verify the order in which different input windows receive events. The touched foreground window
8909 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8910 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8911 * receive events before ones belows it.
8912 *
8913 * Here, we set up a scenario with four windows in the following Z order from the top:
8914 * spy1, spy2, window, spy3.
8915 * We then inject an event and verify that the foreground "window" receives it first, followed by
8916 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8917 * window.
8918 */
8919TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
8920 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008921 auto spy1 = createSpy();
8922 auto spy2 = createSpy();
8923 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008924 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
8925 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
8926 const size_t numChannels = channels.size();
8927
Michael Wright8e9a8562022-02-09 13:44:29 +00008928 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008929 if (!epollFd.ok()) {
8930 FAIL() << "Failed to create epoll fd";
8931 }
8932
8933 for (size_t i = 0; i < numChannels; i++) {
8934 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
8935 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
8936 FAIL() << "Failed to add fd to epoll";
8937 }
8938 }
8939
8940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8941 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8942 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8943
8944 std::vector<size_t> eventOrder;
8945 std::vector<struct epoll_event> events(numChannels);
8946 for (;;) {
8947 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
8948 (100ms).count());
8949 if (nFds < 0) {
8950 FAIL() << "Failed to call epoll_wait";
8951 }
8952 if (nFds == 0) {
8953 break; // epoll_wait timed out
8954 }
8955 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07008956 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07008957 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008958 channels[i]->consumeMotionDown();
8959 }
8960 }
8961
8962 // Verify the order in which the events were received.
8963 EXPECT_EQ(3u, eventOrder.size());
8964 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
8965 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
8966 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
8967}
8968
8969/**
8970 * A spy window using the NOT_TOUCHABLE flag does not receive events.
8971 */
8972TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
8973 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008974 auto spy = createSpy();
8975 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008976 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8977
8978 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8979 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8980 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8981 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8982 spy->assertNoEvents();
8983}
8984
8985/**
8986 * A spy window will only receive gestures that originate within its touchable region. Gestures that
8987 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
8988 * to the window.
8989 */
8990TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
8991 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008992 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008993 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
8994 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
8995
8996 // Inject an event outside the spy window's touchable region.
8997 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8998 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8999 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9000 window->consumeMotionDown();
9001 spy->assertNoEvents();
9002 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9003 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9004 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9005 window->consumeMotionUp();
9006 spy->assertNoEvents();
9007
9008 // Inject an event inside the spy window's touchable region.
9009 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9010 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9011 {5, 10}))
9012 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9013 window->consumeMotionDown();
9014 spy->consumeMotionDown();
9015}
9016
9017/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009018 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009019 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009020 */
9021TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9022 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009023 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009024 auto spy = createSpy();
9025 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009026 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009027 spy->setFrame(Rect{0, 0, 20, 20});
9028 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9029
9030 // Inject an event outside the spy window's frame and touchable region.
9031 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009032 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9033 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009034 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9035 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009036 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009037}
9038
9039/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009040 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9041 * pointers that are down within its bounds.
9042 */
9043TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9044 auto windowLeft = createForeground();
9045 windowLeft->setFrame({0, 0, 100, 200});
9046 auto windowRight = createForeground();
9047 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009048 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009049 spy->setFrame({0, 0, 200, 200});
9050 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9051
9052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9053 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9054 {50, 50}))
9055 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9056 windowLeft->consumeMotionDown();
9057 spy->consumeMotionDown();
9058
9059 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009060 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009061 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009062 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9063 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009064 .build();
9065 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9066 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9067 InputEventInjectionSync::WAIT_FOR_RESULT))
9068 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9069 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009070 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009071}
9072
9073/**
9074 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9075 * the spy should receive the second pointer with ACTION_DOWN.
9076 */
9077TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9078 auto window = createForeground();
9079 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009080 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009081 spyRight->setFrame({100, 0, 200, 200});
9082 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9083
9084 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9085 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9086 {50, 50}))
9087 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9088 window->consumeMotionDown();
9089 spyRight->assertNoEvents();
9090
9091 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009092 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009093 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009094 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9095 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009096 .build();
9097 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9098 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9099 InputEventInjectionSync::WAIT_FOR_RESULT))
9100 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009101 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009102 spyRight->consumeMotionDown();
9103}
9104
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009105/**
9106 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9107 * windows should be allowed to control split touch.
9108 */
9109TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009110 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009111 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009112 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009113 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009114
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009115 auto window = createForeground();
9116 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009117
9118 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9119
9120 // First finger down, no window touched.
9121 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9122 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9123 {100, 200}))
9124 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9125 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9126 window->assertNoEvents();
9127
9128 // Second finger down on window, the window should receive touch down.
9129 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009130 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009131 .displayId(ADISPLAY_ID_DEFAULT)
9132 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009133 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9134 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009135 .build();
9136 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9137 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9138 InputEventInjectionSync::WAIT_FOR_RESULT))
9139 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9140
9141 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009142 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009143}
9144
9145/**
9146 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9147 * do not receive key events.
9148 */
9149TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009150 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009151 spy->setFocusable(false);
9152
9153 auto window = createForeground();
9154 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9155 setFocusedWindow(window);
9156 window->consumeFocusEvent(true);
9157
9158 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9159 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9160 window->consumeKeyDown(ADISPLAY_ID_NONE);
9161
9162 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9163 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9164 window->consumeKeyUp(ADISPLAY_ID_NONE);
9165
9166 spy->assertNoEvents();
9167}
9168
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009169using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9170
9171/**
9172 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9173 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9174 */
9175TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9176 auto window = createForeground();
9177 auto spy1 = createSpy();
9178 auto spy2 = createSpy();
9179 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9180
9181 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9182 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9183 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9184 window->consumeMotionDown();
9185 spy1->consumeMotionDown();
9186 spy2->consumeMotionDown();
9187
9188 // Pilfer pointers from the second spy window.
9189 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9190 spy2->assertNoEvents();
9191 spy1->consumeMotionCancel();
9192 window->consumeMotionCancel();
9193
9194 // The rest of the gesture should only be sent to the second spy window.
9195 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9196 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9197 ADISPLAY_ID_DEFAULT))
9198 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9199 spy2->consumeMotionMove();
9200 spy1->assertNoEvents();
9201 window->assertNoEvents();
9202}
9203
9204/**
9205 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9206 * in the middle of the gesture.
9207 */
9208TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9209 auto window = createForeground();
9210 auto spy = createSpy();
9211 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9212
9213 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9214 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9215 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9216 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9217 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9218
9219 window->releaseChannel();
9220
9221 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9222
9223 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9224 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9225 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9226 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9227}
9228
9229/**
9230 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9231 * the spy, but not to any other windows.
9232 */
9233TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9234 auto spy = createSpy();
9235 auto window = createForeground();
9236
9237 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9238
9239 // First finger down on the window and the spy.
9240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9241 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9242 {100, 200}))
9243 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9244 spy->consumeMotionDown();
9245 window->consumeMotionDown();
9246
9247 // Spy window pilfers the pointers.
9248 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9249 window->consumeMotionCancel();
9250
9251 // Second finger down on the window and spy, but the window should not receive the pointer down.
9252 const MotionEvent secondFingerDownEvent =
9253 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9254 .displayId(ADISPLAY_ID_DEFAULT)
9255 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009256 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9257 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009258 .build();
9259 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9260 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9261 InputEventInjectionSync::WAIT_FOR_RESULT))
9262 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9263
Harry Cutts33476232023-01-30 19:57:29 +00009264 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009265
9266 // Third finger goes down outside all windows, so injection should fail.
9267 const MotionEvent thirdFingerDownEvent =
9268 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9269 .displayId(ADISPLAY_ID_DEFAULT)
9270 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009271 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9272 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9273 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009274 .build();
9275 ASSERT_EQ(InputEventInjectionResult::FAILED,
9276 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9277 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009278 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009279
9280 spy->assertNoEvents();
9281 window->assertNoEvents();
9282}
9283
9284/**
9285 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9286 */
9287TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9288 auto spy = createSpy();
9289 spy->setFrame(Rect(0, 0, 100, 100));
9290 auto window = createForeground();
9291 window->setFrame(Rect(0, 0, 200, 200));
9292
9293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9294
9295 // First finger down on the window only
9296 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9297 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9298 {150, 150}))
9299 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9300 window->consumeMotionDown();
9301
9302 // Second finger down on the spy and window
9303 const MotionEvent secondFingerDownEvent =
9304 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9305 .displayId(ADISPLAY_ID_DEFAULT)
9306 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009307 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9308 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009309 .build();
9310 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9311 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9312 InputEventInjectionSync::WAIT_FOR_RESULT))
9313 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9314 spy->consumeMotionDown();
9315 window->consumeMotionPointerDown(1);
9316
9317 // Third finger down on the spy and window
9318 const MotionEvent thirdFingerDownEvent =
9319 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9320 .displayId(ADISPLAY_ID_DEFAULT)
9321 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009322 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9323 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9324 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009325 .build();
9326 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9327 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9328 InputEventInjectionSync::WAIT_FOR_RESULT))
9329 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9330 spy->consumeMotionPointerDown(1);
9331 window->consumeMotionPointerDown(2);
9332
9333 // Spy window pilfers the pointers.
9334 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9335 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9336 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9337
9338 spy->assertNoEvents();
9339 window->assertNoEvents();
9340}
9341
9342/**
9343 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9344 * other windows should be canceled. If this results in the cancellation of all pointers for some
9345 * window, then that window should receive ACTION_CANCEL.
9346 */
9347TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9348 auto spy = createSpy();
9349 spy->setFrame(Rect(0, 0, 100, 100));
9350 auto window = createForeground();
9351 window->setFrame(Rect(0, 0, 200, 200));
9352
9353 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9354
9355 // First finger down on both spy and window
9356 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9357 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9358 {10, 10}))
9359 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9360 window->consumeMotionDown();
9361 spy->consumeMotionDown();
9362
9363 // Second finger down on the spy and window
9364 const MotionEvent secondFingerDownEvent =
9365 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9366 .displayId(ADISPLAY_ID_DEFAULT)
9367 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009368 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9369 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009370 .build();
9371 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9372 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9373 InputEventInjectionSync::WAIT_FOR_RESULT))
9374 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9375 spy->consumeMotionPointerDown(1);
9376 window->consumeMotionPointerDown(1);
9377
9378 // Spy window pilfers the pointers.
9379 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9380 window->consumeMotionCancel();
9381
9382 spy->assertNoEvents();
9383 window->assertNoEvents();
9384}
9385
9386/**
9387 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9388 * be sent to other windows
9389 */
9390TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9391 auto spy = createSpy();
9392 spy->setFrame(Rect(0, 0, 100, 100));
9393 auto window = createForeground();
9394 window->setFrame(Rect(0, 0, 200, 200));
9395
9396 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9397
9398 // First finger down on both window and spy
9399 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9400 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9401 {10, 10}))
9402 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9403 window->consumeMotionDown();
9404 spy->consumeMotionDown();
9405
9406 // Spy window pilfers the pointers.
9407 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9408 window->consumeMotionCancel();
9409
9410 // Second finger down on the window only
9411 const MotionEvent secondFingerDownEvent =
9412 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9413 .displayId(ADISPLAY_ID_DEFAULT)
9414 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009415 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9416 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009417 .build();
9418 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9419 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9420 InputEventInjectionSync::WAIT_FOR_RESULT))
9421 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9422 window->consumeMotionDown();
9423 window->assertNoEvents();
9424
9425 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9426 spy->consumeMotionMove();
9427 spy->assertNoEvents();
9428}
9429
Prabir Pradhand65552b2021-10-07 11:23:50 -07009430class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9431public:
9432 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9433 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9434 std::make_shared<FakeApplicationHandle>();
9435 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009436 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9437 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009438 overlay->setFocusable(false);
9439 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009440 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009441 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009442 overlay->setTrustedOverlay(true);
9443
9444 std::shared_ptr<FakeApplicationHandle> application =
9445 std::make_shared<FakeApplicationHandle>();
9446 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009447 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9448 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009449 window->setFocusable(true);
9450 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009451
9452 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9453 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9454 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009455 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009456 return {std::move(overlay), std::move(window)};
9457 }
9458
9459 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009460 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009461 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009462 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009463 }
9464
9465 void sendStylusEvent(int32_t action) {
9466 NotifyMotionArgs motionArgs =
9467 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9468 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009469 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009470 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009471 }
9472};
9473
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009474using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9475
9476TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9477 ScopedSilentDeath _silentDeath;
9478
Prabir Pradhand65552b2021-10-07 11:23:50 -07009479 auto [overlay, window] = setupStylusOverlayScenario();
9480 overlay->setTrustedOverlay(false);
9481 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9482 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9483 ".* not a trusted overlay");
9484}
9485
9486TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9487 auto [overlay, window] = setupStylusOverlayScenario();
9488 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9489
9490 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9491 overlay->consumeMotionDown();
9492 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9493 overlay->consumeMotionUp();
9494
9495 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9496 window->consumeMotionDown();
9497 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9498 window->consumeMotionUp();
9499
9500 overlay->assertNoEvents();
9501 window->assertNoEvents();
9502}
9503
9504TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9505 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009506 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009507 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9508
9509 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9510 overlay->consumeMotionDown();
9511 window->consumeMotionDown();
9512 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9513 overlay->consumeMotionUp();
9514 window->consumeMotionUp();
9515
9516 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9517 window->consumeMotionDown();
9518 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9519 window->consumeMotionUp();
9520
9521 overlay->assertNoEvents();
9522 window->assertNoEvents();
9523}
9524
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009525/**
9526 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9527 * The scenario is as follows:
9528 * - The stylus interceptor overlay is configured as a spy window.
9529 * - The stylus interceptor spy receives the start of a new stylus gesture.
9530 * - It pilfers pointers and then configures itself to no longer be a spy.
9531 * - The stylus interceptor continues to receive the rest of the gesture.
9532 */
9533TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9534 auto [overlay, window] = setupStylusOverlayScenario();
9535 overlay->setSpy(true);
9536 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9537
9538 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9539 overlay->consumeMotionDown();
9540 window->consumeMotionDown();
9541
9542 // The interceptor pilfers the pointers.
9543 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9544 window->consumeMotionCancel();
9545
9546 // The interceptor configures itself so that it is no longer a spy.
9547 overlay->setSpy(false);
9548 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9549
9550 // It continues to receive the rest of the stylus gesture.
9551 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9552 overlay->consumeMotionMove();
9553 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9554 overlay->consumeMotionUp();
9555
9556 window->assertNoEvents();
9557}
9558
Prabir Pradhan5735a322022-04-11 17:23:34 +00009559struct User {
9560 int32_t mPid;
9561 int32_t mUid;
9562 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9563 std::unique_ptr<InputDispatcher>& mDispatcher;
9564
9565 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9566 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9567
9568 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9569 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9570 ADISPLAY_ID_DEFAULT, {100, 200},
9571 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9572 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9573 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9574 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9575 }
9576
9577 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009578 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009579 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009580 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009581 mPolicyFlags);
9582 }
9583
9584 sp<FakeWindowHandle> createWindow() const {
9585 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9586 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009587 sp<FakeWindowHandle> window =
9588 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9589 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009590 window->setOwnerInfo(mPid, mUid);
9591 return window;
9592 }
9593};
9594
9595using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9596
9597TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9598 auto owner = User(mDispatcher, 10, 11);
9599 auto window = owner.createWindow();
9600 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9601
9602 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9603 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9604 window->consumeMotionDown();
9605
9606 setFocusedWindow(window);
9607 window->consumeFocusEvent(true);
9608
9609 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9610 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9611 window->consumeKeyDown(ADISPLAY_ID_NONE);
9612}
9613
9614TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9615 auto owner = User(mDispatcher, 10, 11);
9616 auto window = owner.createWindow();
9617 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9618
9619 auto rando = User(mDispatcher, 20, 21);
9620 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9621 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9622
9623 setFocusedWindow(window);
9624 window->consumeFocusEvent(true);
9625
9626 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9627 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9628 window->assertNoEvents();
9629}
9630
9631TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9632 auto owner = User(mDispatcher, 10, 11);
9633 auto window = owner.createWindow();
9634 auto spy = owner.createWindow();
9635 spy->setSpy(true);
9636 spy->setTrustedOverlay(true);
9637 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9638
9639 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9640 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9641 spy->consumeMotionDown();
9642 window->consumeMotionDown();
9643}
9644
9645TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9646 auto owner = User(mDispatcher, 10, 11);
9647 auto window = owner.createWindow();
9648
9649 auto rando = User(mDispatcher, 20, 21);
9650 auto randosSpy = rando.createWindow();
9651 randosSpy->setSpy(true);
9652 randosSpy->setTrustedOverlay(true);
9653 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9654
9655 // The event is targeted at owner's window, so injection should succeed, but the spy should
9656 // not receive the event.
9657 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9658 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9659 randosSpy->assertNoEvents();
9660 window->consumeMotionDown();
9661}
9662
9663TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9664 auto owner = User(mDispatcher, 10, 11);
9665 auto window = owner.createWindow();
9666
9667 auto rando = User(mDispatcher, 20, 21);
9668 auto randosSpy = rando.createWindow();
9669 randosSpy->setSpy(true);
9670 randosSpy->setTrustedOverlay(true);
9671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9672
9673 // A user that has injection permission can inject into any window.
9674 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9675 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9676 ADISPLAY_ID_DEFAULT));
9677 randosSpy->consumeMotionDown();
9678 window->consumeMotionDown();
9679
9680 setFocusedWindow(randosSpy);
9681 randosSpy->consumeFocusEvent(true);
9682
9683 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9684 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9685 window->assertNoEvents();
9686}
9687
9688TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9689 auto owner = User(mDispatcher, 10, 11);
9690 auto window = owner.createWindow();
9691
9692 auto rando = User(mDispatcher, 20, 21);
9693 auto randosWindow = rando.createWindow();
9694 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9695 randosWindow->setWatchOutsideTouch(true);
9696 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9697
9698 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9699 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9700 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9701 window->consumeMotionDown();
9702 randosWindow->consumeMotionOutside();
9703}
9704
Garfield Tane84e6f92019-08-29 17:28:41 -07009705} // namespace android::inputdispatcher