blob: 3660f81b92db3817f251d4944d914264a76e9fe5 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Garfield Tan0fc2fa72019-08-29 17:22:15 -070017#include "../dispatcher/InputDispatcher.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -070019#include <android-base/properties.h>
Prabir Pradhana3ab87a2022-01-27 10:00:21 -080020#include <android-base/silent_death_test.h>
Garfield Tan1c7bc862020-01-28 13:24:04 -080021#include <android-base/stringprintf.h>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070022#include <android-base/thread_annotations.h>
Robert Carr803535b2018-08-02 16:38:15 -070023#include <binder/Binder.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000024#include <fcntl.h>
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080025#include <gmock/gmock.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <gtest/gtest.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100027#include <input/Input.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <linux/input.h>
Prabir Pradhan07e05b62021-11-19 03:57:24 -080029#include <sys/epoll.h>
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -100030
Garfield Tan1c7bc862020-01-28 13:24:04 -080031#include <cinttypes>
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080032#include <compare>
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070033#include <thread>
Garfield Tan1c7bc862020-01-28 13:24:04 -080034#include <unordered_set>
chaviwd1c23182019-12-20 18:44:56 -080035#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
Garfield Tan1c7bc862020-01-28 13:24:04 -080037using android::base::StringPrintf;
chaviw3277faf2021-05-19 16:45:23 -050038using android::gui::FocusRequest;
39using android::gui::TouchOcclusionMode;
40using android::gui::WindowInfo;
41using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080042using android::os::InputEventInjectionResult;
43using android::os::InputEventInjectionSync;
Garfield Tan1c7bc862020-01-28 13:24:04 -080044
Garfield Tane84e6f92019-08-29 17:28:41 -070045namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070047using namespace ftl::flag_operators;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -080048using testing::AllOf;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070049
Michael Wrightd02c5b62014-02-10 15:10:22 -080050// An arbitrary time value.
Prabir Pradhan5735a322022-04-11 17:23:34 +000051static constexpr nsecs_t ARBITRARY_TIME = 1234;
Michael Wrightd02c5b62014-02-10 15:10:22 -080052
53// An arbitrary device id.
Prabir Pradhan5735a322022-04-11 17:23:34 +000054static constexpr int32_t DEVICE_ID = 1;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080055static constexpr int32_t SECOND_DEVICE_ID = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Jeff Brownf086ddb2014-02-11 14:28:48 -080057// An arbitrary display id.
Arthur Hungabbb9d82021-09-01 14:52:30 +000058static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
59static constexpr int32_t SECOND_DISPLAY_ID = 1;
Jeff Brownf086ddb2014-02-11 14:28:48 -080060
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080061static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN;
62static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE;
63static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP;
64static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -070065static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080066static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -080067static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -080068static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080069/**
70 * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the
71 * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at
72 * index 0) is the new pointer going down. The same pointer could have been placed at a different
73 * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In
74 * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if
75 * pointer id=0 leaves but the pointer id=1 remains.
76 */
77static constexpr int32_t POINTER_0_DOWN =
78 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080079static constexpr int32_t POINTER_1_DOWN =
80 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Prabir Pradhanb60b1dc2022-03-15 14:02:35 +000081static constexpr int32_t POINTER_2_DOWN =
82 AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000083static constexpr int32_t POINTER_3_DOWN =
84 AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Arthur Hungc539dbb2022-12-08 07:45:36 +000085static constexpr int32_t POINTER_0_UP =
86 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080087static constexpr int32_t POINTER_1_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Harry Cuttsb166c002023-05-09 13:06:05 +000089static constexpr int32_t POINTER_2_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -080091
Antonio Kantek15beb512022-06-13 22:35:41 +000092// The default pid and uid for windows created on the primary display by the test.
Prabir Pradhan5735a322022-04-11 17:23:34 +000093static constexpr int32_t WINDOW_PID = 999;
94static constexpr int32_t WINDOW_UID = 1001;
95
Antonio Kantek15beb512022-06-13 22:35:41 +000096// The default pid and uid for the windows created on the secondary display by the test.
97static constexpr int32_t SECONDARY_WINDOW_PID = 1010;
98static constexpr int32_t SECONDARY_WINDOW_UID = 1012;
99
Prabir Pradhan5735a322022-04-11 17:23:34 +0000100// The default policy flags to use for event injection by tests.
101static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
Siarhei Vishniakou58cfc602020-12-14 23:21:30 +0000103// An arbitrary pid of the gesture monitor window
104static constexpr int32_t MONITOR_PID = 2001;
105
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800106static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
107
Arthur Hungc539dbb2022-12-08 07:45:36 +0000108static constexpr int expectedWallpaperFlags =
109 AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
110
Siarhei Vishniakou56e79092023-02-21 19:13:16 -0800111using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID;
112
chaviwd1c23182019-12-20 18:44:56 -0800113struct PointF {
114 float x;
115 float y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800116 auto operator<=>(const PointF&) const = default;
chaviwd1c23182019-12-20 18:44:56 -0800117};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Gang Wang342c9272020-01-13 13:15:04 -0500119/**
120 * Return a DOWN key event with KEYCODE_A.
121 */
122static KeyEvent getTestKeyEvent() {
123 KeyEvent event;
124
Garfield Tanfbe732e2020-01-24 11:26:14 -0800125 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
126 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
127 ARBITRARY_TIME, ARBITRARY_TIME);
Gang Wang342c9272020-01-13 13:15:04 -0500128 return event;
129}
130
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000131static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) {
132 ASSERT_EQ(expectedAction, receivedAction)
133 << "expected " << MotionEvent::actionToString(expectedAction) << ", got "
134 << MotionEvent::actionToString(receivedAction);
135}
136
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800137MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
138 bool matches = action == arg.getAction();
139 if (!matches) {
140 *result_listener << "expected action " << MotionEvent::actionToString(action)
141 << ", but got " << MotionEvent::actionToString(arg.getAction());
142 }
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800143 if (action == AMOTION_EVENT_ACTION_DOWN) {
144 if (!matches) {
145 *result_listener << "; ";
146 }
147 *result_listener << "downTime should match eventTime for ACTION_DOWN events";
148 matches &= arg.getDownTime() == arg.getEventTime();
149 }
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800150 if (action == AMOTION_EVENT_ACTION_CANCEL) {
151 if (!matches) {
152 *result_listener << "; ";
153 }
154 *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
155 matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
156 }
157 return matches;
158}
159
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -0800160MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
161 return arg.getDownTime() == downTime;
162}
163
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800164MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
165 return arg.getDisplayId() == displayId;
166}
167
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800168MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
169 return arg.getDeviceId() == deviceId;
170}
171
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800172MATCHER_P(WithSource, source, "InputEvent with specified source") {
173 *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
174 << inputEventSourceToString(arg.getSource());
175 return arg.getSource() == source;
176}
177
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800178MATCHER_P(WithFlags, flags, "InputEvent with specified flags") {
179 return arg.getFlags() == flags;
180}
181
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800182MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") {
183 if (arg.getPointerCount() != 1) {
184 *result_listener << "Expected 1 pointer, got " << arg.getPointerCount();
185 return false;
186 }
Harry Cutts33476232023-01-30 19:57:29 +0000187 return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y;
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800188}
189
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800190MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") {
191 return arg.getPointerCount() == pointerCount;
192}
193
Siarhei Vishniakou487c49b2022-12-02 15:48:57 -0800194MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") {
195 // Build a map for the received pointers, by pointer id
196 std::map<int32_t /*pointerId*/, PointF> actualPointers;
197 for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) {
198 const int32_t pointerId = arg.getPointerId(pointerIndex);
199 actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)};
200 }
201 return pointers == actualPointers;
202}
203
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204// --- FakeInputDispatcherPolicy ---
205
206class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
207 InputDispatcherConfiguration mConfig;
208
Prabir Pradhanedd96402022-02-15 01:46:16 -0800209 using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>;
210
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211public:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000212 FakeInputDispatcherPolicy() = default;
213 virtual ~FakeInputDispatcherPolicy() = default;
Jackal Guof9696682018-10-05 12:23:23 +0800214
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800215 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700216 assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700217 ASSERT_EQ(event.getType(), InputEventType::KEY);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700218 EXPECT_EQ(event.getDisplayId(), args.displayId);
219
220 const auto& keyEvent = static_cast<const KeyEvent&>(event);
221 EXPECT_EQ(keyEvent.getEventTime(), args.eventTime);
222 EXPECT_EQ(keyEvent.getAction(), args.action);
223 });
Jackal Guof9696682018-10-05 12:23:23 +0800224 }
225
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700226 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) {
227 assertFilterInputEventWasCalledInternal([&](const InputEvent& event) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700228 ASSERT_EQ(event.getType(), InputEventType::MOTION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700229 EXPECT_EQ(event.getDisplayId(), args.displayId);
230
231 const auto& motionEvent = static_cast<const MotionEvent&>(event);
232 EXPECT_EQ(motionEvent.getEventTime(), args.eventTime);
233 EXPECT_EQ(motionEvent.getAction(), args.action);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000234 EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION);
235 EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION);
236 EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION);
237 EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION);
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700238 });
Jackal Guof9696682018-10-05 12:23:23 +0800239 }
240
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700241 void assertFilterInputEventWasNotCalled() {
242 std::scoped_lock lock(mLock);
243 ASSERT_EQ(nullptr, mFilteredEvent);
244 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800246 void assertNotifyConfigurationChangedWasCalled(nsecs_t when) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700247 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800248 ASSERT_TRUE(mConfigurationChangedTime)
249 << "Timed out waiting for configuration changed call";
250 ASSERT_EQ(*mConfigurationChangedTime, when);
251 mConfigurationChangedTime = std::nullopt;
252 }
253
254 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700255 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800256 ASSERT_TRUE(mLastNotifySwitch);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800257 // We do not check id because it is not exposed to the policy
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800258 EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime);
259 EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags);
260 EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues);
261 EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask);
262 mLastNotifySwitch = std::nullopt;
263 }
264
chaviwfd6d3512019-03-25 13:23:49 -0700265 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700266 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800267 ASSERT_EQ(touchedToken, mOnPointerDownToken);
268 mOnPointerDownToken.clear();
269 }
270
271 void assertOnPointerDownWasNotCalled() {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700272 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800273 ASSERT_TRUE(mOnPointerDownToken == nullptr)
274 << "Expected onPointerDownOutsideFocus to not have been called";
chaviwfd6d3512019-03-25 13:23:49 -0700275 }
276
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700277 // This function must be called soon after the expected ANR timer starts,
278 // because we are also checking how much time has passed.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500279 void assertNotifyNoFocusedWindowAnrWasCalled(
Chris Yea209fde2020-07-22 13:54:51 -0700280 std::chrono::nanoseconds timeout,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500281 const std::shared_ptr<InputApplicationHandle>& expectedApplication) {
Prabir Pradhanedd96402022-02-15 01:46:16 -0800282 std::unique_lock lock(mLock);
283 android::base::ScopedLockAssertion assumeLocked(mLock);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500284 std::shared_ptr<InputApplicationHandle> application;
Prabir Pradhanedd96402022-02-15 01:46:16 -0800285 ASSERT_NO_FATAL_FAILURE(
286 application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock));
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500287 ASSERT_EQ(expectedApplication, application);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700288 }
289
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000290 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
Prabir Pradhanedd96402022-02-15 01:46:16 -0800291 const sp<WindowInfoHandle>& window) {
292 LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null");
293 assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(),
294 window->getInfo()->ownerPid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500295 }
296
Prabir Pradhanedd96402022-02-15 01:46:16 -0800297 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
298 const sp<IBinder>& expectedToken,
299 int32_t expectedPid) {
300 std::unique_lock lock(mLock);
301 android::base::ScopedLockAssertion assumeLocked(mLock);
302 AnrResult result;
303 ASSERT_NO_FATAL_FAILURE(result =
304 getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock));
305 const auto& [token, pid] = result;
306 ASSERT_EQ(expectedToken, token);
307 ASSERT_EQ(expectedPid, pid);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500308 }
309
Prabir Pradhanedd96402022-02-15 01:46:16 -0800310 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000311 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500312 std::unique_lock lock(mLock);
313 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800314 AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock);
315 const auto& [token, _] = result;
316 return token;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000317 }
318
Prabir Pradhanedd96402022-02-15 01:46:16 -0800319 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
320 int32_t expectedPid) {
321 std::unique_lock lock(mLock);
322 android::base::ScopedLockAssertion assumeLocked(mLock);
323 AnrResult result;
324 ASSERT_NO_FATAL_FAILURE(
325 result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock));
326 const auto& [token, pid] = result;
327 ASSERT_EQ(expectedToken, token);
328 ASSERT_EQ(expectedPid, pid);
329 }
330
331 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000332 sp<IBinder> getResponsiveWindowToken() {
333 std::unique_lock lock(mLock);
334 android::base::ScopedLockAssertion assumeLocked(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800335 AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock);
336 const auto& [token, _] = result;
337 return token;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700338 }
339
340 void assertNotifyAnrWasNotCalled() {
341 std::scoped_lock lock(mLock);
342 ASSERT_TRUE(mAnrApplications.empty());
Prabir Pradhanedd96402022-02-15 01:46:16 -0800343 ASSERT_TRUE(mAnrWindows.empty());
344 ASSERT_TRUE(mResponsiveWindows.empty())
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500345 << "ANR was not called, but please also consume the 'connection is responsive' "
346 "signal";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700347 }
348
Garfield Tan1c7bc862020-01-28 13:24:04 -0800349 void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) {
350 mConfig.keyRepeatTimeout = timeout;
351 mConfig.keyRepeatDelay = delay;
352 }
353
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000354 PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -0800355 std::unique_lock lock(mLock);
356 base::ScopedLockAssertion assumeLocked(mLock);
357
358 if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms,
359 [this, enabled]() REQUIRES(mLock) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000360 return mPointerCaptureRequest->enable ==
Prabir Pradhan99987712020-11-10 18:43:05 -0800361 enabled;
362 })) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled
364 << ") to be called.";
365 return {};
Prabir Pradhan99987712020-11-10 18:43:05 -0800366 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000367 auto request = *mPointerCaptureRequest;
368 mPointerCaptureRequest.reset();
369 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -0800370 }
371
372 void assertSetPointerCaptureNotCalled() {
373 std::unique_lock lock(mLock);
374 base::ScopedLockAssertion assumeLocked(mLock);
375
376 if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000377 FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
Prabir Pradhan99987712020-11-10 18:43:05 -0800378 "enabled = "
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 << std::to_string(mPointerCaptureRequest->enable);
Prabir Pradhan99987712020-11-10 18:43:05 -0800380 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000381 mPointerCaptureRequest.reset();
Prabir Pradhan99987712020-11-10 18:43:05 -0800382 }
383
arthurhungf452d0b2021-01-06 00:19:52 +0800384 void assertDropTargetEquals(const sp<IBinder>& targetToken) {
385 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800386 ASSERT_TRUE(mNotifyDropWindowWasCalled);
arthurhungf452d0b2021-01-06 00:19:52 +0800387 ASSERT_EQ(targetToken, mDropTargetWindowToken);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800388 mNotifyDropWindowWasCalled = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800389 }
390
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800391 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) {
392 std::unique_lock lock(mLock);
393 base::ScopedLockAssertion assumeLocked(mLock);
394 std::optional<sp<IBinder>> receivedToken =
395 getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
396 mNotifyInputChannelBroken);
397 ASSERT_TRUE(receivedToken.has_value());
398 ASSERT_EQ(token, *receivedToken);
399 }
400
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800401 /**
402 * Set policy timeout. A value of zero means next key will not be intercepted.
403 */
404 void setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
405 mInterceptKeyTimeout = timeout;
406 }
407
Josep del Riob3981622023-04-18 15:49:45 +0000408 void assertUserActivityPoked() {
409 std::scoped_lock lock(mLock);
410 ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
411 }
412
413 void assertUserActivityNotPoked() {
414 std::scoped_lock lock(mLock);
415 ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked";
416 }
417
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418private:
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700419 std::mutex mLock;
420 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
421 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
422 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
423 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Jackal Guof9696682018-10-05 12:23:23 +0800424
Prabir Pradhan99987712020-11-10 18:43:05 -0800425 std::condition_variable mPointerCaptureChangedCondition;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000426
427 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
Prabir Pradhan99987712020-11-10 18:43:05 -0800428
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700429 // ANR handling
Chris Yea209fde2020-07-22 13:54:51 -0700430 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800431 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
432 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700433 std::condition_variable mNotifyAnr;
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800434 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
435 std::condition_variable mNotifyInputChannelBroken;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700436
arthurhungf452d0b2021-01-06 00:19:52 +0800437 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800438 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
Josep del Riob3981622023-04-18 15:49:45 +0000439 bool mPokedUserActivity GUARDED_BY(mLock) = false;
arthurhungf452d0b2021-01-06 00:19:52 +0800440
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800441 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
442
Prabir Pradhanedd96402022-02-15 01:46:16 -0800443 // All three ANR-related callbacks behave the same way, so we use this generic function to wait
444 // for a specific container to become non-empty. When the container is non-empty, return the
445 // first entry from the container and erase it.
446 template <class T>
447 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
448 std::unique_lock<std::mutex>& lock) REQUIRES(mLock) {
449 // If there is an ANR, Dispatcher won't be idle because there are still events
450 // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle
451 // before checking if ANR was called.
452 // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need
453 // to provide it some time to act. 100ms seems reasonable.
454 std::chrono::duration timeToWait = timeout + 100ms; // provide some slack
455 const std::chrono::time_point start = std::chrono::steady_clock::now();
456 std::optional<T> token =
457 getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr);
458 if (!token.has_value()) {
459 ADD_FAILURE() << "Did not receive the ANR callback";
460 return {};
461 }
462
463 const std::chrono::duration waited = std::chrono::steady_clock::now() - start;
464 // Ensure that the ANR didn't get raised too early. We can't be too strict here because
465 // the dispatcher started counting before this function was called
466 if (std::chrono::abs(timeout - waited) > 100ms) {
467 ADD_FAILURE() << "ANR was raised too early or too late. Expected "
468 << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()
469 << "ms, but waited "
470 << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count()
471 << "ms instead";
472 }
473 return *token;
474 }
475
476 template <class T>
477 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
478 std::queue<T>& storage,
479 std::unique_lock<std::mutex>& lock,
480 std::condition_variable& condition)
481 REQUIRES(mLock) {
482 condition.wait_for(lock, timeout,
483 [&storage]() REQUIRES(mLock) { return !storage.empty(); });
484 if (storage.empty()) {
485 ADD_FAILURE() << "Did not receive the expected callback";
486 return std::nullopt;
487 }
488 T item = storage.front();
489 storage.pop();
490 return std::make_optional(item);
491 }
492
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600493 void notifyConfigurationChanged(nsecs_t when) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700494 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800495 mConfigurationChangedTime = when;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 }
497
Prabir Pradhanedd96402022-02-15 01:46:16 -0800498 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
499 const std::string&) override {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700500 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800501 ASSERT_TRUE(pid.has_value());
502 mAnrWindows.push({connectionToken, *pid});
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700503 mNotifyAnr.notify_all();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500504 }
505
Prabir Pradhanedd96402022-02-15 01:46:16 -0800506 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
507 std::optional<int32_t> pid) override {
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500508 std::scoped_lock lock(mLock);
Prabir Pradhanedd96402022-02-15 01:46:16 -0800509 ASSERT_TRUE(pid.has_value());
510 mResponsiveWindows.push({connectionToken, *pid});
Siarhei Vishniakou234129c2020-10-22 22:28:12 -0500511 mNotifyAnr.notify_all();
512 }
513
514 void notifyNoFocusedWindowAnr(
515 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
516 std::scoped_lock lock(mLock);
517 mAnrApplications.push(applicationHandle);
518 mNotifyAnr.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800519 }
520
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -0800521 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override {
522 std::scoped_lock lock(mLock);
523 mBrokenInputChannels.push(connectionToken);
524 mNotifyInputChannelBroken.notify_all();
525 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600527 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Robert Carr740167f2018-10-11 19:03:41 -0700528
Chris Yef59a2f42020-10-16 12:55:26 -0700529 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
530 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
531 const std::vector<float>& values) override {}
532
533 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
534 InputDeviceSensorAccuracy accuracy) override {}
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000535
Chris Yefb552902021-02-03 17:18:37 -0800536 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
537
Prabir Pradhana41d2442023-04-20 21:30:40 +0000538 InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539
Prabir Pradhana41d2442023-04-20 21:30:40 +0000540 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700541 std::scoped_lock lock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +0000542 switch (inputEvent.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700543 case InputEventType::KEY: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000544 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent);
545 mFilteredEvent = std::make_unique<KeyEvent>(keyEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800546 break;
547 }
548
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700549 case InputEventType::MOTION: {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000550 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent);
551 mFilteredEvent = std::make_unique<MotionEvent>(motionEvent);
Jackal Guof9696682018-10-05 12:23:23 +0800552 break;
553 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700554 default: {
555 ADD_FAILURE() << "Should only filter keys or motions";
556 break;
557 }
Jackal Guof9696682018-10-05 12:23:23 +0800558 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 return true;
560 }
561
Prabir Pradhana41d2442023-04-20 21:30:40 +0000562 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override {
563 if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800564 // Clear intercept state when we handled the event.
565 mInterceptKeyTimeout = 0ms;
566 }
567 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600569 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570
Prabir Pradhana41d2442023-04-20 21:30:40 +0000571 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override {
Arthur Hung2ee6d0b2022-03-03 20:19:38 +0800572 nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
573 // Clear intercept state so we could dispatch the event in next wake.
574 mInterceptKeyTimeout = 0ms;
575 return delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 }
577
Prabir Pradhana41d2442023-04-20 21:30:40 +0000578 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&,
579 uint32_t) override {
580 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 }
582
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600583 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
584 uint32_t policyFlags) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700585 std::scoped_lock lock(mLock);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800586 /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is
587 * essentially a passthrough for notifySwitch.
588 */
Harry Cutts33476232023-01-30 19:57:29 +0000589 mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591
Josep del Riob3981622023-04-18 15:49:45 +0000592 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {
593 std::scoped_lock lock(mLock);
594 mPokedUserActivity = true;
595 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600597 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700598 std::scoped_lock lock(mLock);
chaviwfd6d3512019-03-25 13:23:49 -0700599 mOnPointerDownToken = newToken;
600 }
601
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000602 void setPointerCapture(const PointerCaptureRequest& request) override {
Prabir Pradhan99987712020-11-10 18:43:05 -0800603 std::scoped_lock lock(mLock);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000604 mPointerCaptureRequest = {request};
Prabir Pradhan99987712020-11-10 18:43:05 -0800605 mPointerCaptureChangedCondition.notify_all();
606 }
607
arthurhungf452d0b2021-01-06 00:19:52 +0800608 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override {
609 std::scoped_lock lock(mLock);
Arthur Hung6d0571e2021-04-09 20:18:16 +0800610 mNotifyDropWindowWasCalled = true;
arthurhungf452d0b2021-01-06 00:19:52 +0800611 mDropTargetWindowToken = token;
612 }
613
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700614 void assertFilterInputEventWasCalledInternal(
615 const std::function<void(const InputEvent&)>& verify) {
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -0700616 std::scoped_lock lock(mLock);
Siarhei Vishniakoud99e1b62019-11-26 11:01:06 -0800617 ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called.";
Prabir Pradhan81420cc2021-09-06 10:28:50 -0700618 verify(*mFilteredEvent);
Siarhei Vishniakou8935a802019-11-15 16:41:44 -0800619 mFilteredEvent = nullptr;
Jackal Guof9696682018-10-05 12:23:23 +0800620 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621};
622
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623// --- InputDispatcherTest ---
624
625class InputDispatcherTest : public testing::Test {
626protected:
Prabir Pradhana41d2442023-04-20 21:30:40 +0000627 std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700628 std::unique_ptr<InputDispatcher> mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000630 void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +0000631 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
632 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
Arthur Hungb92218b2018-08-14 12:00:21 +0800633 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000634 // Start InputDispatcher thread
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700635 ASSERT_EQ(OK, mDispatcher->start());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 }
637
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000638 void TearDown() override {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700639 ASSERT_EQ(OK, mDispatcher->stop());
Prabir Pradhana41d2442023-04-20 21:30:40 +0000640 mFakePolicy.reset();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700641 mDispatcher.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700643
644 /**
645 * Used for debugging when writing the test
646 */
647 void dumpDispatcherState() {
648 std::string dump;
649 mDispatcher->dump(dump);
650 std::stringstream ss(dump);
651 std::string to;
652
653 while (std::getline(ss, to, '\n')) {
654 ALOGE("%s", to.c_str());
655 }
656 }
Vishnu Nair958da932020-08-21 17:12:37 -0700657
Chavi Weingarten847e8512023-03-29 00:26:09 +0000658 void setFocusedWindow(const sp<WindowInfoHandle>& window) {
Vishnu Nair958da932020-08-21 17:12:37 -0700659 FocusRequest request;
660 request.token = window->getToken();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +0000661 request.windowName = window->getName();
Vishnu Nair958da932020-08-21 17:12:37 -0700662 request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
663 request.displayId = window->getInfo()->displayId;
664 mDispatcher->setFocusedWindow(request);
665 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666};
667
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
669 KeyEvent event;
670
671 // Rejects undefined key actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800672 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
673 INVALID_HMAC,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600674 /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME,
675 ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800676 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000677 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000678 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 << "Should reject key events with undefined action.";
680
681 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800682 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
683 INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600684 ARBITRARY_TIME, ARBITRARY_TIME);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800685 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000686 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000687 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 << "Should reject key events with ACTION_MULTIPLE.";
689}
690
691TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
692 MotionEvent event;
693 PointerProperties pointerProperties[MAX_POINTERS + 1];
694 PointerCoords pointerCoords[MAX_POINTERS + 1];
Siarhei Vishniakou01747382022-01-20 13:23:27 -0800695 for (size_t i = 0; i <= MAX_POINTERS; i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 pointerProperties[i].clear();
697 pointerProperties[i].id = i;
698 pointerCoords[i].clear();
699 }
700
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800701 // Some constants commonly used below
702 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
703 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
704 constexpr int32_t metaState = AMETA_NONE;
705 constexpr MotionClassification classification = MotionClassification::NONE;
706
chaviw9eaa22c2020-07-01 16:21:27 -0700707 ui::Transform identityTransform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 // Rejects undefined motion actions.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800709 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
chaviw9eaa22c2020-07-01 16:21:27 -0700710 /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification,
711 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700712 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
713 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700714 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800715 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000716 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000717 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 << "Should reject motion events with undefined action.";
719
720 // Rejects pointer down with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800721 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800722 POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
723 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
724 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
725 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500726 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800727 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000728 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000729 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 << "Should reject motion events with pointer down index too large.";
731
Garfield Tanfbe732e2020-01-24 11:26:14 -0800732 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700733 AMOTION_EVENT_ACTION_POINTER_DOWN |
734 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700735 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
736 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700737 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500738 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800739 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000740 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000741 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 << "Should reject motion events with pointer down index too small.";
743
744 // Rejects pointer up with invalid index.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800745 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -0800746 POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform,
747 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
748 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
749 ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500750 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800751 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000752 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000753 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 << "Should reject motion events with pointer up index too large.";
755
Garfield Tanfbe732e2020-01-24 11:26:14 -0800756 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
Garfield Tan00f511d2019-06-12 16:55:40 -0700757 AMOTION_EVENT_ACTION_POINTER_UP |
758 (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
chaviw9eaa22c2020-07-01 16:21:27 -0700759 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0,
760 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700761 identityTransform, ARBITRARY_TIME, ARBITRARY_TIME,
chaviw3277faf2021-05-19 16:45:23 -0500762 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800763 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000764 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000765 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766 << "Should reject motion events with pointer up index too small.";
767
768 // Rejects motion events with invalid number of pointers.
Garfield Tanfbe732e2020-01-24 11:26:14 -0800769 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
770 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700771 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700772 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
773 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700774 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800775 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000776 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000777 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800778 << "Should reject motion events with 0 pointers.";
779
Garfield Tanfbe732e2020-01-24 11:26:14 -0800780 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
781 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700782 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700783 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
784 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700785 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800786 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000787 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000788 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789 << "Should reject motion events with more than MAX_POINTERS pointers.";
790
791 // Rejects motion events with invalid pointer ids.
792 pointerProperties[0].id = -1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800793 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
794 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700795 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700796 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
797 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700798 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800799 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000800 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000801 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 << "Should reject motion events with pointer ids less than 0.";
803
804 pointerProperties[0].id = MAX_POINTER_ID + 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800805 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
806 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700807 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700808 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
809 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700810 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800811 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000812 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000813 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
815
816 // Rejects motion events with duplicate pointer ids.
817 pointerProperties[0].id = 1;
818 pointerProperties[1].id = 1;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800819 event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC,
820 AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification,
chaviw9eaa22c2020-07-01 16:21:27 -0700821 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700822 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME,
823 ARBITRARY_TIME,
Garfield Tan00f511d2019-06-12 16:55:40 -0700824 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800825 ASSERT_EQ(InputEventInjectionResult::FAILED,
Harry Cutts33476232023-01-30 19:57:29 +0000826 mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +0000827 0ms, 0))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 << "Should reject motion events with duplicate pointer ids.";
829}
830
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800831/* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */
832
833TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) {
834 constexpr nsecs_t eventTime = 20;
Prabir Pradhan678438e2023-04-13 19:32:51 +0000835 mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime});
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800836 ASSERT_TRUE(mDispatcher->waitForIdle());
837
838 mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime);
839}
840
841TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) {
Harry Cutts33476232023-01-30 19:57:29 +0000842 NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1,
843 /*switchMask=*/2);
Prabir Pradhan678438e2023-04-13 19:32:51 +0000844 mDispatcher->notifySwitch(args);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800845
846 // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener
847 args.policyFlags |= POLICY_FLAG_TRUSTED;
848 mFakePolicy->assertNotifySwitchWasCalled(args);
849}
850
Arthur Hungb92218b2018-08-14 12:00:21 +0800851// --- InputDispatcherTest SetInputWindowTest ---
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700852static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms;
Siarhei Vishniakou1c494c52021-08-11 20:25:01 -0700853// Default input dispatching timeout if there is no focused application or paused window
854// from which to determine an appropriate dispatching timeout.
855static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
856 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
857 android::base::HwTimeoutMultiplier());
Arthur Hungb92218b2018-08-14 12:00:21 +0800858
859class FakeApplicationHandle : public InputApplicationHandle {
860public:
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700861 FakeApplicationHandle() {
862 mInfo.name = "Fake Application";
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700863 mInfo.token = sp<BBinder>::make();
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500864 mInfo.dispatchingTimeoutMillis =
865 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700866 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800867 virtual ~FakeApplicationHandle() {}
868
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -1000869 virtual bool updateInfo() override { return true; }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700870
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500871 void setDispatchingTimeout(std::chrono::milliseconds timeout) {
872 mInfo.dispatchingTimeoutMillis = timeout.count();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700873 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800874};
875
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800876class FakeInputReceiver {
Arthur Hungb92218b2018-08-14 12:00:21 +0800877public:
Garfield Tan15601662020-09-22 15:32:38 -0700878 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name)
chaviwd1c23182019-12-20 18:44:56 -0800879 : mName(name) {
Garfield Tan15601662020-09-22 15:32:38 -0700880 mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel));
chaviwd1c23182019-12-20 18:44:56 -0800881 }
882
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800883 InputEvent* consume() {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700884 InputEvent* event;
885 std::optional<uint32_t> consumeSeq = receiveEvent(&event);
886 if (!consumeSeq) {
887 return nullptr;
888 }
889 finishEvent(*consumeSeq);
890 return event;
891 }
892
893 /**
894 * Receive an event without acknowledging it.
895 * Return the sequence number that could later be used to send finished signal.
896 */
897 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Arthur Hungb92218b2018-08-14 12:00:21 +0800898 uint32_t consumeSeq;
899 InputEvent* event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800900
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800901 std::chrono::time_point start = std::chrono::steady_clock::now();
902 status_t status = WOULD_BLOCK;
903 while (status == WOULD_BLOCK) {
Harry Cutts33476232023-01-30 19:57:29 +0000904 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq,
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800905 &event);
906 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
907 if (elapsed > 100ms) {
908 break;
909 }
910 }
911
912 if (status == WOULD_BLOCK) {
913 // Just means there's no event available.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700914 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800915 }
916
917 if (status != OK) {
918 ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700919 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800920 }
921 if (event == nullptr) {
922 ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700923 return std::nullopt;
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800924 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700925 if (outEvent != nullptr) {
926 *outEvent = event;
927 }
928 return consumeSeq;
929 }
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800930
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700931 /**
932 * To be used together with "receiveEvent" to complete the consumption of an event.
933 */
934 void finishEvent(uint32_t consumeSeq) {
935 const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true);
936 ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800937 }
938
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000939 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
940 const status_t status = mConsumer->sendTimeline(inputEventId, timeline);
941 ASSERT_EQ(OK, status);
942 }
943
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700944 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000945 std::optional<int32_t> expectedDisplayId,
946 std::optional<int32_t> expectedFlags) {
Siarhei Vishniakou08b574f2019-11-15 18:05:52 -0800947 InputEvent* event = consume();
948
949 ASSERT_NE(nullptr, event) << mName.c_str()
950 << ": consumer should have returned non-NULL event.";
Arthur Hungb92218b2018-08-14 12:00:21 +0800951 ASSERT_EQ(expectedEventType, event->getType())
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700952 << mName.c_str() << " expected " << ftl::enum_string(expectedEventType)
953 << " event, got " << *event;
Arthur Hungb92218b2018-08-14 12:00:21 +0800954
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000955 if (expectedDisplayId.has_value()) {
956 EXPECT_EQ(expectedDisplayId, event->getDisplayId());
957 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800958
Tiger Huang8664f8c2018-10-11 19:14:35 +0800959 switch (expectedEventType) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700960 case InputEventType::KEY: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800961 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event);
962 EXPECT_EQ(expectedAction, keyEvent.getAction());
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000963 if (expectedFlags.has_value()) {
964 EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags());
965 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800966 break;
967 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700968 case InputEventType::MOTION: {
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -0800969 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000970 assertMotionAction(expectedAction, motionEvent.getAction());
971
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +0000972 if (expectedFlags.has_value()) {
973 EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags());
974 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800975 break;
976 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700977 case InputEventType::FOCUS: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100978 FAIL() << "Use 'consumeFocusEvent' for FOCUS events";
979 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700980 case InputEventType::CAPTURE: {
Prabir Pradhan99987712020-11-10 18:43:05 -0800981 FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events";
982 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700983 case InputEventType::TOUCH_MODE: {
Antonio Kantekf16f2832021-09-28 04:39:20 +0000984 FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events";
985 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700986 case InputEventType::DRAG: {
arthurhungb89ccb02020-12-30 16:19:01 +0800987 FAIL() << "Use 'consumeDragEvent' for DRAG events";
988 }
Tiger Huang8664f8c2018-10-11 19:14:35 +0800989 }
Arthur Hungb92218b2018-08-14 12:00:21 +0800990 }
991
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800992 MotionEvent* consumeMotion() {
993 InputEvent* event = consume();
994
995 if (event == nullptr) {
996 ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one.";
997 return nullptr;
998 }
999
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001000 if (event->getType() != InputEventType::MOTION) {
1001 ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001002 return nullptr;
1003 }
1004 return static_cast<MotionEvent*>(event);
1005 }
1006
1007 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1008 MotionEvent* motionEvent = consumeMotion();
1009 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
1010 ASSERT_THAT(*motionEvent, matcher);
1011 }
1012
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001013 void consumeFocusEvent(bool hasFocus, bool inTouchMode) {
1014 InputEvent* event = consume();
1015 ASSERT_NE(nullptr, event) << mName.c_str()
1016 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001017 ASSERT_EQ(InputEventType::FOCUS, event->getType())
1018 << "Instead of FocusEvent, got " << *event;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001019
1020 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1021 << mName.c_str() << ": event displayId should always be NONE.";
1022
1023 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
1024 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001025 }
1026
Prabir Pradhan99987712020-11-10 18:43:05 -08001027 void consumeCaptureEvent(bool hasCapture) {
1028 const InputEvent* event = consume();
1029 ASSERT_NE(nullptr, event) << mName.c_str()
1030 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001031 ASSERT_EQ(InputEventType::CAPTURE, event->getType())
1032 << "Instead of CaptureEvent, got " << *event;
Prabir Pradhan99987712020-11-10 18:43:05 -08001033
1034 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1035 << mName.c_str() << ": event displayId should always be NONE.";
1036
1037 const auto& captureEvent = static_cast<const CaptureEvent&>(*event);
1038 EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled());
1039 }
1040
arthurhungb89ccb02020-12-30 16:19:01 +08001041 void consumeDragEvent(bool isExiting, float x, float y) {
1042 const InputEvent* event = consume();
1043 ASSERT_NE(nullptr, event) << mName.c_str()
1044 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001045 ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event;
arthurhungb89ccb02020-12-30 16:19:01 +08001046
1047 EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1048 << mName.c_str() << ": event displayId should always be NONE.";
1049
1050 const auto& dragEvent = static_cast<const DragEvent&>(*event);
1051 EXPECT_EQ(isExiting, dragEvent.isExiting());
1052 EXPECT_EQ(x, dragEvent.getX());
1053 EXPECT_EQ(y, dragEvent.getY());
1054 }
1055
Antonio Kantekf16f2832021-09-28 04:39:20 +00001056 void consumeTouchModeEvent(bool inTouchMode) {
1057 const InputEvent* event = consume();
1058 ASSERT_NE(nullptr, event) << mName.c_str()
1059 << ": consumer should have returned non-NULL event.";
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001060 ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType())
1061 << "Instead of TouchModeEvent, got " << *event;
Antonio Kantekf16f2832021-09-28 04:39:20 +00001062
1063 ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId())
1064 << mName.c_str() << ": event displayId should always be NONE.";
1065 const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
1066 EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode());
1067 }
1068
chaviwd1c23182019-12-20 18:44:56 -08001069 void assertNoEvents() {
1070 InputEvent* event = consume();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001071 if (event == nullptr) {
1072 return;
1073 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001074 if (event->getType() == InputEventType::KEY) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075 KeyEvent& keyEvent = static_cast<KeyEvent&>(*event);
1076 ADD_FAILURE() << "Received key event "
1077 << KeyEvent::actionToString(keyEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001078 } else if (event->getType() == InputEventType::MOTION) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001079 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1080 ADD_FAILURE() << "Received motion event "
1081 << MotionEvent::actionToString(motionEvent.getAction());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001082 } else if (event->getType() == InputEventType::FOCUS) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001083 FocusEvent& focusEvent = static_cast<FocusEvent&>(*event);
1084 ADD_FAILURE() << "Received focus event, hasFocus = "
1085 << (focusEvent.getHasFocus() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001086 } else if (event->getType() == InputEventType::CAPTURE) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001087 const auto& captureEvent = static_cast<CaptureEvent&>(*event);
1088 ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = "
1089 << (captureEvent.getPointerCaptureEnabled() ? "true" : "false");
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001090 } else if (event->getType() == InputEventType::TOUCH_MODE) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00001091 const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event);
1092 ADD_FAILURE() << "Received touch mode event, inTouchMode = "
1093 << (touchModeEvent.isInTouchMode() ? "true" : "false");
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001094 }
1095 FAIL() << mName.c_str()
1096 << ": should not have received any events, so consume() should return NULL";
chaviwd1c23182019-12-20 18:44:56 -08001097 }
1098
1099 sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); }
1100
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001101 int getChannelFd() { return mConsumer->getChannel()->getFd().get(); }
1102
chaviwd1c23182019-12-20 18:44:56 -08001103protected:
1104 std::unique_ptr<InputConsumer> mConsumer;
1105 PreallocatedInputEventFactory mEventFactory;
1106
1107 std::string mName;
1108};
1109
chaviw3277faf2021-05-19 16:45:23 -05001110class FakeWindowHandle : public WindowInfoHandle {
chaviwd1c23182019-12-20 18:44:56 -08001111public:
1112 static const int32_t WIDTH = 600;
1113 static const int32_t HEIGHT = 800;
chaviwd1c23182019-12-20 18:44:56 -08001114
Chris Yea209fde2020-07-22 13:54:51 -07001115 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001116 const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001117 int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt)
chaviwd1c23182019-12-20 18:44:56 -08001118 : mName(name) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001119 if (token == std::nullopt) {
Garfield Tan15601662020-09-22 15:32:38 -07001120 base::Result<std::unique_ptr<InputChannel>> channel =
1121 dispatcher->createInputChannel(name);
1122 token = (*channel)->getConnectionToken();
1123 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
chaviwd1c23182019-12-20 18:44:56 -08001124 }
1125
1126 inputApplicationHandle->updateInfo();
1127 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
1128
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001129 mInfo.token = *token;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001130 mInfo.id = sId++;
chaviwd1c23182019-12-20 18:44:56 -08001131 mInfo.name = name;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001132 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001133 mInfo.alpha = 1.0;
chaviwd1c23182019-12-20 18:44:56 -08001134 mInfo.frameLeft = 0;
1135 mInfo.frameTop = 0;
1136 mInfo.frameRight = WIDTH;
1137 mInfo.frameBottom = HEIGHT;
chaviw1ff3d1e2020-07-01 15:53:47 -07001138 mInfo.transform.set(0, 0);
chaviwd1c23182019-12-20 18:44:56 -08001139 mInfo.globalScaleFactor = 1.0;
1140 mInfo.touchableRegion.clear();
1141 mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT));
Prabir Pradhan5735a322022-04-11 17:23:34 +00001142 mInfo.ownerPid = WINDOW_PID;
1143 mInfo.ownerUid = WINDOW_UID;
chaviwd1c23182019-12-20 18:44:56 -08001144 mInfo.displayId = displayId;
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001145 mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT;
chaviwd1c23182019-12-20 18:44:56 -08001146 }
1147
Arthur Hungabbb9d82021-09-01 14:52:30 +00001148 sp<FakeWindowHandle> clone(
1149 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001150 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00001151 sp<FakeWindowHandle> handle =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001152 sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher,
1153 mInfo.name + "(Mirror)", displayId, mInfo.token);
Arthur Hungabbb9d82021-09-01 14:52:30 +00001154 return handle;
1155 }
1156
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001157 void setTouchable(bool touchable) {
1158 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable);
1159 }
chaviwd1c23182019-12-20 18:44:56 -08001160
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001161 void setFocusable(bool focusable) {
1162 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable);
1163 }
1164
1165 void setVisible(bool visible) {
1166 mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible);
1167 }
Vishnu Nair958da932020-08-21 17:12:37 -07001168
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001169 void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05001170 mInfo.dispatchingTimeout = timeout;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001171 }
1172
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001173 void setPaused(bool paused) {
1174 mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused);
1175 }
1176
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08001177 void setPreventSplitting(bool preventSplitting) {
1178 mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001179 }
1180
1181 void setSlippery(bool slippery) {
1182 mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery);
1183 }
1184
1185 void setWatchOutsideTouch(bool watchOutside) {
1186 mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
1187 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001188
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001189 void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); }
1190
1191 void setInterceptsStylus(bool interceptsStylus) {
1192 mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
1193 }
1194
1195 void setDropInput(bool dropInput) {
1196 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput);
1197 }
1198
1199 void setDropInputIfObscured(bool dropInputIfObscured) {
1200 mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
1201 }
1202
1203 void setNoInputChannel(bool noInputChannel) {
1204 mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel);
1205 }
1206
Josep del Riob3981622023-04-18 15:49:45 +00001207 void setDisableUserActivity(bool disableUserActivity) {
1208 mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
1209 }
1210
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001211 void setAlpha(float alpha) { mInfo.alpha = alpha; }
1212
chaviw3277faf2021-05-19 16:45:23 -05001213 void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; }
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001214
Bernardo Rufino7393d172021-02-26 13:56:11 +00001215 void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
1216
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001217 void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) {
chaviwd1c23182019-12-20 18:44:56 -08001218 mInfo.frameLeft = frame.left;
1219 mInfo.frameTop = frame.top;
1220 mInfo.frameRight = frame.right;
1221 mInfo.frameBottom = frame.bottom;
1222 mInfo.touchableRegion.clear();
1223 mInfo.addTouchableRegion(frame);
Prabir Pradhanc44ce4d2021-10-05 05:26:29 -07001224
1225 const Rect logicalDisplayFrame = displayTransform.transform(frame);
1226 ui::Transform translate;
1227 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
1228 mInfo.transform = translate * displayTransform;
chaviwd1c23182019-12-20 18:44:56 -08001229 }
1230
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001231 void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
1232
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001233 void setIsWallpaper(bool isWallpaper) {
1234 mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper);
1235 }
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001236
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001237 void setDupTouchToWallpaper(bool hasWallpaper) {
1238 mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
1239 }
chaviwd1c23182019-12-20 18:44:56 -08001240
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08001241 void setTrustedOverlay(bool trustedOverlay) {
1242 mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay);
1243 }
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001244
chaviw9eaa22c2020-07-01 16:21:27 -07001245 void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
1246 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
1247 }
1248
1249 void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); }
chaviwaf87b3e2019-10-01 16:59:28 -07001250
yunho.shinf4a80b82020-11-16 21:13:57 +09001251 void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); }
1252
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001253 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001254 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags);
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001255 }
1256
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001257 void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001258 consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001259 }
1260
Svet Ganov5d3bc372020-01-26 23:11:07 -08001261 void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001262 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001263 consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId),
1264 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001265 }
1266
1267 void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001268 int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08001269 consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1270 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
Svet Ganov5d3bc372020-01-26 23:11:07 -08001271 }
1272
1273 void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001274 int32_t expectedFlags = 0) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001275 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
1276 }
1277
1278 void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
1279 std::optional<int32_t> expectedFlags = std::nullopt) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001280 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08001281 expectedFlags);
1282 }
1283
Svet Ganov5d3bc372020-01-26 23:11:07 -08001284 void consumeMotionPointerDown(int32_t pointerIdx,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001285 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1286 int32_t expectedFlags = 0) {
1287 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
1288 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001289 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001290 }
1291
1292 void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001293 int32_t expectedFlags = 0) {
1294 int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
1295 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001296 consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags);
Svet Ganov5d3bc372020-01-26 23:11:07 -08001297 }
1298
1299 void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001300 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001301 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId,
Michael Wright3a240c42019-12-10 20:53:41 +00001302 expectedFlags);
1303 }
1304
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001305 void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1306 int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001307 consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId,
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05001308 expectedFlags);
1309 }
1310
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001311 void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
1312 int32_t expectedFlags = 0) {
1313 InputEvent* event = consume();
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08001314 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001315 ASSERT_EQ(InputEventType::MOTION, event->getType());
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001316 const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
1317 EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
1318 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX());
1319 EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY());
1320 }
1321
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001322 void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
1323 ASSERT_NE(mInputReceiver, nullptr)
1324 << "Cannot consume events from a window with no receiver";
1325 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
1326 }
1327
Prabir Pradhan99987712020-11-10 18:43:05 -08001328 void consumeCaptureEvent(bool hasCapture) {
1329 ASSERT_NE(mInputReceiver, nullptr)
1330 << "Cannot consume events from a window with no receiver";
1331 mInputReceiver->consumeCaptureEvent(hasCapture);
1332 }
1333
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001334 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) {
1335 MotionEvent* motionEvent = consumeMotion();
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08001336 ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08001337 ASSERT_THAT(*motionEvent, matcher);
1338 }
1339
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001340 void consumeEvent(InputEventType expectedEventType, int32_t expectedAction,
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00001341 std::optional<int32_t> expectedDisplayId,
1342 std::optional<int32_t> expectedFlags) {
chaviwd1c23182019-12-20 18:44:56 -08001343 ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver";
1344 mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId,
1345 expectedFlags);
1346 }
1347
arthurhungb89ccb02020-12-30 16:19:01 +08001348 void consumeDragEvent(bool isExiting, float x, float y) {
1349 mInputReceiver->consumeDragEvent(isExiting, x, y);
1350 }
1351
Antonio Kantekf16f2832021-09-28 04:39:20 +00001352 void consumeTouchModeEvent(bool inTouchMode) {
1353 ASSERT_NE(mInputReceiver, nullptr)
1354 << "Cannot consume events from a window with no receiver";
1355 mInputReceiver->consumeTouchModeEvent(inTouchMode);
1356 }
1357
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001358 std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) {
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001359 if (mInputReceiver == nullptr) {
1360 ADD_FAILURE() << "Invalid receive event on window with no receiver";
1361 return std::nullopt;
1362 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001363 return mInputReceiver->receiveEvent(outEvent);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001364 }
1365
1366 void finishEvent(uint32_t sequenceNum) {
1367 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1368 mInputReceiver->finishEvent(sequenceNum);
1369 }
1370
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001371 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
1372 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
1373 mInputReceiver->sendTimeline(inputEventId, timeline);
1374 }
1375
chaviwaf87b3e2019-10-01 16:59:28 -07001376 InputEvent* consume() {
1377 if (mInputReceiver == nullptr) {
1378 return nullptr;
1379 }
1380 return mInputReceiver->consume();
1381 }
1382
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001383 MotionEvent* consumeMotion() {
1384 InputEvent* event = consume();
1385 if (event == nullptr) {
1386 ADD_FAILURE() << "Consume failed : no event";
1387 return nullptr;
1388 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001389 if (event->getType() != InputEventType::MOTION) {
1390 ADD_FAILURE() << "Instead of motion event, got " << *event;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00001391 return nullptr;
1392 }
1393 return static_cast<MotionEvent*>(event);
1394 }
1395
Arthur Hungb92218b2018-08-14 12:00:21 +08001396 void assertNoEvents() {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001397 if (mInputReceiver == nullptr &&
Prabir Pradhan51e7db02022-02-07 06:02:57 -08001398 mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05001399 return; // Can't receive events if the window does not have input channel
1400 }
1401 ASSERT_NE(nullptr, mInputReceiver)
1402 << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL";
chaviwd1c23182019-12-20 18:44:56 -08001403 mInputReceiver->assertNoEvents();
Arthur Hungb92218b2018-08-14 12:00:21 +08001404 }
1405
chaviwaf87b3e2019-10-01 16:59:28 -07001406 sp<IBinder> getToken() { return mInfo.token; }
1407
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001408 const std::string& getName() { return mName; }
1409
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10001410 void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) {
1411 mInfo.ownerPid = ownerPid;
1412 mInfo.ownerUid = ownerUid;
1413 }
1414
Prabir Pradhanedd96402022-02-15 01:46:16 -08001415 int32_t getPid() const { return mInfo.ownerPid; }
1416
Siarhei Vishniakou7aa3e942021-11-18 09:49:11 -08001417 void destroyReceiver() { mInputReceiver = nullptr; }
1418
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001419 int getChannelFd() { return mInputReceiver->getChannelFd(); }
1420
chaviwd1c23182019-12-20 18:44:56 -08001421private:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001422 const std::string mName;
chaviwd1c23182019-12-20 18:44:56 -08001423 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001424 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001425};
1426
Siarhei Vishniakou540dbae2020-05-05 18:17:17 -07001427std::atomic<int32_t> FakeWindowHandle::sId{1};
1428
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001429static InputEventInjectionResult injectKey(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001430 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001431 int32_t displayId = ADISPLAY_ID_NONE,
1432 InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001433 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001434 bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {},
1435 uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Arthur Hungb92218b2018-08-14 12:00:21 +08001436 KeyEvent event;
1437 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1438
1439 // Define a valid key down event.
Garfield Tanfbe732e2020-01-24 11:26:14 -08001440 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId,
Harry Cutts33476232023-01-30 19:57:29 +00001441 INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount,
1442 currentTime, currentTime);
Arthur Hungb92218b2018-08-14 12:00:21 +08001443
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001444 if (!allowKeyRepeat) {
1445 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
1446 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001447 // Inject event until dispatch out.
Prabir Pradhan5735a322022-04-11 17:23:34 +00001448 return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags);
Arthur Hungb92218b2018-08-14 12:00:21 +08001449}
1450
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001451static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001452 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001453 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001454}
1455
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001456// Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without
1457// sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it
1458// has to be woken up to process the repeating key.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001459static InputEventInjectionResult injectKeyDownNoRepeat(
1460 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001461 return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId,
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001462 InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT,
Harry Cutts33476232023-01-30 19:57:29 +00001463 /*allowKeyRepeat=*/false);
Prabir Pradhan93f342c2021-03-11 15:05:30 -08001464}
1465
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001466static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001467 int32_t displayId = ADISPLAY_ID_NONE) {
Harry Cutts33476232023-01-30 19:57:29 +00001468 return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001469}
1470
Garfield Tandf26e862020-07-01 20:18:19 -07001471class PointerBuilder {
1472public:
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001473 PointerBuilder(int32_t id, ToolType toolType) {
Garfield Tandf26e862020-07-01 20:18:19 -07001474 mProperties.clear();
1475 mProperties.id = id;
1476 mProperties.toolType = toolType;
1477 mCoords.clear();
1478 }
1479
1480 PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); }
1481
1482 PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); }
1483
1484 PointerBuilder& axis(int32_t axis, float value) {
1485 mCoords.setAxisValue(axis, value);
1486 return *this;
1487 }
1488
1489 PointerProperties buildProperties() const { return mProperties; }
1490
1491 PointerCoords buildCoords() const { return mCoords; }
1492
1493private:
1494 PointerProperties mProperties;
1495 PointerCoords mCoords;
1496};
1497
1498class MotionEventBuilder {
1499public:
1500 MotionEventBuilder(int32_t action, int32_t source) {
1501 mAction = action;
1502 mSource = source;
1503 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001504 mDownTime = mEventTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001505 }
1506
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001507 MotionEventBuilder& deviceId(int32_t deviceId) {
1508 mDeviceId = deviceId;
1509 return *this;
1510 }
1511
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001512 MotionEventBuilder& downTime(nsecs_t downTime) {
1513 mDownTime = downTime;
1514 return *this;
1515 }
1516
Garfield Tandf26e862020-07-01 20:18:19 -07001517 MotionEventBuilder& eventTime(nsecs_t eventTime) {
1518 mEventTime = eventTime;
1519 return *this;
1520 }
1521
1522 MotionEventBuilder& displayId(int32_t displayId) {
1523 mDisplayId = displayId;
1524 return *this;
1525 }
1526
1527 MotionEventBuilder& actionButton(int32_t actionButton) {
1528 mActionButton = actionButton;
1529 return *this;
1530 }
1531
arthurhung6d4bed92021-03-17 11:59:33 +08001532 MotionEventBuilder& buttonState(int32_t buttonState) {
1533 mButtonState = buttonState;
Garfield Tandf26e862020-07-01 20:18:19 -07001534 return *this;
1535 }
1536
1537 MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) {
1538 mRawXCursorPosition = rawXCursorPosition;
1539 return *this;
1540 }
1541
1542 MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) {
1543 mRawYCursorPosition = rawYCursorPosition;
1544 return *this;
1545 }
1546
1547 MotionEventBuilder& pointer(PointerBuilder pointer) {
1548 mPointers.push_back(pointer);
1549 return *this;
1550 }
1551
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001552 MotionEventBuilder& addFlag(uint32_t flags) {
1553 mFlags |= flags;
1554 return *this;
1555 }
1556
Garfield Tandf26e862020-07-01 20:18:19 -07001557 MotionEvent build() {
1558 std::vector<PointerProperties> pointerProperties;
1559 std::vector<PointerCoords> pointerCoords;
1560 for (const PointerBuilder& pointer : mPointers) {
1561 pointerProperties.push_back(pointer.buildProperties());
1562 pointerCoords.push_back(pointer.buildCoords());
1563 }
1564
1565 // Set mouse cursor position for the most common cases to avoid boilerplate.
1566 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou70c6ee82023-05-15 17:43:48 -07001567 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
Garfield Tandf26e862020-07-01 20:18:19 -07001568 mRawXCursorPosition = pointerCoords[0].getX();
1569 mRawYCursorPosition = pointerCoords[0].getY();
1570 }
1571
1572 MotionEvent event;
chaviw9eaa22c2020-07-01 16:21:27 -07001573 ui::Transform identityTransform;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001574 event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC,
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001575 mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
chaviw9eaa22c2020-07-01 16:21:27 -07001576 mButtonState, MotionClassification::NONE, identityTransform,
1577 /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001578 mRawYCursorPosition, identityTransform, mDownTime, mEventTime,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001579 mPointers.size(), pointerProperties.data(), pointerCoords.data());
Garfield Tandf26e862020-07-01 20:18:19 -07001580
1581 return event;
1582 }
1583
1584private:
1585 int32_t mAction;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08001586 int32_t mDeviceId = DEVICE_ID;
Garfield Tandf26e862020-07-01 20:18:19 -07001587 int32_t mSource;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08001588 nsecs_t mDownTime;
Garfield Tandf26e862020-07-01 20:18:19 -07001589 nsecs_t mEventTime;
1590 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1591 int32_t mActionButton{0};
1592 int32_t mButtonState{0};
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08001593 int32_t mFlags{0};
Garfield Tandf26e862020-07-01 20:18:19 -07001594 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1595 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1596
1597 std::vector<PointerBuilder> mPointers;
1598};
1599
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001600class MotionArgsBuilder {
1601public:
1602 MotionArgsBuilder(int32_t action, int32_t source) {
1603 mAction = action;
1604 mSource = source;
1605 mEventTime = systemTime(SYSTEM_TIME_MONOTONIC);
1606 mDownTime = mEventTime;
1607 }
1608
1609 MotionArgsBuilder& deviceId(int32_t deviceId) {
1610 mDeviceId = deviceId;
1611 return *this;
1612 }
1613
1614 MotionArgsBuilder& downTime(nsecs_t downTime) {
1615 mDownTime = downTime;
1616 return *this;
1617 }
1618
1619 MotionArgsBuilder& eventTime(nsecs_t eventTime) {
1620 mEventTime = eventTime;
1621 return *this;
1622 }
1623
1624 MotionArgsBuilder& displayId(int32_t displayId) {
1625 mDisplayId = displayId;
1626 return *this;
1627 }
1628
1629 MotionArgsBuilder& policyFlags(int32_t policyFlags) {
1630 mPolicyFlags = policyFlags;
1631 return *this;
1632 }
1633
1634 MotionArgsBuilder& actionButton(int32_t actionButton) {
1635 mActionButton = actionButton;
1636 return *this;
1637 }
1638
1639 MotionArgsBuilder& buttonState(int32_t buttonState) {
1640 mButtonState = buttonState;
1641 return *this;
1642 }
1643
1644 MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) {
1645 mRawXCursorPosition = rawXCursorPosition;
1646 return *this;
1647 }
1648
1649 MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) {
1650 mRawYCursorPosition = rawYCursorPosition;
1651 return *this;
1652 }
1653
1654 MotionArgsBuilder& pointer(PointerBuilder pointer) {
1655 mPointers.push_back(pointer);
1656 return *this;
1657 }
1658
1659 MotionArgsBuilder& addFlag(uint32_t flags) {
1660 mFlags |= flags;
1661 return *this;
1662 }
1663
Harry Cuttsb166c002023-05-09 13:06:05 +00001664 MotionArgsBuilder& classification(MotionClassification classification) {
1665 mClassification = classification;
1666 return *this;
1667 }
1668
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001669 NotifyMotionArgs build() {
1670 std::vector<PointerProperties> pointerProperties;
1671 std::vector<PointerCoords> pointerCoords;
1672 for (const PointerBuilder& pointer : mPointers) {
1673 pointerProperties.push_back(pointer.buildProperties());
1674 pointerCoords.push_back(pointer.buildCoords());
1675 }
1676
1677 // Set mouse cursor position for the most common cases to avoid boilerplate.
1678 if (mSource == AINPUT_SOURCE_MOUSE &&
Siarhei Vishniakou70c6ee82023-05-15 17:43:48 -07001679 !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001680 mRawXCursorPosition = pointerCoords[0].getX();
1681 mRawYCursorPosition = pointerCoords[0].getY();
1682 }
1683
1684 NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId,
1685 mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags,
Harry Cuttsb166c002023-05-09 13:06:05 +00001686 AMETA_NONE, mButtonState, mClassification, /*edgeFlags=*/0,
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001687 mPointers.size(), pointerProperties.data(), pointerCoords.data(),
1688 /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition,
1689 mRawYCursorPosition, mDownTime, /*videoFrames=*/{});
1690
1691 return args;
1692 }
1693
1694private:
1695 int32_t mAction;
1696 int32_t mDeviceId = DEVICE_ID;
1697 int32_t mSource;
1698 nsecs_t mDownTime;
1699 nsecs_t mEventTime;
1700 int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
1701 int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS;
1702 int32_t mActionButton{0};
1703 int32_t mButtonState{0};
1704 int32_t mFlags{0};
Harry Cuttsb166c002023-05-09 13:06:05 +00001705 MotionClassification mClassification{MotionClassification::NONE};
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08001706 float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1707 float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
1708
1709 std::vector<PointerBuilder> mPointers;
1710};
1711
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001712static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001713 const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event,
Garfield Tandf26e862020-07-01 20:18:19 -07001714 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001715 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
1716 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
1717 return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout,
1718 policyFlags);
Garfield Tandf26e862020-07-01 20:18:19 -07001719}
1720
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001721static InputEventInjectionResult injectMotionEvent(
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001722 const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source,
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001723 int32_t displayId, const PointF& position = {100, 200},
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07001724 const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001725 AMOTION_EVENT_INVALID_CURSOR_POSITION},
1726 std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001727 InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT,
Prabir Pradhan5735a322022-04-11 17:23:34 +00001728 nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC),
1729 std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) {
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001730 MotionEventBuilder motionBuilder =
1731 MotionEventBuilder(action, source)
1732 .displayId(displayId)
1733 .eventTime(eventTime)
1734 .rawXCursorPosition(cursorPosition.x)
1735 .rawYCursorPosition(cursorPosition.y)
1736 .pointer(
1737 PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y));
1738 if (MotionEvent::getActionMasked(action) == ACTION_DOWN) {
1739 motionBuilder.downTime(eventTime);
1740 }
Arthur Hungb92218b2018-08-14 12:00:21 +08001741
1742 // Inject event until dispatch out.
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07001743 return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode,
1744 targetUid, 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
Siarhei Vishniakou90ee4782023-05-08 11:57:24 -07004926TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
4927 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4928 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4929 "Fake Window", ADISPLAY_ID_DEFAULT);
4930
4931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
4932
4933 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
4934 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
4935 ADISPLAY_ID_DEFAULT, {100, 100}))
4936 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
4937
4938 window->consumeMotionEvent(
4939 AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT)));
4940
4941 // Should have poked user activity
4942 mFakePolicy->assertUserActivityPoked();
4943}
4944
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004945TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07004946 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004947 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4948 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004949
Arthur Hung72d8dc32020-03-28 00:48:39 +00004950 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004951
Prabir Pradhan678438e2023-04-13 19:32:51 +00004952 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004953 mDispatcher->waitForIdle();
4954
4955 window->assertNoEvents();
4956}
4957
4958// If a window is touchable, but does not have focus, it should receive motion events, but not keys
4959TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) {
Chris Yea209fde2020-07-22 13:54:51 -07004960 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004961 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
4962 "Fake Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004963
Arthur Hung72d8dc32020-03-28 00:48:39 +00004964 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004965
4966 // Send key
Prabir Pradhan678438e2023-04-13 19:32:51 +00004967 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004968 // Send motion
Prabir Pradhan678438e2023-04-13 19:32:51 +00004969 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4970 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01004971
4972 // Window should receive only the motion event
4973 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
4974 window->assertNoEvents(); // Key event or focus event will not be received
4975}
4976
arthurhungea3f4fc2020-12-21 23:18:53 +08004977TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) {
4978 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
4979
arthurhungea3f4fc2020-12-21 23:18:53 +08004980 sp<FakeWindowHandle> firstWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004981 sp<FakeWindowHandle>::make(application, mDispatcher, "First Window",
4982 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004983 firstWindow->setFrame(Rect(0, 0, 600, 400));
arthurhungea3f4fc2020-12-21 23:18:53 +08004984
arthurhungea3f4fc2020-12-21 23:18:53 +08004985 sp<FakeWindowHandle> secondWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07004986 sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window",
4987 ADISPLAY_ID_DEFAULT);
arthurhungea3f4fc2020-12-21 23:18:53 +08004988 secondWindow->setFrame(Rect(0, 400, 600, 800));
arthurhungea3f4fc2020-12-21 23:18:53 +08004989
4990 // Add the windows to the dispatcher
4991 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}});
4992
4993 PointF pointInFirst = {300, 200};
4994 PointF pointInSecond = {300, 600};
4995
4996 // Send down to the first window
Prabir Pradhan678438e2023-04-13 19:32:51 +00004997 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
4998 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
4999 {pointInFirst}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005000 // Only the first window should get the down event
5001 firstWindow->consumeMotionDown();
5002 secondWindow->assertNoEvents();
5003
5004 // Send down to the second window
Prabir Pradhan678438e2023-04-13 19:32:51 +00005005 mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5006 ADISPLAY_ID_DEFAULT,
5007 {pointInFirst, pointInSecond}));
arthurhungea3f4fc2020-12-21 23:18:53 +08005008 // The first window gets a move and the second a down
5009 firstWindow->consumeMotionMove();
5010 secondWindow->consumeMotionDown();
5011
5012 // Send pointer cancel to the second window
5013 NotifyMotionArgs pointerUpMotionArgs =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08005014 generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
arthurhungea3f4fc2020-12-21 23:18:53 +08005015 {pointInFirst, pointInSecond});
5016 pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED;
Prabir Pradhan678438e2023-04-13 19:32:51 +00005017 mDispatcher->notifyMotion(pointerUpMotionArgs);
arthurhungea3f4fc2020-12-21 23:18:53 +08005018 // The first window gets move and the second gets cancel.
5019 firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5020 secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
5021
5022 // Send up event.
Prabir Pradhan678438e2023-04-13 19:32:51 +00005023 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
5024 ADISPLAY_ID_DEFAULT));
arthurhungea3f4fc2020-12-21 23:18:53 +08005025 // The first window gets up and the second gets nothing.
5026 firstWindow->consumeMotionUp();
5027 secondWindow->assertNoEvents();
5028}
5029
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005030TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) {
5031 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5032
5033 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005034 sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005035 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5036 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
5037 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2;
5038 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3;
5039
Harry Cutts33476232023-01-30 19:57:29 +00005040 window->sendTimeline(/*inputEventId=*/1, graphicsTimeline);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00005041 window->assertNoEvents();
5042 mDispatcher->waitForIdle();
5043}
5044
chaviwd1c23182019-12-20 18:44:56 -08005045class FakeMonitorReceiver {
Michael Wright3a240c42019-12-10 20:53:41 +00005046public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07005047 FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005048 int32_t displayId) {
Garfield Tan15601662020-09-22 15:32:38 -07005049 base::Result<std::unique_ptr<InputChannel>> channel =
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005050 dispatcher->createInputMonitor(displayId, name, MONITOR_PID);
Garfield Tan15601662020-09-22 15:32:38 -07005051 mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name);
Michael Wright3a240c42019-12-10 20:53:41 +00005052 }
5053
chaviwd1c23182019-12-20 18:44:56 -08005054 sp<IBinder> getToken() { return mInputReceiver->getToken(); }
5055
5056 void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005057 mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId,
5058 expectedFlags);
chaviwd1c23182019-12-20 18:44:56 -08005059 }
5060
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005061 std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); }
5062
5063 void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); }
5064
chaviwd1c23182019-12-20 18:44:56 -08005065 void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005066 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
chaviwd1c23182019-12-20 18:44:56 -08005067 expectedDisplayId, expectedFlags);
5068 }
5069
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005070 void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005071 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE,
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005072 expectedDisplayId, expectedFlags);
5073 }
5074
chaviwd1c23182019-12-20 18:44:56 -08005075 void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005076 mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP,
chaviwd1c23182019-12-20 18:44:56 -08005077 expectedDisplayId, expectedFlags);
5078 }
5079
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005080 void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08005081 mInputReceiver->consumeMotionEvent(
5082 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
5083 WithDisplayId(expectedDisplayId),
5084 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005085 }
5086
Arthur Hungfbfa5722021-11-16 02:45:54 +00005087 void consumeMotionPointerDown(int32_t pointerIdx) {
5088 int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
5089 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005090 mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005091 /*expectedFlags=*/0);
Arthur Hungfbfa5722021-11-16 02:45:54 +00005092 }
5093
Evan Rosky84f07f02021-04-16 10:42:42 -07005094 MotionEvent* consumeMotion() {
5095 InputEvent* event = mInputReceiver->consume();
5096 if (!event) {
5097 ADD_FAILURE() << "No event was produced";
5098 return nullptr;
5099 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005100 if (event->getType() != InputEventType::MOTION) {
5101 ADD_FAILURE() << "Expected MotionEvent, got " << *event;
Evan Rosky84f07f02021-04-16 10:42:42 -07005102 return nullptr;
5103 }
5104 return static_cast<MotionEvent*>(event);
5105 }
5106
chaviwd1c23182019-12-20 18:44:56 -08005107 void assertNoEvents() { mInputReceiver->assertNoEvents(); }
5108
5109private:
5110 std::unique_ptr<FakeInputReceiver> mInputReceiver;
Michael Wright3a240c42019-12-10 20:53:41 +00005111};
5112
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005113using InputDispatcherMonitorTest = InputDispatcherTest;
5114
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005115/**
5116 * Two entities that receive touch: A window, and a global monitor.
5117 * The touch goes to the window, and then the window disappears.
5118 * The monitor does not get cancel right away. But if more events come in, the touch gets canceled
5119 * for the monitor, as well.
5120 * 1. foregroundWindow
5121 * 2. monitor <-- global monitor (doesn't observe z order, receives all events)
5122 */
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005123TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005124 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5125 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005126 sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005127
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005128 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005129
5130 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5131 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5132 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5133 {100, 200}))
5134 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5135
5136 // Both the foreground window and the global monitor should receive the touch down
5137 window->consumeMotionDown();
5138 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
5139
5140 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5141 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5142 ADISPLAY_ID_DEFAULT, {110, 200}))
5143 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5144
5145 window->consumeMotionMove();
5146 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5147
5148 // Now the foreground window goes away
5149 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
5150 window->consumeMotionCancel();
5151 monitor.assertNoEvents(); // Global monitor does not get a cancel yet
5152
5153 // If more events come in, there will be no more foreground window to send them to. This will
5154 // cause a cancel for the monitor, as well.
5155 ASSERT_EQ(InputEventInjectionResult::FAILED,
5156 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5157 ADISPLAY_ID_DEFAULT, {120, 200}))
5158 << "Injection should fail because the window was removed";
5159 window->assertNoEvents();
5160 // Global monitor now gets the cancel
5161 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
5162}
5163
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005164TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) {
Chris Yea209fde2020-07-22 13:54:51 -07005165 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005166 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5167 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005168 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005169
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005170 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005171
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005172 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005173 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005174 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Michael Wright3a240c42019-12-10 20:53:41 +00005175 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005176 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005177}
5178
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005179TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) {
5180 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005181
Chris Yea209fde2020-07-22 13:54:51 -07005182 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005183 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5184 "Fake Window", ADISPLAY_ID_DEFAULT);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005185 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Michael Wright3a240c42019-12-10 20:53:41 +00005186
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005187 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Michael Wright3a240c42019-12-10 20:53:41 +00005188 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005189 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
chaviwd1c23182019-12-20 18:44:56 -08005190 monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005191 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005192
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005193 // Pilfer pointers from the monitor.
5194 // This should not do anything and the window should continue to receive events.
5195 EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken()));
Michael Wright3a240c42019-12-10 20:53:41 +00005196
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005197 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005198 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
5199 ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005200 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005201
5202 monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT);
5203 window->consumeMotionMove(ADISPLAY_ID_DEFAULT);
Michael Wright3a240c42019-12-10 20:53:41 +00005204}
5205
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005206TEST_F(InputDispatcherMonitorTest, NoWindowTransform) {
Evan Rosky84f07f02021-04-16 10:42:42 -07005207 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005208 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5209 "Fake Window", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005210 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5211 window->setWindowOffset(20, 40);
5212 window->setWindowTransform(0, 1, -1, 0);
5213
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005214 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Evan Rosky84f07f02021-04-16 10:42:42 -07005215
5216 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5217 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5218 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5219 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5220 MotionEvent* event = monitor.consumeMotion();
5221 // Even though window has transform, gesture monitor must not.
5222 ASSERT_EQ(ui::Transform(), event->getTransform());
5223}
5224
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005225TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) {
Arthur Hungb3307ee2021-10-14 10:57:37 +00005226 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005227 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
Arthur Hungb3307ee2021-10-14 10:57:37 +00005228
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005229 ASSERT_EQ(InputEventInjectionResult::FAILED,
Arthur Hungb3307ee2021-10-14 10:57:37 +00005230 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08005231 << "Injection should fail if there is a monitor, but no touchable window";
5232 monitor.assertNoEvents();
Arthur Hungb3307ee2021-10-14 10:57:37 +00005233}
5234
chaviw81e2bb92019-12-18 15:03:51 -08005235TEST_F(InputDispatcherTest, TestMoveEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005236 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005237 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5238 "Fake Window", ADISPLAY_ID_DEFAULT);
chaviw81e2bb92019-12-18 15:03:51 -08005239
Arthur Hung72d8dc32020-03-28 00:48:39 +00005240 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
chaviw81e2bb92019-12-18 15:03:51 -08005241
5242 NotifyMotionArgs motionArgs =
5243 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5244 ADISPLAY_ID_DEFAULT);
5245
Prabir Pradhan678438e2023-04-13 19:32:51 +00005246 mDispatcher->notifyMotion(motionArgs);
chaviw81e2bb92019-12-18 15:03:51 -08005247 // Window should receive motion down event.
5248 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
5249
5250 motionArgs.action = AMOTION_EVENT_ACTION_MOVE;
Garfield Tanc51d1ba2020-01-28 13:24:04 -08005251 motionArgs.id += 1;
chaviw81e2bb92019-12-18 15:03:51 -08005252 motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
5253 motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5254 motionArgs.pointerCoords[0].getX() - 10);
5255
Prabir Pradhan678438e2023-04-13 19:32:51 +00005256 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005257 window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005258 /*expectedFlags=*/0);
chaviw81e2bb92019-12-18 15:03:51 -08005259}
5260
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005261/**
5262 * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to
5263 * the device default right away. In the test scenario, we check both the default value,
5264 * and the action of enabling / disabling.
5265 */
5266TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) {
Chris Yea209fde2020-07-22 13:54:51 -07005267 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005268 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5269 "Test window", ADISPLAY_ID_DEFAULT);
Antonio Kantekea47acb2021-12-23 12:41:25 -08005270 const WindowInfo& windowInfo = *window->getInfo();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005271
5272 // Set focused application.
5273 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005274 window->setFocusable(true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005275
5276 SCOPED_TRACE("Check default value of touch mode");
Arthur Hung72d8dc32020-03-28 00:48:39 +00005277 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005278 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005279 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005280
5281 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005282 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005283 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005284 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005285
5286 SCOPED_TRACE("Disable touch mode");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005287 mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005288 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005289 window->consumeTouchModeEvent(false);
Vishnu Nair47074b82020-08-14 11:54:47 -07005290 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005291 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005292 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005293 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005294
5295 SCOPED_TRACE("Remove the window to trigger focus loss");
Vishnu Nair47074b82020-08-14 11:54:47 -07005296 window->setFocusable(false);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005297 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Harry Cutts33476232023-01-30 19:57:29 +00005298 window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005299
5300 SCOPED_TRACE("Enable touch mode again");
Antonio Kantekea47acb2021-12-23 12:41:25 -08005301 mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00005302 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005303 window->consumeTouchModeEvent(true);
Vishnu Nair47074b82020-08-14 11:54:47 -07005304 window->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005305 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005306 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00005307 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005308
5309 window->assertNoEvents();
5310}
5311
Gang Wange9087892020-01-07 12:17:14 -05005312TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005313 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005314 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5315 "Test window", ADISPLAY_ID_DEFAULT);
Gang Wange9087892020-01-07 12:17:14 -05005316
5317 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07005318 window->setFocusable(true);
Gang Wange9087892020-01-07 12:17:14 -05005319
Arthur Hung72d8dc32020-03-28 00:48:39 +00005320 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005321 setFocusedWindow(window);
5322
Harry Cutts33476232023-01-30 19:57:29 +00005323 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Gang Wange9087892020-01-07 12:17:14 -05005324
Prabir Pradhan678438e2023-04-13 19:32:51 +00005325 const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
5326 mDispatcher->notifyKey(keyArgs);
Gang Wange9087892020-01-07 12:17:14 -05005327
5328 InputEvent* event = window->consume();
5329 ASSERT_NE(event, nullptr);
5330
5331 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5332 ASSERT_NE(verified, nullptr);
5333 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY);
5334
5335 ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos);
5336 ASSERT_EQ(keyArgs.deviceId, verified->deviceId);
5337 ASSERT_EQ(keyArgs.source, verified->source);
5338 ASSERT_EQ(keyArgs.displayId, verified->displayId);
5339
5340 const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified);
5341
5342 ASSERT_EQ(keyArgs.action, verifiedKey.action);
Gang Wange9087892020-01-07 12:17:14 -05005343 ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005344 ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos);
Gang Wange9087892020-01-07 12:17:14 -05005345 ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode);
5346 ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode);
5347 ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState);
5348 ASSERT_EQ(0, verifiedKey.repeatCount);
5349}
5350
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005351TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) {
Chris Yea209fde2020-07-22 13:54:51 -07005352 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005353 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
5354 "Test window", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005355
5356 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5357
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005358 ui::Transform transform;
5359 transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
5360
5361 gui::DisplayInfo displayInfo;
5362 displayInfo.displayId = ADISPLAY_ID_DEFAULT;
5363 displayInfo.transform = transform;
5364
Patrick Williamsd828f302023-04-28 17:52:08 -05005365 mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0});
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005366
Prabir Pradhan678438e2023-04-13 19:32:51 +00005367 const NotifyMotionArgs motionArgs =
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005368 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
5369 ADISPLAY_ID_DEFAULT);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005370 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005371
5372 InputEvent* event = window->consume();
5373 ASSERT_NE(event, nullptr);
5374
5375 std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event);
5376 ASSERT_NE(verified, nullptr);
5377 ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION);
5378
5379 EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos);
5380 EXPECT_EQ(motionArgs.deviceId, verified->deviceId);
5381 EXPECT_EQ(motionArgs.source, verified->source);
5382 EXPECT_EQ(motionArgs.displayId, verified->displayId);
5383
5384 const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified);
5385
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07005386 const vec2 rawXY =
5387 MotionEvent::calculateTransformedXY(motionArgs.source, transform,
5388 motionArgs.pointerCoords[0].getXYValue());
5389 EXPECT_EQ(rawXY.x, verifiedMotion.rawX);
5390 EXPECT_EQ(rawXY.y, verifiedMotion.rawY);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005391 EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005392 EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags);
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08005393 EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos);
Siarhei Vishniakou47040bf2020-02-28 15:03:13 -08005394 EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState);
5395 EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState);
5396}
5397
chaviw09c8d2d2020-08-24 15:48:26 -07005398/**
5399 * Ensure that separate calls to sign the same data are generating the same key.
5400 * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance
5401 * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky
5402 * tests.
5403 */
5404TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) {
5405 KeyEvent event = getTestKeyEvent();
5406 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5407
5408 std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent);
5409 std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent);
5410 ASSERT_EQ(hmac1, hmac2);
5411}
5412
5413/**
5414 * Ensure that changes in VerifiedKeyEvent produce a different hmac.
5415 */
5416TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) {
5417 KeyEvent event = getTestKeyEvent();
5418 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event);
5419 std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent);
5420
5421 verifiedEvent.deviceId += 1;
5422 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5423
5424 verifiedEvent.source += 1;
5425 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5426
5427 verifiedEvent.eventTimeNanos += 1;
5428 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5429
5430 verifiedEvent.displayId += 1;
5431 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5432
5433 verifiedEvent.action += 1;
5434 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5435
5436 verifiedEvent.downTimeNanos += 1;
5437 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5438
5439 verifiedEvent.flags += 1;
5440 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5441
5442 verifiedEvent.keyCode += 1;
5443 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5444
5445 verifiedEvent.scanCode += 1;
5446 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5447
5448 verifiedEvent.metaState += 1;
5449 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5450
5451 verifiedEvent.repeatCount += 1;
5452 ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent));
5453}
5454
Vishnu Nair958da932020-08-21 17:12:37 -07005455TEST_F(InputDispatcherTest, SetFocusedWindow) {
5456 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5457 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005458 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005459 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005460 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005461 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5462
5463 // Top window is also focusable but is not granted focus.
5464 windowTop->setFocusable(true);
5465 windowSecond->setFocusable(true);
5466 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5467 setFocusedWindow(windowSecond);
5468
5469 windowSecond->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005470 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5471 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005472
5473 // Focused window should receive event.
5474 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5475 windowTop->assertNoEvents();
5476}
5477
5478TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) {
5479 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5480 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005481 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005482 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5483
5484 window->setFocusable(true);
5485 // Release channel for window is no longer valid.
5486 window->releaseChannel();
5487 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5488 setFocusedWindow(window);
5489
5490 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005491 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5492 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005493
5494 // window channel is invalid, so it should not receive any input event.
5495 window->assertNoEvents();
5496}
5497
5498TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) {
5499 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5500 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005501 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005502 window->setFocusable(false);
Vishnu Nair958da932020-08-21 17:12:37 -07005503 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5504
Vishnu Nair958da932020-08-21 17:12:37 -07005505 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5506 setFocusedWindow(window);
5507
5508 // Test inject a key down, should timeout.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005509 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
5510 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07005511
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08005512 // window is not focusable, so it should not receive any input event.
Vishnu Nair958da932020-08-21 17:12:37 -07005513 window->assertNoEvents();
5514}
5515
5516TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) {
5517 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5518 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005519 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005520 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005521 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005522 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5523
5524 windowTop->setFocusable(true);
5525 windowSecond->setFocusable(true);
5526 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
5527 setFocusedWindow(windowTop);
5528 windowTop->consumeFocusEvent(true);
5529
Chavi Weingarten847e8512023-03-29 00:26:09 +00005530 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
5531 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005532 windowSecond->consumeFocusEvent(true);
5533 windowTop->consumeFocusEvent(false);
5534
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005535 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5536 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005537
5538 // Focused window should receive event.
5539 windowSecond->consumeKeyDown(ADISPLAY_ID_NONE);
5540}
5541
Chavi Weingarten847e8512023-03-29 00:26:09 +00005542TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) {
Vishnu Nair958da932020-08-21 17:12:37 -07005543 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5544 sp<FakeWindowHandle> windowTop =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005545 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005546 sp<FakeWindowHandle> windowSecond =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005547 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005548 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5549
5550 windowTop->setFocusable(true);
Chavi Weingarten847e8512023-03-29 00:26:09 +00005551 windowSecond->setFocusable(false);
5552 windowTop->editInfo()->focusTransferTarget = windowSecond->getToken();
Vishnu Nair958da932020-08-21 17:12:37 -07005553 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}});
Chavi Weingarten847e8512023-03-29 00:26:09 +00005554 setFocusedWindow(windowTop);
5555 windowTop->consumeFocusEvent(true);
Vishnu Nair958da932020-08-21 17:12:37 -07005556
Chavi Weingarten847e8512023-03-29 00:26:09 +00005557 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
5558 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07005559
5560 // Event should be dropped.
Chavi Weingarten847e8512023-03-29 00:26:09 +00005561 windowTop->consumeKeyDown(ADISPLAY_ID_NONE);
Vishnu Nair958da932020-08-21 17:12:37 -07005562 windowSecond->assertNoEvents();
5563}
5564
5565TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) {
5566 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5567 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005568 sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005569 sp<FakeWindowHandle> previousFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005570 sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow",
5571 ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07005572 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5573
5574 window->setFocusable(true);
5575 previousFocusedWindow->setFocusable(true);
5576 window->setVisible(false);
5577 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}});
5578 setFocusedWindow(previousFocusedWindow);
5579 previousFocusedWindow->consumeFocusEvent(true);
5580
5581 // Requesting focus on invisible window takes focus from currently focused window.
5582 setFocusedWindow(window);
5583 previousFocusedWindow->consumeFocusEvent(false);
5584
5585 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005586 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00005587 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
5588 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07005589
5590 // Window does not get focus event or key down.
5591 window->assertNoEvents();
5592
5593 // Window becomes visible.
5594 window->setVisible(true);
5595 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5596
5597 // Window receives focus event.
5598 window->consumeFocusEvent(true);
5599 // Focused window receives key down.
5600 window->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5601}
5602
Vishnu Nair599f1412021-06-21 10:39:58 -07005603TEST_F(InputDispatcherTest, DisplayRemoved) {
5604 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5605 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005606 sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT);
Vishnu Nair599f1412021-06-21 10:39:58 -07005607 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5608
5609 // window is granted focus.
5610 window->setFocusable(true);
5611 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
5612 setFocusedWindow(window);
5613 window->consumeFocusEvent(true);
5614
5615 // When a display is removed window loses focus.
5616 mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT);
5617 window->consumeFocusEvent(false);
5618}
5619
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005620/**
5621 * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY,
5622 * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top
5623 * of the 'slipperyEnterWindow'.
5624 *
5625 * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such
5626 * a way so that the touched location is no longer covered by the top window.
5627 *
5628 * Next, inject a MOVE event. Because the top window already moved earlier, this event is now
5629 * positioned over the bottom (slipperyEnterWindow) only. And because the top window had
5630 * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive
5631 * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting
5632 * with ACTION_DOWN).
5633 * Thus, the touch has been transferred from the top window into the bottom window, because the top
5634 * window moved itself away from the touched location and had Flag::SLIPPERY.
5635 *
5636 * Even though the top window moved away from the touched location, it is still obscuring the bottom
5637 * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_
5638 * OBSCURED should be set for the MotionEvent that reaches the bottom window.
5639 *
5640 * In this test, we ensure that the event received by the bottom window has
5641 * FLAG_WINDOW_IS_PARTIALLY_OBSCURED.
5642 */
5643TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00005644 constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1;
5645 constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005646
5647 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5648 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
5649
5650 sp<FakeWindowHandle> slipperyExitWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005651 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005652 slipperyExitWindow->setSlippery(true);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005653 // Make sure this one overlaps the bottom window
5654 slipperyExitWindow->setFrame(Rect(25, 25, 75, 75));
5655 // Change the owner uid/pid of the window so that it is considered to be occluding the bottom
5656 // one. Windows with the same owner are not considered to be occluding each other.
5657 slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID);
5658
5659 sp<FakeWindowHandle> slipperyEnterWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005660 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005661 slipperyExitWindow->setFrame(Rect(0, 0, 100, 100));
5662
5663 mDispatcher->setInputWindows(
5664 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5665
5666 // Use notifyMotion instead of injecting to avoid dealing with injection permissions
Prabir Pradhan678438e2023-04-13 19:32:51 +00005667 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
5668 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5669 {{50, 50}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005670 slipperyExitWindow->consumeMotionDown();
5671 slipperyExitWindow->setFrame(Rect(70, 70, 100, 100));
5672 mDispatcher->setInputWindows(
5673 {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}});
5674
Prabir Pradhan678438e2023-04-13 19:32:51 +00005675 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE,
5676 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
5677 {{51, 51}}));
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005678
5679 slipperyExitWindow->consumeMotionCancel();
5680
5681 slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT,
5682 AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
5683}
5684
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07005685/**
5686 * Two windows, one on the left and another on the right. The left window is slippery. The right
5687 * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the
5688 * touch moves from the left window into the right window, the gesture should continue to go to the
5689 * left window. Touch shouldn't slip because the right window can't receive touches. This test
5690 * reproduces a crash.
5691 */
5692TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) {
5693 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5694
5695 sp<FakeWindowHandle> leftSlipperyWindow =
5696 sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT);
5697 leftSlipperyWindow->setSlippery(true);
5698 leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100));
5699
5700 sp<FakeWindowHandle> rightDropTouchesWindow =
5701 sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT);
5702 rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100));
5703 rightDropTouchesWindow->setDropInput(true);
5704
5705 mDispatcher->setInputWindows(
5706 {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}});
5707
5708 // Start touch in the left window
5709 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
5710 .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50))
5711 .build());
5712 leftSlipperyWindow->consumeMotionDown();
5713
5714 // And move it into the right window
5715 mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
5716 .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50))
5717 .build());
5718
5719 // Since the right window isn't eligible to receive input, touch does not slip.
5720 // The left window continues to receive the gesture.
5721 leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE));
5722 rightDropTouchesWindow->assertNoEvents();
5723}
5724
Garfield Tan1c7bc862020-01-28 13:24:04 -08005725class InputDispatcherKeyRepeatTest : public InputDispatcherTest {
5726protected:
5727 static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms
5728 static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms
5729
Chris Yea209fde2020-07-22 13:54:51 -07005730 std::shared_ptr<FakeApplicationHandle> mApp;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005731 sp<FakeWindowHandle> mWindow;
5732
5733 virtual void SetUp() override {
Prabir Pradhana41d2442023-04-20 21:30:40 +00005734 mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005735 mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY);
Prabir Pradhana41d2442023-04-20 21:30:40 +00005736 mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
Prabir Pradhan87112a72023-04-20 19:13:39 +00005737 mDispatcher->requestRefreshConfiguration();
Garfield Tan1c7bc862020-01-28 13:24:04 -08005738 mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
5739 ASSERT_EQ(OK, mDispatcher->start());
5740
5741 setUpWindow();
5742 }
5743
5744 void setUpWindow() {
Chris Yea209fde2020-07-22 13:54:51 -07005745 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005746 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005747
Vishnu Nair47074b82020-08-14 11:54:47 -07005748 mWindow->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005749 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005750 setFocusedWindow(mWindow);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005751 mWindow->consumeFocusEvent(true);
5752 }
5753
Chris Ye2ad95392020-09-01 13:44:44 -07005754 void sendAndConsumeKeyDown(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005755 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005756 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005757 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005758 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005759
5760 // Window should receive key down event.
5761 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
5762 }
5763
5764 void expectKeyRepeatOnce(int32_t repeatCount) {
5765 SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount));
5766 InputEvent* repeatEvent = mWindow->consume();
5767 ASSERT_NE(nullptr, repeatEvent);
5768
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005769 ASSERT_EQ(InputEventType::KEY, repeatEvent->getType());
Garfield Tan1c7bc862020-01-28 13:24:04 -08005770
5771 KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent);
5772 uint32_t eventAction = repeatKeyEvent->getAction();
5773 EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction);
5774 EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount());
5775 }
5776
Chris Ye2ad95392020-09-01 13:44:44 -07005777 void sendAndConsumeKeyUp(int32_t deviceId) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08005778 NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT);
Chris Ye2ad95392020-09-01 13:44:44 -07005779 keyArgs.deviceId = deviceId;
Garfield Tan1c7bc862020-01-28 13:24:04 -08005780 keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event
Prabir Pradhan678438e2023-04-13 19:32:51 +00005781 mDispatcher->notifyKey(keyArgs);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005782
5783 // Window should receive key down event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005784 mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00005785 /*expectedFlags=*/0);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005786 }
5787};
5788
5789TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) {
Harry Cutts33476232023-01-30 19:57:29 +00005790 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005791 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5792 expectKeyRepeatOnce(repeatCount);
5793 }
5794}
5795
5796TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) {
Harry Cutts33476232023-01-30 19:57:29 +00005797 sendAndConsumeKeyDown(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005798 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5799 expectKeyRepeatOnce(repeatCount);
5800 }
Harry Cutts33476232023-01-30 19:57:29 +00005801 sendAndConsumeKeyDown(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005802 /* repeatCount will start from 1 for deviceId 2 */
Garfield Tan1c7bc862020-01-28 13:24:04 -08005803 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5804 expectKeyRepeatOnce(repeatCount);
5805 }
5806}
5807
5808TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005809 sendAndConsumeKeyDown(/*deviceId=*/1);
5810 expectKeyRepeatOnce(/*repeatCount=*/1);
5811 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005812 mWindow->assertNoEvents();
5813}
5814
5815TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005816 sendAndConsumeKeyDown(/*deviceId=*/1);
5817 expectKeyRepeatOnce(/*repeatCount=*/1);
5818 sendAndConsumeKeyDown(/*deviceId=*/2);
5819 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005820 // Stale key up from device 1.
Harry Cutts33476232023-01-30 19:57:29 +00005821 sendAndConsumeKeyUp(/*deviceId=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005822 // Device 2 is still down, keep repeating
Harry Cutts33476232023-01-30 19:57:29 +00005823 expectKeyRepeatOnce(/*repeatCount=*/2);
5824 expectKeyRepeatOnce(/*repeatCount=*/3);
Chris Ye2ad95392020-09-01 13:44:44 -07005825 // Device 2 key up
Harry Cutts33476232023-01-30 19:57:29 +00005826 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005827 mWindow->assertNoEvents();
5828}
5829
5830TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) {
Harry Cutts33476232023-01-30 19:57:29 +00005831 sendAndConsumeKeyDown(/*deviceId=*/1);
5832 expectKeyRepeatOnce(/*repeatCount=*/1);
5833 sendAndConsumeKeyDown(/*deviceId=*/2);
5834 expectKeyRepeatOnce(/*repeatCount=*/1);
Chris Ye2ad95392020-09-01 13:44:44 -07005835 // Device 2 which holds the key repeating goes up, expect the repeating to stop.
Harry Cutts33476232023-01-30 19:57:29 +00005836 sendAndConsumeKeyUp(/*deviceId=*/2);
Chris Ye2ad95392020-09-01 13:44:44 -07005837 // Device 1 still holds key down, but the repeating was already stopped
Garfield Tan1c7bc862020-01-28 13:24:04 -08005838 mWindow->assertNoEvents();
5839}
5840
liushenxiang42232912021-05-21 20:24:09 +08005841TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) {
5842 sendAndConsumeKeyDown(DEVICE_ID);
Harry Cutts33476232023-01-30 19:57:29 +00005843 expectKeyRepeatOnce(/*repeatCount=*/1);
Prabir Pradhan678438e2023-04-13 19:32:51 +00005844 mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID});
liushenxiang42232912021-05-21 20:24:09 +08005845 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT,
5846 AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS);
5847 mWindow->assertNoEvents();
5848}
5849
Garfield Tan1c7bc862020-01-28 13:24:04 -08005850TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005851 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005852 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005853 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5854 InputEvent* repeatEvent = mWindow->consume();
5855 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5856 EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER,
5857 IdGenerator::getSource(repeatEvent->getId()));
5858 }
5859}
5860
5861TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) {
Michael Wrighte1cbbd62023-02-22 19:32:13 +00005862 GTEST_SKIP() << "Flaky test (b/270393106)";
Harry Cutts33476232023-01-30 19:57:29 +00005863 sendAndConsumeKeyDown(/*deviceId=*/1);
Garfield Tan1c7bc862020-01-28 13:24:04 -08005864
5865 std::unordered_set<int32_t> idSet;
5866 for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) {
5867 InputEvent* repeatEvent = mWindow->consume();
5868 ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount;
5869 int32_t id = repeatEvent->getId();
5870 EXPECT_EQ(idSet.end(), idSet.find(id));
5871 idSet.insert(id);
5872 }
5873}
5874
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005875/* Test InputDispatcher for MultiDisplay */
5876class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest {
5877public:
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005878 virtual void SetUp() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005879 InputDispatcherTest::SetUp();
Arthur Hungb92218b2018-08-14 12:00:21 +08005880
Chris Yea209fde2020-07-22 13:54:51 -07005881 application1 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005882 windowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005883 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005884
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005885 // Set focus window for primary display, but focused display would be second one.
5886 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1);
Vishnu Nair47074b82020-08-14 11:54:47 -07005887 windowInPrimary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005888 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005889 setFocusedWindow(windowInPrimary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005890 windowInPrimary->consumeFocusEvent(true);
Arthur Hungb92218b2018-08-14 12:00:21 +08005891
Chris Yea209fde2020-07-22 13:54:51 -07005892 application2 = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10005893 windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005894 sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005895 // Set focus to second display window.
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005896 // Set focus display to second one.
5897 mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID);
5898 // Set focus window for second display.
5899 mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2);
Vishnu Nair47074b82020-08-14 11:54:47 -07005900 windowInSecondary->setFocusable(true);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005901 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
Vishnu Nair958da932020-08-21 17:12:37 -07005902 setFocusedWindow(windowInSecondary);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005903 windowInSecondary->consumeFocusEvent(true);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005904 }
5905
Prabir Pradhan3608aad2019-10-02 17:08:26 -07005906 virtual void TearDown() override {
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005907 InputDispatcherTest::TearDown();
5908
Chris Yea209fde2020-07-22 13:54:51 -07005909 application1.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005910 windowInPrimary.clear();
Chris Yea209fde2020-07-22 13:54:51 -07005911 application2.reset();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005912 windowInSecondary.clear();
5913 }
5914
5915protected:
Chris Yea209fde2020-07-22 13:54:51 -07005916 std::shared_ptr<FakeApplicationHandle> application1;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005917 sp<FakeWindowHandle> windowInPrimary;
Chris Yea209fde2020-07-22 13:54:51 -07005918 std::shared_ptr<FakeApplicationHandle> application2;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005919 sp<FakeWindowHandle> windowInSecondary;
5920};
5921
5922TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) {
5923 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5925 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5926 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005927 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hungb92218b2018-08-14 12:00:21 +08005928 windowInSecondary->assertNoEvents();
5929
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005930 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005931 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5932 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5933 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005934 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005935 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hungb92218b2018-08-14 12:00:21 +08005936}
5937
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005938TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005939 // Test inject a key down with display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005940 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5941 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005942 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005943 windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Tiger Huang721e26f2018-07-24 22:26:19 +08005944 windowInSecondary->assertNoEvents();
5945
5946 // Test inject a key down without display id specified.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005947 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005948 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hungb92218b2018-08-14 12:00:21 +08005949 windowInPrimary->assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005950 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hungb92218b2018-08-14 12:00:21 +08005951
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08005952 // Remove all windows in secondary display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00005953 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}});
Arthur Hungb92218b2018-08-14 12:00:21 +08005954
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005955 // Old focus should receive a cancel event.
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005956 windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE,
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005957 AKEY_EVENT_FLAG_CANCELED);
Arthur Hungb92218b2018-08-14 12:00:21 +08005958
5959 // Test inject a key down, should timeout because of no target window.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08005960 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005961 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Arthur Hungb92218b2018-08-14 12:00:21 +08005962 windowInPrimary->assertNoEvents();
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01005963 windowInSecondary->consumeFocusEvent(false);
Arthur Hungb92218b2018-08-14 12:00:21 +08005964 windowInSecondary->assertNoEvents();
5965}
5966
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005967// Test per-display input monitors for motion event.
5968TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) {
chaviwd1c23182019-12-20 18:44:56 -08005969 FakeMonitorReceiver monitorInPrimary =
5970 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
5971 FakeMonitorReceiver monitorInSecondary =
5972 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005973
5974 // Test touch down on primary display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005975 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5976 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
5977 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005978 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
chaviwd1c23182019-12-20 18:44:56 -08005979 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005980 windowInSecondary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005981 monitorInSecondary.assertNoEvents();
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005982
5983 // Test touch down on second display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005984 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5985 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5986 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005987 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08005988 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08005989 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
chaviwd1c23182019-12-20 18:44:56 -08005990 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005991
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -08005992 // Lift up the touch from the second display
5993 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
5994 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
5995 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
5996 windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID);
5997 monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID);
5998
Arthur Hung2fbf37f2018-09-13 18:16:41 +08005999 // Test inject a non-pointer motion event.
6000 // If specific a display, it will dispatch to the focused window of particular display,
6001 // or it will dispatch to the focused window of focused display.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006002 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6003 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE))
6004 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006005 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006006 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006007 windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006008 monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006009}
6010
6011// Test per-display input monitors for key event.
6012TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) {
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006013 // Input monitor per display.
chaviwd1c23182019-12-20 18:44:56 -08006014 FakeMonitorReceiver monitorInPrimary =
6015 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6016 FakeMonitorReceiver monitorInSecondary =
6017 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006018
6019 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006020 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
6021 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006022 windowInPrimary->assertNoEvents();
chaviwd1c23182019-12-20 18:44:56 -08006023 monitorInPrimary.assertNoEvents();
Siarhei Vishniakouc5ca85c2019-11-15 17:20:00 -08006024 windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE);
chaviwd1c23182019-12-20 18:44:56 -08006025 monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006026}
6027
Vishnu Nair958da932020-08-21 17:12:37 -07006028TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) {
6029 sp<FakeWindowHandle> secondWindowInPrimary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006030 sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT);
Vishnu Nair958da932020-08-21 17:12:37 -07006031 secondWindowInPrimary->setFocusable(true);
6032 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}});
6033 setFocusedWindow(secondWindowInPrimary);
6034 windowInPrimary->consumeFocusEvent(false);
6035 secondWindowInPrimary->consumeFocusEvent(true);
6036
6037 // Test inject a key down.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006038 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
6039 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07006040 windowInPrimary->assertNoEvents();
6041 windowInSecondary->assertNoEvents();
6042 secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT);
6043}
6044
Arthur Hungdfd528e2021-12-08 13:23:04 +00006045TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) {
6046 FakeMonitorReceiver monitorInPrimary =
6047 FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6048 FakeMonitorReceiver monitorInSecondary =
6049 FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID);
6050
6051 // Test touch down on primary display.
6052 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6053 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
6054 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6055 windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6056 monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT);
6057
6058 // Test touch down on second display.
6059 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6060 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID))
6061 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6062 windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID);
6063 monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID);
6064
6065 // Trigger cancel touch.
6066 mDispatcher->cancelCurrentTouch();
6067 windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6068 monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6069 windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID);
6070 monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID);
6071
6072 // Test inject a move motion event, no window/monitor should receive the event.
6073 ASSERT_EQ(InputEventInjectionResult::FAILED,
6074 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6075 ADISPLAY_ID_DEFAULT, {110, 200}))
6076 << "Inject motion event should return InputEventInjectionResult::FAILED";
6077 windowInPrimary->assertNoEvents();
6078 monitorInPrimary.assertNoEvents();
6079
6080 ASSERT_EQ(InputEventInjectionResult::FAILED,
6081 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6082 SECOND_DISPLAY_ID, {110, 200}))
6083 << "Inject motion event should return InputEventInjectionResult::FAILED";
6084 windowInSecondary->assertNoEvents();
6085 monitorInSecondary.assertNoEvents();
6086}
6087
Jackal Guof9696682018-10-05 12:23:23 +08006088class InputFilterTest : public InputDispatcherTest {
6089protected:
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006090 void testNotifyMotion(int32_t displayId, bool expectToBeFiltered,
6091 const ui::Transform& transform = ui::Transform()) {
Jackal Guof9696682018-10-05 12:23:23 +08006092 NotifyMotionArgs motionArgs;
6093
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006094 motionArgs =
6095 generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006096 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006097 motionArgs =
6098 generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006099 mDispatcher->notifyMotion(motionArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006100 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006101 if (expectToBeFiltered) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006102 const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue());
6103 mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy);
Jackal Guof9696682018-10-05 12:23:23 +08006104 } else {
6105 mFakePolicy->assertFilterInputEventWasNotCalled();
6106 }
6107 }
6108
6109 void testNotifyKey(bool expectToBeFiltered) {
6110 NotifyKeyArgs keyArgs;
6111
6112 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006113 mDispatcher->notifyKey(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006114 keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP);
Prabir Pradhan678438e2023-04-13 19:32:51 +00006115 mDispatcher->notifyKey(keyArgs);
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006116 ASSERT_TRUE(mDispatcher->waitForIdle());
Jackal Guof9696682018-10-05 12:23:23 +08006117
6118 if (expectToBeFiltered) {
Siarhei Vishniakou8935a802019-11-15 16:41:44 -08006119 mFakePolicy->assertFilterInputEventWasCalled(keyArgs);
Jackal Guof9696682018-10-05 12:23:23 +08006120 } else {
6121 mFakePolicy->assertFilterInputEventWasNotCalled();
6122 }
6123 }
6124};
6125
6126// Test InputFilter for MotionEvent
6127TEST_F(InputFilterTest, MotionEvent_InputFilter) {
6128 // Since the InputFilter is disabled by default, check if touch events aren't filtered.
6129 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6130 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6131
6132 // Enable InputFilter
6133 mDispatcher->setInputFilterEnabled(true);
6134 // Test touch on both primary and second display, and check if both events are filtered.
6135 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true);
6136 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true);
6137
6138 // Disable InputFilter
6139 mDispatcher->setInputFilterEnabled(false);
6140 // Test touch on both primary and second display, and check if both events aren't filtered.
6141 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false);
6142 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false);
6143}
6144
6145// Test InputFilter for KeyEvent
6146TEST_F(InputFilterTest, KeyEvent_InputFilter) {
6147 // Since the InputFilter is disabled by default, check if key event aren't filtered.
6148 testNotifyKey(/*expectToBeFiltered*/ false);
6149
6150 // Enable InputFilter
6151 mDispatcher->setInputFilterEnabled(true);
6152 // Send a key event, and check if it is filtered.
6153 testNotifyKey(/*expectToBeFiltered*/ true);
6154
6155 // Disable InputFilter
6156 mDispatcher->setInputFilterEnabled(false);
6157 // Send a key event, and check if it isn't filtered.
6158 testNotifyKey(/*expectToBeFiltered*/ false);
6159}
6160
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006161// Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the
6162// logical display coordinate space.
6163TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) {
6164 ui::Transform firstDisplayTransform;
6165 firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1});
6166 ui::Transform secondDisplayTransform;
6167 secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1});
6168
6169 std::vector<gui::DisplayInfo> displayInfos(2);
6170 displayInfos[0].displayId = ADISPLAY_ID_DEFAULT;
6171 displayInfos[0].transform = firstDisplayTransform;
6172 displayInfos[1].displayId = SECOND_DISPLAY_ID;
6173 displayInfos[1].transform = secondDisplayTransform;
6174
Patrick Williamsd828f302023-04-28 17:52:08 -05006175 mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0});
Prabir Pradhan81420cc2021-09-06 10:28:50 -07006176
6177 // Enable InputFilter
6178 mDispatcher->setInputFilterEnabled(true);
6179
6180 // Ensure the correct transforms are used for the displays.
6181 testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform);
6182 testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform);
6183}
6184
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006185class InputFilterInjectionPolicyTest : public InputDispatcherTest {
6186protected:
6187 virtual void SetUp() override {
6188 InputDispatcherTest::SetUp();
6189
6190 /**
6191 * We don't need to enable input filter to test the injected event policy, but we enabled it
6192 * here to make the tests more realistic, since this policy only matters when inputfilter is
6193 * on.
6194 */
6195 mDispatcher->setInputFilterEnabled(true);
6196
6197 std::shared_ptr<InputApplicationHandle> application =
6198 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006199 mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window",
6200 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006201
6202 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
6203 mWindow->setFocusable(true);
6204 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6205 setFocusedWindow(mWindow);
6206 mWindow->consumeFocusEvent(true);
6207 }
6208
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006209 void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6210 int32_t flags) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006211 KeyEvent event;
6212
6213 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6214 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD,
6215 ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A,
Harry Cutts33476232023-01-30 19:57:29 +00006216 KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006217 const int32_t additionalPolicyFlags =
6218 POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT;
6219 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006220 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006221 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6222 policyFlags | additionalPolicyFlags));
6223
6224 InputEvent* received = mWindow->consume();
6225 ASSERT_NE(nullptr, received);
6226 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006227 ASSERT_EQ(received->getType(), InputEventType::KEY);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006228 KeyEvent& keyEvent = static_cast<KeyEvent&>(*received);
6229 ASSERT_EQ(flags, keyEvent.getFlags());
6230 }
6231
6232 void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId,
6233 int32_t flags) {
6234 MotionEvent event;
6235 PointerProperties pointerProperties[1];
6236 PointerCoords pointerCoords[1];
6237 pointerProperties[0].clear();
6238 pointerProperties[0].id = 0;
6239 pointerCoords[0].clear();
6240 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300);
6241 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400);
6242
6243 ui::Transform identityTransform;
6244 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC);
6245 event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN,
6246 DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0,
6247 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE,
6248 identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07006249 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime,
Evan Rosky09576692021-07-01 12:22:09 -07006250 eventTime,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006251 /*pointerCount*/ 1, pointerProperties, pointerCoords);
6252
6253 const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER;
6254 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00006255 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006256 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms,
6257 policyFlags | additionalPolicyFlags));
6258
6259 InputEvent* received = mWindow->consume();
6260 ASSERT_NE(nullptr, received);
6261 ASSERT_EQ(resolvedDeviceId, received->getDeviceId());
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006262 ASSERT_EQ(received->getType(), InputEventType::MOTION);
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006263 MotionEvent& motionEvent = static_cast<MotionEvent&>(*received);
6264 ASSERT_EQ(flags, motionEvent.getFlags());
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006265 }
6266
6267private:
6268 sp<FakeWindowHandle> mWindow;
6269};
6270
6271TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) {
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006272 // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input
6273 // filter. Without it, the event will no different from a regularly injected event, and the
6274 // injected device id will be overwritten.
Harry Cutts33476232023-01-30 19:57:29 +00006275 testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
6276 /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006277}
6278
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006279TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006280 testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006281 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006282 AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
6283}
6284
6285TEST_F(InputFilterInjectionPolicyTest,
6286 MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) {
6287 testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY,
Harry Cutts33476232023-01-30 19:57:29 +00006288 /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3,
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00006289 AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006290}
6291
6292TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) {
Harry Cutts33476232023-01-30 19:57:29 +00006293 testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3,
6294 /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0);
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00006295}
6296
chaviwfd6d3512019-03-25 13:23:49 -07006297class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006298 virtual void SetUp() override {
chaviwfd6d3512019-03-25 13:23:49 -07006299 InputDispatcherTest::SetUp();
6300
Chris Yea209fde2020-07-22 13:54:51 -07006301 std::shared_ptr<FakeApplicationHandle> application =
6302 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006303 mUnfocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006304 sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006305 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
chaviwfd6d3512019-03-25 13:23:49 -07006306
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006307 mFocusedWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006308 sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006309 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
chaviwfd6d3512019-03-25 13:23:49 -07006310
6311 // Set focused application.
6312 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
Vishnu Nair47074b82020-08-14 11:54:47 -07006313 mFocusedWindow->setFocusable(true);
chaviwfd6d3512019-03-25 13:23:49 -07006314
6315 // Expect one focus window exist in display.
Arthur Hung72d8dc32020-03-28 00:48:39 +00006316 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006317 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006318 mFocusedWindow->consumeFocusEvent(true);
chaviwfd6d3512019-03-25 13:23:49 -07006319 }
6320
Prabir Pradhan3608aad2019-10-02 17:08:26 -07006321 virtual void TearDown() override {
chaviwfd6d3512019-03-25 13:23:49 -07006322 InputDispatcherTest::TearDown();
6323
6324 mUnfocusedWindow.clear();
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006325 mFocusedWindow.clear();
chaviwfd6d3512019-03-25 13:23:49 -07006326 }
6327
6328protected:
6329 sp<FakeWindowHandle> mUnfocusedWindow;
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006330 sp<FakeWindowHandle> mFocusedWindow;
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006331 static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60};
chaviwfd6d3512019-03-25 13:23:49 -07006332};
6333
6334// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6335// DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received
6336// the onPointerDownOutsideFocus callback.
6337TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006338 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006339 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6340 {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006341 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006342 mUnfocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006343
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006344 ASSERT_TRUE(mDispatcher->waitForIdle());
chaviwfd6d3512019-03-25 13:23:49 -07006345 mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken());
6346}
6347
6348// Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action
6349// DOWN on the window that doesn't have focus. Ensure no window received the
6350// onPointerDownOutsideFocus callback.
6351TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006352 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006353 injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20}))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006354 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006355 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006356
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006357 ASSERT_TRUE(mDispatcher->waitForIdle());
6358 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006359}
6360
6361// Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't
6362// have focus. Ensure no window received the onPointerDownOutsideFocus callback.
6363TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006364 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6365 injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006366 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006367 mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
chaviwfd6d3512019-03-25 13:23:49 -07006368
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006369 ASSERT_TRUE(mDispatcher->waitForIdle());
6370 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006371}
6372
6373// Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action
6374// DOWN on the window that already has focus. Ensure no window received the
6375// onPointerDownOutsideFocus callback.
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10006376TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006377 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoub9b15352019-11-26 13:19:26 -08006378 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -07006379 FOCUSED_WINDOW_TOUCH_POINT))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006380 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakou03aee2a2020-04-13 20:44:54 -07006381 mFocusedWindow->consumeMotionDown();
chaviwfd6d3512019-03-25 13:23:49 -07006382
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006383 ASSERT_TRUE(mDispatcher->waitForIdle());
6384 mFakePolicy->assertOnPointerDownWasNotCalled();
chaviwfd6d3512019-03-25 13:23:49 -07006385}
6386
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006387// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
6388// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
6389TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
6390 const MotionEvent event =
6391 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
6392 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006393 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20))
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08006394 .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
6395 .build();
6396 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
6397 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
6398 mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
6399
6400 ASSERT_TRUE(mDispatcher->waitForIdle());
6401 mFakePolicy->assertOnPointerDownWasNotCalled();
6402 // Ensure that the unfocused window did not receive any FOCUS events.
6403 mUnfocusedWindow->assertNoEvents();
6404}
6405
chaviwaf87b3e2019-10-01 16:59:28 -07006406// These tests ensures we can send touch events to a single client when there are multiple input
6407// windows that point to the same client token.
6408class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {
6409 virtual void SetUp() override {
6410 InputDispatcherTest::SetUp();
6411
Chris Yea209fde2020-07-22 13:54:51 -07006412 std::shared_ptr<FakeApplicationHandle> application =
6413 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006414 mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1",
6415 ADISPLAY_ID_DEFAULT);
chaviwaf87b3e2019-10-01 16:59:28 -07006416 mWindow1->setFrame(Rect(0, 0, 100, 100));
6417
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006418 mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2",
6419 ADISPLAY_ID_DEFAULT, mWindow1->getToken());
chaviwaf87b3e2019-10-01 16:59:28 -07006420 mWindow2->setFrame(Rect(100, 100, 200, 200));
6421
Arthur Hung72d8dc32020-03-28 00:48:39 +00006422 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
chaviwaf87b3e2019-10-01 16:59:28 -07006423 }
6424
6425protected:
6426 sp<FakeWindowHandle> mWindow1;
6427 sp<FakeWindowHandle> mWindow2;
6428
6429 // Helper function to convert the point from screen coordinates into the window's space
chaviw3277faf2021-05-19 16:45:23 -05006430 static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) {
chaviw1ff3d1e2020-07-01 15:53:47 -07006431 vec2 vals = windowInfo->transform.transform(point.x, point.y);
6432 return {vals.x, vals.y};
chaviwaf87b3e2019-10-01 16:59:28 -07006433 }
6434
6435 void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction,
6436 const std::vector<PointF>& points) {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01006437 const std::string name = window->getName();
chaviwaf87b3e2019-10-01 16:59:28 -07006438 InputEvent* event = window->consume();
6439
6440 ASSERT_NE(nullptr, event) << name.c_str()
6441 << ": consumer should have returned non-NULL event.";
6442
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07006443 ASSERT_EQ(InputEventType::MOTION, event->getType())
6444 << name.c_str() << ": expected MotionEvent, got " << *event;
chaviwaf87b3e2019-10-01 16:59:28 -07006445
6446 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00006447 assertMotionAction(expectedAction, motionEvent.getAction());
Siarhei Vishniakoue9349e72022-12-02 11:39:20 -08006448 ASSERT_EQ(points.size(), motionEvent.getPointerCount());
chaviwaf87b3e2019-10-01 16:59:28 -07006449
6450 for (size_t i = 0; i < points.size(); i++) {
6451 float expectedX = points[i].x;
6452 float expectedY = points[i].y;
6453
6454 EXPECT_EQ(expectedX, motionEvent.getX(i))
6455 << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str()
6456 << ", got " << motionEvent.getX(i);
6457 EXPECT_EQ(expectedY, motionEvent.getY(i))
6458 << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str()
6459 << ", got " << motionEvent.getY(i);
6460 }
6461 }
chaviw9eaa22c2020-07-01 16:21:27 -07006462
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -08006463 void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints,
chaviw9eaa22c2020-07-01 16:21:27 -07006464 std::vector<PointF> expectedPoints) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00006465 mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN,
6466 ADISPLAY_ID_DEFAULT, touchedPoints));
chaviw9eaa22c2020-07-01 16:21:27 -07006467
6468 // Always consume from window1 since it's the window that has the InputReceiver
6469 consumeMotionEvent(mWindow1, action, expectedPoints);
6470 }
chaviwaf87b3e2019-10-01 16:59:28 -07006471};
6472
6473TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) {
6474 // Touch Window 1
6475 PointF touchedPoint = {10, 10};
6476 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006477 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006478
6479 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006480 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006481
6482 // Touch Window 2
6483 touchedPoint = {150, 150};
6484 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006485 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006486}
6487
chaviw9eaa22c2020-07-01 16:21:27 -07006488TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) {
6489 // Set scale value for window2
chaviwaf87b3e2019-10-01 16:59:28 -07006490 mWindow2->setWindowScale(0.5f, 0.5f);
6491
6492 // Touch Window 1
6493 PointF touchedPoint = {10, 10};
6494 PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006495 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006496 // Release touch on Window 1
chaviw9eaa22c2020-07-01 16:21:27 -07006497 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006498
6499 // Touch Window 2
6500 touchedPoint = {150, 150};
6501 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
chaviw9eaa22c2020-07-01 16:21:27 -07006502 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
6503 touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006504
chaviw9eaa22c2020-07-01 16:21:27 -07006505 // Update the transform so rotation is set
6506 mWindow2->setWindowTransform(0, -1, 1, 0);
6507 expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint);
6508 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint});
chaviwaf87b3e2019-10-01 16:59:28 -07006509}
6510
chaviw9eaa22c2020-07-01 16:21:27 -07006511TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006512 mWindow2->setWindowScale(0.5f, 0.5f);
6513
6514 // Touch Window 1
6515 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6516 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006517 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006518
6519 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006520 touchedPoints.push_back(PointF{150, 150});
6521 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006522 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006523
chaviw9eaa22c2020-07-01 16:21:27 -07006524 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006525 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006526 expectedPoints.pop_back();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006527
chaviw9eaa22c2020-07-01 16:21:27 -07006528 // Update the transform so rotation is set for Window 2
6529 mWindow2->setWindowTransform(0, -1, 1, 0);
6530 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006531 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006532}
6533
chaviw9eaa22c2020-07-01 16:21:27 -07006534TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006535 mWindow2->setWindowScale(0.5f, 0.5f);
6536
6537 // Touch Window 1
6538 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6539 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006540 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006541
6542 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006543 touchedPoints.push_back(PointF{150, 150});
6544 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006545
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006546 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006547
6548 // Move both windows
6549 touchedPoints = {{20, 20}, {175, 175}};
6550 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6551 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6552
chaviw9eaa22c2020-07-01 16:21:27 -07006553 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006554
chaviw9eaa22c2020-07-01 16:21:27 -07006555 // Release Window 2
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006556 touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006557 expectedPoints.pop_back();
6558
6559 // Touch Window 2
6560 mWindow2->setWindowTransform(0, -1, 1, 0);
6561 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006562 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
chaviw9eaa22c2020-07-01 16:21:27 -07006563
6564 // Move both windows
6565 touchedPoints = {{20, 20}, {175, 175}};
6566 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6567 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6568
6569 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006570}
6571
6572TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) {
6573 mWindow1->setWindowScale(0.5f, 0.5f);
6574
6575 // Touch Window 1
6576 std::vector<PointF> touchedPoints = {PointF{10, 10}};
6577 std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])};
chaviw9eaa22c2020-07-01 16:21:27 -07006578 touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006579
6580 // Touch Window 2
chaviw9eaa22c2020-07-01 16:21:27 -07006581 touchedPoints.push_back(PointF{150, 150});
6582 expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1]));
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006583
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08006584 touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006585
6586 // Move both windows
6587 touchedPoints = {{20, 20}, {175, 175}};
6588 expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]),
6589 getPointInWindow(mWindow2->getInfo(), touchedPoints[1])};
6590
chaviw9eaa22c2020-07-01 16:21:27 -07006591 touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00006592}
6593
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07006594/**
6595 * When one of the windows is slippery, the touch should not slip into the other window with the
6596 * same input channel.
6597 */
6598TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) {
6599 mWindow1->setSlippery(true);
6600 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6601
6602 // Touch down in window 1
6603 mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6604 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6605 consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}});
6606
6607 // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip.
6608 // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN
6609 // getting generated.
6610 mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6611 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6612
6613 consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}});
6614}
6615
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07006616/**
6617 * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and
6618 * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window
6619 * that the pointer is hovering over may have a different transform.
6620 */
6621TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) {
6622 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}});
6623
6624 // Start hover in window 1
6625 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN,
6626 ADISPLAY_ID_DEFAULT, {{50, 50}}));
6627 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6628 {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})});
6629
6630 // Move hover to window 2.
6631 mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
6632 ADISPLAY_ID_DEFAULT, {{150, 150}}));
6633
6634 consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}});
6635 consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER,
6636 {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})});
6637}
6638
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006639class InputDispatcherSingleWindowAnr : public InputDispatcherTest {
6640 virtual void SetUp() override {
6641 InputDispatcherTest::SetUp();
6642
Chris Yea209fde2020-07-22 13:54:51 -07006643 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006644 mApplication->setDispatchingTimeout(20ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006645 mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow",
6646 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006647 mWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05006648 mWindow->setDispatchingTimeout(30ms);
Vishnu Nair47074b82020-08-14 11:54:47 -07006649 mWindow->setFocusable(true);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006650
6651 // Set focused application.
6652 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
6653
6654 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07006655 setFocusedWindow(mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006656 mWindow->consumeFocusEvent(true);
6657 }
6658
6659 virtual void TearDown() override {
6660 InputDispatcherTest::TearDown();
6661 mWindow.clear();
6662 }
6663
6664protected:
Chris Yea209fde2020-07-22 13:54:51 -07006665 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006666 sp<FakeWindowHandle> mWindow;
6667 static constexpr PointF WINDOW_LOCATION = {20, 20};
6668
6669 void tapOnWindow() {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006670 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006671 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6672 WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006673 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006674 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6675 WINDOW_LOCATION));
6676 }
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006677
6678 sp<FakeWindowHandle> addSpyWindow() {
6679 sp<FakeWindowHandle> spy =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006680 sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006681 spy->setTrustedOverlay(true);
6682 spy->setFocusable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006683 spy->setSpy(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006684 spy->setDispatchingTimeout(30ms);
6685 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}});
6686 return spy;
6687 }
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006688};
6689
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006690// Send a tap and respond, which should not cause an ANR.
6691TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) {
6692 tapOnWindow();
6693 mWindow->consumeMotionDown();
6694 mWindow->consumeMotionUp();
6695 ASSERT_TRUE(mDispatcher->waitForIdle());
6696 mFakePolicy->assertNotifyAnrWasNotCalled();
6697}
6698
6699// Send a regular key and respond, which should not cause an ANR.
6700TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) {
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006701 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006702 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
6703 ASSERT_TRUE(mDispatcher->waitForIdle());
6704 mFakePolicy->assertNotifyAnrWasNotCalled();
6705}
6706
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006707TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) {
6708 mWindow->setFocusable(false);
6709 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6710 mWindow->consumeFocusEvent(false);
6711
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006712 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006713 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6714 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
6715 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006716 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05006717 // Key will not go to window because we have no focused window.
6718 // The 'no focused window' ANR timer should start instead.
6719
6720 // Now, the focused application goes away.
6721 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr);
6722 // The key should get dropped and there should be no ANR.
6723
6724 ASSERT_TRUE(mDispatcher->waitForIdle());
6725 mFakePolicy->assertNotifyAnrWasNotCalled();
6726}
6727
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006728// Send an event to the app and have the app not respond right away.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006729// When ANR is raised, policy will tell the dispatcher to cancel the events for that window.
6730// So InputDispatcher will enqueue ACTION_CANCEL event as well.
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006731TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006732 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006733 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6734 WINDOW_LOCATION));
6735
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006736 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN
6737 ASSERT_TRUE(sequenceNum);
6738 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006739 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006740
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006741 mWindow->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006742 mWindow->consumeMotionEvent(
6743 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006744 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006745 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006746}
6747
6748// Send a key to the app and have the app not respond right away.
6749TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) {
6750 // Inject a key, and don't respond - expect that ANR is called.
Prabir Pradhan93f342c2021-03-11 15:05:30 -08006751 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher));
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006752 std::optional<uint32_t> sequenceNum = mWindow->receiveEvent();
6753 ASSERT_TRUE(sequenceNum);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006754 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006755 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006756 ASSERT_TRUE(mDispatcher->waitForIdle());
6757}
6758
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006759// We have a focused application, but no focused window
6760TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006761 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006762 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6763 mWindow->consumeFocusEvent(false);
6764
6765 // taps on the window work as normal
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006766 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006767 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6768 WINDOW_LOCATION));
6769 ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown());
6770 mDispatcher->waitForIdle();
6771 mFakePolicy->assertNotifyAnrWasNotCalled();
6772
6773 // Once a focused event arrives, we get an ANR for this application
6774 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6775 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006776 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006777 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6778 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006779 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006780 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Vishnu Naire4df8752022-09-08 09:17:55 -07006781 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006782 ASSERT_TRUE(mDispatcher->waitForIdle());
6783}
6784
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006785/**
6786 * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window,
6787 * there will not be an ANR.
6788 */
6789TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
6790 mWindow->setFocusable(false);
6791 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6792 mWindow->consumeFocusEvent(false);
6793
6794 KeyEvent event;
6795 const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
6796 std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();
6797
6798 // Define a valid key down event that is stale (too old).
6799 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE,
6800 INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A,
Harry Cutts33476232023-01-30 19:57:29 +00006801 AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006802
6803 const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER;
6804
6805 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006806 mDispatcher->injectInputEvent(&event, /*targetUid=*/{},
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08006807 InputEventInjectionSync::WAIT_FOR_RESULT,
6808 INJECT_EVENT_TIMEOUT, policyFlags);
6809 ASSERT_EQ(InputEventInjectionResult::FAILED, result)
6810 << "Injection should fail because the event is stale";
6811
6812 ASSERT_TRUE(mDispatcher->waitForIdle());
6813 mFakePolicy->assertNotifyAnrWasNotCalled();
6814 mWindow->assertNoEvents();
6815}
6816
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006817// We have a focused application, but no focused window
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006818// Make sure that we don't notify policy twice about the same ANR.
6819TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006820 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006821 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6822 mWindow->consumeFocusEvent(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006823
6824 // Once a focused event arrives, we get an ANR for this application
6825 // We specify the injection timeout to be smaller than the application timeout, to ensure that
6826 // injection times out (instead of failing).
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006827 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006828 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
6829 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006830 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Vishnu Naire4df8752022-09-08 09:17:55 -07006831 const std::chrono::duration appTimeout =
6832 mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6833 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006834
Vishnu Naire4df8752022-09-08 09:17:55 -07006835 std::this_thread::sleep_for(appTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006836 // ANR should not be raised again. It is up to policy to do that if it desires.
6837 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006838
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006839 // If we now get a focused window, the ANR should stop, but the policy handles that via
6840 // 'notifyFocusChanged' callback. This is implemented in the policy so we can't test it here.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006841 ASSERT_TRUE(mDispatcher->waitForIdle());
6842}
6843
6844// We have a focused application, but no focused window
6845TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) {
Vishnu Nair47074b82020-08-14 11:54:47 -07006846 mWindow->setFocusable(false);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006847 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
6848 mWindow->consumeFocusEvent(false);
6849
6850 // Once a focused event arrives, we get an ANR for this application
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006851 const InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00006852 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006853 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
6854 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006855
Vishnu Naire4df8752022-09-08 09:17:55 -07006856 const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
6857 mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006858
6859 // Future focused events get dropped right away
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006860 ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006861 ASSERT_TRUE(mDispatcher->waitForIdle());
6862 mWindow->assertNoEvents();
6863}
6864
6865/**
6866 * Ensure that the implementation is valid. Since we are using multiset to keep track of the
6867 * ANR timeouts, we are allowing entries with identical timestamps in the same connection.
6868 * If we process 1 of the events, but ANR on the second event with the same timestamp,
6869 * the ANR mechanism should still work.
6870 *
6871 * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the
6872 * DOWN event, while not responding on the second one.
6873 */
6874TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) {
6875 nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
6876 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
6877 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6878 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6879 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006880 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006881
6882 // Now send ACTION_UP, with identical timestamp
6883 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
6884 ADISPLAY_ID_DEFAULT, WINDOW_LOCATION,
6885 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
6886 AMOTION_EVENT_INVALID_CURSOR_POSITION},
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006887 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006888
6889 // We have now sent down and up. Let's consume first event and then ANR on the second.
6890 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6891 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006892 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006893}
6894
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006895// A spy window can receive an ANR
6896TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) {
6897 sp<FakeWindowHandle> spy = addSpyWindow();
6898
6899 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6900 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6901 WINDOW_LOCATION));
6902 mWindow->consumeMotionDown();
6903
6904 std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN
6905 ASSERT_TRUE(sequenceNum);
6906 const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006907 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006908
6909 spy->finishEvent(*sequenceNum);
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08006910 spy->consumeMotionEvent(
6911 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006912 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006913 mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid());
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006914}
6915
6916// If an app is not responding to a key event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006917// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006918TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) {
6919 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006920
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006921 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6922 injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006923 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08006924 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006925
6926 // Stuck on the ACTION_UP
6927 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006928 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006929
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006930 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006931 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006932 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6933 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006934
6935 mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6936 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006937 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006938 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006939 spy->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006940}
6941
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006942// If an app is not responding to a motion event, spy windows should continue to receive
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006943// new motion events
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006944TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) {
6945 sp<FakeWindowHandle> spy = addSpyWindow();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006946
6947 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006948 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6949 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006950
6951 mWindow->consumeMotionDown();
6952 // Stuck on the ACTION_UP
6953 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08006954 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006955
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006956 // New tap will go to the spy window, but not to the window
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006957 tapOnWindow();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006958 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6959 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006960
6961 mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion
6962 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08006963 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006964 mWindow->assertNoEvents();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006965 spy->assertNoEvents();
6966}
6967
6968TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) {
6969 mDispatcher->setMonitorDispatchingTimeoutForTest(30ms);
6970
6971 FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT);
6972
6973 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
6974 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
6975 WINDOW_LOCATION));
6976
6977 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
6978 const std::optional<uint32_t> consumeSeq = monitor.receiveEvent();
6979 ASSERT_TRUE(consumeSeq);
6980
Prabir Pradhanedd96402022-02-15 01:46:16 -08006981 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006982
6983 monitor.finishEvent(*consumeSeq);
6984 monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT);
6985
6986 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006987 mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006988}
6989
6990// If a window is unresponsive, then you get anr. if the window later catches up and starts to
6991// process events, you don't get an anr. When the window later becomes unresponsive again, you
6992// get an ANR again.
6993// 1. tap -> block on ACTION_UP -> receive ANR
6994// 2. consume all pending events (= queue becomes healthy again)
6995// 3. tap again -> block on ACTION_UP again -> receive ANR second time
6996TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) {
6997 tapOnWindow();
6998
6999 mWindow->consumeMotionDown();
7000 // Block on ACTION_UP
7001 const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007002 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007003 mWindow->consumeMotionUp(); // Now the connection should be healthy again
7004 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007005 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007006 mWindow->assertNoEvents();
7007
7008 tapOnWindow();
7009 mWindow->consumeMotionDown();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007010 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007011 mWindow->consumeMotionUp();
7012
7013 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007014 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007015 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007016 mWindow->assertNoEvents();
7017}
7018
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007019// If a connection remains unresponsive for a while, make sure policy is only notified once about
7020// it.
7021TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007023 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7024 WINDOW_LOCATION));
7025
7026 const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007027 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007028 std::this_thread::sleep_for(windowTimeout);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007029 // 'notifyConnectionUnresponsive' should only be called once per connection
7030 mFakePolicy->assertNotifyAnrWasNotCalled();
7031 // When the ANR happened, dispatcher should abort the current event stream via ACTION_CANCEL
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007032 mWindow->consumeMotionDown();
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08007033 mWindow->consumeMotionEvent(
7034 AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT)));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007035 mWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007036 mDispatcher->waitForIdle();
Prabir Pradhanedd96402022-02-15 01:46:16 -08007037 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007038 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007039}
7040
7041/**
7042 * If a window is processing a motion event, and then a key event comes in, the key event should
7043 * not to to the focused window until the motion is processed.
7044 *
7045 * Warning!!!
7046 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7047 * and the injection timeout that we specify when injecting the key.
7048 * We must have the injection timeout (10ms) be smaller than
7049 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7050 *
7051 * If that value changes, this test should also change.
7052 */
7053TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) {
7054 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7055 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7056
7057 tapOnWindow();
7058 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7059 ASSERT_TRUE(downSequenceNum);
7060 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7061 ASSERT_TRUE(upSequenceNum);
7062 // Don't finish the events yet, and send a key
7063 // Injection will "succeed" because we will eventually give up and send the key to the focused
7064 // window even if motions are still being processed. But because the injection timeout is short,
7065 // we will receive INJECTION_TIMED_OUT as the result.
7066
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007067 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007068 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007069 InputEventInjectionSync::WAIT_FOR_RESULT, 10ms);
7070 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007071 // Key will not be sent to the window, yet, because the window is still processing events
7072 // and the key remains pending, waiting for the touch events to be processed
7073 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7074 ASSERT_FALSE(keySequenceNum);
7075
7076 std::this_thread::sleep_for(500ms);
7077 // if we wait long enough though, dispatcher will give up, and still send the key
7078 // to the focused window, even though we have not yet finished the motion event
7079 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7080 mWindow->finishEvent(*downSequenceNum);
7081 mWindow->finishEvent(*upSequenceNum);
7082}
7083
7084/**
7085 * If a window is processing a motion event, and then a key event comes in, the key event should
7086 * not go to the focused window until the motion is processed.
7087 * If then a new motion comes in, then the pending key event should be going to the currently
7088 * focused window right away.
7089 */
7090TEST_F(InputDispatcherSingleWindowAnr,
7091 PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) {
7092 mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering
7093 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
7094
7095 tapOnWindow();
7096 std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent();
7097 ASSERT_TRUE(downSequenceNum);
7098 std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent();
7099 ASSERT_TRUE(upSequenceNum);
7100 // Don't finish the events yet, and send a key
7101 // Injection is async, so it will succeed
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007102 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007103 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7104 InputEventInjectionSync::NONE));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007105 // At this point, key is still pending, and should not be sent to the application yet.
7106 std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent();
7107 ASSERT_FALSE(keySequenceNum);
7108
7109 // Now tap down again. It should cause the pending key to go to the focused window right away.
7110 tapOnWindow();
7111 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd
7112 // the other events yet. We can finish events in any order.
7113 mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN
7114 mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP
7115 mWindow->consumeMotionDown();
7116 mWindow->consumeMotionUp();
7117 mWindow->assertNoEvents();
7118}
7119
7120class InputDispatcherMultiWindowAnr : public InputDispatcherTest {
7121 virtual void SetUp() override {
7122 InputDispatcherTest::SetUp();
7123
Chris Yea209fde2020-07-22 13:54:51 -07007124 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007125 mApplication->setDispatchingTimeout(10ms);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007126 mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused",
7127 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007128 mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007129 // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007130 mUnfocusedWindow->setWatchOutsideTouch(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007131
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007132 mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused",
7133 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua7d36fd2020-06-30 19:32:39 -05007134 mFocusedWindow->setDispatchingTimeout(30ms);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007135 mFocusedWindow->setFrame(Rect(50, 50, 100, 100));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007136
7137 // Set focused application.
7138 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
Vishnu Nair47074b82020-08-14 11:54:47 -07007139 mFocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007140
7141 // Expect one focus window exist in display.
7142 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007143 setFocusedWindow(mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007144 mFocusedWindow->consumeFocusEvent(true);
7145 }
7146
7147 virtual void TearDown() override {
7148 InputDispatcherTest::TearDown();
7149
7150 mUnfocusedWindow.clear();
7151 mFocusedWindow.clear();
7152 }
7153
7154protected:
Chris Yea209fde2020-07-22 13:54:51 -07007155 std::shared_ptr<FakeApplicationHandle> mApplication;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007156 sp<FakeWindowHandle> mUnfocusedWindow;
7157 sp<FakeWindowHandle> mFocusedWindow;
7158 static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20};
7159 static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75};
7160 static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40};
7161
7162 void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); }
7163
7164 void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); }
7165
7166private:
7167 void tap(const PointF& location) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007168 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007169 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7170 location));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007171 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007172 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7173 location));
7174 }
7175};
7176
7177// If we have 2 windows that are both unresponsive, the one with the shortest timeout
7178// should be ANR'd first.
7179TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007180 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007181 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7182 FOCUSED_WINDOW_LOCATION))
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007183 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007184 mFocusedWindow->consumeMotionDown();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007185 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007186 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007187 // We consumed all events, so no ANR
7188 ASSERT_TRUE(mDispatcher->waitForIdle());
7189 mFakePolicy->assertNotifyAnrWasNotCalled();
7190
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007191 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007192 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7193 FOCUSED_WINDOW_LOCATION));
7194 std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent();
7195 ASSERT_TRUE(unfocusedSequenceNum);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007196
7197 const std::chrono::duration timeout =
7198 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007199 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007200 // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event
7201 // sequence to make it consistent
7202 mFocusedWindow->consumeMotionCancel();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007203 mUnfocusedWindow->finishEvent(*unfocusedSequenceNum);
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007204 mFocusedWindow->consumeMotionDown();
7205 // This cancel is generated because the connection was unresponsive
7206 mFocusedWindow->consumeMotionCancel();
7207 mFocusedWindow->assertNoEvents();
7208 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007209 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007210 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7211 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007212 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007213}
7214
7215// If we have 2 windows with identical timeouts that are both unresponsive,
7216// it doesn't matter which order they should have ANR.
7217// But we should receive ANR for both.
7218TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) {
7219 // Set the timeout for unfocused window to match the focused window
7220 mUnfocusedWindow->setDispatchingTimeout(10ms);
7221 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7222
7223 tapOnFocusedWindow();
7224 // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window
Prabir Pradhanedd96402022-02-15 01:46:16 -08007225 sp<IBinder> anrConnectionToken1, anrConnectionToken2;
7226 ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms));
7227 ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007228
7229 // We don't know which window will ANR first. But both of them should happen eventually.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007230 ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 ||
7231 mFocusedWindow->getToken() == anrConnectionToken2);
7232 ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 ||
7233 mUnfocusedWindow->getToken() == anrConnectionToken2);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007234
7235 ASSERT_TRUE(mDispatcher->waitForIdle());
7236 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007237
7238 mFocusedWindow->consumeMotionDown();
7239 mFocusedWindow->consumeMotionUp();
7240 mUnfocusedWindow->consumeMotionOutside();
7241
Prabir Pradhanedd96402022-02-15 01:46:16 -08007242 sp<IBinder> responsiveToken1, responsiveToken2;
7243 ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken());
7244 ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007245
7246 // Both applications should be marked as responsive, in any order
7247 ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 ||
7248 mFocusedWindow->getToken() == responsiveToken2);
7249 ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 ||
7250 mUnfocusedWindow->getToken() == responsiveToken2);
7251 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007252}
7253
7254// If a window is already not responding, the second tap on the same window should be ignored.
7255// We should also log an error to account for the dropped event (not tested here).
7256// At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events.
7257TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) {
7258 tapOnFocusedWindow();
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007259 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007260 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007261 // Receive the events, but don't respond
7262 std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN
7263 ASSERT_TRUE(downEventSequenceNum);
7264 std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP
7265 ASSERT_TRUE(upEventSequenceNum);
7266 const std::chrono::duration timeout =
7267 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007268 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007269
7270 // Tap once again
7271 // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007272 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007273 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7274 FOCUSED_WINDOW_LOCATION));
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007275 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007276 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7277 FOCUSED_WINDOW_LOCATION));
7278 // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a
7279 // valid touch target
7280 mUnfocusedWindow->assertNoEvents();
7281
7282 // Consume the first tap
7283 mFocusedWindow->finishEvent(*downEventSequenceNum);
7284 mFocusedWindow->finishEvent(*upEventSequenceNum);
7285 ASSERT_TRUE(mDispatcher->waitForIdle());
7286 // The second tap did not go to the focused window
7287 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007288 // Since all events are finished, connection should be deemed healthy again
Prabir Pradhanedd96402022-02-15 01:46:16 -08007289 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7290 mFocusedWindow->getPid());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007291 mFakePolicy->assertNotifyAnrWasNotCalled();
7292}
7293
7294// If you tap outside of all windows, there will not be ANR
7295TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) {
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007296 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007297 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7298 LOCATION_OUTSIDE_ALL_WINDOWS));
7299 ASSERT_TRUE(mDispatcher->waitForIdle());
7300 mFakePolicy->assertNotifyAnrWasNotCalled();
7301}
7302
7303// Since the focused window is paused, tapping on it should not produce any events
7304TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) {
7305 mFocusedWindow->setPaused(true);
7306 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}});
7307
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007308 ASSERT_EQ(InputEventInjectionResult::FAILED,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007309 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7310 FOCUSED_WINDOW_LOCATION));
7311
7312 std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT));
7313 ASSERT_TRUE(mDispatcher->waitForIdle());
7314 // Should not ANR because the window is paused, and touches shouldn't go to it
7315 mFakePolicy->assertNotifyAnrWasNotCalled();
7316
7317 mFocusedWindow->assertNoEvents();
7318 mUnfocusedWindow->assertNoEvents();
7319}
7320
7321/**
7322 * If a window is processing a motion event, and then a key event comes in, the key event should
7323 * not to to the focused window until the motion is processed.
7324 * If a different window becomes focused at this time, the key should go to that window instead.
7325 *
7326 * Warning!!!
7327 * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT
7328 * and the injection timeout that we specify when injecting the key.
7329 * We must have the injection timeout (10ms) be smaller than
7330 * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms).
7331 *
7332 * If that value changes, this test should also change.
7333 */
7334TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) {
7335 // Set a long ANR timeout to prevent it from triggering
7336 mFocusedWindow->setDispatchingTimeout(2s);
7337 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7338
7339 tapOnUnfocusedWindow();
7340 std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent();
7341 ASSERT_TRUE(downSequenceNum);
7342 std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent();
7343 ASSERT_TRUE(upSequenceNum);
7344 // Don't finish the events yet, and send a key
7345 // Injection will succeed because we will eventually give up and send the key to the focused
7346 // window even if motions are still being processed.
7347
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007348 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007349 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7350 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007351 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007352 // Key will not be sent to the window, yet, because the window is still processing events
7353 // and the key remains pending, waiting for the touch events to be processed
7354 std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent();
7355 ASSERT_FALSE(keySequenceNum);
7356
7357 // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there
Vishnu Nair47074b82020-08-14 11:54:47 -07007358 mFocusedWindow->setFocusable(false);
7359 mUnfocusedWindow->setFocusable(true);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007360 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
Vishnu Nair958da932020-08-21 17:12:37 -07007361 setFocusedWindow(mUnfocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007362
7363 // Focus events should precede the key events
7364 mUnfocusedWindow->consumeFocusEvent(true);
7365 mFocusedWindow->consumeFocusEvent(false);
7366
7367 // Finish the tap events, which should unblock dispatcher
7368 mUnfocusedWindow->finishEvent(*downSequenceNum);
7369 mUnfocusedWindow->finishEvent(*upSequenceNum);
7370
7371 // Now that all queues are cleared and no backlog in the connections, the key event
7372 // can finally go to the newly focused "mUnfocusedWindow".
7373 mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7374 mFocusedWindow->assertNoEvents();
7375 mUnfocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007376 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007377}
7378
7379// When the touch stream is split across 2 windows, and one of them does not respond,
7380// then ANR should be raised and the touch should be canceled for the unresponsive window.
7381// The other window should not be affected by that.
7382TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) {
7383 // Touch Window 1
Prabir Pradhan678438e2023-04-13 19:32:51 +00007384 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7385 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7386 {FOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007387 mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE,
Harry Cutts33476232023-01-30 19:57:29 +00007388 ADISPLAY_ID_DEFAULT, /*flags=*/0);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007389
7390 // Touch Window 2
Prabir Pradhan678438e2023-04-13 19:32:51 +00007391 mDispatcher->notifyMotion(
7392 generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7393 {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007394
7395 const std::chrono::duration timeout =
7396 mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT);
Prabir Pradhanedd96402022-02-15 01:46:16 -08007397 mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007398
7399 mUnfocusedWindow->consumeMotionDown();
7400 mFocusedWindow->consumeMotionDown();
7401 // Focused window may or may not receive ACTION_MOVE
7402 // But it should definitely receive ACTION_CANCEL due to the ANR
7403 InputEvent* event;
7404 std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event);
7405 ASSERT_TRUE(moveOrCancelSequenceNum);
7406 mFocusedWindow->finishEvent(*moveOrCancelSequenceNum);
7407 ASSERT_NE(nullptr, event);
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07007408 ASSERT_EQ(event->getType(), InputEventType::MOTION);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007409 MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
7410 if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) {
7411 mFocusedWindow->consumeMotionCancel();
7412 } else {
7413 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction());
7414 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007415 ASSERT_TRUE(mDispatcher->waitForIdle());
Prabir Pradhanedd96402022-02-15 01:46:16 -08007416 mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(),
7417 mFocusedWindow->getPid());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007418
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007419 mUnfocusedWindow->assertNoEvents();
7420 mFocusedWindow->assertNoEvents();
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05007421 mFakePolicy->assertNotifyAnrWasNotCalled();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07007422}
7423
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007424/**
7425 * If we have no focused window, and a key comes in, we start the ANR timer.
7426 * The focused application should add a focused window before the timer runs out to prevent ANR.
7427 *
7428 * If the user touches another application during this time, the key should be dropped.
7429 * Next, if a new focused window comes in, without toggling the focused application,
7430 * then no ANR should occur.
7431 *
7432 * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication',
7433 * but in some cases the policy may not update the focused application.
7434 */
7435TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
7436 std::shared_ptr<FakeApplicationHandle> focusedApplication =
7437 std::make_shared<FakeApplicationHandle>();
7438 focusedApplication->setDispatchingTimeout(60ms);
7439 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
7440 // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
7441 mFocusedWindow->setFocusable(false);
7442
7443 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7444 mFocusedWindow->consumeFocusEvent(false);
7445
7446 // Send a key. The ANR timer should start because there is no focused window.
7447 // 'focusedApplication' will get blamed if this timer completes.
7448 // Key will not be sent anywhere because we have no focused window. It will remain pending.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007449 InputEventInjectionResult result =
Harry Cutts33476232023-01-30 19:57:29 +00007450 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7451 InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms,
7452 /*allowKeyRepeat=*/false);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result);
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007454
7455 // Wait until dispatcher starts the "no focused window" timer. If we don't wait here,
7456 // then the injected touches won't cause the focused event to get dropped.
7457 // The dispatcher only checks for whether the queue should be pruned upon queueing.
7458 // If we inject the touch right away and the ANR timer hasn't started, the touch event would
7459 // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'.
7460 // For this test, it means that the key would get delivered to the window once it becomes
7461 // focused.
7462 std::this_thread::sleep_for(10ms);
7463
7464 // Touch unfocused window. This should force the pending key to get dropped.
Prabir Pradhan678438e2023-04-13 19:32:51 +00007465 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7466 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7467 {UNFOCUSED_WINDOW_LOCATION}));
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05007468
7469 // We do not consume the motion right away, because that would require dispatcher to first
7470 // process (== drop) the key event, and by that time, ANR will be raised.
7471 // Set the focused window first.
7472 mFocusedWindow->setFocusable(true);
7473 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}});
7474 setFocusedWindow(mFocusedWindow);
7475 mFocusedWindow->consumeFocusEvent(true);
7476 // We do not call "setFocusedApplication" here, even though the newly focused window belongs
7477 // to another application. This could be a bug / behaviour in the policy.
7478
7479 mUnfocusedWindow->consumeMotionDown();
7480
7481 ASSERT_TRUE(mDispatcher->waitForIdle());
7482 // Should not ANR because we actually have a focused window. It was just added too slowly.
7483 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled());
7484}
7485
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007486// These tests ensure we cannot send touch events to a window that's positioned behind a window
7487// that has feature NO_INPUT_CHANNEL.
7488// Layout:
7489// Top (closest to user)
7490// mNoInputWindow (above all windows)
7491// mBottomWindow
7492// Bottom (furthest from user)
7493class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest {
7494 virtual void SetUp() override {
7495 InputDispatcherTest::SetUp();
7496
7497 mApplication = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007498 mNoInputWindow =
7499 sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7500 "Window without input channel", ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +00007501 /*token=*/std::make_optional<sp<IBinder>>(nullptr));
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007502 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007503 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7504 // It's perfectly valid for this window to not have an associated input channel
7505
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007506 mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window",
7507 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007508 mBottomWindow->setFrame(Rect(0, 0, 100, 100));
7509
7510 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7511 }
7512
7513protected:
7514 std::shared_ptr<FakeApplicationHandle> mApplication;
7515 sp<FakeWindowHandle> mNoInputWindow;
7516 sp<FakeWindowHandle> mBottomWindow;
7517};
7518
7519TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) {
7520 PointF touchedPoint = {10, 10};
7521
Prabir Pradhan678438e2023-04-13 19:32:51 +00007522 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7523 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7524 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007525
7526 mNoInputWindow->assertNoEvents();
7527 // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have
7528 // an input channel, it is not marked as FLAG_NOT_TOUCHABLE,
7529 // and therefore should prevent mBottomWindow from receiving touches
7530 mBottomWindow->assertNoEvents();
7531}
7532
7533/**
7534 * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel,
7535 * ensure that this window does not receive any touches, and blocks touches to windows underneath.
7536 */
7537TEST_F(InputDispatcherMultiWindowOcclusionTests,
7538 NoInputChannelFeature_DropsTouchesWithValidChannel) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007539 mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher,
7540 "Window with input channel and NO_INPUT_CHANNEL",
7541 ADISPLAY_ID_DEFAULT);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007542
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007543 mNoInputWindow->setNoInputChannel(true);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007544 mNoInputWindow->setFrame(Rect(0, 0, 100, 100));
7545 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}});
7546
7547 PointF touchedPoint = {10, 10};
7548
Prabir Pradhan678438e2023-04-13 19:32:51 +00007549 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7550 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7551 {touchedPoint}));
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05007552
7553 mNoInputWindow->assertNoEvents();
7554 mBottomWindow->assertNoEvents();
7555}
7556
Vishnu Nair958da932020-08-21 17:12:37 -07007557class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest {
7558protected:
7559 std::shared_ptr<FakeApplicationHandle> mApp;
7560 sp<FakeWindowHandle> mWindow;
7561 sp<FakeWindowHandle> mMirror;
7562
7563 virtual void SetUp() override {
7564 InputDispatcherTest::SetUp();
7565 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007566 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
7567 mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror",
7568 ADISPLAY_ID_DEFAULT, mWindow->getToken());
Vishnu Nair958da932020-08-21 17:12:37 -07007569 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7570 mWindow->setFocusable(true);
7571 mMirror->setFocusable(true);
7572 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7573 }
7574};
7575
7576TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) {
7577 // Request focus on a mirrored window
7578 setFocusedWindow(mMirror);
7579
7580 // window gets focused
7581 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007582 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7583 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007584 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
7585}
7586
7587// A focused & mirrored window remains focused only if the window and its mirror are both
7588// focusable.
7589TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) {
7590 setFocusedWindow(mMirror);
7591
7592 // window gets focused
7593 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007594 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7595 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007596 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007597 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7598 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007599 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7600
7601 mMirror->setFocusable(false);
7602 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7603
7604 // window loses focus since one of the windows associated with the token in not focusable
7605 mWindow->consumeFocusEvent(false);
7606
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007607 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7608 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007609 mWindow->assertNoEvents();
7610}
7611
7612// A focused & mirrored window remains focused until the window and its mirror both become
7613// invisible.
7614TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) {
7615 setFocusedWindow(mMirror);
7616
7617 // window gets focused
7618 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007619 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7620 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007621 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007622 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7623 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007624 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7625
7626 mMirror->setVisible(false);
7627 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7628
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007629 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7630 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007631 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007632 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7633 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007634 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7635
7636 mWindow->setVisible(false);
7637 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7638
7639 // window loses focus only after all windows associated with the token become invisible.
7640 mWindow->consumeFocusEvent(false);
7641
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007642 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7643 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007644 mWindow->assertNoEvents();
7645}
7646
7647// A focused & mirrored window remains focused until both windows are removed.
7648TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) {
7649 setFocusedWindow(mMirror);
7650
7651 // window gets focused
7652 mWindow->consumeFocusEvent(true);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007653 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7654 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007655 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007656 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7657 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007658 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7659
7660 // single window is removed but the window token remains focused
7661 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}});
7662
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007663 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
7664 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007665 mWindow->consumeKeyDown(ADISPLAY_ID_NONE);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007666 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
7667 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
Vishnu Nair958da932020-08-21 17:12:37 -07007668 mWindow->consumeKeyUp(ADISPLAY_ID_NONE);
7669
7670 // Both windows are removed
7671 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}});
7672 mWindow->consumeFocusEvent(false);
7673
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007674 ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher))
7675 << "Inject key event should return InputEventInjectionResult::TIMED_OUT";
Vishnu Nair958da932020-08-21 17:12:37 -07007676 mWindow->assertNoEvents();
7677}
7678
7679// Focus request can be pending until one window becomes visible.
7680TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) {
7681 // Request focus on an invisible mirror.
7682 mWindow->setVisible(false);
7683 mMirror->setVisible(false);
7684 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7685 setFocusedWindow(mMirror);
7686
7687 // Injected key goes to pending queue.
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08007688 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Harry Cutts33476232023-01-30 19:57:29 +00007689 injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
7690 InputEventInjectionSync::NONE));
Vishnu Nair958da932020-08-21 17:12:37 -07007691
7692 mMirror->setVisible(true);
7693 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}});
7694
7695 // window gets focused
7696 mWindow->consumeFocusEvent(true);
7697 // window gets the pending key event
7698 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
7699}
Prabir Pradhan99987712020-11-10 18:43:05 -08007700
7701class InputDispatcherPointerCaptureTests : public InputDispatcherTest {
7702protected:
7703 std::shared_ptr<FakeApplicationHandle> mApp;
7704 sp<FakeWindowHandle> mWindow;
7705 sp<FakeWindowHandle> mSecondWindow;
7706
7707 void SetUp() override {
7708 InputDispatcherTest::SetUp();
7709 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007710 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007711 mWindow->setFocusable(true);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007712 mSecondWindow =
7713 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Prabir Pradhan99987712020-11-10 18:43:05 -08007714 mSecondWindow->setFocusable(true);
7715
7716 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
7717 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}});
7718
7719 setFocusedWindow(mWindow);
7720 mWindow->consumeFocusEvent(true);
7721 }
7722
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007723 void notifyPointerCaptureChanged(const PointerCaptureRequest& request) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007724 mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request));
Prabir Pradhan99987712020-11-10 18:43:05 -08007725 }
7726
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007727 PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window,
7728 bool enabled) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007729 mDispatcher->requestPointerCapture(window->getToken(), enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007730 auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled);
7731 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007732 window->consumeCaptureEvent(enabled);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007733 return request;
Prabir Pradhan99987712020-11-10 18:43:05 -08007734 }
7735};
7736
7737TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) {
7738 // Ensure that capture cannot be obtained for unfocused windows.
7739 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
7740 mFakePolicy->assertSetPointerCaptureNotCalled();
7741 mSecondWindow->assertNoEvents();
7742
7743 // Ensure that capture can be enabled from the focus window.
7744 requestAndVerifyPointerCapture(mWindow, true);
7745
7746 // Ensure that capture cannot be disabled from a window that does not have capture.
7747 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false);
7748 mFakePolicy->assertSetPointerCaptureNotCalled();
7749
7750 // Ensure that capture can be disabled from the window with capture.
7751 requestAndVerifyPointerCapture(mWindow, false);
7752}
7753
7754TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007755 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007756
7757 setFocusedWindow(mSecondWindow);
7758
7759 // Ensure that the capture disabled event was sent first.
7760 mWindow->consumeCaptureEvent(false);
7761 mWindow->consumeFocusEvent(false);
7762 mSecondWindow->consumeFocusEvent(true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007763 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007764
7765 // Ensure that additional state changes from InputReader are not sent to the window.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007766 notifyPointerCaptureChanged({});
7767 notifyPointerCaptureChanged(request);
7768 notifyPointerCaptureChanged({});
Prabir Pradhan99987712020-11-10 18:43:05 -08007769 mWindow->assertNoEvents();
7770 mSecondWindow->assertNoEvents();
7771 mFakePolicy->assertSetPointerCaptureNotCalled();
7772}
7773
7774TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007775 auto request = requestAndVerifyPointerCapture(mWindow, true);
Prabir Pradhan99987712020-11-10 18:43:05 -08007776
7777 // InputReader unexpectedly disables and enables pointer capture.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007778 notifyPointerCaptureChanged({});
7779 notifyPointerCaptureChanged(request);
Prabir Pradhan99987712020-11-10 18:43:05 -08007780
7781 // Ensure that Pointer Capture is disabled.
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007782 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan99987712020-11-10 18:43:05 -08007783 mWindow->consumeCaptureEvent(false);
7784 mWindow->assertNoEvents();
7785}
7786
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007787TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) {
7788 requestAndVerifyPointerCapture(mWindow, true);
7789
7790 // The first window loses focus.
7791 setFocusedWindow(mSecondWindow);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007792 mFakePolicy->assertSetPointerCaptureCalled(false);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007793 mWindow->consumeCaptureEvent(false);
7794
7795 // Request Pointer Capture from the second window before the notification from InputReader
7796 // arrives.
7797 mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007798 auto request = mFakePolicy->assertSetPointerCaptureCalled(true);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007799
7800 // InputReader notifies Pointer Capture was disabled (because of the focus change).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007801 notifyPointerCaptureChanged({});
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007802
7803 // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request).
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007804 notifyPointerCaptureChanged(request);
Prabir Pradhan167e6d92021-02-04 16:18:17 -08007805
7806 mSecondWindow->consumeFocusEvent(true);
7807 mSecondWindow->consumeCaptureEvent(true);
7808}
7809
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007810TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) {
7811 // App repeatedly enables and disables capture.
7812 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7813 auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7814 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7815 mFakePolicy->assertSetPointerCaptureCalled(false);
7816 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7817 auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7818
7819 // InputReader notifies that PointerCapture has been enabled for the first request. Since the
7820 // first request is now stale, this should do nothing.
7821 notifyPointerCaptureChanged(firstRequest);
7822 mWindow->assertNoEvents();
7823
7824 // InputReader notifies that the second request was enabled.
7825 notifyPointerCaptureChanged(secondRequest);
7826 mWindow->consumeCaptureEvent(true);
7827}
7828
Prabir Pradhan7092e262022-05-03 16:51:09 +00007829TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) {
7830 requestAndVerifyPointerCapture(mWindow, true);
7831
7832 // App toggles pointer capture off and on.
7833 mDispatcher->requestPointerCapture(mWindow->getToken(), false);
7834 mFakePolicy->assertSetPointerCaptureCalled(false);
7835
7836 mDispatcher->requestPointerCapture(mWindow->getToken(), true);
7837 auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true);
7838
7839 // InputReader notifies that the latest "enable" request was processed, while skipping over the
7840 // preceding "disable" request.
7841 notifyPointerCaptureChanged(enableRequest);
7842
7843 // Since pointer capture was never disabled during the rapid toggle, the window does not receive
7844 // any notifications.
7845 mWindow->assertNoEvents();
7846}
7847
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007848class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest {
7849protected:
7850 constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8;
Bernardo Rufino7393d172021-02-26 13:56:11 +00007851
7852 constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9;
7853 static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY);
7854
7855 constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7;
7856 static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7857
7858 // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold
7859 constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5;
7860 static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY);
7861 static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) <
7862 MAXIMUM_OBSCURING_OPACITY);
7863
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007864 static const int32_t TOUCHED_APP_UID = 10001;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007865 static const int32_t APP_B_UID = 10002;
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007866 static const int32_t APP_C_UID = 10003;
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007867
7868 sp<FakeWindowHandle> mTouchWindow;
7869
7870 virtual void SetUp() override {
7871 InputDispatcherTest::SetUp();
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007872 mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched");
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007873 mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY);
7874 }
7875
7876 virtual void TearDown() override {
7877 InputDispatcherTest::TearDown();
7878 mTouchWindow.clear();
7879 }
7880
chaviw3277faf2021-05-19 16:45:23 -05007881 sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode,
7882 float alpha = 1.0f) {
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007883 sp<FakeWindowHandle> window = getWindow(uid, name);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007884 window->setTouchable(false);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007885 window->setTouchOcclusionMode(mode);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007886 window->setAlpha(alpha);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007887 return window;
7888 }
7889
7890 sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) {
7891 std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>();
7892 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007893 sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT);
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007894 // Generate an arbitrary PID based on the UID
7895 window->setOwnerInfo(1777 + (uid % 10000), uid);
7896 return window;
7897 }
7898
7899 void touch(const std::vector<PointF>& points = {PointF{100, 200}}) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00007900 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
7901 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
7902 points));
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007903 }
7904};
7905
7906TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007907 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007908 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007909 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007910
7911 touch();
7912
7913 mTouchWindow->assertNoEvents();
7914}
7915
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007916TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufino7393d172021-02-26 13:56:11 +00007917 WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) {
7918 const sp<FakeWindowHandle>& w =
7919 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f);
7920 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7921
7922 touch();
7923
7924 mTouchWindow->assertNoEvents();
7925}
7926
7927TEST_F(InputDispatcherUntrustedTouchesTest,
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007928 WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) {
7929 const sp<FakeWindowHandle>& w =
7930 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
7931 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7932
7933 touch();
7934
7935 w->assertNoEvents();
7936}
7937
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007938TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007939 const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW);
7940 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007941
7942 touch();
7943
7944 mTouchWindow->consumeAnyMotionDown();
7945}
7946
7947TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007948 const sp<FakeWindowHandle>& w =
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007949 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007950 w->setFrame(Rect(0, 0, 50, 50));
7951 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007952
7953 touch({PointF{100, 100}});
7954
7955 mTouchWindow->consumeAnyMotionDown();
7956}
7957
7958TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007959 const sp<FakeWindowHandle>& w =
Bernardo Rufino6d52e542021-02-15 18:38:10 +00007960 getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00007961 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7962
7963 touch();
7964
7965 mTouchWindow->consumeAnyMotionDown();
7966}
7967
7968TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) {
7969 const sp<FakeWindowHandle>& w =
7970 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7971 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00007972
7973 touch();
7974
7975 mTouchWindow->consumeAnyMotionDown();
7976}
7977
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007978TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) {
7979 const sp<FakeWindowHandle>& w =
7980 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
7981 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7982
7983 touch();
7984
7985 w->assertNoEvents();
7986}
7987
7988/**
7989 * This is important to make sure apps can't indirectly learn the position of touches (outside vs
7990 * inside) while letting them pass-through. Note that even though touch passes through the occluding
7991 * window, the occluding window will still receive ACTION_OUTSIDE event.
7992 */
7993TEST_F(InputDispatcherUntrustedTouchesTest,
7994 WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) {
7995 const sp<FakeWindowHandle>& w =
7996 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08007997 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00007998 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
7999
8000 touch();
8001
8002 w->consumeMotionOutside();
8003}
8004
8005TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) {
8006 const sp<FakeWindowHandle>& w =
8007 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008008 w->setWatchOutsideTouch(true);
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008009 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8010
8011 touch();
8012
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08008013 w->consumeMotionOutsideWithZeroedCoords();
Bernardo Rufinoa43a5a42021-02-17 12:21:14 +00008014}
8015
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008016TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008017 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008018 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8019 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008020 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8021
8022 touch();
8023
8024 mTouchWindow->consumeAnyMotionDown();
8025}
8026
8027TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) {
8028 const sp<FakeWindowHandle>& w =
8029 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8030 MAXIMUM_OBSCURING_OPACITY);
8031 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008032
8033 touch();
8034
8035 mTouchWindow->consumeAnyMotionDown();
8036}
8037
8038TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) {
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008039 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008040 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8041 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008042 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8043
8044 touch();
8045
8046 mTouchWindow->assertNoEvents();
8047}
8048
8049TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) {
8050 // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91
8051 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008052 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8053 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008054 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008055 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8056 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008057 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8058
8059 touch();
8060
8061 mTouchWindow->assertNoEvents();
8062}
8063
8064TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) {
8065 // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75
8066 const sp<FakeWindowHandle>& w1 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008067 getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY,
8068 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008069 const sp<FakeWindowHandle>& w2 =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008070 getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY,
8071 OPACITY_FAR_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008072 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8073
8074 touch();
8075
8076 mTouchWindow->consumeAnyMotionDown();
8077}
8078
8079TEST_F(InputDispatcherUntrustedTouchesTest,
8080 WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) {
8081 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008082 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8083 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008084 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008085 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8086 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008087 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8088
8089 touch();
8090
8091 mTouchWindow->consumeAnyMotionDown();
8092}
8093
8094TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) {
8095 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008096 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8097 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008098 const sp<FakeWindowHandle>& wC =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008099 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8100 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino1d0d1f22021-02-12 15:08:43 +00008101 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
Bernardo Rufinoc3b7b8c2021-01-29 17:38:07 +00008102
8103 touch();
8104
8105 mTouchWindow->assertNoEvents();
8106}
8107
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008108TEST_F(InputDispatcherUntrustedTouchesTest,
8109 WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) {
8110 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008111 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8112 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008113 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008114 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8115 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008116 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8117
8118 touch();
8119
8120 mTouchWindow->assertNoEvents();
8121}
8122
8123TEST_F(InputDispatcherUntrustedTouchesTest,
8124 WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) {
8125 const sp<FakeWindowHandle>& wA =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008126 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8127 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008128 const sp<FakeWindowHandle>& wB =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008129 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8130 OPACITY_BELOW_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008131 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}});
8132
8133 touch();
8134
8135 mTouchWindow->consumeAnyMotionDown();
8136}
8137
8138TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) {
8139 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008140 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY,
8141 OPACITY_ABOVE_THRESHOLD);
Bernardo Rufino6d52e542021-02-15 18:38:10 +00008142 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8143
8144 touch();
8145
8146 mTouchWindow->consumeAnyMotionDown();
8147}
8148
8149TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) {
8150 const sp<FakeWindowHandle>& w =
8151 getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED);
8152 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8153
8154 touch();
8155
8156 mTouchWindow->consumeAnyMotionDown();
8157}
8158
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008159TEST_F(InputDispatcherUntrustedTouchesTest,
8160 OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) {
8161 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8162 const sp<FakeWindowHandle>& w =
8163 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f);
8164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8165
8166 touch();
8167
8168 mTouchWindow->assertNoEvents();
8169}
8170
8171TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) {
8172 mDispatcher->setMaximumObscuringOpacityForTouch(0.0f);
8173 const sp<FakeWindowHandle>& w =
8174 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f);
8175 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8176
8177 touch();
8178
8179 mTouchWindow->consumeAnyMotionDown();
8180}
8181
8182TEST_F(InputDispatcherUntrustedTouchesTest,
8183 OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) {
8184 mDispatcher->setMaximumObscuringOpacityForTouch(1.0f);
8185 const sp<FakeWindowHandle>& w =
Bernardo Rufino7393d172021-02-26 13:56:11 +00008186 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8187 OPACITY_ABOVE_THRESHOLD);
8188 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8189
8190 touch();
8191
8192 mTouchWindow->consumeAnyMotionDown();
8193}
8194
8195TEST_F(InputDispatcherUntrustedTouchesTest,
8196 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) {
8197 const sp<FakeWindowHandle>& w1 =
8198 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8199 OPACITY_BELOW_THRESHOLD);
8200 const sp<FakeWindowHandle>& w2 =
8201 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY,
8202 OPACITY_BELOW_THRESHOLD);
8203 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}});
8204
8205 touch();
8206
8207 mTouchWindow->assertNoEvents();
8208}
8209
8210/**
8211 * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the
8212 * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold
8213 * (which alone would result in allowing touches) does not affect the blocking behavior.
8214 */
8215TEST_F(InputDispatcherUntrustedTouchesTest,
8216 WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) {
8217 const sp<FakeWindowHandle>& wB =
8218 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED,
8219 OPACITY_BELOW_THRESHOLD);
8220 const sp<FakeWindowHandle>& wC =
8221 getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY,
8222 OPACITY_BELOW_THRESHOLD);
8223 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}});
8224
8225 touch();
8226
8227 mTouchWindow->assertNoEvents();
8228}
8229
8230/**
8231 * This test is testing that a window from a different UID but with same application token doesn't
8232 * block the touch. Apps can share the application token for close UI collaboration for example.
8233 */
8234TEST_F(InputDispatcherUntrustedTouchesTest,
8235 WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) {
8236 const sp<FakeWindowHandle>& w =
8237 getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED);
8238 w->setApplicationToken(mTouchWindow->getApplicationToken());
Bernardo Rufinoccd3dd62021-02-15 18:47:42 +00008239 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}});
8240
8241 touch();
8242
8243 mTouchWindow->consumeAnyMotionDown();
8244}
8245
arthurhungb89ccb02020-12-30 16:19:01 +08008246class InputDispatcherDragTests : public InputDispatcherTest {
8247protected:
8248 std::shared_ptr<FakeApplicationHandle> mApp;
8249 sp<FakeWindowHandle> mWindow;
8250 sp<FakeWindowHandle> mSecondWindow;
8251 sp<FakeWindowHandle> mDragWindow;
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008252 sp<FakeWindowHandle> mSpyWindow;
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008253 // Mouse would force no-split, set the id as non-zero to verify if drag state could track it.
8254 static constexpr int32_t MOUSE_POINTER_ID = 1;
arthurhungb89ccb02020-12-30 16:19:01 +08008255
8256 void SetUp() override {
8257 InputDispatcherTest::SetUp();
8258 mApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008259 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008260 mWindow->setFrame(Rect(0, 0, 100, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008261
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008262 mSecondWindow =
8263 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
arthurhungb89ccb02020-12-30 16:19:01 +08008264 mSecondWindow->setFrame(Rect(100, 0, 200, 100));
arthurhungb89ccb02020-12-30 16:19:01 +08008265
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008266 mSpyWindow =
8267 sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008268 mSpyWindow->setSpy(true);
8269 mSpyWindow->setTrustedOverlay(true);
8270 mSpyWindow->setFrame(Rect(0, 0, 200, 100));
8271
arthurhungb89ccb02020-12-30 16:19:01 +08008272 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008273 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008274 }
8275
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008276 void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
8277 switch (fromSource) {
8278 case AINPUT_SOURCE_TOUCHSCREEN:
8279 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8280 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN,
8281 ADISPLAY_ID_DEFAULT, {50, 50}))
8282 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8283 break;
8284 case AINPUT_SOURCE_STYLUS:
8285 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8286 injectMotionEvent(
8287 mDispatcher,
8288 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8289 AINPUT_SOURCE_STYLUS)
8290 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008291 .pointer(PointerBuilder(0, ToolType::STYLUS)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008292 .x(50)
8293 .y(50))
8294 .build()));
8295 break;
8296 case AINPUT_SOURCE_MOUSE:
8297 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8298 injectMotionEvent(
8299 mDispatcher,
8300 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
8301 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8302 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008303 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008304 .x(50)
8305 .y(50))
8306 .build()));
8307 break;
8308 default:
8309 FAIL() << "Source " << fromSource << " doesn't support drag and drop";
8310 }
arthurhungb89ccb02020-12-30 16:19:01 +08008311
8312 // Window should receive motion event.
8313 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008314 // Spy window should also receive motion event
8315 mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Arthur Hung54745652022-04-20 07:17:41 +00008316 }
8317
8318 // Start performing drag, we will create a drag window and transfer touch to it.
8319 // @param sendDown : if true, send a motion down on first window before perform drag and drop.
8320 // Returns true on success.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008321 bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) {
Arthur Hung54745652022-04-20 07:17:41 +00008322 if (sendDown) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008323 injectDown(fromSource);
Arthur Hung54745652022-04-20 07:17:41 +00008324 }
arthurhungb89ccb02020-12-30 16:19:01 +08008325
8326 // The drag window covers the entire display
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008327 mDragWindow =
8328 sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008329 mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}});
arthurhungb89ccb02020-12-30 16:19:01 +08008330 mDispatcher->setInputWindows(
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008331 {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}});
arthurhungb89ccb02020-12-30 16:19:01 +08008332
8333 // Transfer touch focus to the drag window
Arthur Hung54745652022-04-20 07:17:41 +00008334 bool transferred =
8335 mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(),
Harry Cutts33476232023-01-30 19:57:29 +00008336 /*isDragDrop=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00008337 if (transferred) {
8338 mWindow->consumeMotionCancel();
8339 mDragWindow->consumeMotionDown();
8340 }
8341 return transferred;
arthurhungb89ccb02020-12-30 16:19:01 +08008342 }
8343};
8344
8345TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008346 startDrag();
arthurhungb89ccb02020-12-30 16:19:01 +08008347
8348 // Move on window.
8349 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8350 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8351 ADISPLAY_ID_DEFAULT, {50, 50}))
8352 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8353 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8354 mWindow->consumeDragEvent(false, 50, 50);
8355 mSecondWindow->assertNoEvents();
8356
8357 // Move to another window.
8358 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8359 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8360 ADISPLAY_ID_DEFAULT, {150, 50}))
8361 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8362 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8363 mWindow->consumeDragEvent(true, 150, 50);
8364 mSecondWindow->consumeDragEvent(false, 50, 50);
8365
8366 // Move back to original window.
8367 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8368 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8369 ADISPLAY_ID_DEFAULT, {50, 50}))
8370 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8371 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8372 mWindow->consumeDragEvent(false, 50, 50);
8373 mSecondWindow->consumeDragEvent(true, -50, 50);
8374
8375 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8376 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50}))
8377 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8378 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8379 mWindow->assertNoEvents();
8380 mSecondWindow->assertNoEvents();
8381}
8382
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008383TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008384 startDrag();
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008385
8386 // No cancel event after drag start
8387 mSpyWindow->assertNoEvents();
8388
8389 const MotionEvent secondFingerDownEvent =
8390 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8391 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008392 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8393 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60))
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00008394 .build();
8395 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8396 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8397 InputEventInjectionSync::WAIT_FOR_RESULT))
8398 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8399
8400 // Receives cancel for first pointer after next pointer down
8401 mSpyWindow->consumeMotionCancel();
8402 mSpyWindow->consumeMotionDown();
8403
8404 mSpyWindow->assertNoEvents();
8405}
8406
arthurhungf452d0b2021-01-06 00:19:52 +08008407TEST_F(InputDispatcherDragTests, DragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008408 startDrag();
arthurhungf452d0b2021-01-06 00:19:52 +08008409
8410 // Move on window.
8411 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8412 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8413 ADISPLAY_ID_DEFAULT, {50, 50}))
8414 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8415 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8416 mWindow->consumeDragEvent(false, 50, 50);
8417 mSecondWindow->assertNoEvents();
8418
8419 // Move to another window.
8420 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8421 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8422 ADISPLAY_ID_DEFAULT, {150, 50}))
8423 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8424 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8425 mWindow->consumeDragEvent(true, 150, 50);
8426 mSecondWindow->consumeDragEvent(false, 50, 50);
8427
8428 // drop to another window.
8429 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8430 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8431 {150, 50}))
8432 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8433 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8434 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8435 mWindow->assertNoEvents();
8436 mSecondWindow->assertNoEvents();
8437}
8438
arthurhung6d4bed92021-03-17 11:59:33 +08008439TEST_F(InputDispatcherDragTests, StylusDragAndDrop) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008440 startDrag(true, AINPUT_SOURCE_STYLUS);
arthurhung6d4bed92021-03-17 11:59:33 +08008441
8442 // Move on window and keep button pressed.
8443 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8444 injectMotionEvent(mDispatcher,
8445 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8446 .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008447 .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008448 .build()))
8449 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8450 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8451 mWindow->consumeDragEvent(false, 50, 50);
8452 mSecondWindow->assertNoEvents();
8453
8454 // Move to another window and release button, expect to drop item.
8455 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8456 injectMotionEvent(mDispatcher,
8457 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
8458 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008459 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008460 .build()))
8461 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8462 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8463 mWindow->assertNoEvents();
8464 mSecondWindow->assertNoEvents();
8465 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8466
8467 // nothing to the window.
8468 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8469 injectMotionEvent(mDispatcher,
8470 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
8471 .buttonState(0)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008472 .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50))
arthurhung6d4bed92021-03-17 11:59:33 +08008473 .build()))
8474 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8475 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8476 mWindow->assertNoEvents();
8477 mSecondWindow->assertNoEvents();
8478}
8479
Arthur Hung54745652022-04-20 07:17:41 +00008480TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008481 startDrag();
Arthur Hung6d0571e2021-04-09 20:18:16 +08008482
8483 // Set second window invisible.
8484 mSecondWindow->setVisible(false);
8485 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}});
8486
8487 // Move on window.
8488 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8489 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8490 ADISPLAY_ID_DEFAULT, {50, 50}))
8491 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8492 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8493 mWindow->consumeDragEvent(false, 50, 50);
8494 mSecondWindow->assertNoEvents();
8495
8496 // Move to another window.
8497 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8498 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8499 ADISPLAY_ID_DEFAULT, {150, 50}))
8500 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8501 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8502 mWindow->consumeDragEvent(true, 150, 50);
8503 mSecondWindow->assertNoEvents();
8504
8505 // drop to another window.
8506 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8507 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8508 {150, 50}))
8509 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8510 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8511 mFakePolicy->assertDropTargetEquals(nullptr);
8512 mWindow->assertNoEvents();
8513 mSecondWindow->assertNoEvents();
8514}
8515
Arthur Hung54745652022-04-20 07:17:41 +00008516TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008517 // Ensure window could track pointerIds if it didn't support split touch.
8518 mWindow->setPreventSplitting(true);
8519
Arthur Hung54745652022-04-20 07:17:41 +00008520 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8521 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8522 {50, 50}))
8523 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8524 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8525
8526 const MotionEvent secondFingerDownEvent =
8527 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8528 .displayId(ADISPLAY_ID_DEFAULT)
8529 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008530 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
8531 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008532 .build();
8533 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8534 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8535 InputEventInjectionSync::WAIT_FOR_RESULT))
8536 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00008537 mWindow->consumeMotionPointerDown(/*pointerIndex=*/1);
Arthur Hung54745652022-04-20 07:17:41 +00008538
8539 // Should not perform drag and drop when window has multi fingers.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008540 ASSERT_FALSE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008541}
8542
8543TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) {
8544 // First down on second window.
8545 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8546 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8547 {150, 50}))
8548 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8549
8550 mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8551
8552 // Second down on first window.
8553 const MotionEvent secondFingerDownEvent =
8554 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
8555 .displayId(ADISPLAY_ID_DEFAULT)
8556 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008557 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8558 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008559 .build();
8560 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8561 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
8562 InputEventInjectionSync::WAIT_FOR_RESULT))
8563 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8564 mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8565
8566 // Perform drag and drop from first window.
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008567 ASSERT_TRUE(startDrag(false));
Arthur Hung54745652022-04-20 07:17:41 +00008568
8569 // Move on window.
8570 const MotionEvent secondFingerMoveEvent =
8571 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN)
8572 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008573 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8574 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008575 .build();
8576 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8577 injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT,
8578 InputEventInjectionSync::WAIT_FOR_RESULT));
8579 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8580 mWindow->consumeDragEvent(false, 50, 50);
8581 mSecondWindow->consumeMotionMove();
8582
8583 // Release the drag pointer should perform drop.
8584 const MotionEvent secondFingerUpEvent =
8585 MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN)
8586 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008587 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50))
8588 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Arthur Hung54745652022-04-20 07:17:41 +00008589 .build();
8590 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8591 injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT,
8592 InputEventInjectionSync::WAIT_FOR_RESULT));
8593 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8594 mFakePolicy->assertDropTargetEquals(mWindow->getToken());
8595 mWindow->assertNoEvents();
8596 mSecondWindow->consumeMotionMove();
8597}
8598
Arthur Hung3915c1f2022-05-31 07:17:17 +00008599TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008600 startDrag();
Arthur Hung3915c1f2022-05-31 07:17:17 +00008601
8602 // Update window of second display.
8603 sp<FakeWindowHandle> windowInSecondary =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008604 sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008605 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8606
8607 // Let second display has a touch state.
8608 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8609 injectMotionEvent(mDispatcher,
8610 MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN,
8611 AINPUT_SOURCE_TOUCHSCREEN)
8612 .displayId(SECOND_DISPLAY_ID)
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008613 .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100))
Arthur Hung3915c1f2022-05-31 07:17:17 +00008614 .build()));
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07008615 windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +00008616 SECOND_DISPLAY_ID, /*expectedFlag=*/0);
Arthur Hung3915c1f2022-05-31 07:17:17 +00008617 // Update window again.
8618 mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}});
8619
8620 // Move on window.
8621 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8622 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8623 ADISPLAY_ID_DEFAULT, {50, 50}))
8624 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8625 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8626 mWindow->consumeDragEvent(false, 50, 50);
8627 mSecondWindow->assertNoEvents();
8628
8629 // Move to another window.
8630 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8631 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
8632 ADISPLAY_ID_DEFAULT, {150, 50}))
8633 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8634 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8635 mWindow->consumeDragEvent(true, 150, 50);
8636 mSecondWindow->consumeDragEvent(false, 50, 50);
8637
8638 // drop to another window.
8639 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8640 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
8641 {150, 50}))
8642 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8643 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8644 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8645 mWindow->assertNoEvents();
8646 mSecondWindow->assertNoEvents();
8647}
8648
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008649TEST_F(InputDispatcherDragTests, MouseDragAndDrop) {
8650 startDrag(true, AINPUT_SOURCE_MOUSE);
8651 // Move on window.
8652 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8653 injectMotionEvent(mDispatcher,
8654 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8655 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8656 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008657 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008658 .x(50)
8659 .y(50))
8660 .build()))
8661 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8662 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8663 mWindow->consumeDragEvent(false, 50, 50);
8664 mSecondWindow->assertNoEvents();
8665
8666 // Move to another window.
8667 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8668 injectMotionEvent(mDispatcher,
8669 MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE)
8670 .buttonState(AMOTION_EVENT_BUTTON_PRIMARY)
8671 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008672 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008673 .x(150)
8674 .y(50))
8675 .build()))
8676 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8677 mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT);
8678 mWindow->consumeDragEvent(true, 150, 50);
8679 mSecondWindow->consumeDragEvent(false, 50, 50);
8680
8681 // drop to another window.
8682 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8683 injectMotionEvent(mDispatcher,
8684 MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE)
8685 .buttonState(0)
8686 .pointer(PointerBuilder(MOUSE_POINTER_ID,
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008687 ToolType::MOUSE)
Arthur Hungb75c2aa2022-07-15 09:35:36 +00008688 .x(150)
8689 .y(50))
8690 .build()))
8691 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8692 mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT);
8693 mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken());
8694 mWindow->assertNoEvents();
8695 mSecondWindow->assertNoEvents();
8696}
8697
Vishnu Nair062a8672021-09-03 16:07:44 -07008698class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {};
8699
8700TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) {
8701 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008702 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8703 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008704 window->setDropInput(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008705 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8706 window->setFocusable(true);
8707 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {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 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008720 window->setDropInput(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008721 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
8722
Prabir Pradhan678438e2023-04-13 19:32:51 +00008723 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008724 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8725
Prabir Pradhan678438e2023-04-13 19:32:51 +00008726 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8727 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008728 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8729 window->assertNoEvents();
8730}
8731
8732TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) {
8733 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8734 std::make_shared<FakeApplicationHandle>();
8735 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008736 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8737 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008738 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8739 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008740 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008741 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008742 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8743 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008744 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008745 window->setOwnerInfo(222, 222);
8746 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8747 window->setFocusable(true);
8748 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8749 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008750 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008751
8752 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008753 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008754 window->assertNoEvents();
8755
Prabir Pradhan678438e2023-04-13 19:32:51 +00008756 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8757 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008758 window->assertNoEvents();
8759
8760 // With the flag cleared, the window should get input
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008761 window->setDropInputIfObscured(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008762 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8763
Prabir Pradhan678438e2023-04-13 19:32:51 +00008764 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008765 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8766
Prabir Pradhan678438e2023-04-13 19:32:51 +00008767 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8768 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008769 window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED);
8770 window->assertNoEvents();
8771}
8772
8773TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) {
8774 std::shared_ptr<FakeApplicationHandle> obscuringApplication =
8775 std::make_shared<FakeApplicationHandle>();
8776 sp<FakeWindowHandle> obscuringWindow =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008777 sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow",
8778 ADISPLAY_ID_DEFAULT);
Vishnu Nair062a8672021-09-03 16:07:44 -07008779 obscuringWindow->setFrame(Rect(0, 0, 50, 50));
8780 obscuringWindow->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008781 obscuringWindow->setTouchable(false);
Vishnu Nair062a8672021-09-03 16:07:44 -07008782 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008783 sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher,
8784 "Test window", ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008785 window->setDropInputIfObscured(true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008786 window->setOwnerInfo(222, 222);
8787 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
8788 window->setFocusable(true);
8789 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}});
8790 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00008791 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Vishnu Nair062a8672021-09-03 16:07:44 -07008792
8793 // With the flag set, window should not get any input
Prabir Pradhan678438e2023-04-13 19:32:51 +00008794 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008795 window->assertNoEvents();
8796
Prabir Pradhan678438e2023-04-13 19:32:51 +00008797 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8798 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008799 window->assertNoEvents();
8800
8801 // When the window is no longer obscured because it went on top, it should get input
8802 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}});
8803
Prabir Pradhan678438e2023-04-13 19:32:51 +00008804 mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008805 window->consumeKeyUp(ADISPLAY_ID_DEFAULT);
8806
Prabir Pradhan678438e2023-04-13 19:32:51 +00008807 mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN,
8808 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT));
Vishnu Nair062a8672021-09-03 16:07:44 -07008809 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8810 window->assertNoEvents();
8811}
8812
Antonio Kantekf16f2832021-09-28 04:39:20 +00008813class InputDispatcherTouchModeChangedTests : public InputDispatcherTest {
8814protected:
8815 std::shared_ptr<FakeApplicationHandle> mApp;
Antonio Kantek15beb512022-06-13 22:35:41 +00008816 std::shared_ptr<FakeApplicationHandle> mSecondaryApp;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008817 sp<FakeWindowHandle> mWindow;
8818 sp<FakeWindowHandle> mSecondWindow;
Antonio Kantek15beb512022-06-13 22:35:41 +00008819 sp<FakeWindowHandle> mThirdWindow;
Antonio Kantekf16f2832021-09-28 04:39:20 +00008820
8821 void SetUp() override {
8822 InputDispatcherTest::SetUp();
8823
8824 mApp = std::make_shared<FakeApplicationHandle>();
Antonio Kantek15beb512022-06-13 22:35:41 +00008825 mSecondaryApp = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008826 mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008827 mWindow->setFocusable(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008828 setFocusedWindow(mWindow);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008829 mSecondWindow =
8830 sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008831 mSecondWindow->setFocusable(true);
Antonio Kantek15beb512022-06-13 22:35:41 +00008832 mThirdWindow =
8833 sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher,
8834 "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID);
8835 mThirdWindow->setFocusable(true);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008836
8837 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp);
Antonio Kantek15beb512022-06-13 22:35:41 +00008838 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}},
8839 {SECOND_DISPLAY_ID, {mThirdWindow}}});
8840 mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008841 mWindow->consumeFocusEvent(true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008842
Antonio Kantek15beb512022-06-13 22:35:41 +00008843 // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode.
Prabir Pradhan5735a322022-04-11 17:23:34 +00008844 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008845 WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) {
Antonio Kantek48710e42022-03-24 14:19:30 -07008846 mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
8847 mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008848 mThirdWindow->assertNoEvents();
8849 }
8850
8851 // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode.
8852 if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID,
Harry Cutts33476232023-01-30 19:57:29 +00008853 SECONDARY_WINDOW_UID, /*hasPermission=*/true,
Antonio Kantek15beb512022-06-13 22:35:41 +00008854 SECOND_DISPLAY_ID)) {
8855 mWindow->assertNoEvents();
8856 mSecondWindow->assertNoEvents();
8857 mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode);
Antonio Kantek48710e42022-03-24 14:19:30 -07008858 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00008859 }
8860
Antonio Kantek15beb512022-06-13 22:35:41 +00008861 void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid,
8862 bool hasPermission) {
Antonio Kanteka042c022022-07-06 16:51:07 -07008863 ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission,
8864 ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008865 mWindow->consumeTouchModeEvent(inTouchMode);
8866 mSecondWindow->consumeTouchModeEvent(inTouchMode);
Antonio Kantek15beb512022-06-13 22:35:41 +00008867 mThirdWindow->assertNoEvents();
Antonio Kantekf16f2832021-09-28 04:39:20 +00008868 }
8869};
8870
Antonio Kantek26defcf2022-02-08 01:12:27 +00008871TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008872 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek15beb512022-06-13 22:35:41 +00008873 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode,
8874 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008875 /* hasPermission=*/false);
Antonio Kantekf16f2832021-09-28 04:39:20 +00008876}
8877
Antonio Kantek26defcf2022-02-08 01:12:27 +00008878TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) {
8879 const WindowInfo& windowInfo = *mWindow->getInfo();
8880 int32_t ownerPid = windowInfo.ownerPid;
8881 int32_t ownerUid = windowInfo.ownerUid;
8882 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
8883 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008884 ownerUid, /*hasPermission=*/false,
Antonio Kanteka042c022022-07-06 16:51:07 -07008885 ADISPLAY_ID_DEFAULT));
Antonio Kantek26defcf2022-02-08 01:12:27 +00008886 mWindow->assertNoEvents();
8887 mSecondWindow->assertNoEvents();
8888}
8889
8890TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) {
8891 const WindowInfo& windowInfo = *mWindow->getInfo();
8892 int32_t ownerPid = windowInfo.ownerPid;
8893 int32_t ownerUid = windowInfo.ownerUid;
8894 mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1);
Antonio Kantek15beb512022-06-13 22:35:41 +00008895 changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid,
Harry Cutts33476232023-01-30 19:57:29 +00008896 ownerUid, /*hasPermission=*/true);
Antonio Kantek26defcf2022-02-08 01:12:27 +00008897}
8898
Antonio Kantekf16f2832021-09-28 04:39:20 +00008899TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08008900 const WindowInfo& windowInfo = *mWindow->getInfo();
Antonio Kantek26defcf2022-02-08 01:12:27 +00008901 ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode,
8902 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008903 /*hasPermission=*/true, ADISPLAY_ID_DEFAULT));
Antonio Kantekf16f2832021-09-28 04:39:20 +00008904 mWindow->assertNoEvents();
8905 mSecondWindow->assertNoEvents();
8906}
8907
Antonio Kantek15beb512022-06-13 22:35:41 +00008908TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) {
8909 const WindowInfo& windowInfo = *mThirdWindow->getInfo();
8910 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8911 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008912 /*hasPermission=*/true, SECOND_DISPLAY_ID));
Antonio Kantek15beb512022-06-13 22:35:41 +00008913 mWindow->assertNoEvents();
8914 mSecondWindow->assertNoEvents();
8915 mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode);
8916}
8917
Antonio Kantek48710e42022-03-24 14:19:30 -07008918TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) {
8919 // Interact with the window first.
8920 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT))
8921 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
8922 mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT);
8923
8924 // Then remove focus.
8925 mWindow->setFocusable(false);
8926 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}});
8927
8928 // Assert that caller can switch touch mode by owning one of the last interacted window.
8929 const WindowInfo& windowInfo = *mWindow->getInfo();
8930 ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode,
8931 windowInfo.ownerPid, windowInfo.ownerUid,
Harry Cutts33476232023-01-30 19:57:29 +00008932 /*hasPermission=*/false, ADISPLAY_ID_DEFAULT));
Antonio Kantek48710e42022-03-24 14:19:30 -07008933}
8934
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008935class InputDispatcherSpyWindowTest : public InputDispatcherTest {
8936public:
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008937 sp<FakeWindowHandle> createSpy() {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008938 std::shared_ptr<FakeApplicationHandle> application =
8939 std::make_shared<FakeApplicationHandle>();
8940 std::string name = "Fake Spy ";
8941 name += std::to_string(mSpyCount++);
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008942 sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher,
8943 name.c_str(), ADISPLAY_ID_DEFAULT);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08008944 spy->setSpy(true);
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008945 spy->setTrustedOverlay(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008946 return spy;
8947 }
8948
8949 sp<FakeWindowHandle> createForeground() {
8950 std::shared_ptr<FakeApplicationHandle> application =
8951 std::make_shared<FakeApplicationHandle>();
8952 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07008953 sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
8954 ADISPLAY_ID_DEFAULT);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08008955 window->setFocusable(true);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008956 return window;
8957 }
8958
8959private:
8960 int mSpyCount{0};
8961};
8962
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008963using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008964/**
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008965 * Adding a spy window that is not a trusted overlay causes Dispatcher to abort.
8966 */
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08008967TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) {
8968 ScopedSilentDeath _silentDeath;
8969
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008970 auto spy = createSpy();
Prabir Pradhan5c85e052021-12-22 02:27:12 -08008971 spy->setTrustedOverlay(false);
8972 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}),
8973 ".* not a trusted overlay");
8974}
8975
8976/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008977 * Input injection into a display with a spy window but no foreground windows should succeed.
8978 */
8979TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08008980 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08008981 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}});
8982
8983 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
8984 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
8985 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
8986 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
8987}
8988
8989/**
8990 * Verify the order in which different input windows receive events. The touched foreground window
8991 * (if there is one) should always receive the event first. When there are multiple spy windows, the
8992 * spy windows will receive the event according to their Z-order, where the top-most spy window will
8993 * receive events before ones belows it.
8994 *
8995 * Here, we set up a scenario with four windows in the following Z order from the top:
8996 * spy1, spy2, window, spy3.
8997 * We then inject an event and verify that the foreground "window" receives it first, followed by
8998 * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground
8999 * window.
9000 */
9001TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) {
9002 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009003 auto spy1 = createSpy();
9004 auto spy2 = createSpy();
9005 auto spy3 = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009006 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}});
9007 const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3};
9008 const size_t numChannels = channels.size();
9009
Michael Wright8e9a8562022-02-09 13:44:29 +00009010 base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009011 if (!epollFd.ok()) {
9012 FAIL() << "Failed to create epoll fd";
9013 }
9014
9015 for (size_t i = 0; i < numChannels; i++) {
9016 struct epoll_event event = {.events = EPOLLIN, .data.u64 = i};
9017 if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) {
9018 FAIL() << "Failed to add fd to epoll";
9019 }
9020 }
9021
9022 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9023 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9024 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9025
9026 std::vector<size_t> eventOrder;
9027 std::vector<struct epoll_event> events(numChannels);
9028 for (;;) {
9029 const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels),
9030 (100ms).count());
9031 if (nFds < 0) {
9032 FAIL() << "Failed to call epoll_wait";
9033 }
9034 if (nFds == 0) {
9035 break; // epoll_wait timed out
9036 }
9037 for (int i = 0; i < nFds; i++) {
Colin Cross5b799302022-10-18 21:52:41 -07009038 ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events);
Siarhei Vishniakou31977182022-09-30 08:51:23 -07009039 eventOrder.push_back(static_cast<size_t>(events[i].data.u64));
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009040 channels[i]->consumeMotionDown();
9041 }
9042 }
9043
9044 // Verify the order in which the events were received.
9045 EXPECT_EQ(3u, eventOrder.size());
9046 EXPECT_EQ(2u, eventOrder[0]); // index 2: window
9047 EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1
9048 EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2
9049}
9050
9051/**
9052 * A spy window using the NOT_TOUCHABLE flag does not receive events.
9053 */
9054TEST_F(InputDispatcherSpyWindowTest, NotTouchable) {
9055 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009056 auto spy = createSpy();
9057 spy->setTouchable(false);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009058 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9059
9060 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9061 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9062 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9063 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9064 spy->assertNoEvents();
9065}
9066
9067/**
9068 * A spy window will only receive gestures that originate within its touchable region. Gestures that
9069 * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched
9070 * to the window.
9071 */
9072TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) {
9073 auto window = createForeground();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009074 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009075 spy->setTouchableRegion(Region{{0, 0, 20, 20}});
9076 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9077
9078 // Inject an event outside the spy window's touchable region.
9079 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9080 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9081 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9082 window->consumeMotionDown();
9083 spy->assertNoEvents();
9084 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9085 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9086 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9087 window->consumeMotionUp();
9088 spy->assertNoEvents();
9089
9090 // Inject an event inside the spy window's touchable region.
9091 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9092 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9093 {5, 10}))
9094 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9095 window->consumeMotionDown();
9096 spy->consumeMotionDown();
9097}
9098
9099/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009100 * A spy window can listen for touches outside its touchable region using the WATCH_OUTSIDE_TOUCHES
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009101 * flag, but it will get zero-ed out coordinates if the foreground has a different owner.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009102 */
9103TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) {
9104 auto window = createForeground();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009105 window->setOwnerInfo(12, 34);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009106 auto spy = createSpy();
9107 spy->setWatchOutsideTouch(true);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009108 spy->setOwnerInfo(56, 78);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009109 spy->setFrame(Rect{0, 0, 20, 20});
9110 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9111
9112 // Inject an event outside the spy window's frame and touchable region.
9113 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009114 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9115 {100, 200}))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009116 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9117 window->consumeMotionDown();
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08009118 spy->consumeMotionOutsideWithZeroedCoords();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009119}
9120
9121/**
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009122 * Even when a spy window spans over multiple foreground windows, the spy should receive all
9123 * pointers that are down within its bounds.
9124 */
9125TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) {
9126 auto windowLeft = createForeground();
9127 windowLeft->setFrame({0, 0, 100, 200});
9128 auto windowRight = createForeground();
9129 windowRight->setFrame({100, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009130 auto spy = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009131 spy->setFrame({0, 0, 200, 200});
9132 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}});
9133
9134 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9135 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9136 {50, 50}))
9137 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9138 windowLeft->consumeMotionDown();
9139 spy->consumeMotionDown();
9140
9141 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009142 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009143 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009144 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9145 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009146 .build();
9147 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9148 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9149 InputEventInjectionSync::WAIT_FOR_RESULT))
9150 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9151 windowRight->consumeMotionDown();
Harry Cutts33476232023-01-30 19:57:29 +00009152 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009153}
9154
9155/**
9156 * When the first pointer lands outside the spy window and the second pointer lands inside it, the
9157 * the spy should receive the second pointer with ACTION_DOWN.
9158 */
9159TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) {
9160 auto window = createForeground();
9161 window->setFrame({0, 0, 200, 200});
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009162 auto spyRight = createSpy();
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009163 spyRight->setFrame({100, 0, 200, 200});
9164 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}});
9165
9166 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9167 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9168 {50, 50}))
9169 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9170 window->consumeMotionDown();
9171 spyRight->assertNoEvents();
9172
9173 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009174 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009175 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009176 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50))
9177 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50))
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009178 .build();
9179 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9180 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9181 InputEventInjectionSync::WAIT_FOR_RESULT))
9182 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
Harry Cutts33476232023-01-30 19:57:29 +00009183 window->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan07e05b62021-11-19 03:57:24 -08009184 spyRight->consumeMotionDown();
9185}
9186
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009187/**
9188 * The spy window should not be able to affect whether or not touches are split. Only the foreground
9189 * windows should be allowed to control split touch.
9190 */
9191TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) {
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009192 // This spy window prevents touch splitting. However, we still expect to split touches
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009193 // because a foreground window has not disabled splitting.
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009194 auto spy = createSpy();
Prabir Pradhan76bdecb2022-01-31 11:14:15 -08009195 spy->setPreventSplitting(true);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009196
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009197 auto window = createForeground();
9198 window->setFrame(Rect(0, 0, 100, 100));
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009199
9200 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9201
9202 // First finger down, no window touched.
9203 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9204 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9205 {100, 200}))
9206 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9207 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9208 window->assertNoEvents();
9209
9210 // Second finger down on window, the window should receive touch down.
9211 const MotionEvent secondFingerDownEvent =
Siarhei Vishniakoua16e3a22022-03-02 15:26:40 -08009212 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009213 .displayId(ADISPLAY_ID_DEFAULT)
9214 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009215 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9216 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009217 .build();
9218 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9219 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9220 InputEventInjectionSync::WAIT_FOR_RESULT))
9221 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9222
9223 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
Harry Cutts33476232023-01-30 19:57:29 +00009224 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009225}
9226
9227/**
9228 * A spy window will usually be implemented as an un-focusable window. Verify that these windows
9229 * do not receive key events.
9230 */
9231TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009232 auto spy = createSpy();
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08009233 spy->setFocusable(false);
9234
9235 auto window = createForeground();
9236 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9237 setFocusedWindow(window);
9238 window->consumeFocusEvent(true);
9239
9240 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher))
9241 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9242 window->consumeKeyDown(ADISPLAY_ID_NONE);
9243
9244 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher))
9245 << "Inject key event should return InputEventInjectionResult::SUCCEEDED";
9246 window->consumeKeyUp(ADISPLAY_ID_NONE);
9247
9248 spy->assertNoEvents();
9249}
9250
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009251using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest;
9252
9253/**
9254 * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that
9255 * are currently sent to any other windows - including other spy windows - will also be cancelled.
9256 */
9257TEST_F(InputDispatcherPilferPointersTest, PilferPointers) {
9258 auto window = createForeground();
9259 auto spy1 = createSpy();
9260 auto spy2 = createSpy();
9261 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}});
9262
9263 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9264 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9265 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9266 window->consumeMotionDown();
9267 spy1->consumeMotionDown();
9268 spy2->consumeMotionDown();
9269
9270 // Pilfer pointers from the second spy window.
9271 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken()));
9272 spy2->assertNoEvents();
9273 spy1->consumeMotionCancel();
9274 window->consumeMotionCancel();
9275
9276 // The rest of the gesture should only be sent to the second spy window.
9277 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9278 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
9279 ADISPLAY_ID_DEFAULT))
9280 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9281 spy2->consumeMotionMove();
9282 spy1->assertNoEvents();
9283 window->assertNoEvents();
9284}
9285
9286/**
9287 * A spy window can pilfer pointers for a gesture even after the foreground window has been removed
9288 * in the middle of the gesture.
9289 */
9290TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) {
9291 auto window = createForeground();
9292 auto spy = createSpy();
9293 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9294
9295 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9296 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9297 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9298 window->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9299 spy->consumeMotionDown(ADISPLAY_ID_DEFAULT);
9300
9301 window->releaseChannel();
9302
9303 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9304
9305 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9306 injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT))
9307 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9308 spy->consumeMotionUp(ADISPLAY_ID_DEFAULT);
9309}
9310
9311/**
9312 * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to
9313 * the spy, but not to any other windows.
9314 */
9315TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) {
9316 auto spy = createSpy();
9317 auto window = createForeground();
9318
9319 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9320
9321 // First finger down on the window and the spy.
9322 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9323 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9324 {100, 200}))
9325 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9326 spy->consumeMotionDown();
9327 window->consumeMotionDown();
9328
9329 // Spy window pilfers the pointers.
9330 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9331 window->consumeMotionCancel();
9332
9333 // Second finger down on the window and spy, but the window should not receive the pointer down.
9334 const MotionEvent secondFingerDownEvent =
9335 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9336 .displayId(ADISPLAY_ID_DEFAULT)
9337 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009338 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9339 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009340 .build();
9341 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9342 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9343 InputEventInjectionSync::WAIT_FOR_RESULT))
9344 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9345
Harry Cutts33476232023-01-30 19:57:29 +00009346 spy->consumeMotionPointerDown(/*pointerIndex=*/1);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009347
9348 // Third finger goes down outside all windows, so injection should fail.
9349 const MotionEvent thirdFingerDownEvent =
9350 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9351 .displayId(ADISPLAY_ID_DEFAULT)
9352 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009353 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200))
9354 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
9355 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009356 .build();
9357 ASSERT_EQ(InputEventInjectionResult::FAILED,
9358 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9359 InputEventInjectionSync::WAIT_FOR_RESULT))
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -08009360 << "Inject motion event should return InputEventInjectionResult::FAILED";
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009361
9362 spy->assertNoEvents();
9363 window->assertNoEvents();
9364}
9365
9366/**
9367 * After a spy window pilfers pointers, only the pointers used by the spy should be canceled
9368 */
9369TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) {
9370 auto spy = createSpy();
9371 spy->setFrame(Rect(0, 0, 100, 100));
9372 auto window = createForeground();
9373 window->setFrame(Rect(0, 0, 200, 200));
9374
9375 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9376
9377 // First finger down on the window only
9378 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9379 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9380 {150, 150}))
9381 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9382 window->consumeMotionDown();
9383
9384 // Second finger down on the spy and window
9385 const MotionEvent secondFingerDownEvent =
9386 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9387 .displayId(ADISPLAY_ID_DEFAULT)
9388 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009389 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9390 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009391 .build();
9392 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9393 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9394 InputEventInjectionSync::WAIT_FOR_RESULT))
9395 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9396 spy->consumeMotionDown();
9397 window->consumeMotionPointerDown(1);
9398
9399 // Third finger down on the spy and window
9400 const MotionEvent thirdFingerDownEvent =
9401 MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9402 .displayId(ADISPLAY_ID_DEFAULT)
9403 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009404 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150))
9405 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10))
9406 .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009407 .build();
9408 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9409 injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT,
9410 InputEventInjectionSync::WAIT_FOR_RESULT))
9411 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9412 spy->consumeMotionPointerDown(1);
9413 window->consumeMotionPointerDown(2);
9414
9415 // Spy window pilfers the pointers.
9416 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9417 window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9418 window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED);
9419
9420 spy->assertNoEvents();
9421 window->assertNoEvents();
9422}
9423
9424/**
9425 * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to
9426 * other windows should be canceled. If this results in the cancellation of all pointers for some
9427 * window, then that window should receive ACTION_CANCEL.
9428 */
9429TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) {
9430 auto spy = createSpy();
9431 spy->setFrame(Rect(0, 0, 100, 100));
9432 auto window = createForeground();
9433 window->setFrame(Rect(0, 0, 200, 200));
9434
9435 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9436
9437 // First finger down on both spy and window
9438 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9439 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9440 {10, 10}))
9441 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9442 window->consumeMotionDown();
9443 spy->consumeMotionDown();
9444
9445 // Second finger down on the spy and window
9446 const MotionEvent secondFingerDownEvent =
9447 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9448 .displayId(ADISPLAY_ID_DEFAULT)
9449 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009450 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9451 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009452 .build();
9453 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9454 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9455 InputEventInjectionSync::WAIT_FOR_RESULT))
9456 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9457 spy->consumeMotionPointerDown(1);
9458 window->consumeMotionPointerDown(1);
9459
9460 // Spy window pilfers the pointers.
9461 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9462 window->consumeMotionCancel();
9463
9464 spy->assertNoEvents();
9465 window->assertNoEvents();
9466}
9467
9468/**
9469 * After a spy window pilfers pointers, new pointers that are not touching the spy window can still
9470 * be sent to other windows
9471 */
9472TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) {
9473 auto spy = createSpy();
9474 spy->setFrame(Rect(0, 0, 100, 100));
9475 auto window = createForeground();
9476 window->setFrame(Rect(0, 0, 200, 200));
9477
9478 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9479
9480 // First finger down on both window and spy
9481 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9482 injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
9483 {10, 10}))
9484 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9485 window->consumeMotionDown();
9486 spy->consumeMotionDown();
9487
9488 // Spy window pilfers the pointers.
9489 EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken()));
9490 window->consumeMotionCancel();
9491
9492 // Second finger down on the window only
9493 const MotionEvent secondFingerDownEvent =
9494 MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN)
9495 .displayId(ADISPLAY_ID_DEFAULT)
9496 .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009497 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10))
9498 .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150))
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00009499 .build();
9500 ASSERT_EQ(InputEventInjectionResult::SUCCEEDED,
9501 injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT,
9502 InputEventInjectionSync::WAIT_FOR_RESULT))
9503 << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
9504 window->consumeMotionDown();
9505 window->assertNoEvents();
9506
9507 // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
9508 spy->consumeMotionMove();
9509 spy->assertNoEvents();
9510}
9511
Prabir Pradhand65552b2021-10-07 11:23:50 -07009512class InputDispatcherStylusInterceptorTest : public InputDispatcherTest {
9513public:
9514 std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() {
9515 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9516 std::make_shared<FakeApplicationHandle>();
9517 sp<FakeWindowHandle> overlay =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009518 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher,
9519 "Stylus interceptor window", ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009520 overlay->setFocusable(false);
9521 overlay->setOwnerInfo(111, 111);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08009522 overlay->setTouchable(false);
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009523 overlay->setInterceptsStylus(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009524 overlay->setTrustedOverlay(true);
9525
9526 std::shared_ptr<FakeApplicationHandle> application =
9527 std::make_shared<FakeApplicationHandle>();
9528 sp<FakeWindowHandle> window =
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009529 sp<FakeWindowHandle>::make(application, mDispatcher, "Application window",
9530 ADISPLAY_ID_DEFAULT);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009531 window->setFocusable(true);
9532 window->setOwnerInfo(222, 222);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009533
9534 mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application);
9535 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9536 setFocusedWindow(window);
Harry Cutts33476232023-01-30 19:57:29 +00009537 window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009538 return {std::move(overlay), std::move(window)};
9539 }
9540
9541 void sendFingerEvent(int32_t action) {
Prabir Pradhan678438e2023-04-13 19:32:51 +00009542 mDispatcher->notifyMotion(
Prabir Pradhand65552b2021-10-07 11:23:50 -07009543 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
Prabir Pradhan678438e2023-04-13 19:32:51 +00009544 ADISPLAY_ID_DEFAULT, {PointF{20, 20}}));
Prabir Pradhand65552b2021-10-07 11:23:50 -07009545 }
9546
9547 void sendStylusEvent(int32_t action) {
9548 NotifyMotionArgs motionArgs =
9549 generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS,
9550 ADISPLAY_ID_DEFAULT, {PointF{30, 40}});
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009551 motionArgs.pointerProperties[0].toolType = ToolType::STYLUS;
Prabir Pradhan678438e2023-04-13 19:32:51 +00009552 mDispatcher->notifyMotion(motionArgs);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009553 }
9554};
9555
Prabir Pradhana3ab87a2022-01-27 10:00:21 -08009556using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest;
9557
9558TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) {
9559 ScopedSilentDeath _silentDeath;
9560
Prabir Pradhand65552b2021-10-07 11:23:50 -07009561 auto [overlay, window] = setupStylusOverlayScenario();
9562 overlay->setTrustedOverlay(false);
9563 // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort.
9564 ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}),
9565 ".* not a trusted overlay");
9566}
9567
9568TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) {
9569 auto [overlay, window] = setupStylusOverlayScenario();
9570 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9571
9572 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9573 overlay->consumeMotionDown();
9574 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9575 overlay->consumeMotionUp();
9576
9577 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9578 window->consumeMotionDown();
9579 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9580 window->consumeMotionUp();
9581
9582 overlay->assertNoEvents();
9583 window->assertNoEvents();
9584}
9585
9586TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) {
9587 auto [overlay, window] = setupStylusOverlayScenario();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08009588 overlay->setSpy(true);
Prabir Pradhand65552b2021-10-07 11:23:50 -07009589 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9590
9591 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9592 overlay->consumeMotionDown();
9593 window->consumeMotionDown();
9594 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9595 overlay->consumeMotionUp();
9596 window->consumeMotionUp();
9597
9598 sendFingerEvent(AMOTION_EVENT_ACTION_DOWN);
9599 window->consumeMotionDown();
9600 sendFingerEvent(AMOTION_EVENT_ACTION_UP);
9601 window->consumeMotionUp();
9602
9603 overlay->assertNoEvents();
9604 window->assertNoEvents();
9605}
9606
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00009607/**
9608 * Set up a scenario to test the behavior used by the stylus handwriting detection feature.
9609 * The scenario is as follows:
9610 * - The stylus interceptor overlay is configured as a spy window.
9611 * - The stylus interceptor spy receives the start of a new stylus gesture.
9612 * - It pilfers pointers and then configures itself to no longer be a spy.
9613 * - The stylus interceptor continues to receive the rest of the gesture.
9614 */
9615TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) {
9616 auto [overlay, window] = setupStylusOverlayScenario();
9617 overlay->setSpy(true);
9618 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9619
9620 sendStylusEvent(AMOTION_EVENT_ACTION_DOWN);
9621 overlay->consumeMotionDown();
9622 window->consumeMotionDown();
9623
9624 // The interceptor pilfers the pointers.
9625 EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken()));
9626 window->consumeMotionCancel();
9627
9628 // The interceptor configures itself so that it is no longer a spy.
9629 overlay->setSpy(false);
9630 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}});
9631
9632 // It continues to receive the rest of the stylus gesture.
9633 sendStylusEvent(AMOTION_EVENT_ACTION_MOVE);
9634 overlay->consumeMotionMove();
9635 sendStylusEvent(AMOTION_EVENT_ACTION_UP);
9636 overlay->consumeMotionUp();
9637
9638 window->assertNoEvents();
9639}
9640
Prabir Pradhan5735a322022-04-11 17:23:34 +00009641struct User {
9642 int32_t mPid;
9643 int32_t mUid;
9644 uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS};
9645 std::unique_ptr<InputDispatcher>& mDispatcher;
9646
9647 User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid)
9648 : mPid(pid), mUid(uid), mDispatcher(dispatcher) {}
9649
9650 InputEventInjectionResult injectTargetedMotion(int32_t action) const {
9651 return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN,
9652 ADISPLAY_ID_DEFAULT, {100, 200},
9653 {AMOTION_EVENT_INVALID_CURSOR_POSITION,
9654 AMOTION_EVENT_INVALID_CURSOR_POSITION},
9655 INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT,
9656 systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags);
9657 }
9658
9659 InputEventInjectionResult injectTargetedKey(int32_t action) const {
Harry Cutts33476232023-01-30 19:57:29 +00009660 return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE,
Prabir Pradhan5735a322022-04-11 17:23:34 +00009661 InputEventInjectionSync::WAIT_FOR_RESULT,
Harry Cutts33476232023-01-30 19:57:29 +00009662 INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid},
Prabir Pradhan5735a322022-04-11 17:23:34 +00009663 mPolicyFlags);
9664 }
9665
9666 sp<FakeWindowHandle> createWindow() const {
9667 std::shared_ptr<FakeApplicationHandle> overlayApplication =
9668 std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009669 sp<FakeWindowHandle> window =
9670 sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window",
9671 ADISPLAY_ID_DEFAULT);
Prabir Pradhan5735a322022-04-11 17:23:34 +00009672 window->setOwnerInfo(mPid, mUid);
9673 return window;
9674 }
9675};
9676
9677using InputDispatcherTargetedInjectionTest = InputDispatcherTest;
9678
9679TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) {
9680 auto owner = User(mDispatcher, 10, 11);
9681 auto window = owner.createWindow();
9682 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9683
9684 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9685 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9686 window->consumeMotionDown();
9687
9688 setFocusedWindow(window);
9689 window->consumeFocusEvent(true);
9690
9691 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9692 owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9693 window->consumeKeyDown(ADISPLAY_ID_NONE);
9694}
9695
9696TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) {
9697 auto owner = User(mDispatcher, 10, 11);
9698 auto window = owner.createWindow();
9699 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
9700
9701 auto rando = User(mDispatcher, 20, 21);
9702 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9703 rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9704
9705 setFocusedWindow(window);
9706 window->consumeFocusEvent(true);
9707
9708 EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH,
9709 rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN));
9710 window->assertNoEvents();
9711}
9712
9713TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) {
9714 auto owner = User(mDispatcher, 10, 11);
9715 auto window = owner.createWindow();
9716 auto spy = owner.createWindow();
9717 spy->setSpy(true);
9718 spy->setTrustedOverlay(true);
9719 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}});
9720
9721 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9722 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9723 spy->consumeMotionDown();
9724 window->consumeMotionDown();
9725}
9726
9727TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) {
9728 auto owner = User(mDispatcher, 10, 11);
9729 auto window = owner.createWindow();
9730
9731 auto rando = User(mDispatcher, 20, 21);
9732 auto randosSpy = rando.createWindow();
9733 randosSpy->setSpy(true);
9734 randosSpy->setTrustedOverlay(true);
9735 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9736
9737 // The event is targeted at owner's window, so injection should succeed, but the spy should
9738 // not receive the event.
9739 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9740 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9741 randosSpy->assertNoEvents();
9742 window->consumeMotionDown();
9743}
9744
9745TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) {
9746 auto owner = User(mDispatcher, 10, 11);
9747 auto window = owner.createWindow();
9748
9749 auto rando = User(mDispatcher, 20, 21);
9750 auto randosSpy = rando.createWindow();
9751 randosSpy->setSpy(true);
9752 randosSpy->setTrustedOverlay(true);
9753 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}});
9754
9755 // A user that has injection permission can inject into any window.
9756 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9757 injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
9758 ADISPLAY_ID_DEFAULT));
9759 randosSpy->consumeMotionDown();
9760 window->consumeMotionDown();
9761
9762 setFocusedWindow(randosSpy);
9763 randosSpy->consumeFocusEvent(true);
9764
9765 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher));
9766 randosSpy->consumeKeyDown(ADISPLAY_ID_NONE);
9767 window->assertNoEvents();
9768}
9769
9770TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) {
9771 auto owner = User(mDispatcher, 10, 11);
9772 auto window = owner.createWindow();
9773
9774 auto rando = User(mDispatcher, 20, 21);
9775 auto randosWindow = rando.createWindow();
9776 randosWindow->setFrame(Rect{-10, -10, -5, -5});
9777 randosWindow->setWatchOutsideTouch(true);
9778 mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}});
9779
9780 // We allow generation of ACTION_OUTSIDE events into windows owned by different uids.
9781 EXPECT_EQ(InputEventInjectionResult::SUCCEEDED,
9782 owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN));
9783 window->consumeMotionDown();
9784 randosWindow->consumeMotionOutside();
9785}
9786
Garfield Tane84e6f92019-08-29 17:28:41 -07009787} // namespace android::inputdispatcher